Repository Public
1// SP Notes Core2// File: Tests/SPNotesDocumentTests/InkWidthLawTests.swift3// Description: The ink width law shared with the web: the fixture the web asserts (`fixtures-ink/width-law.json`, same numbers)4// is read with Core's model and drawn widths, pressures and bounds are compared.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import Testing10@testable import SPNotesDocument1112@Suite struct InkWidthLawTests {13 @Test func coreDrawsTheWidthsOfTheSharedFixture() throws {14 let root = try JSONValue(data: try Fixture.data("ink-width-law"))15 let ink = try InkDocument(json: try #require(root["ink"]))16 let expected = try #require(root["expected"]?.arrayValue)17 #expect(root["highlighterAlpha"]?.doubleValue == InkRenderer.highlighterOpacity)18 #expect(ink.strokes.count == expected.count)19 for (stroke, want) in zip(ink.strokes, expected) {20 let pressures = try #require(want["pressures"]?.arrayValue).compactMap(\.doubleValue)21 let widths = try #require(want["widths"]?.arrayValue).compactMap(\.doubleValue)22 let bounds = try #require(want["bounds"]?.arrayValue).compactMap(\.doubleValue)23 #expect(stroke.points.map(\.pressure) == pressures, "a point without pressure counts as 1")24 let drawn = stroke.points.map { p -> Double in stroke.tool == .pen ? Double(InkRenderer.penWidth(CGFloat(stroke.width), p.pressure)) : stroke.width }25 #expect(drawn.map { ($0 * 1000).rounded() / 1000 } == widths, "\(stroke.tool)")26 let box = stroke.boundingBox27 #expect([Double(box.minX), Double(box.minY), Double(box.maxX), Double(box.maxY)] == bounds)28 }29 }30}31