Repository Public
1// SP Notes Core2// File: Tests/SPNotesFeaturesTests/MediaCanvasModelTests.swift3// Description: The canvas view model against the fake backend: loading (sample, empty, offline, server error), editing4// with undo / redo, gestures in screen coordinates, saving and syncing (offline too), changes coming from5// another device, the conflict review (choices, the three decisions, edit-vs-delete, stale), tidy-up, the6// router, the drawing-editor hook, and the hosted renders of the screen.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import CoreGraphics11import Foundation12import SPNotesDocument13import SPNotesSync14import SwiftUI15import Testing16@testable import SPNotesFeatures17@testable import SPNotesFeaturesCatalog1819@MainActor20private func loadedModel(_ world: FeatureWorld, router: FeatureRouterRecorder? = nil) async -> CanvasModel {21 let model = CanvasModel(repositories: world.repositories, pageId: FeatureSamples.canvasPageId, router: router, attachments: FeatureSamples.attachmentLoader(world))22 model.saveDelay = .seconds(3600) // the tests save explicitly23 model.viewSize = CGSize(width: 1000, height: 700)24 await model.load()25 return model26}2728@Suite("Canvas model")29@MainActor30struct MediaCanvasModelTests {31 @Test("happy path: the synced canvas loads with every node kind, titles and pictures")32 func loads() async throws {33 let world = try await FeatureWorld()34 let model = await loadedModel(world)35 #expect(model.phase == .loaded && model.title == "Carte du projet")36 #expect(Set(model.document.nodes.map(\.kind)).isSuperset(of: Set(CanvasNodeKind.allKnown)))37 #expect(model.document.nodes.contains { !$0.kind.isKnown }, "the unknown card is kept")38 #expect(model.titles[FeatureSamples.canvasLinkedPageId] == "Volatilité conditionnelle — notes de lecture")39 #expect(model.titles[FeatureSamples.canvasReferenceId]?.contains("Engle") == true)40 if case .image? = model.media[FeatureSamples.canvasImageId] {} else { Issue.record("the picture was not decoded") }41 if case .image? = model.media[FeatureSamples.canvasPDFId] {} else { Issue.record("the first page of the PDF was not rendered") }42 if case .ink(let ink)? = model.media[FeatureSamples.canvasInkId] { #expect(ink.strokes.count == 3) } else { Issue.record("the ink was not decoded") }43 #expect(model.saveState == .saved && !model.hasConflict && !model.canUndo)44 #expect(model.viewport.zoom == 0.62)45 #expect(CanvasScreen.status(model)?.contains("Cartes : 12 · liens : 7") == true)46 #expect(model.outline.prefix(3).map(\.id) == ["n-text", "n-page", "n-ref"], "reading order: top row first, left to right")47 }4849 @Test("empty: a canvas created on the device (offline) is editable at once and reaches the server later")50 func emptyAndOfflineCreation() async throws {51 let world = try await FeatureWorld(samples: false)52 world.setOffline(true)53 let id = try await world.repositories.canvases.create(title: "Nouveau canevas")54 let model = CanvasModel(repositories: world.repositories, pageId: id)55 model.saveDelay = .seconds(3600)56 await model.load()57 #expect(model.phase == .loaded && model.document.nodes.isEmpty && model.title == "Nouveau canevas")58 let a = model.addNode(.text, at: CGPoint(x: 0, y: 0)) { $0.text = "Idée" }59 let b = model.addNode(.shape, at: CGPoint(x: 400, y: 0))60 #expect(model.connect(from: a, to: b) != nil)61 await model.saveNow()62 #expect(model.saveState == .saved, "saving works offline")63 world.setOffline(false)64 await world.sync()65 let server = try #require(world.fake.canvasData(id).flatMap { try? CanvasDocument(data: $0) })66 #expect(server.nodes.map(\.id).sorted() == [a, b].sorted() && server.edges.count == 1)67 }6869 @Test("offline with nothing on the device: a calm typed failure, then a retry succeeds")70 func offlineUnknownCanvas() async throws {71 let world = try await FeatureWorld(samples: false)72 let id = world.fake.webCreateCanvas(title: "Seulement sur le serveur")73 world.setOffline(true)74 let model = CanvasModel(repositories: world.repositories, pageId: id)75 await model.load()76 #expect(model.phase.failure?.isOffline == true)77 world.setOffline(false)78 await model.load()79 #expect(model.phase == .loaded, "read from the server on demand")80 }8182 @Test("server error and unknown page: the load fails with the server's kind, never a spinner")83 func serverError() async throws {84 let world = try await FeatureWorld(samples: false)85 let id = world.fake.webCreateCanvas(title: "Canevas")86 world.fake.inject(.status(500), method: "GET", path: "^/api/canvases/")87 let model = CanvasModel(repositories: world.repositories, pageId: id)88 await model.load()89 #expect(model.phase.failure?.kind == .server)90 let missing = CanvasModel(repositories: world.repositories, pageId: "00000000-0000-4000-8000-00000000dead")91 await missing.load()92 #expect(missing.phase.failure != nil && missing.phase != .loading)93 }9495 @Test("editing: add, text runs, connect, delete, undo / redo; the stored canvas follows and syncs")96 func editing() async throws {97 let world = try await FeatureWorld()98 let model = await loadedModel(world)99 let count = model.document.nodes.count100 let id = model.addNode(.text, at: CGPoint(x: 2000, y: 2000))101 #expect(model.selection == [id] && model.saveState == .dirty && model.canUndo)102 #expect(model.document.node(id: id)?.frame == CGRect(x: 1880, y: 1940, width: 240, height: 120))103 #expect((model.document.node(id: id)?.z ?? 0) > (model.document.nodes.filter { $0.id != id }.map(\.z).max() ?? 0), "a new card is on top")104 for text in ["B", "Bo", "Bon"] { model.updateNode(id: id, coalescing: true) { $0.text = text } }105 model.endEditing()106 model.undo()107 #expect(model.document.node(id: id)?.text == nil, "a typing run is one undo step")108 model.redo()109 #expect(model.document.node(id: id)?.text == "Bon")110 let edge = try #require(model.connect(from: "n-text", to: id))111 #expect(model.selectedEdgeId == edge && model.connect(from: "n-text", to: id) == edge, "no duplicate edge")112 #expect(model.connect(from: id, to: id) == nil)113 model.updateEdge(id: edge) { $0.label = "mène à"; $0.kind = .line }114 model.select(id)115 model.deleteSelection()116 #expect(model.document.nodes.count == count && model.document.edge(id: edge) == nil, "deleting a card removes its edges")117 model.undo()118 #expect(model.document.edge(id: edge)?.label == "mène à")119 world.setOffline(true) // a save schedules a background cycle: keep it from pushing before the check120 await model.saveNow()121 #expect(model.saveState == .saved)122 let stored = try #require(try await world.repositories.canvases.state(pageId: FeatureSamples.canvasPageId))123 #expect(stored.isDirty && stored.document.node(id: id)?.text == "Bon")124 world.setOffline(false)125 await world.sync()126 let server = try #require(world.fake.canvasData(FeatureSamples.canvasPageId).flatMap { try? CanvasDocument(data: $0) })127 #expect(server.node(id: id)?.text == "Bon" && server.edge(id: edge)?.kind == .line)128 #expect(server.node(id: "n-unknown")?.kind.rawValue == "hologram", "the unknown card went through untouched")129 }130131 @Test("gestures: tap selects, drag moves (one undo step), marquee, resize by handle, connect by drag and by taps, pan")132 func gestures() async throws {133 let world = try await FeatureWorld()134 let model = await loadedModel(world)135 model.setZoom(1)136 func screen(_ world: CGPoint) -> CGPoint { CanvasViewportMath.worldToScreen(model.viewport, world) }137 let text = try #require(model.document.node(id: "n-text"))138139 model.tap(at: screen(text.center))140 #expect(model.selection == ["n-text"])141 model.tap(at: screen(CGPoint(x: 400, y: 80)), extend: true)142 #expect(model.selection == ["n-text", "n-page"])143 model.tap(at: screen(CGPoint(x: -500, y: -500)))144 #expect(model.selection.isEmpty)145146 // Drag a card: moved by the translation divided by the zoom, as one step.147 model.setZoom(2, at: .zero)148 let start = screen(text.center)149 model.beginDrag(at: start)150 model.continueDrag(to: CGPoint(x: start.x + 30, y: start.y + 10))151 model.continueDrag(to: CGPoint(x: start.x + 100, y: start.y - 40))152 model.endDrag(at: CGPoint(x: start.x + 100, y: start.y - 40))153 #expect(model.document.node(id: "n-text")?.frame.origin == CGPoint(x: text.x + 50, y: text.y - 20))154 #expect(model.saveState == .dirty)155 model.undo()156 #expect(model.document.node(id: "n-text")?.frame.origin == text.frame.origin)157 model.setZoom(1, at: .zero)158159 // Marquee on the empty background (pointer behaviour).160 model.clearSelection()161 model.beginDrag(at: screen(CGPoint(x: 20, y: 20)))162 model.continueDrag(to: screen(CGPoint(x: 660, y: 180)))163 #expect(model.marquee != nil && model.selection == ["n-text", "n-page"])164 model.endDrag(at: screen(CGPoint(x: 660, y: 180)))165 #expect(model.marquee == nil)166167 // Resize the group by its south-east handle.168 let box = model.selectionBounds169 let handle = screen(CGPoint(x: box.maxX, y: box.maxY))170 #expect(model.handle(at: handle) == .se)171 model.beginDrag(at: handle)172 model.continueDrag(to: CGPoint(x: handle.x + 60, y: handle.y + 30))173 model.endDrag(at: CGPoint(x: handle.x + 60, y: handle.y + 30))174 #expect(abs(model.selectionBounds.width - (box.width + 60)) < 0.02 && abs(model.selectionBounds.height - (box.height + 30)) < 0.02)175 model.undo()176177 // Connect by dragging from a card to another, then by two taps.178 model.tool = .connect179 let edges = model.document.edges.count180 model.beginDrag(at: screen(CGPoint(x: 160, y: 300))) // inside the diamond n-shape181 model.continueDrag(to: screen(CGPoint(x: 480, y: 280)))182 #expect(model.connectionPreview != nil)183 model.endDrag(at: screen(CGPoint(x: 480, y: 280))) // n-math184 #expect(model.document.edges.count == edges + 1 && model.document.edges.last?.from == "n-shape" && model.document.edges.last?.to == "n-math")185 model.tap(at: screen(CGPoint(x: 850, y: 300))) // n-image186 #expect(model.pendingConnectionFrom == "n-image")187 model.tap(at: screen(CGPoint(x: 1140, y: 400))) // n-link188 #expect(model.document.edges.count == edges + 2 && model.pendingConnectionFrom == nil)189 // A tap on an edge selects it.190 model.tool = .select191 model.tap(at: screen(CGPoint(x: 320, y: 98)))192 #expect(model.selectedEdgeId == "e1" && model.selection.isEmpty)193194 // Pan tool and touch-style background drag move the viewport, never the cards.195 let before = model.document, origin = model.viewport196 model.tool = .pan197 model.beginDrag(at: CGPoint(x: 100, y: 100))198 model.continueDrag(to: CGPoint(x: 160, y: 130))199 model.endDrag(at: CGPoint(x: 160, y: 130))200 #expect(model.viewport.x == origin.x - 60 && model.viewport.y == origin.y - 30 && model.document == before)201 model.tool = .select202 model.emptyDragPans = true203 model.beginDrag(at: screen(CGPoint(x: -300, y: -300)))204 model.continueDrag(to: CGPoint(x: 0, y: 0))205 #expect(model.marquee == nil)206 model.endDrag(at: .zero)207 model.fitToContent()208 #expect(CanvasViewportMath.visibleRect(model.viewport, size: model.viewSize).contains(model.document.bounds))209 }210211 @Test("tidy-up applies Core's deterministic layouts; a multi-selection is laid out alone")212 func tidy() async throws {213 let world = try await FeatureWorld()214 let model = await loadedModel(world)215 let before = model.document216 model.tidy(.grid)217 #expect(model.document == CanvasLayout.grid(before, spacing: 48, origin: before.bounds.origin))218 #expect(CanvasLayout.overlapArea(model.document) == 0)219 model.undo()220 #expect(model.document == before)221 model.selection = ["n-text", "n-page"]222 model.tidy(.grid)223 #expect(model.document.node(id: "n-ref") == before.node(id: "n-ref"), "the rest does not move")224 model.undo()225 model.clearSelection()226 // The web's policy: connected cards → the seeded force layout, translated so that the top-left corner stays.227 model.tidy()228 let raw = CanvasLayout.forceDirected(before, iterations: 200, seed: 1)229 let dx = before.bounds.minX - raw.bounds.minX, dy = before.bounds.minY - raw.bounds.minY230 for node in model.document.nodes {231 let expected = try #require(raw.node(id: node.id))232 #expect(abs(node.x - (expected.x + dx)) < 0.011 && abs(node.y - (expected.y + dy)) < 0.011, "\(node.id)")233 }234 #expect(abs(model.document.bounds.minX - before.bounds.minX) < 0.011 && abs(model.document.bounds.minY - before.bounds.minY) < 0.011)235 model.undo()236 #expect(model.document == before, "one undo step")237 // Without connections among the cards to lay out: the grid.238 model.selection = ["n-link", "n-unknown", "n-drawing"]239 model.tidy()240 #expect(model.document == CanvasLayout.grid(before, spacing: 48, origin: CGPoint(x: 720, y: 360), pinned: Set(before.nodes.map(\.id)).subtracting(["n-link", "n-unknown", "n-drawing"])))241 }242243 @Test("another device edits other cards while this one edits: merged by id, nothing lost, no conflict")244 func externalChanges() async throws {245 let world = try await FeatureWorld()246 let model = await loadedModel(world)247 // Idle editor: the server copy is simply adopted.248 var remote = FeatureSamples.sampleCanvas()249 remote.updateNode(id: "n-link") { $0.text = "Econometrica (revue)" }250 world.fake.webSaveCanvas(FeatureSamples.canvasPageId, data: remote.jsonData())251 await world.sync()252 await model.storeChanged()253 #expect(model.document.node(id: "n-link")?.text == "Econometrica (revue)" && model.saveState == .saved && !model.canUndo)254 // Editing (offline) while the other device adds a card.255 world.setOffline(true)256 model.updateNode(id: "n-text") { $0.text = "Ma question" }257 remote.addNode(CanvasNode(id: "n-remote", kind: .text, x: 0, y: 900))258 world.fake.webSaveCanvas(FeatureSamples.canvasPageId, data: remote.jsonData())259 await model.saveNow()260 world.setOffline(false)261 await world.sync()262 await model.storeChanged()263 #expect(model.document.node(id: "n-remote") != nil && model.document.node(id: "n-text")?.text == "Ma question")264 #expect(!model.hasConflict)265 await world.sync()266 let server = try #require(world.fake.canvasData(FeatureSamples.canvasPageId).flatMap { try? CanvasDocument(data: $0) })267 #expect(server.node(id: "n-remote") != nil && server.node(id: "n-text")?.text == "Ma question")268 }269270 /// The same card edited here and on the server → a conflict waiting in the store.271 private func conflicted(_ world: FeatureWorld, remoteChange: (inout CanvasDocument) -> Void, localChange: (CanvasModel) -> Void) async -> CanvasModel {272 let model = await loadedModel(world)273 // Offline while editing: a save schedules a background cycle, which must not push before the other device writes.274 world.setOffline(true)275 localChange(model)276 await model.saveNow()277 var remote = FeatureSamples.sampleCanvas()278 remoteChange(&remote)279 world.fake.webSaveCanvas(FeatureSamples.canvasPageId, data: remote.jsonData())280 world.setOffline(false)281 await world.sync()282 await model.storeChanged()283 return model284 }285286 @Test("conflict: both copies are kept, the choices build the merged proposal, « appliquer mes choix » settles it everywhere")287 func conflictChoices() async throws {288 let world = try await FeatureWorld()289 let model = await conflicted(world, remoteChange: { remote in290 remote.updateNode(id: "n-text") { $0.text = "Version du serveur" }291 remote.updateNode(id: "n-link") { $0.text = "Lien du serveur" }292 remote.addNode(CanvasNode(id: "n-remote", kind: .text, x: 0, y: 900))293 }, localChange: { model in294 model.updateNode(id: "n-text") { $0.text = "Ma version" }295 model.updateNode(id: "n-link") { $0.text = "Mon lien" }296 })297 let conflict = try #require(model.conflict)298 #expect(conflict.merge.conflicts.map(\.id).sorted() == ["n-link", "n-text"])299 #expect(model.document.node(id: "n-text")?.text == "Ma version", "nothing changed on screen before the decision")300 #expect(model.conflictProposal?.node(id: "n-text")?.text == "Ma version" && model.conflictProposal?.node(id: "n-remote") != nil)301 let textConflict = try #require(conflict.merge.conflicts.first { $0.id == "n-text" })302 #expect(CanvasConflictText.reason(textConflict) == "Modifié sur cet appareil et ailleurs.")303 #expect(CanvasConflictText.side(textConflict.remote, kind: .node).hasPrefix("Version du serveur"))304 model.choose(textConflict, takingRemote: true)305 #expect(model.takesRemote(textConflict) && model.conflictProposal?.node(id: "n-text")?.text == "Version du serveur")306 await model.resolveConflict(.applyChoices)307 #expect(!model.hasConflict && model.actionFailure == nil)308 #expect(model.document.node(id: "n-text")?.text == "Version du serveur" && model.document.node(id: "n-link")?.text == "Mon lien")309 #expect(model.document.node(id: "n-remote") != nil)310 await world.sync()311 let server = try #require(world.fake.canvasData(FeatureSamples.canvasPageId).flatMap { try? CanvasDocument(data: $0) })312 #expect(server.node(id: "n-text")?.text == "Version du serveur" && server.node(id: "n-link")?.text == "Mon lien")313 }314315 @Test("conflict decisions: keep mine, take the server's")316 func conflictDecisions() async throws {317 let mineWorld = try await FeatureWorld()318 let mine = await conflicted(mineWorld, remoteChange: { $0.updateNode(id: "n-text") { $0.text = "Serveur" } }, localChange: { $0.updateNode(id: "n-text") { $0.text = "Moi" } })319 #expect(mine.hasConflict)320 await mine.resolveConflict(.keepMine)321 #expect(!mine.hasConflict && mine.document.node(id: "n-text")?.text == "Moi")322 await mineWorld.sync()323 #expect(mineWorld.fake.canvasData(FeatureSamples.canvasPageId).flatMap { try? CanvasDocument(data: $0) }?.node(id: "n-text")?.text == "Moi")324325 let theirsWorld = try await FeatureWorld()326 let theirs = await conflicted(theirsWorld, remoteChange: { $0.updateNode(id: "n-text") { $0.text = "Serveur" } }, localChange: { $0.updateNode(id: "n-text") { $0.text = "Moi" } })327 await theirs.resolveConflict(.keepTheirs)328 #expect(!theirs.hasConflict && theirs.document.node(id: "n-text")?.text == "Serveur" && !theirs.canUndo)329 }330331 @Test("edited here, deleted on the server: the edited card is kept and the conflict says so (SPEC §7)")332 func editVersusDelete() async throws {333 let world = try await FeatureWorld()334 let model = await conflicted(world, remoteChange: { $0.removeNode(id: "n-ellipse") }, localChange: { $0.updateNode(id: "n-ellipse") { $0.text = "Données 2005–2025" } })335 let conflict = try #require(model.conflict?.merge.conflicts.first { $0.id == "n-ellipse" })336 #expect(conflict.reason == .editedAndDeleted)337 #expect(CanvasConflictText.reason(conflict) == "Modifié ici, supprimé ailleurs : votre version est conservée.")338 #expect(CanvasConflictText.side(conflict.remote, kind: .node) == "Supprimé")339 #expect(model.conflictProposal?.node(id: "n-ellipse")?.text == "Données 2005–2025")340 // The user may still follow the deletion.341 model.choose(conflict, takingRemote: true)342 #expect(model.conflictProposal?.node(id: "n-ellipse") == nil)343 #expect(model.conflictProposal?.edges.contains { $0.from == "n-ellipse" || $0.to == "n-ellipse" } == false)344 }345346 @Test("an edit made while the conflict waits is saved first; the decision is taken on the fresh conflict")347 func conflictAfterFurtherEdits() async throws {348 let world = try await FeatureWorld()349 let model = await conflicted(world, remoteChange: { $0.updateNode(id: "n-text") { $0.text = "Serveur" } }, localChange: { $0.updateNode(id: "n-text") { $0.text = "Moi" } })350 model.updateNode(id: "n-link") { $0.text = "Ajout pendant le conflit" }351 await model.resolveConflict(.keepMine)352 #expect(!model.hasConflict && model.actionFailure == nil)353 #expect(model.document.node(id: "n-link")?.text == "Ajout pendant le conflit" && model.document.node(id: "n-text")?.text == "Moi")354 }355356 @Test("opening what a card points to goes through the router; unsafe links are ignored")357 func opening() async throws {358 let world = try await FeatureWorld()359 let router = FeatureRouterRecorder()360 let model = await loadedModel(world, router: router)361 model.open("n-page")362 model.open("n-link")363 model.open("n-text")364 #expect(router.calls == [.page(id: FeatureSamples.canvasLinkedPageId, blockId: nil), .url(URL(string: "https://www.econometricsociety.org/publications/econometrica")!)])365 model.updateNode(id: "n-link") { $0.url = "javascript:alert(1)" }366 model.open("n-link")367 #expect(router.calls.count == 2)368 #expect(model.canOpen(model.document.node(id: "n-page")!) && !model.canOpen(model.document.node(id: "n-text")!))369 }370371 @Test("drawing cards: the host's editor hook (PencilKit on iOS) is called with the ink and its answer is stored; without a hook the built-in editor opens")372 func drawingHook() async throws {373 let world = try await FeatureWorld()374 let model = await loadedModel(world)375 await model.editDrawing("n-drawing")376 #expect(model.inkSession?.nodeId == "n-drawing" && model.inkSession?.document.strokes.count == 3)377 let fresh = model.addNode(.drawing, at: .zero)378 await model.editDrawing(fresh)379 #expect(model.inkSession?.document.size == CGSize(width: 360, height: 240), "a new drawing has the size of its card (CANVAS.md §1.1)")380 await model.editDrawing("n-drawing")381 // The built-in editor saves through the loader: a new attachment, the card points to it.382 var edited = try #require(model.inkSession?.document)383 edited.strokes.removeLast()384 await model.finishInkSession(edited)385 let newInk = try #require(model.document.node(id: "n-drawing")?.ink)386 #expect(model.inkSession == nil && newInk != FeatureSamples.canvasInkId)387 #expect(try InkDocument(data: try await FeatureSamples.attachmentLoader(world).data(id: newInk)).strokes.count == 2)388 // Offline the upload fails calmly, the editor stays open, the card is unchanged.389 await model.editDrawing("n-drawing")390 world.setOffline(true)391 await model.finishInkSession(InkDocument(width: 10, height: 10))392 #expect(model.actionFailure?.isOffline == true && model.inkSession != nil && model.document.node(id: "n-drawing")?.ink == newInk)393 world.setOffline(false)394 model.inkSession = nil395 // Host hook.396 var received: CanvasDrawingEditRequest?397 model.drawingEditor = { request in398 received = request399 return CanvasDrawingEditResult(inkAttachmentId: "c0a50001-0000-4000-8000-0000000000aa", ink: InkDocument(width: 5, height: 5))400 }401 await model.editDrawing("n-drawing")402 #expect(received?.nodeId == "n-drawing" && received?.inkAttachmentId == newInk && received?.ink?.strokes.count == 2 && received?.pageId == FeatureSamples.canvasPageId)403 #expect(model.document.node(id: "n-drawing")?.ink == "c0a50001-0000-4000-8000-0000000000aa" && model.inkSession == nil)404 model.drawingEditor = { _ in nil }405 await model.editDrawing("n-drawing")406 #expect(model.document.node(id: "n-drawing")?.ink == "c0a50001-0000-4000-8000-0000000000aa", "cancelled: unchanged")407 }408409 @Test("the attachment loader: unavailable fails calmly; standard reads the server copy and uploads")410 func attachmentLoader() async throws {411 let world = try await FeatureWorld()412 await #expect(throws: FeatureFailure.self) { _ = try await FeatureAttachmentLoader.unavailable.data(id: "x") }413 #expect(!FeatureAttachmentLoader.unavailable.canUpload)414 let loader = FeatureSamples.attachmentLoader(world)415 #expect(try await loader.data(id: FeatureSamples.canvasImageId) == FeatureSamples.samplePNG())416 let id = try await loader.upload(Data("abc".utf8), filename: "a.txt", mimeType: "text/plain", pageId: nil)417 #expect(try await loader.data(id: id) == Data("abc".utf8))418 world.setOffline(true)419 do { _ = try await loader.data(id: id); Issue.record("should fail offline") } catch { #expect((error as? FeatureFailure)?.isOffline == true) }420 }421422 @Test("the catalogue lists the media screens and can prepare a conflicted canvas")423 func catalogue() async throws {424 let ids = Set(FeatureCatalog.mediaEntries.map(\.id))425 #expect(ids.isSuperset(of: ["canvas", "canvas-empty", "canvas-conflict", "ink-editor", "drawing-viewer", "pdf-block", "chart-live", "chart-bar", "chart-line", "chart-area", "chart-pie", "chart-scatter"]))426 let world = try await FeatureWorld()427 let id = try await FeatureSamples.makeCanvasConflict(world)428 let model = CanvasModel(repositories: world.repositories, pageId: id)429 await model.load()430 #expect(model.conflict?.merge.conflicts.map(\.id).sorted() == ["n-ellipse", "n-text"])431 let router = FeatureRouterRecorder()432 for entry in FeatureCatalog.mediaEntries { _ = entry.make(world, router) }433 }434435 #if os(macOS)436 @Test("hosted renders: the canvas with every node kind, a selection, the outline, the conflict review, the empty canvas")437 func snapshots() async throws {438 let world = try await FeatureWorld()439 let model = await loadedModel(world)440 model.viewSize = CGSize(width: 880, height: 640)441 Snapshot.render("canvas") { CanvasScreen(model: model) }442 model.select("n-shape")443 Snapshot.render("canvas-selected") { CanvasScreen(model: model) }444 model.clearSelection()445 model.selectedEdgeId = "e1"446 Snapshot.render("canvas-edge") { CanvasScreen(model: model) }447 model.clearSelection()448 model.compactMode = .canvas449 Snapshot.render("canvas-surface") { CanvasScreen(model: model) }450 model.compactMode = .outline451 Snapshot.render("canvas-card-detail") { ScrollView { CanvasNodeDetail(model: model, nodeId: "n-table", compact: true, close: {}).padding(16) } }452453 let conflictWorld = try await FeatureWorld()454 let conflicted = await conflicted(conflictWorld, remoteChange: { remote in455 remote.updateNode(id: "n-text") { $0.text = "Question de recherche (reformulée sur le Web)" }456 remote.removeNode(id: "n-ellipse")457 remote.updateEdge(id: "e2") { $0.label = "s'appuie sur" }458 }, localChange: { model in459 model.updateNode(id: "n-text") { $0.text = "Question de recherche — version du Mac" }460 model.updateNode(id: "n-ellipse") { $0.text = "Données 2005–2025" }461 model.updateEdge(id: "e2") { $0.label = "cite et discute" }462 })463 #expect(conflicted.conflict?.merge.conflicts.count == 3)464 Snapshot.render("canvas-conflict") { CanvasConflictReview(model: conflicted, close: {}) }465 Snapshot.render("canvas-conflict-banner") { CanvasScreen(model: conflicted) }466467 let emptyWorld = try await FeatureWorld(samples: false)468 let id = try await emptyWorld.repositories.canvases.create(title: "Nouveau canevas")469 let empty = CanvasModel(repositories: emptyWorld.repositories, pageId: id)470 await empty.load()471 Snapshot.render("canvas-empty") { CanvasScreen(model: empty) }472473 let offlineWorld = try await FeatureWorld(samples: false)474 let remoteOnly = offlineWorld.fake.webCreateCanvas(title: "Ailleurs")475 offlineWorld.setOffline(true)476 let offline = CanvasModel(repositories: offlineWorld.repositories, pageId: remoteOnly)477 await offline.load()478 Snapshot.render("canvas-offline") { CanvasScreen(model: offline) }479 }480 #endif481}482