SPB Git

Repository Public

NextGraphFixtureTests.swift 25.4 KB 417 lines swift

1// SP Notes Core2// File: Tests/SPNotesAPITests/NextGraphFixtureTests.swift3// Description: The knowledge graph models decode the real answers of the scratch backend (group `graph`, recorded by4//              Scripts/recorders/graph.py): relations, concepts, the seven views and the 2.x form, insights, errors.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import XCTest10@testable import SPNotesAPI1112final class NextGraphFixtureTests: XCTestCase {13    // MARK: relations1415    func testRelationLifecycle() throws {16        let created = try NextFixture.decode(GraphRelation.self, "graph", "relation-create")17        XCTAssertEqual(try NextFixture.load("graph", "relation-create").entry.status, 201)18        XCTAssertEqual(created.kind, RelationKind.relation)19        XCTAssertEqual(created.relType, RelationType.supports)20        XCTAssertEqual(created.origin, KnowledgeOrigin.user)21        XCTAssertEqual(created.status, DecisionStatus.accepted)22        XCTAssertEqual(created.sourceKind, GraphEntityKind.page)23        XCTAssertEqual(created.targetKind, GraphEntityKind.page)24        XCTAssertEqual(created.note, "Les données appuient la revue")25        XCTAssertEqual(created.validFrom.map(APIDate.string(from:)), "2026-01-01T00:00:00.000Z")26        XCTAssertEqual(created.validTo.map(APIDate.string(from:)), "2026-12-31T05:00:00.000Z", "an offset is normalised to UTC")27        XCTAssertNotNil(created.decidedAt, "a user relation is decided at creation")28        XCTAssertNil(created.confidence)29        XCTAssertNil(created.model)30        XCTAssertEqual(created.fromTitle, "Fixture · graphe · données")31        XCTAssertEqual(created.toTitle, "Fixture · graphe · revue")32        XCTAssertTrue(created.isEditable)33        XCTAssertFalse(created.isAI)34        let rev = try XCTUnwrap(created.rev)3536        // Same client id again: 200 + x-existing, the stored relation untouched.37        let again = try NextFixture.load("graph", "relation-create-existing")38        XCTAssertEqual(again.entry.status, 200)39        XCTAssertEqual(again.entry.headers["x-existing"], "1")40        XCTAssertEqual(try APIJSON.decoder().decode(GraphRelation.self, from: again.data), created)4142        let patched = try NextFixture.decode(GraphRelation.self, "graph", "relation-patch")43        XCTAssertEqual(patched.id, created.id)44        XCTAssertEqual(patched.relType, RelationType.extends)45        XCTAssertNil(patched.note)46        XCTAssertNil(patched.validTo)47        XCTAssertNotNil(patched.validFrom)48        XCTAssertGreaterThan(try XCTUnwrap(patched.rev), rev)4950        let rejected = try NextFixture.decode(GraphRelation.self, "graph", "relation-patch-rejected")51        XCTAssertEqual(rejected.status, DecisionStatus.rejected)52        XCTAssertEqual(rejected.origin, KnowledgeOrigin.user, "origin never changes")53        let accepted = try NextFixture.decode(GraphRelation.self, "graph", "relation-patch-accepted")54        XCTAssertEqual(accepted.status, DecisionStatus.accepted)55        XCTAssertGreaterThanOrEqual(try XCTUnwrap(accepted.decidedAt), try XCTUnwrap(rejected.decidedAt))56        XCTAssertEqual(try NextFixture.decode(GraphRelation.self, "graph", "relation-get"), accepted)5758        let minimal = try NextFixture.decode(GraphRelation.self, "graph", "relation-create-minimal")59        XCTAssertEqual(minimal.relType, RelationType.precedes)60        XCTAssertNil(minimal.validFrom)61        XCTAssertNil(minimal.sourceBlockId)62        let toConcept = try NextFixture.decode(GraphRelation.self, "graph", "relation-create-concept")63        XCTAssertEqual(toConcept.targetKind, GraphEntityKind.concept)64        XCTAssertEqual(toConcept.relType, RelationType.exampleOf)65        XCTAssertEqual(toConcept.toTitle, "Fixture · hétéroscédasticité")6667        XCTAssertTrue(try NextFixture.decode(OKResult.self, "graph", "relation-delete").ok)68        let gone = try NextFixture.error("graph", "relation-delete-again")69        XCTAssertTrue(gone.isNotFound, "deleting a relation is not idempotent")70        XCTAssertEqual(gone.code, APIError.NextCode.notFound)71    }7273    func testRelationLists() throws {74        let created = try NextFixture.decode(GraphRelation.self, "graph", "relation-create")75        let plain = try NextFixture.decode(RelationListEnvelope.self, "graph", "relations-list").relations76        XCTAssertFalse(plain.isEmpty)77        XCTAssertTrue(plain.allSatisfy { $0.kind == RelationKind.relation }, "kind relation only by default")78        XCTAssertTrue(plain.contains { $0.id == created.id })79        XCTAssertEqual(plain.map(\.createdAt), plain.map(\.createdAt).sorted(by: >), "newest first")8081        let wide = try NextFixture.decode(RelationListEnvelope.self, "graph", "relations-list-include").relations82        let kinds = Set(wide.map(\.kind))83        XCTAssertTrue(kinds.isSuperset(of: [RelationKind.relation, RelationKind.link, RelationKind.cites]), "\(kinds)")84        let link = try XCTUnwrap(wide.first { $0.kind == RelationKind.link })85        XCTAssertEqual(link.origin, KnowledgeOrigin.system)86        XCTAssertFalse(link.isEditable)87        XCTAssertNotNil(link.sourceBlockId, "a document link carries the block that holds it")88        XCTAssertNil(link.decidedAt)89        let cites = try XCTUnwrap(wide.first { $0.kind == RelationKind.cites })90        XCTAssertEqual(cites.targetKind, GraphEntityKind.reference)91        XCTAssertEqual(cites.relType, RelationType.cites)92        XCTAssertEqual(cites.toTitle, "Fixture · Autoregressive conditional heteroscedasticity")9394        let filtered = try NextFixture.decode(RelationListEnvelope.self, "graph", "relations-list-filtered").relations95        XCTAssertFalse(filtered.isEmpty)96        XCTAssertTrue(filtered.allSatisfy { $0.relType == RelationType.precedes && $0.origin == KnowledgeOrigin.user && $0.status == DecisionStatus.accepted })97    }9899    func testRelationErrors() throws {100        let created = try NextFixture.decode(GraphRelation.self, "graph", "relation-create")101        let exists = try NextFixture.error("graph", "relation-exists")102        XCTAssertEqual(exists.status, 409)103        XCTAssertEqual(exists.code, APIError.NextCode.relationExists)104        XCTAssertEqual(exists.existingRelation, ExistingEntity(id: created.id, status: DecisionStatus.accepted))105        XCTAssertNil(exists.existingConcept)106        XCTAssertFalse(exists.isConflict)107108        let badNode = try NextFixture.error("graph", "relation-bad-node")109        XCTAssertEqual(badNode.status, 400)110        XCTAssertEqual(badNode.code, APIError.NextCode.badNode)111        XCTAssertEqual(badNode.badNodeField, "toId")112        let same = try NextFixture.error("graph", "relation-bad-node-same")113        XCTAssertEqual(same.code, APIError.NextCode.badNode)114        XCTAssertNil(same.badNodeField, "both ends are the same node: no field")115        XCTAssertNil(exists.badNodeField)116117        let validity = try NextFixture.error("graph", "relation-bad-validity")118        XCTAssertEqual(validity.code, APIError.NextCode.badRequest)119        XCTAssertTrue(validity.isBadRequest)120        for name in ["relation-validation", "relation-patch-validation"] {121            let validation = try NextFixture.error("graph", name)122            XCTAssertEqual(validation.status, 400)123            XCTAssertNil(validation.code, "body-schema validation carries no code")124            XCTAssertTrue(validation.message.hasPrefix("Validation: relType"))125        }126        for name in ["relation-derived-edge", "relation-delete-derived"] {127            let derived = try NextFixture.error("graph", name)128            XCTAssertEqual(derived.status, 409)129            XCTAssertTrue(derived.isDerivedEdge)130            XCTAssertNil(derived.existingRelation)131        }132        XCTAssertEqual(try NextFixture.error("graph", "relation-not-found").code, APIError.NextCode.notFound)133        XCTAssertEqual(try NextFixture.error("graph", "relation-bad-id").code, APIError.NextCode.badRequest)134    }135136    // MARK: concepts137138    func testConcepts() throws {139        let created = try NextFixture.decode(Concept.self, "graph", "concept-create")140        XCTAssertEqual(try NextFixture.load("graph", "concept-create").entry.status, 201)141        XCTAssertEqual(created.name, "Fixture · hétéroscédasticité")142        XCTAssertEqual(created.description, "Variance non constante")143        XCTAssertEqual(created.origin, KnowledgeOrigin.user)144        XCTAssertEqual(created.status, DecisionStatus.accepted)145        XCTAssertEqual(created.pages, 2)146        XCTAssertEqual(created.suggestedPages, 0)147        XCTAssertNotNil(created.rev)148        XCTAssertNotNil(created.decidedAt)149        let links = try XCTUnwrap(created.pageLinks)150        XCTAssertEqual(links.count, 2)151        XCTAssertEqual(Set(links.compactMap(\.title)), ["Fixture · graphe · revue", "Fixture · graphe · données"])152        XCTAssertTrue(links.allSatisfy { $0.status == DecisionStatus.accepted && $0.origin == KnowledgeOrigin.user && $0.createdAt != nil })153154        let again = try NextFixture.load("graph", "concept-create-existing")155        XCTAssertEqual(again.entry.status, 200)156        XCTAssertEqual(again.entry.headers["x-existing"], "1")157        XCTAssertEqual(try APIJSON.decoder().decode(Concept.self, from: again.data).description, "Variance non constante", "a retry never overwrites")158159        XCTAssertEqual(try NextFixture.decode(Concept.self, "graph", "concept-get"), created)160161        let patched = try NextFixture.decode(Concept.self, "graph", "concept-patch")162        XCTAssertEqual(patched.description, "Variance conditionnelle")163        XCTAssertEqual(patched.pages, 2)164        XCTAssertEqual(Set(patched.pageLinks?.compactMap(\.title) ?? []), ["Fixture · graphe · revue", "Fixture · graphe · méthode"])165        XCTAssertGreaterThan(try XCTUnwrap(patched.rev), try XCTUnwrap(created.rev))166167        let decided = try NextFixture.decode(Concept.self, "graph", "concept-patch-page-links")168        XCTAssertEqual(decided.pages, 1, "a rejected link no longer counts")169        XCTAssertEqual(decided.pageLinks?.first { $0.title == "Fixture · graphe · méthode" }?.status, DecisionStatus.rejected)170171        let minimal = try NextFixture.decode(Concept.self, "graph", "concept-create-minimal")172        XCTAssertNil(minimal.description)173        XCTAssertEqual(minimal.pageLinks, [])174        let viaCollection = try NextFixture.decode(Concept.self, "graph", "concept-patch-collection")175        XCTAssertEqual(viaCollection.id, minimal.id)176        XCTAssertEqual(viaCollection.status, DecisionStatus.rejected)177178        let list = try NextFixture.decode(ConceptListEnvelope.self, "graph", "concepts-list").concepts179        let listed = try XCTUnwrap(list.first { $0.id == created.id })180        XCTAssertNil(listed.pageLinks, "the list has counts, not links")181        XCTAssertEqual(listed.pages, 1)182        XCTAssertFalse(list.contains { $0.id == minimal.id }, "accepted concepts only by default")183        XCTAssertEqual(list.map(\.name), list.map(\.name).sorted { $0.localizedCaseInsensitiveCompare($1) == .orderedAscending }, "by name")184        XCTAssertEqual(try NextFixture.decode(ConceptListEnvelope.self, "graph", "concepts-list-page").concepts.map(\.id), [created.id])185        let rejected = try NextFixture.decode(ConceptListEnvelope.self, "graph", "concepts-list-rejected").concepts186        XCTAssertTrue(rejected.contains { $0.id == minimal.id })187        XCTAssertTrue(rejected.allSatisfy { $0.status == DecisionStatus.rejected })188189        XCTAssertTrue(try NextFixture.decode(OKResult.self, "graph", "concept-delete").ok)190        XCTAssertTrue(try NextFixture.error("graph", "concept-delete-again").isNotFound)191    }192193    func testConceptErrors() throws {194        let created = try NextFixture.decode(Concept.self, "graph", "concept-create")195        let exists = try NextFixture.error("graph", "concept-exists")196        XCTAssertEqual(exists.status, 409)197        XCTAssertEqual(exists.existingConcept, ExistingEntity(id: created.id, status: DecisionStatus.accepted), "names are compared case-insensitively")198        XCTAssertNil(exists.existingRelation)199        // Renaming onto an existing name: the body has the id but no status.200        let rename = try NextFixture.error("graph", "concept-patch-exists")201        let other = try NextFixture.decode(Concept.self, "graph", "concept-create-minimal")202        XCTAssertEqual(rename.existingConcept?.id, other.id)203204        let badPage = try NextFixture.error("graph", "concept-bad-page")205        XCTAssertEqual(badPage.code, APIError.NextCode.badPage)206        XCTAssertEqual(badPage.unknownPageIDs?.count, 1)207        XCTAssertNil(exists.unknownPageIDs)208        let validation = try NextFixture.error("graph", "concept-validation")209        XCTAssertEqual(validation.status, 400)210        XCTAssertNil(validation.code)211        XCTAssertEqual(try NextFixture.error("graph", "concept-not-found").code, APIError.NextCode.notFound)212    }213214    // MARK: views215216    func testLocalView() throws {217        let graph = try NextFixture.decode(KnowledgeGraph.self, "graph", "graph-local")218        XCTAssertEqual(graph.view, GraphView.local)219        let center = try XCTUnwrap(graph.center)220        let root = try XCTUnwrap(graph.nodes.first { $0.id == center })221        XCTAssertEqual(root.title, "Fixture · graphe · revue")222        XCTAssertEqual(root.kind, GraphNodeKind.page)223        XCTAssertEqual(root.entityId, root.id)224        XCTAssertEqual(root.section, "projects")225        XCTAssertNotNil(root.createdAt)226        XCTAssertEqual(root.degree, (root.inDegree ?? 0) + (root.outDegree ?? 0))227        XCTAssertEqual(Set(graph.nodes.map(\.kind)), [GraphNodeKind.page, GraphNodeKind.reference, GraphNodeKind.concept, GraphNodeKind.attachment])228        XCTAssertEqual(graph.nodes.map { $0.degree ?? 0 }, graph.nodes.map { $0.degree ?? 0 }.sorted(by: >), "sorted by degree")229230        let concept = try XCTUnwrap(graph.nodes.first { $0.kind == GraphNodeKind.concept })231        XCTAssertTrue(concept.id.hasPrefix("concept:"))232        XCTAssertEqual("concept:" + concept.entityUUID, concept.id)233        XCTAssertEqual(concept.status, DecisionStatus.accepted)234        XCTAssertEqual(concept.origin, KnowledgeOrigin.user)235        XCTAssertNil(concept.section)236        XCTAssertTrue(try XCTUnwrap(graph.nodes.first { $0.kind == GraphNodeKind.reference }).id.hasPrefix("ref:"))237        let attachment = try XCTUnwrap(graph.nodes.first { $0.kind == GraphNodeKind.attachment })238        XCTAssertTrue(attachment.id.hasPrefix("att:"))239        XCTAssertEqual(attachment.title, "fixture-graphe.txt")240        XCTAssertNil(root.status)241242        let ids = Set(graph.nodes.map(\.id))243        XCTAssertTrue(graph.edges.allSatisfy { ids.contains($0.source) && ids.contains($0.target) }, "every edge joins two nodes of the answer")244        XCTAssertEqual(Set(graph.edges.map(\.kind)), [GraphEdgeKind.about, GraphEdgeKind.attachment, GraphEdgeKind.cites, GraphEdgeKind.link, GraphEdgeKind.typed])245        let typed = try XCTUnwrap(graph.edges.first { $0.kind == GraphEdgeKind.typed && $0.relType == RelationType.extends })246        XCTAssertFalse(typed.isSynthetic)247        XCTAssertEqual(typed.id, try NextFixture.decode(GraphRelation.self, "graph", "relation-create").id, "a typed edge carries the id of its relation")248        XCTAssertEqual(typed.validFrom.map(APIDate.string(from:)), "2026-01-01T00:00:00.000Z")249        XCTAssertEqual(typed.target, center)250        let about = try XCTUnwrap(graph.edges.first { $0.kind == GraphEdgeKind.about })251        XCTAssertTrue(about.isSynthetic)252        XCTAssertEqual(about.target, concept.id)253        XCTAssertNotNil(try XCTUnwrap(graph.edges.first { $0.kind == GraphEdgeKind.link }).sourceBlockId)254255        XCTAssertEqual(graph.stats.pages, graph.nodes.filter { $0.kind == GraphNodeKind.page }.count)256        XCTAssertEqual(graph.stats.references, 1)257        XCTAssertEqual(graph.stats.concepts, 1)258        XCTAssertEqual(graph.stats.edges, graph.edges.count)259        XCTAssertEqual(graph.stats.suggested, 0)260        XCTAssertNil(graph.range)261        XCTAssertNil(graph.buckets)262    }263264    func testOtherViews() throws {265        let project = try NextFixture.decode(KnowledgeGraph.self, "graph", "graph-project")266        XCTAssertEqual(project.view, GraphView.project)267        XCTAssertEqual(project.nodes.first { $0.id == project.center }?.title, "Fixture · graphe · projet")268        XCTAssertEqual(project.stats.pages, 4)269270        let global = try NextFixture.decode(KnowledgeGraph.self, "graph", "graph-global")271        XCTAssertEqual(global.view, GraphView.global)272        XCTAssertNil(global.center)273        XCTAssertTrue(global.nodes.allSatisfy { ($0.degree ?? 0) > 0 }, "hideOrphans=1")274        let all = try NextFixture.decode(KnowledgeGraph.self, "graph", "graph-global-no-suggestions")275        XCTAssertGreaterThan(all.nodes.count, global.nodes.count, "without hideOrphans the orphans are there")276        XCTAssertTrue(all.edges.allSatisfy { $0.status != DecisionStatus.suggested })277278        let timeline = try NextFixture.decode(KnowledgeGraph.self, "graph", "graph-timeline")279        XCTAssertEqual(timeline.view, GraphView.timeline)280        let range = try XCTUnwrap(timeline.range)281        XCTAssertEqual(APIDate.string(from: range.from), "2026-11-01T00:00:00.000Z")282        XCTAssertEqual(APIDate.string(from: range.to), "2027-01-01T00:00:00.000Z")283        let buckets = try XCTUnwrap(timeline.buckets)284        XCTAssertFalse(buckets.isEmpty)285        XCTAssertTrue(buckets.allSatisfy { $0.date.count == 10 && $0.created >= 0 && $0.updated >= 0 && $0.edges >= 0 })286        XCTAssertEqual(buckets.map(\.created).reduce(0, +), timeline.stats.pages, "every page of this workspace was created in the range")287288        let concepts = try NextFixture.decode(KnowledgeGraph.self, "graph", "graph-concepts")289        XCTAssertEqual(concepts.view, GraphView.concepts)290        XCTAssertTrue(concepts.edges.allSatisfy { $0.kind == GraphEdgeKind.about || $0.source.hasPrefix("concept:") || $0.target.hasPrefix("concept:") })291        XCTAssertGreaterThanOrEqual(concepts.stats.concepts ?? 0, 1)292293        let documents = try NextFixture.decode(KnowledgeGraph.self, "graph", "graph-documents")294        XCTAssertEqual(documents.view, GraphView.documents)295        XCTAssertTrue(documents.nodes.contains { $0.title == "Fixture · graphe · document" })296        XCTAssertTrue(documents.nodes.filter { $0.kind == GraphNodeKind.page }.allSatisfy { $0.section == "documents" })297298        let references = try NextFixture.decode(KnowledgeGraph.self, "graph", "graph-references")299        XCTAssertEqual(references.view, GraphView.references)300        XCTAssertTrue(references.edges.allSatisfy { $0.source.hasPrefix("ref:") || $0.target.hasPrefix("ref:") })301        XCTAssertTrue(references.edges.contains { $0.kind == GraphEdgeKind.cites })302    }303304    func testLegacyGraph() throws {305        let graph = try NextFixture.decode(KnowledgeGraph.self, "graph", "graph-legacy-page")306        XCTAssertNil(graph.view, "the 2.x answer has no view")307        XCTAssertNil(graph.stats.references)308        XCTAssertNil(graph.stats.concepts)309        XCTAssertEqual(graph.nodes.first { $0.id == graph.center }?.title, "Fixture · graphe · revue")310        XCTAssertTrue(graph.nodes.allSatisfy { $0.entityId == nil && $0.createdAt == nil }, "2.x node shape")311        let tag = try XCTUnwrap(graph.nodes.first { $0.kind == GraphNodeKind.tag })312        XCTAssertTrue(tag.id.hasPrefix("tag:"))313        XCTAssertEqual(tag.entityUUID.count, 36)314        XCTAssertTrue(tag.title?.hasPrefix("#") ?? false)315        XCTAssertNil(tag.updatedAt)316        XCTAssertTrue(graph.edges.contains { $0.kind == GraphEdgeKind.tag && $0.target == tag.id })317        XCTAssertTrue(graph.edges.contains { $0.kind == GraphEdgeKind.link })318        XCTAssertTrue(graph.edges.allSatisfy { $0.relType == nil && $0.createdAt == nil }, "2.x edge shape")319        let whole = try NextFixture.decode(KnowledgeGraph.self, "graph", "graph-legacy")320        XCTAssertNil(whole.center)321        XCTAssertGreaterThanOrEqual(whole.stats.pages, graph.stats.pages)322    }323324    func testViewErrors() throws {325        let badView = try NextFixture.error("graph", "graph-bad-view")326        XCTAssertEqual(badView.status, 400)327        XCTAssertEqual(badView.code, APIError.NextCode.badView)328        for view in GraphView.all { XCTAssertTrue(badView.message.contains(view), "the message lists \(view)") }329        XCTAssertEqual(try NextFixture.error("graph", "graph-local-no-page").code, APIError.NextCode.badRequest)330        XCTAssertEqual(try NextFixture.error("graph", "graph-local-bad-depth").code, APIError.NextCode.badRequest)331        let missing = try NextFixture.error("graph", "graph-project-not-found")332        XCTAssertTrue(missing.isNotFound)333        XCTAssertFalse(missing.isRouteMissing, "a JSON 404 is a missing object, not a missing route")334    }335336    // MARK: insights337338    func testInsights() throws {339        let run = try NextFixture.decode(InsightRunResult.self, "graph", "insights-run")340        XCTAssertEqual(run.mode, InsightRunMode.deterministic)341        XCTAssertFalse(run.queued)342        XCTAssertNil(run.jobId)343        let counts = try XCTUnwrap(run.deterministic)344        XCTAssertGreaterThanOrEqual(counts.created, 3, "a cluster and two duplicates at least")345        XCTAssertGreaterThanOrEqual(counts.considered, counts.created)346        let again = try NextFixture.decode(InsightRunResult.self, "graph", "insights-run-again")347        XCTAssertEqual(again.deterministic?.created, 0, "a known finding is not stored twice")348349        let answer = try NextFixture.decode(GraphInsights.self, "graph", "insights")350        let intelligence = try XCTUnwrap(answer.intelligence)351        XCTAssertNotNil(intelligence.computedAt)352        let stats = try XCTUnwrap(intelligence.stats)353        XCTAssertGreaterThanOrEqual(stats.pages, 7)354        XCTAssertGreaterThanOrEqual(stats.clusters, 1)355        let central = try XCTUnwrap(intelligence.centralPages.first { $0.title == "Fixture · graphe · revue" })356        XCTAssertGreaterThan(try XCTUnwrap(central.pagerank), 0)357        XCTAssertEqual(central.inDegree, 2)358        XCTAssertEqual(central.outDegree, 1)359        XCTAssertEqual(intelligence.centralPages.compactMap(\.pagerank), intelligence.centralPages.compactMap(\.pagerank).sorted(by: >))360        let concept = try XCTUnwrap(intelligence.centralConcepts.first { $0.name == "Fixture · hétéroscédasticité" })361        XCTAssertEqual(concept.pages, 1)362        XCTAssertTrue(intelligence.orphans.contains { $0.title == "Fixture · graphe · projet" && $0.section == "projects" && $0.updatedAt != nil })363        let cluster = try XCTUnwrap(intelligence.clusters.first { $0.label == "Fixture · graphe · revue" })364        XCTAssertEqual(cluster.size, 3)365        XCTAssertEqual(cluster.pageIds.count, 3)366        XCTAssertEqual(cluster.id.count, 40, "a stable hash of the member set")367        XCTAssertEqual(cluster.top?.first?.title, "Fixture · graphe · revue")368        let topic = try XCTUnwrap(intelligence.recurringTopics.first { $0.name == "fixture-graphe" })369        XCTAssertEqual(topic.kind, "tag")370        XCTAssertEqual(topic.pages, 3)371        XCTAssertEqual(topic.pageIds.count, 3)372        XCTAssertNotNil(topic.lastActivityAt)373        let duplicates = intelligence.duplicates.filter { $0.title == "Fixture · graphe · doublon" }374        XCTAssertEqual(Set(duplicates.compactMap(\.reason)), ["title", "content"])375        XCTAssertTrue(duplicates.allSatisfy { $0.pageIds.count == 2 })376377        XCTAssertFalse(answer.insights.isEmpty)378        XCTAssertTrue(answer.insights.allSatisfy { $0.status == InsightStatus.suggested }, "suggested only by default")379        let duplicate = try XCTUnwrap(answer.insights.first { $0.kind == InsightKind.duplicate })380        XCTAssertEqual(duplicate.origin, KnowledgeOrigin.system)381        XCTAssertFalse(duplicate.isAI)382        XCTAssertNil(duplicate.model)383        XCTAssertEqual(duplicate.pageIds.count, 2)384        XCTAssertEqual(duplicate.blockIds, [])385        XCTAssertNil(duplicate.decidedAt)386        XCTAssertNil(duplicate.relationId)387        XCTAssertTrue(answer.insights.contains { $0.kind == InsightKind.cluster && $0.pageIds.count == 3 })388        XCTAssertGreaterThanOrEqual(try XCTUnwrap(answer.counts).suggested, answer.insights.count)389390        let bare = try NextFixture.decode(GraphInsights.self, "graph", "insights-no-intelligence")391        XCTAssertNil(bare.intelligence, "intelligence=0")392        // Observed: `limit` caps the lists of the intelligence, not the stored insights (6 came back for limit=5).393        XCTAssertTrue(bare.insights.contains { $0.status == InsightStatus.dismissed }, "status=all")394        XCTAssertEqual(bare.counts?.suggested, answer.counts?.suggested)395        let byKind = try NextFixture.decode(GraphInsights.self, "graph", "insights-kind")396        XCTAssertTrue(byKind.insights.allSatisfy { $0.kind == InsightKind.recurringTopic })397    }398399    func testInsightDecisions() throws {400        let accepted = try NextFixture.decode(KnowledgeInsight.self, "graph", "insight-accept")401        XCTAssertEqual(accepted.status, InsightStatus.accepted)402        XCTAssertNotNil(accepted.decidedAt)403        XCTAssertNil(accepted.relationId, "only an accepted contradiction records a relation")404        XCTAssertFalse(accepted.pageIds.isEmpty)405        let dismissed = try NextFixture.decode(KnowledgeInsight.self, "graph", "insight-dismiss")406        XCTAssertEqual(dismissed.id, accepted.id)407        XCTAssertEqual(dismissed.status, InsightStatus.dismissed)408409        let validation = try NextFixture.error("graph", "insight-validation")410        XCTAssertEqual(validation.status, 400)411        XCTAssertNil(validation.code)412        XCTAssertTrue(try NextFixture.error("graph", "insight-not-found").isNotFound)413        XCTAssertEqual(try NextFixture.error("graph", "insights-bad-status").code, APIError.NextCode.badRequest)414        XCTAssertNil(try NextFixture.error("graph", "insights-run-validation").code)415    }416}417
Files main
Menu

Theme