Repository Public
1// SP Notes Core2// File: Tests/SPNotesDocumentTests/ModelTests.swift3// Description: JSON values, raw nodes, lossless flattening and rebuilding, block ids.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9@testable import SPNotesDocument1011@Suite struct JSONValueTests {12 @Test func numbersKeepTheirKind() throws {13 let v = try JSONValue(jsonString: #"{"a":1,"b":1.0,"c":1e2,"d":-0.5,"e":9007199254740993,"f":18446744073709551616,"g":-0}"#)14 #expect(v["a"] == .int(1))15 #expect(v["b"] == .double(1.0))16 #expect(v["c"] == .double(100))17 #expect(v["d"] == .double(-0.5))18 #expect(v["e"] == .int(9007199254740993))19 #expect(v["f"] == .double(18446744073709551616))20 #expect(v["g"] == .int(0))21 }2223 @Test func canonicalEncodingSortsKeysAndEscapes() throws {24 let v: JSONValue = ["b": [1, 2.5, nil, true], "a": "q\"\\\n\t\u{1}é😀", "c": [:], "A": 2.0]25 #expect(v.canonicalString() == #"{"A":2,"a":"q\"\\\n\t\u0001é😀","b":[1,2.5,null,true],"c":{}}"#)26 #expect(try JSONValue(data: v.canonicalData()).semanticallyEquals(v))27 #expect(try JSONValue(jsonString: v.canonicalString(pretty: true)).semanticallyEquals(v))28 }2930 @Test func semanticEquality() throws {31 #expect(JSONValue.int(2) != .double(2.0))32 #expect(JSONValue.int(2).semanticallyEquals(.double(2.0)))33 let a = try JSONValue(jsonString: #"{"x":[1,{"k":2.0}],"y":null}"#)34 let b = try JSONValue(jsonString: #"{ "y": null, "x": [1.0, {"k": 2}] }"#)35 #expect(a.semanticallyEquals(b))36 #expect(a.canonicalData() == b.canonicalData())37 #expect(!a.semanticallyEquals(try JSONValue(jsonString: #"{"x":[1,{"k":2.0}]}"#)))38 #expect(!JSONValue.null.semanticallyEquals(.bool(false)))39 #expect(!JSONValue.string("1").semanticallyEquals(.int(1)))40 }4142 @Test func stringsAndEscapes() throws {43 #expect(try JSONValue(jsonString: #""\u00e9\ud83d\ude00\n\/\b\f\r\t""#) == .string("é😀\n/\u{8}\u{c}\r\t"))44 #expect(try JSONValue(jsonString: #""\ud800x""#) == .string("\u{FFFD}x"))45 #expect(try JSONValue(jsonString: " [ ] ") == .array([]))46 }4748 @Test(arguments: ["", "{", "[1,]", "{\"a\":}", "01", "1.", "tru", "\"abc", "{\"a\":1} x", "[1 2]", "\"\\q\"", "nul", "-", "{1:2}"])49 func invalidJSONThrows(_ s: String) {50 #expect(throws: DocumentError.self) { try JSONValue(jsonString: s) }51 }5253 @Test func codable() throws {54 let v: JSONValue = ["n": 3, "d": 1.5, "s": "x", "b": false, "z": nil, "a": [1, "two", ["k": 9]]]55 let back = try JSONDecoder().decode(JSONValue.self, from: JSONEncoder().encode(v))56 #expect(back.semanticallyEquals(v))57 #expect(back["n"] == .int(3))58 #expect(v["a"]?.arrayValue?.count == 3 && v["s"]?.stringValue == "x" && v["b"]?.boolValue == false && v["z"]?.isNull == true)59 #expect(v["n"]?.intValue == 3 && v["d"]?.doubleValue == 1.5 && JSONValue.double(4).intValue == 4 && v["d"]?.intValue == nil)60 }61}6263@Suite struct RawTreeTests {64 @Test func nodeKeepsAbsentVersusEmpty() throws {65 for json in [#"{"type":"paragraph"}"#, #"{"type":"paragraph","attrs":{}}"#, #"{"type":"paragraph","content":[]}"#,66 #"{"type":"paragraph","attrs":{},"content":[],"marks":[]}"#, #"{"type":"x","attrs":null,"weird":[1,{"a":2}],"text":5}"#,67 #"{"type":"text","text":"a","marks":[{"type":"bold","attrs":{}},{"type":"m","extra":1}]}"#] {68 let v = try JSONValue(jsonString: json)69 let n = try PMNode(json: v)70 #expect(n.jsonValue.semanticallyEquals(v), "\(json)")71 #expect(try PMNode(data: n.jsonData()) == n)72 }73 let n = try PMNode(data: Data(#"{"type":"x","attrs":null,"k":1}"#.utf8))74 #expect(n.attrs == nil && n.extra == ["attrs": .null, "k": 1])75 }7677 @Test func invalidNodesThrow() {78 for json in [#"[]"#, #"{"attrs":{}}"#, #"{"type":1}"#, #"{"type":"doc","content":[1]}"#, #"{"type":"doc","content":[{"text":"a"}]}"#] {79 #expect(throws: DocumentError.self) { try PMNode(data: Data(json.utf8)) }80 }81 }8283 @Test func codable() throws {84 let node = try Fixture.node("unknown")85 let back = try JSONDecoder().decode(PMNode.self, from: JSONEncoder().encode(node))86 #expect(back == node)87 #expect(throws: (any Error).self) { try JSONDecoder().decode(PMNode.self, from: Data("{}".utf8)) }88 let mark = try JSONDecoder().decode(PMMark.self, from: Data(#"{"type":"link","attrs":{"href":"x"}}"#.utf8))89 #expect(mark == PMMark(type: "link", attrs: ["href": "x"]))90 }91}9293@Suite struct RoundTripTests {94 @Test(arguments: ["rich", "unknown"])95 func fixtureIsIdentical(_ name: String) throws {96 let source = try JSONValue(data: Fixture.data(name))97 let doc = try Fixture.doc(name)98 #expect(doc.pmNode().jsonValue.semanticallyEquals(source))99 #expect(try JSONValue(data: doc.jsonData()).semanticallyEquals(source))100 #expect(try doc.jsonData() == source.canonicalData())101 #expect(try NoteDocument(json: doc.jsonData()) == doc)102 }103104 @Test(arguments: [#"{"type":"doc"}"#, #"{"type":"doc","content":[]}"#, #"{"type":"doc","content":[{"type":"paragraph"}]}"#,105 #"{"type":"doc","attrs":{},"content":[{"type":"paragraph","content":[]},{"type":"paragraph","attrs":{}}]}"#,106 #"{"type":"doc","content":[{"type":"bulletList"},{"type":"listItem","content":[]},{"type":"blockquote","content":[{"type":"blockquote","content":[{"type":"horizontalRule"}]}]}]}"#,107 #"{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"a"},{"type":"text","text":"b"},{"type":"text","text":""},{"type":"text","text":"c","marks":[]},{"type":"text","text":"d","marks":[{"type":"italic"},{"type":"bold"}]}]}]}"#,108 #"{"type":"note","content":[{"type":"text","text":"top-level text"},{"type":"details","content":[{"type":"detailsSummary"},{"type":"detailsContent","content":[{"type":"paragraph"}]}]}]}"#])109 func edgeDocumentsAreIdentical(_ json: String) throws {110 let source = try JSONValue(jsonString: json)111 let doc = try NoteDocument(json: Data(json.utf8))112 #expect(doc.pmNode().jsonValue.semanticallyEquals(source))113 }114115 @Test func randomDocumentsAreIdentical() throws {116 var r = SplitMix64(seed: 20261118)117 for i in 0..<600 {118 let node = Random.anyNode(&r, depth: 1 + r.int(5), type: "doc")119 let doc = NoteDocument(node: node)120 #expect(doc.pmNode() == node, "random document \(i)")121 let bytes = node.jsonData()122 #expect(try NoteDocument(json: bytes).jsonData() == bytes, "random document \(i) through JSON")123 }124 }125126 @Test func flatteningRules() throws {127 let doc = try Fixture.doc("rich")128 // A nested checklist: [taskList, taskItem, taskList, taskItem].129 let sub = try #require(doc.lines.first { $0.runs.first == .text("Sous-tâche ", marks: []) })130 #expect(sub.path.map(\.type) == ["taskList", "taskItem", "taskList", "taskItem"])131 #expect(Set(sub.path.map(\.key)).count == 4)132 #expect(sub.path[3].attrs == ["checked": false])133 // A code block is one line whose text holds the line feeds.134 let code = try #require(doc.lines.first { $0.type == "codeBlock" && $0.attrs["language"] == "python" })135 #expect(code.runs == [.text("def f(x):\n return x * 2\n\nprint(f(21))", marks: [])])136 // Block atoms are single raw lines.137 let table = try #require(doc.lines.first { $0.type == "table" })138 #expect(table.atomNode?.content?.count == 3 && table.path.isEmpty)139 for type in ["image", "fileBlock", "blockMath", "horizontalRule", "youtube"] {140 let line = try #require(doc.lines.first { $0.type == type && $0.path.isEmpty })141 #expect(!line.isText && line.atomNode?.type == type)142 }143 // Details: the summary sits in the details frame, the body one frame deeper.144 let summary = try #require(doc.lines.first { $0.type == "detailsSummary" })145 #expect(summary.path.map(\.type) == ["details"])146 let hidden = try #require(doc.lines.first { $0.runs == [.text("Contenu caché.", marks: [])] })147 #expect(hidden.path.map(\.type) == ["details", "detailsContent"] && hidden.path[0].key == summary.path[0].key)148 // Inline atoms and marks.149 let mentionLine = try #require(doc.lines.first { $0.runs.contains { if case .atom(let n) = $0 { return n.type == "mention" }; return false } })150 #expect(mentionLine.runs.count == 11)151 #expect(mentionLine.inlineLength == mentionLine.displayString.utf16.count)152 // Every container key designates one contiguous group.153 expectValid(doc, "rich")154 // 53 lines: every text block and atom, containers are frames only.155 #expect(doc.lines.allSatisfy { !["bulletList", "orderedList", "taskList", "listItem", "taskItem", "blockquote", "callout", "details", "detailsContent", "aiBlock"].contains($0.type) })156 }157158 @Test func unknownNodesAreClassifiedFromTheirChildren() throws {159 let doc = try Fixture.doc("unknown")160 #expect(doc.lines.map(\.type) == ["paragraph", "widget", "paragraph", "mysteryText", "paragraph", "bulletList", "paragraph", "heading", "paragraph", "rawText", "image"])161 #expect(doc.lines[2].path.map(\.type) == ["columns", "column"])162 #expect(doc.lines[3].isText && doc.lines[3].path.map(\.type) == ["columns", "column"])163 #expect(doc.lines[4].path[1].key != doc.lines[3].path[1].key && doc.lines[4].path[0].key == doc.lines[3].path[0].key)164 #expect(!doc.lines[1].isText && !doc.lines[5].isText && !doc.lines[9].isText)165 // Odd text nodes are kept raw so that they come back unchanged.166 #expect(doc.lines[0].runs.count == 7)167 #expect(doc.lines[0].runs.filter { if case .text = $0 { return true }; return false }.count == 3)168 }169170 @Test func serializationMergesEqualRunsAndDropsEmptyOnes() {171 var doc = NoteDocument()172 doc.lines[0].content = .inline([.text("a", marks: []), .text("", marks: [.mark(.bold)]), .text("b", marks: []), .text("c", marks: [.mark(.bold)]),173 .text("d", marks: [.mark(.bold)]), .atom(.hardBreak()), .text("", marks: [])])174 #expect(sexp(doc) == #"p("ab" "cd"[bold] br)"#)175 doc.lines[0].content = .inline([.text("", marks: [])])176 #expect(doc.pmNode().content?[0].content == nil)177 }178179 @Test func structuralEqualityIgnoresKeys() throws {180 let a = try Fixture.doc("rich")181 var b = a182 let key = b.lines[6].path[0].key183 let fresh = b.newContainerKey()184 b.updateContainer(key: key) { $0.key = fresh }185 #expect(a != b && a.isStructurallyEqual(to: b))186 b.lines[0].content = .inline([.text("x", marks: [])])187 #expect(!a.isStructurallyEqual(to: b))188 }189190 @Test func emptyAndBuiltDocuments() throws {191 let fresh = NoteDocument()192 expectValid(fresh)193 #expect(sexp(fresh) == "p")194 var built = NoteDocument(lines: [.heading(level: 7, [.text("T", marks: [])]), .paragraph(text: "a\nb"), .codeBlock(text: "x\ny")])195 let quote = built.makeContainer(NodeType.blockquote)196 #expect(quote.key == 1 && built.newContainerKey() == 2)197 built.lines.append(.paragraph(text: "q", path: [quote]))198 #expect(sexp(built) == "h3(\"T\") p(\"a\" br \"b\") code(\"x\ny\") bq(p(\"q\"))")199 expectValid(built)200 let existing = NoteDocument(lines: [.paragraph(path: [Container(key: 41, type: "blockquote", attrs: ["id": "abcdefghij"])])])201 var copy = existing202 #expect(copy.newContainerKey() == 42)203 }204205 @Test func pastedLinesGetFreshKeys() throws {206 var doc = md("- a\n- b")207 let source = md("- x\n - y\n\n> q")208 doc.insert(lines: source.lines, at: 2)209 #expect(sexp(doc) == #"ul(li(p("a")) li(p("b"))) ul(li(p("x") ul(li(p("y"))))) bq(p("q"))"#)210 #expect(Set(doc.lines.flatMap(\.path).map(\.key)).count == 3 + 5)211 doc.ensureBlockIds()212 expectValid(doc)213 }214}215216@Suite struct BlockIdTests {217 static let alphabet = Set("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-")218219 func ids(_ n: PMNode) -> [(String, JSONValue?)] {220 var out: [(String, JSONValue?)] = []221 TextTools.walk(n) { out.append(($0.type, $0.attrs?["id"])) }222 return out223 }224225 @Test func generatedIdsUseTheNanoidAlphabet() {226 var seen = Set<String>()227 for _ in 0..<2000 {228 let id = BlockID.generate()229 #expect(id.count == 10 && id.allSatisfy(Self.alphabet.contains))230 seen.insert(id)231 }232 #expect(seen.count == 2000)233 }234235 @Test func completeDocumentIsLeftUntouched() throws {236 // rich.json was written by the 2.x web editor: every block has its id except the list items, which take one237 // since 3.0. Nothing else moves, and a second pass changes nothing.238 let doc = try Fixture.doc("rich")239 var copy = doc240 copy.ensureBlockIds()241 func withoutItemIds(_ n: PMNode) -> PMNode {242 var n = n243 if n.type == "listItem" || n.type == "taskItem" { n.attrs?["id"] = nil; if n.attrs?.isEmpty == true { n.attrs = nil } }244 n.content = n.content?.map(withoutItemIds)245 return n246 }247 #expect(copy != doc)248 #expect(withoutItemIds(copy.pmNode()) == doc.pmNode())249 let again = copy250 copy.ensureBlockIds()251 #expect(copy == again)252 #expect(copy.schemaViolations(strict: true) == doc.schemaViolations(strict: true))253 }254255 @Test func missingIdsAreAssignedToTheServerTypesOnly() throws {256 let json = #"""257 {"type":"doc","content":[258 {"type":"paragraph"},{"type":"heading","attrs":{"level":2}},{"type":"paragraph","attrs":{"id":""}},{"type":"paragraph","attrs":{"id":7}},259 {"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph"},{"type":"taskList","content":[{"type":"taskItem","attrs":{"checked":true},"content":[{"type":"paragraph"}]}]}]}]},260 {"type":"blockquote","content":[{"type":"codeBlock"}]},261 {"type":"callout","content":[{"type":"paragraph"}]},{"type":"aiBlock","content":[{"type":"paragraph"}]},262 {"type":"details","content":[{"type":"detailsSummary"},{"type":"detailsContent","content":[{"type":"blockMath"}]}]},263 {"type":"table","content":[{"type":"tableRow","content":[{"type":"tableCell","content":[{"type":"paragraph"},{"type":"image"}]}]}]},264 {"type":"image"},{"type":"fileBlock"},{"type":"orderedList","content":[{"type":"listItem","content":[{"type":"paragraph"}]}]},265 {"type":"horizontalRule"},{"type":"youtube"},{"type":"footnote"},{"type":"widget"},{"type":"paragraph","content":[{"type":"mention"},{"type":"text","text":"x"}]}266 ]}267 """#268 var doc = try NoteDocument(json: Data(json.utf8))269 let before = doc.pmNode()270 doc.ensureBlockIds()271 let after = doc.pmNode()272 let b = ids(before), a = ids(after)273 #expect(a.map(\.0) == b.map(\.0))274 var seen = Set<String>()275 let idTypes: Set = ["paragraph", "heading", "blockquote", "codeBlock", "bulletList", "orderedList", "taskList", "table", "callout", "details", "aiBlock", "blockMath", "image", "fileBlock",276 "listItem", "taskItem"] // 3.0: list items too (the ten new block atoms are covered in NextNodeTests)277 for ((type, old), (_, new)) in zip(b, a) {278 if idTypes.contains(type) {279 let id = try #require(new?.stringValue, "\(type) has no id")280 #expect(id.count == 10 && id.allSatisfy(Self.alphabet.contains) && seen.insert(id).inserted)281 } else {282 #expect(new == old, "\(type) must not receive an id")283 }284 }285 #expect(seen.count == 27)286 // Nothing else changed, and a second pass changes nothing.287 func stripped(_ n: PMNode) -> PMNode {288 var n = n289 if idTypes.contains(n.type) { n.attrs?["id"] = nil; if n.attrs?.isEmpty == true { n.attrs = nil } }290 n.content = n.content?.map(stripped)291 return n292 }293 #expect(stripped(after) == stripped(try PMNode(data: Data(json.replacingOccurrences(of: #"{"id":""}"#, with: "{}").replacingOccurrences(of: #"{"id":7}"#, with: "{}").utf8))))294 let again = doc295 doc.ensureBlockIds()296 #expect(doc == again)297 }298299 @Test func duplicatedIdsKeepTheFirstInDocumentOrder() throws {300 let json = #"""301 {"type":"doc","content":[302 {"type":"blockquote","attrs":{"id":"dup0000001"},"content":[{"type":"paragraph","attrs":{"id":"dup0000001"}},{"type":"paragraph","attrs":{"id":"keep000001"}}]},303 {"type":"paragraph","attrs":{"id":"keep000001"}},304 {"type":"table","attrs":{"id":"tab0000001"},"content":[{"type":"tableRow","content":[{"type":"tableCell","content":[{"type":"paragraph","attrs":{"id":"tab0000001"}},{"type":"paragraph","attrs":{"id":"cell000001"}}]}]}]},305 {"type":"paragraph","attrs":{"id":"cell000001"}}306 ]}307 """#308 var doc = try NoteDocument(json: Data(json.utf8))309 doc.ensureBlockIds()310 let all = ids(doc.pmNode()).filter { ["blockquote", "paragraph", "table"].contains($0.0) }.map { $0.1!.stringValue! }311 #expect(all[0] == "dup0000001" && all[1] != "dup0000001" && all[2] == "keep000001" && all[3] != "keep000001")312 #expect(all[4] == "tab0000001" && all[5] != "tab0000001" && all[6] == "cell000001" && all[7] != "cell000001")313 #expect(Set(all).count == 8)314 // The frame of a container is updated on every line of its group.315 #expect(doc.lines[0].path[0].attrs == doc.lines[1].path[0].attrs)316 }317}318