Repository Public
1// SP Notes iOS2// File: Tests/DrawingBlockTests.swift3// Description: The `drawing` block: saving writes ink + preview + PencilKit data, loading prefers the PencilKit data and rebuilds4// from the ink otherwise, missing files change nothing; the 1.0 drawings (picture + source) stay editable as before5// and convert to a block only on request, without losing anything; the block detail context and the provider.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import PencilKit10import Testing11import UIKit12import SPNotesCore13@testable import SPNotes1415@MainActor16private func openNote(_ app: AppModel, markdown: String = "Avant\n\nAprès") async throws -> NoteEditorModel {17 let id = try await app.createNote(title: "Note", document: NoteDocument(markdown: markdown))18 let model = NoteEditorModel(noteId: id, app: app)19 model.controller.textView.frame = CGRect(x: 0, y: 0, width: 800, height: 1000)20 await model.load()21 return model22}2324/// The atoms of the editor, in order, with their ranges.25@MainActor26private func atoms(_ model: NoteEditorModel) -> [(node: PMNode, range: NSRange)] {27 var found: [(PMNode, NSRange)] = []28 let storage = model.controller.storage29 storage.enumerateAttribute(.attachment, in: NSRange(location: 0, length: storage.length)) { value, range, _ in30 if let atom = value as? AtomAttachment { found.append((atom.node, range)) }31 }32 return found33}3435@MainActor36private func file(_ id: String, _ app: AppModel) async throws -> Data { try Data(contentsOf: try await app.localFile(forAttachment: id)) }3738@MainActor39@Suite("Drawing — bloc Dessin") struct DrawingBlockTests {40 @Test func savingWritesInkPreviewAndPencilKitData() async throws {41 let app = try makeApp()42 let noteId = try await app.createNote(title: "Dessins")43 let drawing = InkSamples.drawing()44 let saved = try await DrawingBlockIO.save(drawing, background: DrawingPaper.white, into: DrawingBlock(), app: app, noteId: noteId)45 let block = saved.block46 let records = try await app.store.attachments(pageId: noteId)47 #expect(records.count == 3)48 let ink = try #require(records.first { $0.id == block.ink })49 let preview = try #require(records.first { $0.id == block.preview })50 let pk = try #require(records.first { $0.id == block.pk })51 #expect(ink.filename.hasPrefix("drawing-") && ink.filename.hasSuffix(".ink.json") && ink.mime == "application/json")52 #expect(preview.filename.hasSuffix(".png") && preview.mime == "image/png")53 #expect(pk.filename.hasSuffix(".pkdrawing"))54 // the three files share a stem55 #expect(Set([ink.filename.dropLast(9), preview.filename.dropLast(4), pk.filename.dropLast(10)].map(String.init)).count == 1)56 #expect(saved.replaced.isEmpty)57 // a new drawing is cropped to its strokes plus a margin58 #expect(abs(block.width - Double(drawing.bounds.width + 48)) <= 2 && abs(block.height - Double(drawing.bounds.height + 48)) <= 2)59 #expect(block.background == "white")60 // ink: valid spn-ink-1 of the same canvas61 let document = try InkDocument(data: try await file(ink.id, app))62 #expect(document.width == block.width && document.height == block.height)63 #expect(document.strokes.count == 5)64 #expect(document.boundingBox.minX >= 0 && document.boundingBox.maxX <= document.width + 1)65 // pk: the PencilKit drawing as placed on the canvas66 let stored = try PKDrawing(data: try await file(pk.id, app))67 #expect(stored == saved.drawing)68 #expect(stored.strokes.count == drawing.strokes.count)69 #expect(abs(stored.bounds.minX - 24) <= 1 && abs(stored.bounds.minY - 24) <= 1)70 // preview: a PNG of the whole canvas at 2×, on white paper71 let image = try #require(UIImage(data: try await file(preview.id, app))?.cgImage)72 #expect(abs(Double(image.width) - block.width * 2) <= 2 && abs(Double(image.height) - block.height * 2) <= 2)73 #expect(image.alphaInfo == .none || image.alphaInfo == .noneSkipLast || image.alphaInfo == .noneSkipFirst)74 // the node references the three files (so sync and export keep them)75 var note = NoteDocument()76 _ = note.insertBlockAtom(block.node(), after: 0)77 #expect(Set(note.attachmentReferences()) == Set([ink.id, preview.id, pk.id]))78 // the ink JSON is hidden from the attachment list like the PencilKit source79 #expect(AttachmentPresentation.isDrawingSource(ink) && AttachmentPresentation.isDrawingSource(pk) && !AttachmentPresentation.isDrawingSource(preview))80 }8182 @Test func aTransparentDrawingHasATransparentPreview() async throws {83 let app = try makeApp()84 let noteId = try await app.createNote(title: "Dessins")85 let saved = try await DrawingBlockIO.save(InkSamples.drawing(), background: DrawingPaper.none, into: DrawingBlock(), app: app, noteId: noteId)86 let previewId = try #require(saved.block.preview)87 let image = try #require(UIImage(data: try await file(previewId, app))?.cgImage)88 #expect(image.alphaInfo == .premultipliedLast || image.alphaInfo == .premultipliedFirst || image.alphaInfo == .last || image.alphaInfo == .first)89 #expect(saved.block.background == "none")90 }9192 @Test func loadingReadsThePencilKitDataAndRebuildsFromInkWithoutIt() async throws {93 let app = try makeApp()94 let noteId = try await app.createNote(title: "Dessins")95 let saved = try await DrawingBlockIO.save(InkSamples.drawing(), background: DrawingPaper.white, into: DrawingBlock(), app: app, noteId: noteId)96 // fast path97 let fast = try await DrawingBlockIO.load(saved.block, app: app).get()98 #expect(fast.origin == .pencilKit)99 #expect(fast.drawing == saved.drawing)100 #expect(fast.canvas == CGSize(width: saved.block.width, height: saved.block.height))101 // pk:null (edited on the web or on the Mac): the strokes are rebuilt from the ink102 var web = saved.block103 web.pk = nil104 let rebuilt = try await DrawingBlockIO.load(web, app: app).get()105 #expect(rebuilt.origin == .rebuiltFromInk)106 #expect(rebuilt.drawing.strokes.count == saved.drawing.strokes.count)107 #expect(rebuilt.drawing.strokes.map(\.ink.inkType) == saved.drawing.strokes.map(\.ink.inkType))108 let a = rebuilt.drawing.bounds, b = saved.drawing.bounds109 #expect(abs(a.minX - b.minX) <= 1 && abs(a.minY - b.minY) <= 1 && abs(a.maxX - b.maxX) <= 1 && abs(a.maxY - b.maxY) <= 1)110 // an unreadable pk falls back on the ink111 var broken = saved.block112 broken.pk = try DrawingBlockIO.attachmentId(of: await app.attach(data: Data("pas un dessin".utf8), filename: "drawing-x.pkdrawing", mime: "application/octet-stream", to: noteId))113 #expect(try await DrawingBlockIO.load(broken, app: app).get().origin == .rebuiltFromInk)114 // a block without any file is a new drawing115 let fresh = try await DrawingBlockIO.load(DrawingBlock(), app: app).get()116 #expect(fresh.origin == .new && fresh.drawing.strokes.isEmpty && fresh.canvas == nil)117 }118119 @Test func aDrawingMadeOnTheWebOpensAndKeepsItsCanvas() async throws {120 let app = try makeApp()121 let noteId = try await app.createNote(title: "Dessins")122 let inkId = try DrawingBlockIO.attachmentId(of: await app.attach(data: try InkConversionTests.webFixture(), filename: "drawing-1763560000000.ink.json", mime: "application/json", to: noteId))123 var block = DrawingBlock(width: 1200, height: 800)124 block.ink = inkId125 let loaded = try await DrawingBlockIO.load(block, app: app).get()126 #expect(loaded.origin == .rebuiltFromInk && loaded.drawing.strokes.count == 5)127 #expect(loaded.canvas == CGSize(width: 1200, height: 800))128 // edited here and saved: the canvas of the web is kept (no crop, no shift), and the block now has PencilKit data129 var edited = loaded.drawing130 edited.strokes.append(InkSamples.stroke(.pen, color: .black, count: 8) { CGPoint(x: 50 + 100 * $0, y: 60) })131 let saved = try await DrawingBlockIO.save(edited, background: block.background, into: block, app: app, noteId: noteId)132 #expect(saved.block.width == 1200 && saved.block.height == 800)133 #expect(saved.block.pk != nil && saved.block.ink != inkId)134 #expect(saved.replaced == [inkId])135 #expect(saved.drawing == edited)136 #expect(saved.ink.strokes.count == 6)137 #expect(saved.block.background == "none")138 }139140 @Test func missingOrUnreadableFilesAreReportedAndNothingChanges() async throws {141 let app = try makeApp()142 let noteId = try await app.createNote(title: "Dessins")143 var block = DrawingBlock(width: 300, height: 200)144 block.ink = UUID().uuidString.lowercased()145 block.pk = UUID().uuidString.lowercased()146 block.preview = UUID().uuidString.lowercased()147 let before = block.node()148 #expect(await DrawingBlockIO.load(block, app: app) .failure == .filesMissing)149 #expect(block.node() == before)150 #expect(try await app.store.attachments(pageId: noteId).isEmpty)151 // only a picture: nothing to edit152 var pictureOnly = DrawingBlock()153 pictureOnly.preview = UUID().uuidString.lowercased()154 #expect(await DrawingBlockIO.load(pictureOnly, app: app).failure == .previewOnly)155 // files that are not drawings156 var garbage = DrawingBlock()157 garbage.ink = try DrawingBlockIO.attachmentId(of: await app.attach(data: Data("{\"format\":\"autre\"}".utf8), filename: "drawing-y.ink.json", mime: "application/json", to: noteId))158 #expect(await DrawingBlockIO.load(garbage, app: app).failure == .unreadable)159 #expect(!DrawingBlockIO.Failure.filesMissing.message.isEmpty)160 }161162 @Test func replacedAttachmentsAreNeverDeleted() async throws {163 let app = try makeApp()164 let noteId = try await app.createNote(title: "Dessins")165 let first = try await DrawingBlockIO.save(InkSamples.drawing(), background: DrawingPaper.white, into: DrawingBlock(), app: app, noteId: noteId)166 var edited = first.drawing167 edited.strokes.removeLast()168 let second = try await DrawingBlockIO.save(edited, background: DrawingPaper.white, into: first.block, app: app, noteId: noteId)169 #expect(Set(second.replaced) == Set([first.block.ink, first.block.preview, first.block.pk].compactMap { $0 }))170 let records = try await app.store.attachments(pageId: noteId)171 #expect(records.count == 6)172 for id in second.replaced { #expect(try await file(id, app).isEmpty == false) }173 // an existing canvas keeps its size when strokes are removed174 #expect(second.block.width == first.block.width && second.block.height == first.block.height)175 #expect(second.block.id == first.block.id)176 }177178 @Test func theProviderPresentsDrawingAndPDFBlocksFullScreen() {179 BlockDetails.resetForTests()180 let drawing = DrawingBlock().node(), pdf = PDFBlock(attachmentId: UUID().uuidString.lowercased(), name: "a.pdf").node()181 #expect(BlockDetails.provider(for: drawing) is PencilBlockDetails)182 #expect(BlockDetails.provider(for: pdf) is PencilBlockDetails)183 #expect(BlockDetails.provider(for: drawing)?.style(for: drawing) == .fullScreen)184 #expect(BlockDetails.provider(for: pdf)?.style(for: pdf) == .fullScreen)185 #expect(!(BlockDetails.provider(for: PMNode.horizontalRule()) is PencilBlockDetails))186 #expect(!(BlockDetails.provider(for: PMNode.image(src: "/api/files/x", alt: "drawing-1.png")) is PencilBlockDetails))187 }188189 @Test func theContextInsertsANewBlockThenWritesBelowIt() async throws {190 let app = try makeApp()191 let model = try await openNote(app)192 model.controller.textView.selectedRange = NSRange(location: 2, length: 0) // in "Avant"193 var dismissed = 0194 let context = model.blockDetailContext(range: nil, dismiss: { dismissed += 1 })195 #expect(context.isEditable)196 let saved = try await DrawingBlockIO.save(InkSamples.drawing(), background: DrawingPaper.white, into: DrawingBlock(), app: app, noteId: model.noteId)197 context.commit(saved.block.node())198 context.insertMarkdownBelow("Texte lu")199 // a second commit replaces the same block (it does not insert another one)200 var again = saved.block201 again.background = "none"202 context.commit(again.node())203 context.dismiss()204 #expect(dismissed == 1)205 let lines = model.controller.document().lines206 #expect(lines.filter { $0.type == "drawing" }.count == 1)207 let index = try #require(lines.firstIndex { $0.type == "drawing" })208 #expect(lines[index].attrs["background"] == .string("none"))209 let texts = lines.map { NoteDocument(lines: [$0]).plainText().trimmingCharacters(in: .whitespacesAndNewlines) }210 #expect(texts[index - 1] == "Avant")211 #expect(texts[(index + 1)...].first { !$0.isEmpty } == "Texte lu")212 #expect(texts.last { !$0.isEmpty } == "Après")213 // nil deletes the block214 context.commit(nil)215 #expect(!model.controller.document().lines.contains { $0.type == "drawing" })216 }217}218219extension Result {220 var failure: Failure? { if case .failure(let error) = self { return error } else { return nil } }221}222223// MARK: - 1.0 drawings224225@MainActor226@Suite("Drawing — dessins 1.0 et conversion") struct DrawingMigrationTests {227 /// A note as SP Notes iOS 1.0 wrote it: a picture `drawing-<uuid>.png` and its companion `.pkdrawing`, found by name.228 func legacyNote(_ app: AppModel) async throws -> (NoteEditorModel, PKDrawing) {229 let model = try await openNote(app)230 model.controller.textView.selectedRange = NSRange(location: 2, length: 0)231 let drawing = InkSamples.drawing()232 await model.insertDrawing(png: DrawingRenderer.png(of: drawing), source: drawing.dataRepresentation(), replacing: nil)233 return (model, drawing)234 }235236 @Test func a10DrawingIsStillAPictureWithItsSource() async throws {237 let app = try makeApp()238 let (model, drawing) = try await legacyNote(app)239 let image = try #require(atoms(model).first { $0.node.type == "image" })240 #expect(NoteEditorModel.isDrawing(image.node))241 let source = try #require(await model.drawingSource(forImage: image.node))242 #expect(try PKDrawing(data: source) == drawing)243 #expect(!model.controller.document().lines.contains { $0.type == "drawing" })244 }245246 @Test func editingWithoutConvertingKeepsThe10Format() async throws {247 let app = try makeApp()248 let (model, drawing) = try await legacyNote(app)249 let image = try #require(atoms(model).first { $0.node.type == "image" })250 // what "Garder le format actuel" does with the edited drawing251 var edited = drawing252 edited.strokes.append(InkSamples.stroke(.pen, color: .black, count: 6) { CGPoint(x: 40 + 80 * $0, y: 700) })253 let result = LegacyDrawingScreen.result(DrawingEditorOutput(drawing: edited, background: "none", insertions: []))254 #expect(!result.isEmpty && result.recognizedText == nil)255 await model.insertDrawing(png: result.png, source: result.drawingData, replacing: image.range)256 let after = atoms(model)257 #expect(after.filter { $0.node.type == "image" }.count == 1)258 #expect(!after.contains { $0.node.type == "drawing" })259 let picture = try #require(after.first { $0.node.type == "image" })260 #expect(NoteEditorModel.isDrawing(picture.node))261 let editedSource = try #require(await model.drawingSource(forImage: picture.node))262 #expect(try PKDrawing(data: editedSource) == edited)263 // the previous picture and source are still attached (1.0 never deleted them either)264 let records = try await app.store.attachments(pageId: model.noteId)265 #expect(records.filter { $0.filename.hasSuffix(".pkdrawing") }.count == 2 && records.filter { $0.filename.hasSuffix(".png") }.count == 2)266 #expect(!records.contains { $0.filename.hasSuffix(".ink.json") })267 }268269 @Test func convertingMakesADrawingBlockAndLosesNothing() async throws {270 let app = try makeApp()271 let (model, drawing) = try await legacyNote(app)272 let image = try #require(atoms(model).first { $0.node.type == "image" })273 let legacyRecords = try await app.store.attachments(pageId: model.noteId)274 #expect(legacyRecords.count == 2)275 let linesBefore = model.controller.document().lines276 let imageIndex = try #require(linesBefore.firstIndex { $0.type == "image" })277278 await model.convertLegacyDrawing(drawing, at: image.range, insertions: ["Bonjour"])279280 let lines = model.controller.document().lines281 #expect(!lines.contains { $0.type == "image" })282 #expect(lines.firstIndex { $0.type == "drawing" } == imageIndex)283 let node = try #require(atoms(model).first { $0.node.type == "drawing" }).node284 let block = try #require(DrawingBlock(node: node))285 #expect(block.background == "white")286 // pk decodes to the same strokes (moved onto the block's canvas)287 let pkId = try #require(block.pk), inkId = try #require(block.ink), previewId = try #require(block.preview)288 let stored = try PKDrawing(data: try await file(pkId, app))289 #expect(stored.strokes.count == drawing.strokes.count)290 // the inks are those the 1.0 source file holds (PencilKit's own data format turns a programmatic `monoline` into `pen`:291 // the comparison is with what PencilKit reads back from the 1.0 file, which is what a 1.0 user has)292 let legacy = try PKDrawing(data: drawing.dataRepresentation())293 #expect(stored.strokes.map(\.ink.inkType) == legacy.strokes.map(\.ink.inkType))294 let dx = stored.bounds.minX - drawing.bounds.minX, dy = stored.bounds.minY - drawing.bounds.minY295 #expect(stored == drawing.transformed(using: CGAffineTransform(translationX: dx, y: dy)) || abs(stored.bounds.width - drawing.bounds.width) < 0.01)296 for (a, b) in zip(stored.strokes, drawing.strokes) {297 #expect(a.path.count == b.path.count)298 let p = a.path.interpolatedPoint(at: 3).location.applying(a.transform), q = b.path.interpolatedPoint(at: 3).location.applying(b.transform)299 #expect(abs(p.x - q.x - dx) < 0.01 && abs(p.y - q.y - dy) < 0.01)300 }301 // the ink round-trips302 let ink = try InkDocument(data: try await file(inkId, app))303 #expect(ink.strokes.count == 5)304 let again = InkConversion.inkDocument(from: InkConversion.drawing(from: ink), canvasSize: ink.size)305 for (a, b) in zip(ink.strokes, again.strokes) { #expect(a.tool == b.tool && a.color == b.color && InkMetrics.deviation(a, b).position <= 0.75) }306 #expect(try await file(previewId, app).count > 100)307 // the 1.0 picture and its source are still attached, with their files308 let records = try await app.store.attachments(pageId: model.noteId)309 #expect(records.count == 5)310 for legacy in legacyRecords {311 #expect(records.contains { $0.id == legacy.id && $0.deletedAt == nil })312 #expect(try await file(legacy.id, app).isEmpty == false)313 }314 let legacySource = try #require(legacyRecords.first { $0.filename.hasSuffix(".pkdrawing") })315 #expect(try PKDrawing(data: try await file(legacySource.id, app)) == drawing)316 // the recognised text the user asked for sits right below the block317 let index = try #require(lines.firstIndex { $0.type == "drawing" })318 #expect(NoteDocument(lines: [lines[index + 1]]).plainText().contains("Bonjour"))319 // one undo of the editor brings the 1.0 picture back, with its source still there320 model.controller.textView.undoManager?.undo() // the inserted text321 model.controller.textView.undoManager?.undo() // the conversion322 let restored = atoms(model)323 if let picture = restored.first(where: { $0.node.type == "image" }) {324 let source = try #require(await model.drawingSource(forImage: picture.node))325 #expect(try PKDrawing(data: source) == drawing)326 }327 }328329 @Test func aConvertedBlockReopensWithTheSameDrawing() async throws {330 let app = try makeApp()331 let (model, drawing) = try await legacyNote(app)332 let image = try #require(atoms(model).first { $0.node.type == "image" })333 await model.convertLegacyDrawing(drawing, at: image.range)334 await model.flushAndWait()335 // read back from the store, as another launch would336 let page = try #require(try await app.store.page(id: model.noteId))337 let stored = try NoteDocument(json: page.content)338 let line = try #require(stored.lines.first { $0.type == "drawing" })339 let block = try #require(DrawingBlock(node: PMNode(type: "drawing", attrs: line.attrs)))340 let loaded = try await DrawingBlockIO.load(block, app: app).get()341 #expect(loaded.origin == .pencilKit && loaded.drawing.strokes.count == drawing.strokes.count)342 #expect(Set(stored.attachmentReferences()).isSuperset(of: [block.ink, block.preview, block.pk].compactMap { $0 }))343 }344}345