Repository Public
1// SP Notes Core2// File: Tests/SPNotesDocumentTests/DumpTests.swift3// Description: Gathers the documents the module emits (fixture, every command, Markdown imports, merges, fuzzing),4// checks them natively and writes them to /tmp/spn-docs.json for the real web-schema validator5// (docs/reference/README.md). With SPN_DUMP=1 also writes documents and outputs for text parity checks.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import Testing11@testable import SPNotesDocument1213enum Scenarios {14 /// Schema-conforming documents produced by the module, with a label.15 static func all() throws -> [(String, NoteDocument)] {16 var out: [(String, NoteDocument)] = []17 let rich = try Fixture.doc("rich")18 out.append(("fixture rich.json", rich))19 out.append(("new document", NoteDocument()))20 func apply(_ label: String, _ source: NoteDocument, _ body: (inout NoteDocument) -> Void) {21 var d = source22 body(&d)23 d.ensureBlockIds()24 out.append((label, d))25 }26 let n = rich.lines.count27 func index(_ type: String) -> Int { rich.lines.firstIndex { $0.type == type }! }28 apply("setLineType heading", rich) { $0.setLineType(.heading(level: 2), at: 0..<n) }29 apply("setLineType code", rich) { $0.setLineType(.codeBlock(language: "swift"), at: 0..<n) }30 apply("setLineType paragraph", rich) { $0.setLineType(.paragraph, at: 0..<n) }31 for kind in ListKind.allCases {32 apply("toggleList \(kind) everywhere", rich) { $0.toggleList(kind, at: 0..<n) }33 apply("toggleList \(kind) on text", rich) { $0.toggleList(kind, at: 0..<4) }34 apply("toggleList \(kind) twice", rich) { $0.toggleList(kind, at: 3..<12); $0.toggleList(kind, at: 5..<9) }35 }36 apply("indent all", rich) { $0.indentListItems(at: 0..<n) }37 apply("indent twice", rich) { $0.indentListItems(at: 0..<n); $0.indentListItems(at: 0..<n) }38 apply("outdent all", rich) { $0.outdentListItems(at: 0..<n) }39 apply("outdent nested", rich) { $0.outdentListItems(at: 7..<8); $0.outdentListItems(at: 17..<18) }40 apply("blockquote all", rich) { $0.toggleBlockquote(at: 0..<n) }41 apply("blockquote parts", rich) { $0.toggleBlockquote(at: 6..<7); $0.toggleBlockquote(at: 1..<3); $0.toggleBlockquote(at: 20..<21) }42 apply("callout", rich) { $0.toggleCallout(kind: "info", icon: "ℹ️", at: 0..<3); $0.toggleCallout(kind: "danger", icon: "🔥", at: 22..<23) }43 apply("callout off", rich) { $0.toggleCallout(kind: "warning", icon: "⚠️", at: 22..<24) }44 apply("tasks", rich) { d in for i in 0..<n { d.setTaskChecked(i % 2 == 0, at: i) } }45 apply("split every line", rich) { d in var i = 0; while i < d.lines.count { i = d.splitLine(at: i, offset: 3).line + 1 } }46 apply("split at ends", rich) { d in var i = 0; while i < d.lines.count { i = d.splitLine(at: i, offset: 9999).line + 1 } }47 apply("merge every line", rich) { d in var i = d.lines.count - 1; while i > 0 { d.mergeWithPrevious(at: i); i = min(i, d.lines.count) - 1 } }48 apply("merge twice", rich) { d in for _ in 0..<2 { var i = 1; while i < d.lines.count { if d.mergeWithPrevious(at: i) == nil { i += 1 } } } }49 apply("block atoms", rich) { d in50 d.insertBlockAtom(.image(src: "/api/files/11111111-2222-4333-8444-555555555555", alt: "Vue", title: "Titre"), after: 0)51 d.insertBlockAtom(.fileBlock(src: "/api/files/11111111-2222-4333-8444-555555555555", name: "a.pdf", size: 12, mime: "application/pdf", attachmentId: "11111111-2222-4333-8444-555555555555"), after: 6)52 d.insertBlockAtom(.horizontalRule(), after: nil)53 d.insertBlockAtom(.blockMath(latex: "a^2"), after: 20)54 d.insertBlockAtom(.table(rows: 3, cols: 4, withHeaderRow: true), after: d.lines.count - 1)55 d.insertBlockAtom(.table(rows: 1, cols: 1, withHeaderRow: false), after: index("detailsSummary") + 3)56 }57 apply("table editing", rich) { d in58 let i = index("table")59 var t = d.lines[i].atomNode!60 t.setTableCellText("Total\nnet", row: 1, column: 0)61 t.insertTableRow(at: 1); t.insertTableColumn(at: 1); t.deleteTableRow(at: 3); t.insertTableRow(at: 99); t.toggleTableHeaderRow()62 t.setTableCellText("x", row: 0, column: 0); t.deleteTableColumn(at: 2)63 d.replaceBlockAtom(at: i, with: t)64 }65 apply("marks", rich) { d in66 for (k, kind) in MarkKind.allCases.enumerated() { for i in 0..<d.lines.count { d.toggleMark(kind, in: k..<(k + 6), at: i) } }67 }68 apply("links", rich) { d in69 for i in 0..<d.lines.count { d.setLink(href: "/p/0a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d", in: 2..<9, at: i); d.removeLink(in: 4..<6, at: i) }70 }71 apply("typing", rich) { d in72 for i in 0..<d.lines.count {73 d.replaceText(in: 1..<3, at: i, with: "é😀\nz")74 d.insertInlineAtom(.mention(id: "3f2b8c1e-7a4d-4e9b-9c1a-5d6e7f8a9b0c", label: "Page"), at: 2, line: i)75 d.insertInlineAtom(.inlineMath(latex: "x"), at: 0, line: i); d.insertInlineAtom(.hardBreak(), at: 99, line: i)76 }77 }78 apply("delete lines", rich) { d in var i = d.lines.count - 1; while i >= 0 { d.deleteLine(at: i); i -= 3 } }79 apply("paste", rich) { d in d.insert(lines: Array(rich.lines[5...12]), at: 2); d.insert(lines: Array(rich.lines[22...24]), at: d.lines.count) }80 let samples = try #require(try JSONValue(data: Fixture.data("md-samples")).arrayValue).compactMap(\.stringValue)81 for (i, s) in samples.enumerated() { out.append(("markdown sample \(i)", NoteDocument(markdown: s))) }82 out.append(("markdown of the fixture", NoteDocument(markdown: rich.markdown())))83 let base = NoteDocument(markdown: "# T\n\na\n\n- b\n- c\n\n> d\n\ne")84 var mine = base, theirs = base85 mine.replaceText(in: 0..<1, at: 1, with: "A"); mine.insertBlockAtom(.horizontalRule(), after: 0)86 theirs.toggleList(.task, at: 2..<3); theirs.splitLine(at: 5, offset: 1)87 if case .merged(let m) = NoteDocument.merge(base: base, mine: mine, theirs: theirs) { out.append(("merge", m)) }88 var r = SplitMix64(seed: 314)89 for i in 0..<120 {90 var d = i % 3 == 0 ? rich : Random.validDocument(&r)91 var trail: [String] = []92 for _ in 0..<10 { trail.append(Random.command(&d, &r)) }93 d.ensureBlockIds()94 out.append(("fuzz \(i) \(trail.joined(separator: ","))", d))95 }96 return out97 }98}99100@Suite struct DumpTests {101 @Test func everyEmittedDocumentIsValidAndDumped() throws {102 let scenarios = try Scenarios.all()103 #expect(scenarios.count > 200)104 for (label, d) in scenarios {105 expectValid(d, label)106 var again = d107 again.ensureBlockIds()108 // The raw fixture was written by the 2.x web editor: its list items have no id (they take one since 3.0).109 if label.hasPrefix("fixture ") { continue }110 #expect(again == d, "\(label): block ids incomplete")111 }112 let array = JSONValue.array(scenarios.map { $0.1.pmNode().jsonValue })113 try array.canonicalData().write(to: URL(fileURLWithPath: "/tmp/spn-docs.json"))114 try scenarios.enumerated().map { "\($0.offset)\t\($0.element.0)" }.joined(separator: "\n").write(toFile: "/tmp/spn-docs.labels.txt", atomically: true, encoding: .utf8)115116 guard ProcessInfo.processInfo.environment["SPN_DUMP"] != nil else { return }117 // Parity material: any kind of tree (unknown types included) with the module's text outputs.118 var docs = scenarios.map { $0.1.pmNode() } + [try Fixture.node("unknown")]119 var r = SplitMix64(seed: 1234)120 for _ in 0..<400 { docs.append(Random.anyNode(&r, depth: 1 + r.int(5), type: "doc")) }121 let mine = docs.map { n -> JSONValue in122 let d = NoteDocument(node: n)123 return ["text": .string(d.plainText()), "words": .int(Int64(d.wordCount)), "markdown": .string(d.markdown()), "refs": .array(d.pageReferences().map(JSONValue.string))]124 }125 try JSONValue.array(docs.map(\.jsonValue)).canonicalData().write(to: URL(fileURLWithPath: "/tmp/spn-parity-docs.json"))126 try JSONValue.array(mine).canonicalData().write(to: URL(fileURLWithPath: "/tmp/spn-parity-mine.json"))127 }128}129