Repository Public
1// SP Notes iOS2// File: Tests/ShellBacklinksTests.swift3// Description: « Liens » of a note from the local copy: incoming links (with the block of the link), typed relations (AI ones4// marked, suggestions never presented as accepted), outgoing mentions; pages in the trash or unknown are left out.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import Testing10import SPNotesCore11@testable import SPNotes1213@Suite("Liens d’une note")14@MainActor15struct BacklinksModelTests {16 private func edge(_ id: String, from: String, to: String, kind: String = "link", relType: String = "related", origin: String = "system", status: String = "accepted",17 sourceBlock: String? = nil, targetBlock: String? = nil, extra: String = "") -> RemoteEntity {18 var json = #"{"id":"\#(id)","fromId":"\#(from)","toId":"\#(to)","sourceKind":"page","targetKind":"page","kind":"\#(kind)","relType":"\#(relType)","origin":"\#(origin)","status":"\#(status)","createdAt":"2026-11-01T10:00:00.000Z""#19 if let sourceBlock { json += #","sourceBlockId":"\#(sourceBlock)""# }20 if let targetBlock { json += #","targetBlockId":"\#(targetBlock)""# }21 return RemoteEntity(kind: .edge, id: id, rev: 0, json: Data((json + extra + "}").utf8))22 }2324 @Test func incomingRelationsAndMentionsAreListedWithTheirBlocks() async throws {25 let app = try ShellFixtures.app()26 let store = app.store27 let alpha = try await app.createNote(title: "Alpha")28 let beta = try await app.createNote(title: "Bêta")29 let gamma = try await app.createNote(title: "Gamma")30 let gone = try await app.createNote(title: "À la corbeille")31 try await app.trash(gone)32 // the open note mentions Bêta (twice) and a page this device does not have33 var document = NoteDocument(markdown: "Voir aussi")34 document.insert(lines: [Line(blockAtom: PMNode.mention(id: beta, label: "Bêta"), path: [])], at: 1)35 let open = try await app.createNote(title: "Note ouverte", document: NoteDocument(markdown: "Texte"))36 _ = try await store.applyRemoteEntities([37 edge("e1", from: alpha, to: open, sourceBlock: "blkA"),38 edge("e2", from: gone, to: open), // its source is in the trash39 edge("e3", from: gamma, to: open, kind: "relation", relType: "supports", origin: "user"),40 edge("e4", from: open, to: beta, kind: "relation", relType: "contradicts", origin: "ai", status: "suggested", targetBlock: "blkB",41 extra: #","model":"claude-haiku","rationale":"Les chiffres ne concordent pas.""#),42 edge("e5", from: open, to: alpha, kind: "relation", relType: "related", origin: "ai", status: "rejected"), // refused by the user43 edge("e6", from: beta, to: gamma), // does not touch the open note44 ])4546 let model = BacklinksModel(pageId: open, graph: app.repositories.graph, store: store)47 await model.load()48 #expect(model.isLoaded)49 #expect(model.incoming.map(\.title) == ["Alpha"])50 #expect(model.incoming.first?.pageId == alpha && model.incoming.first?.blockId == "blkA", "the row opens the linking note at the block of the link")5152 #expect(model.relations.map(\.pageId) == [gamma, beta], "accepted first, suggestions after; the rejected one is not shown")53 let supports = try #require(model.relations.first)54 #expect(supports.relationLabel == "appuyée par" && !supports.isAI && !supports.isSuggested)55 let suggestion = try #require(model.relations.last)56 #expect(suggestion.isAI && suggestion.isSuggested, "an AI suggestion is marked and stays a suggestion")57 #expect(suggestion.relationLabel == "contredit" && suggestion.blockId == "blkB" && suggestion.model == "claude-haiku")58 #expect(suggestion.note == "Les chiffres ne concordent pas.")59 // nothing here changed the relation60 #expect(try await app.repositories.graph.localRelations(touching: open).first { $0.id == "e4" }?.status == "suggested")61 #expect(model.count == 3 && !model.isEmpty)62 // what a row routes to63 #expect(AppRoute.note(suggestion.pageId, blockId: suggestion.blockId) == .note(beta, blockId: "blkB"))64 }6566 @Test func outgoingMentionsComeFromTheDocument() async throws {67 let app = try ShellFixtures.app()68 let beta = try await app.createNote(title: "Bêta")69 let missing = "0b7f6c0e-3f0a-4d5e-9b0a-999999999999"70 var document = NoteDocument(markdown: "Voir aussi")71 document.insert(lines: [Line(blockAtom: PMNode.mention(id: beta, label: "Bêta"), path: []),72 Line(blockAtom: PMNode.mention(id: beta, label: "Bêta"), path: []),73 Line(blockAtom: PMNode.mention(id: missing, label: "Inconnue"), path: [])], at: 1)74 let open = try await app.createNote(title: "Note ouverte", document: document)75 #expect(document.pageReferences().contains(beta))76 let model = BacklinksModel(pageId: open, graph: app.repositories.graph, store: app.store)77 await model.load()78 #expect(model.outgoing.map(\.pageId) == [beta], "once per note; a page unknown on this device is not offered")79 #expect(model.outgoing.first?.title == "Bêta" && model.outgoing.first?.blockId == nil)80 #expect(model.incoming.isEmpty && model.relations.isEmpty)81 }8283 @Test func aNoteWithoutLinksIsEmptyAndRelationsReadBothWays() async throws {84 let app = try ShellFixtures.app()85 let lonely = try await app.createNote(title: "Seule")86 let model = BacklinksModel(pageId: lonely, graph: app.repositories.graph, store: app.store)87 await model.load()88 #expect(model.isLoaded && model.isEmpty && model.count == 0)89 #expect(BacklinksText.count(0) == "Aucun lien" && BacklinksText.count(1) == "1 lien" && BacklinksText.count(3) == "3 liens")90 #expect(BacklinksText.relation(RelationType.supports, outward: true) == "appuie")91 #expect(BacklinksText.relation(RelationType.supports, outward: false) == "appuyée par")92 #expect(BacklinksText.relation(RelationType.precedes, outward: false) == BacklinksText.relation(RelationType.follows, outward: true))93 #expect(BacklinksText.relation("something_new", outward: true) == "liée à", "a relation type from a newer server still reads")94 }95}96