SPB Git

Repository Public

ParityKnowledgeFixtureTests.swift 18.4 KB 290 lines swift

1// SP Notes Core2// File: Tests/SPNotesAPITests/ParityKnowledgeFixtureTests.swift3// Description: Search, 2.x graph and inbox models decode the real answers of the scratch backend (groups `search`,4//              `graph2`, `inbox`), and the Smart Inbox models decode the examples of the AI contract (`ai-doc-parity`).5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import XCTest10@testable import SPNotesAPI1112final class ParityKnowledgeFixtureTests: XCTestCase {13    // MARK: search1415    func testQuickSearch() throws {16        let hits = try NextFixture.decode([SearchHit].self, "search", "quick")17        XCTAssertEqual(hits.count, 2)18        XCTAssertTrue(hits.allSatisfy { $0.title?.contains("zyxwparity") == true && $0.id != nil && $0.pageId == nil })19        XCTAssertEqual(Set(hits.compactMap(\.section)), ["notes", "research"])20        XCTAssertEqual(hits.first?.targetPageID, hits.first?.id)21        // A matching quick search lets the PostgreSQL text form of the date through: it is still read.22        XCTAssertTrue(hits.first?.raw.string.contains("-05") ?? false)23        XCTAssertTrue(hits.allSatisfy { $0.updatedAt != nil })24        let recent = try NextFixture.decode([SearchHit].self, "search", "quick-empty-query")25        XCTAssertEqual(recent.count, 2, "an empty query lists recent pages")26        XCTAssertTrue(recent.allSatisfy { $0.updatedAt != nil })27        XCTAssertEqual(try NextFixture.decode([SearchHit].self, "search", "quick-default-mode").map(\.id), hits.map(\.id), "mode defaults to quick")28    }2930    func testHybridSearch() throws {31        // No worker runs on the scratch instance: pages have no indexed passage, only references match (grouped).32        XCTAssertEqual(try NextFixture.decode([SearchHit].self, "search", "hybrid").count, 0)33        XCTAssertEqual(try NextFixture.decode([SearchHit].self, "search", "hybrid-filters").count, 0)34        XCTAssertEqual(try NextFixture.decode([SearchHit].self, "search", "hybrid-short-query").count, 0)35        let grouped = try NextFixture.decode([SearchHit].self, "search", "hybrid-grouped")36        let hit = try XCTUnwrap(grouped.first)37        XCTAssertEqual(hit.kind, SearchKind.reference)38        XCTAssertEqual(hit.sourceType, "reference")39        XCTAssertEqual(hit.chunkId, hit.sourceId.map { "ref:\($0)" })40        XCTAssertEqual(hit.referenceId, hit.sourceId)41        XCTAssertEqual(hit.title, "Fixture · Zyxwparity and rents")42        XCTAssertEqual(hit.icon, "📚")43        XCTAssertEqual(hit.content, "Jane Doe · 2021 · Journal of Housing Finance · @doe2021fixture")44        XCTAssertEqual(hit.score, 0)45        XCTAssertEqual(hit.keywordScore, 1)46        XCTAssertEqual(hit.semanticScore, 0)47        XCTAssertNil(hit.pageId)48        XCTAssertNil(hit.blockId)49        XCTAssertNil(hit.more)50        XCTAssertNotNil(hit.updatedAt)51        XCTAssertEqual(try NextFixture.decode([SearchHit].self, "search", "hybrid-references").map(\.sourceId), grouped.map(\.sourceId))52        for name in ["hybrid-bad-date", "hybrid-bad-section"] {53            let error = try NextFixture.error("search", name)54            XCTAssertEqual(error.status, 400)55            XCTAssertNil(error.code)56        }57    }5859    func testSnippetSegments() {60        XCTAssertEqual(SearchHit.segments(of: "la \u{1}volatilité\u{2} des \u{1}loyers\u{2}"),61                       [.init(text: "la ", isMatch: false), .init(text: "volatilité", isMatch: true), .init(text: " des ", isMatch: false), .init(text: "loyers", isMatch: true)])62        XCTAssertEqual(SearchHit.segments(of: ""), [])63        XCTAssertEqual(SearchHit.segments(of: "plain"), [.init(text: "plain", isMatch: false)])64    }6566    func testIndexStateAndDuplicates() throws {67        let state = try NextFixture.decode(SearchIndexState.self, "search", "index-state")68        // Counters arrive as text on this route ("pages": "2").69        XCTAssertEqual(state.pages, 2)70        XCTAssertNotNil(state.chunks)71        XCTAssertNotNil(state.embedded)72        XCTAssertNotNil(state.indexedSources)73        XCTAssertEqual(state.model, "text-embedding-3-small")74        XCTAssertEqual(state.version, 1)75        XCTAssertNotNil(state.lastIndexed, "PostgreSQL text date")76        let health = try XCTUnwrap(state.health)77        XCTAssertEqual(health.pending.count, 2, "no worker: the two pages of the scenario wait")78        XCTAssertTrue(health.pending.allSatisfy { $0.title?.contains("zyxwparity") == true && $0.updatedAt != nil })79        XCTAssertEqual(health.failed, [])80        XCTAssertEqual(health.pendingFiles, 0)81        XCTAssertGreaterThan(health.queued ?? 0, 0)82        XCTAssertEqual(health.embeddingsConfigured, true)83        XCTAssertEqual(try NextFixture.decode([DuplicatePair].self, "search", "duplicates"), [])84        XCTAssertEqual(try NextFixture.error("search", "index-state-validation").status, 400)85    }8687    // MARK: graph (2.x)8889    func testBacklinksAndMentions() throws {90        let found = try NextFixture.decode(BacklinksOverview.self, "graph2", "backlinks-search")91        let target = try XCTUnwrap(found.pages.first)92        XCTAssertEqual(found.pages.count, 1, "q filters the pages by title")93        XCTAssertEqual(target.title, "Fixture · Refinancement qwparity")94        XCTAssertEqual(target.incoming, 1)95        XCTAssertEqual(target.outgoing, 0)96        XCTAssertEqual(target.suggested, 0)97        XCTAssertEqual(target.section, "projects")98        XCTAssertNotNil(target.updatedAt)99        let mention = try XCTUnwrap(found.mentions.first { $0.toId == target.id })100        XCTAssertEqual(mention.fromTitle, "Fixture · Mention sans lien")101        XCTAssertEqual(mention.toTitle, target.title)102        XCTAssertEqual(mention.excerpt, "Le dossier Fixture · Refinancement qwparity avance bien cette semaine.")103        XCTAssertEqual(mention.key, "\(mention.fromId):\(mention.toId)")104        XCTAssertEqual(mention.id, mention.key)105106        XCTAssertGreaterThanOrEqual(try NextFixture.decode(BacklinksOverview.self, "graph2", "backlinks-page").pages.count, 3)107        XCTAssertTrue(try NextFixture.decode(OKResult.self, "graph2", "mention-dismiss").ok)108        XCTAssertEqual(try NextFixture.decode(BacklinksOverview.self, "graph2", "backlinks-after-dismiss").mentions, [], "a dismissed mention is hidden")109        XCTAssertTrue(try NextFixture.decode(OKResult.self, "graph2", "mention-undismiss").ok)110        XCTAssertTrue(try NextFixture.decode(OKResult.self, "graph2", "mention-link").ok)111        let after = try NextFixture.decode(BacklinksOverview.self, "graph2", "backlinks-after-link")112        XCTAssertEqual(after.pages.first?.incoming, 2, "linking a mention creates a real link")113        XCTAssertEqual(after.mentions, [])114115        XCTAssertEqual(try NextFixture.error("graph2", "mention-link-self").status, 400)116        XCTAssertTrue(try NextFixture.error("graph2", "mention-link-not-found").isNotFound)117        XCTAssertEqual(try NextFixture.error("graph2", "mention-validation").status, 400)118        XCTAssertEqual(MentionAction.allCases.map(\.rawValue), ["link", "dismiss", "undismiss"])119    }120121    func testNeighbourhood() throws {122        let graph = try NextFixture.decode(GraphNeighborhood.self, "graph2", "neighbors")123        XCTAssertEqual(graph.nodes.count, 3)124        let center = try XCTUnwrap(graph.nodes.first { $0.id == graph.center })125        XCTAssertEqual(center.kind, "page")126        XCTAssertEqual(center.degree, 2)127        XCTAssertEqual(center.incoming, 2)128        XCTAssertEqual(center.outgoing, 0)129        XCTAssertEqual(graph.edges.count, 2)130        XCTAssertTrue(graph.edges.allSatisfy { $0.target == center.id && $0.kind == "link" && $0.status == "accepted" && $0.sub == "link" })131        XCTAssertEqual(graph.stats, GraphNeighborStats(pages: 3, edges: 2, suggested: 0))132        XCTAssertNil(graph.linked)133134        let withLinked = try NextFixture.decode(GraphNeighborhood.self, "graph2", "neighbors-linked")135        XCTAssertEqual(withLinked.linked?.count, 2)136        XCTAssertTrue(withLinked.linked?.allSatisfy { $0.direction == "in" && $0.kind == "link" && $0.title != nil } ?? false)137        XCTAssertEqual(withLinked.suggestions, [])138139        let unknown = try NextFixture.decode(GraphNeighborhood.self, "graph2", "neighbors-unknown-page")140        XCTAssertEqual(unknown.nodes, [])141        XCTAssertNotNil(unknown.center, "an unknown page answers an empty graph, not a 404")142        XCTAssertEqual(try NextFixture.error("graph2", "neighbors-no-page").status, 400)143    }144145    func testLinkSuggestions() throws {146        // No suggestion can exist on the scratch instance (creating one calls a model): lists and counts only.147        let list = try NextFixture.decode(LinkSuggestionList.self, "graph2", "suggestions")148        XCTAssertEqual(list.items, [])149        XCTAssertEqual(list.counts, LinkSuggestionCounts(suggested: 0, accepted: 0, rejected: 0))150        XCTAssertEqual(try NextFixture.decode(LinkSuggestionList.self, "graph2", "suggestions-accepted").items, [])151        let inline = try NextFixture.decode(LinkSuggestRequestResult.self, "graph2", "suggest-inline")152        XCTAssertFalse(inline.queued)153        XCTAssertEqual(inline.result?.skipped, "no candidates", "a page without embeddings is skipped: no model call")154        XCTAssertEqual(inline.result?.candidates, 0)155        XCTAssertNil(inline.result?.model)156        XCTAssertNil(inline.jobId)157        XCTAssertTrue(try NextFixture.error("graph2", "suggestion-not-found").isNotFound)158        XCTAssertTrue(try NextFixture.error("graph2", "suggestion-revert-not-found").isNotFound)159        XCTAssertEqual(try NextFixture.error("graph2", "suggestion-validation").status, 400)160        XCTAssertEqual(try NextFixture.error("graph2", "suggest-validation").status, 400)161        // The shape of an item, from the server's row (not observed): it decodes with only its ids.162        let item = try APIJSON.decoder().decode(LinkSuggestion.self, from: Data(#"{"id":"a","fromId":"b","toId":"c","status":"suggested","confidence":0.71,"createdAt":"2026-11-19T18:00:00.000Z","decidedAt":null}"#.utf8))163        XCTAssertEqual(item.confidence, 0.71)164        XCTAssertNil(item.decidedAt)165    }166167    // MARK: inbox and capture168169    func testCaptureKinds() throws {170        for kind in CaptureKindName.baseline {171            let result = try NextFixture.decode(CaptureResult.self, "inbox", "capture-\(kind)")172            XCTAssertEqual(result.kind, kind)173            XCTAssertFalse(result.title.isEmpty)174            XCTAssertNotNil(result.inboxAt)175            XCTAssertEqual(try NextFixture.load("inbox", "capture-\(kind)").entry.status, 201)176        }177        XCTAssertEqual(try NextFixture.decode(CaptureResult.self, "inbox", "capture-note").title, "Fixture · idée parity", "the first line is the title")178        XCTAssertTrue(try NextFixture.decode(CaptureResult.self, "inbox", "capture-photo").title.hasPrefix("Photo · "))179        XCTAssertTrue(try NextFixture.decode(CaptureResult.self, "inbox", "capture-audio").title.hasPrefix("Voice memo · "))180        XCTAssertEqual(CaptureKindName.baseline, CaptureKind.allCases.map(\.rawValue), "the enum of 1.x covers what every server accepts")181182        // The 3.0 kinds are refused by the foundation server.183        for kind in [CaptureKindName.screenshot, CaptureKindName.selection, CaptureKindName.document] {184            let error = try NextFixture.error("inbox", "capture-next-\(kind)")185            XCTAssertEqual(error.status, 400)186            XCTAssertTrue(error.message.hasPrefix("Validation: kind"))187        }188        for name in ["capture-empty", "capture-link-no-url", "capture-unknown-attachment"] {189            XCTAssertEqual(try NextFixture.error("inbox", name).status, 400, name)190        }191        let first = try NextFixture.decode(CaptureResult.self, "inbox", "capture-keyed")192        let replay = try NextFixture.load("inbox", "capture-keyed-replay")193        XCTAssertEqual(replay.entry.headers["idempotent-replay"], "true")194        XCTAssertEqual(try APIJSON.decoder().decode(CaptureResult.self, from: replay.data), first, "same key: the stored answer, no second page")195    }196197    func testInboxListAndTriage() throws {198        let inbox = try NextFixture.decode([InboxItem].self, "inbox", "inbox-list")199        XCTAssertEqual(inbox.count, 7)200        XCTAssertEqual(try NextFixture.decode(InboxCount.self, "inbox", "inbox-count").count, 7)201        XCTAssertEqual(Set(inbox.compactMap(\.kind)), Set(CaptureKindName.baseline))202        let link = try XCTUnwrap(inbox.first { $0.kind == "link" })203        XCTAssertEqual(link.url, "https://example.org/article")204        let photo = try XCTUnwrap(inbox.first { $0.kind == "photo" })205        XCTAssertTrue(photo.image?.hasPrefix("/api/files/") ?? false)206        XCTAssertEqual(inbox.first { $0.kind == "tasks" }?.openTasks, 2)207        XCTAssertEqual(inbox.first { $0.title == "Fixture · idée parity" }?.excerpt?.contains("Deuxième ligne"), true)208209        let kept = try NextFixture.decode(TriageResult.self, "inbox", "triage-keep")210        XCTAssertEqual(kept.summary, "Filed in projects")211        XCTAssertEqual(kept.createdObjects, TriageCreated())212        let task = try NextFixture.decode(TriageResult.self, "inbox", "triage-task")213        XCTAssertEqual(task.createdObjects.taskIds?.count, 2, "one task per open task item")214        XCTAssertEqual(task.summary, "2 tasks created")215        let row = try NextFixture.decode(TriageResult.self, "inbox", "triage-database").createdObjects216        XCTAssertNotNil(row.rowId)217        XCTAssertNotNil(row.databaseId)218        XCTAssertNotNil(try NextFixture.decode(TriageResult.self, "inbox", "triage-reference").createdObjects.referenceId)219        XCTAssertEqual(try NextFixture.decode(TriageResult.self, "inbox", "triage-archive").summary, "Archived")220        XCTAssertEqual(try NextFixture.decode(TriageResult.self, "inbox", "triage-trash").summary, "Moved to trash")221222        XCTAssertEqual(try NextFixture.decode(UndoResult.self, "inbox", "undo").already, false)223        XCTAssertEqual(try NextFixture.decode(UndoResult.self, "inbox", "undo-again").already, true)224        XCTAssertTrue(try NextFixture.error("inbox", "undo-not-found").isNotFound)225226        let gone = try NextFixture.error("inbox", "triage-not-in-inbox")227        XCTAssertTrue(gone.isNotInInbox)228        XCTAssertEqual(gone.code, ParityCode.notInInbox)229        XCTAssertEqual(try NextFixture.error("inbox", "triage-validation").status, 400)230        XCTAssertEqual(try NextFixture.error("inbox", "triage-bad-section").status, 400)231        XCTAssertTrue(try NextFixture.error("inbox", "triage-database-not-found").isNotFound)232        XCTAssertTrue(try NextFixture.error("inbox", "triage-page-not-found").isNotFound)233        // After the triages: the undone capture is back, the five others left the inbox.234        XCTAssertEqual(try NextFixture.decode([InboxItem].self, "inbox", "inbox-list-after").count, 2)235    }236237    /// The six actions of the server are the six cases of `InboxAction` (bodies checked against the recorded requests).238    func testInboxActionsCoverTheServer() throws {239        let index = try NextFixture.index("inbox").filter { $0.name.hasPrefix("triage-") && $0.status == 200 }240        XCTAssertEqual(Set(index.map { String($0.name.dropFirst("triage-".count)) }), ["keep", "task", "database", "reference", "archive", "trash"])241        func body(_ action: InboxAction) throws -> String { String(decoding: try APIJSON.encoder().encode(action), as: UTF8.self) }242        XCTAssertEqual(try body(.keep(section: .projects, parentId: .value("p"))), #"{"action":"keep","parentId":"p","section":"projects"}"#)243        XCTAssertEqual(try body(.task(dueAt: .null, priority: .high)), #"{"action":"task","dueAt":null,"priority":"high"}"#)244        XCTAssertEqual(try body(.database(databaseId: "d")), #"{"action":"database","databaseId":"d"}"#)245        XCTAssertEqual(try body(.reference), #"{"action":"reference"}"#)246        XCTAssertEqual(try body(.archive), #"{"action":"archive"}"#)247        XCTAssertEqual(try body(.trash), #"{"action":"trash"}"#)248    }249250    /// Smart Inbox: the foundation server has no such route; the models are checked against the contract's examples.251    func testSmartInboxFromTheContractDocument() throws {252        let missing = try NextFixture.error("inbox", "suggestions-cached-only")253        XCTAssertEqual(missing.status, 404)254        XCTAssertTrue(missing.isRouteMissing, "HTML 404: the route does not exist on the foundation server")255        XCTAssertTrue(try NextFixture.error("inbox", "apply-nothing").isRouteMissing)256257        let suggestions = try NextFixture.decode(InboxSuggestions.self, "ai-doc-parity", "inbox-suggestions")258        XCTAssertEqual(suggestions.status, "ready")259        XCTAssertEqual(suggestions.ai, true)260        XCTAssertEqual(suggestions.model, "claude-haiku-5-5")261        XCTAssertEqual(suggestions.cached, false)262        XCTAssertNotNil(suggestions.generatedAt)263        XCTAssertEqual(suggestions.costUsd ?? 0, 0.00010823, accuracy: 1e-9)264        let set = try XCTUnwrap(suggestions.suggestions)265        XCTAssertEqual(set.title, InboxTitleSuggestion(value: "Rappeler la banque triplex", current: "rappeler la banque triplex"))266        XCTAssertEqual(set.tags?.map(\.name), ["refinancement", "triplex", "banque"])267        XCTAssertEqual(set.tags?.first?.existing, true)268        XCTAssertEqual(set.destination?.section, "projects")269        XCTAssertEqual(set.destination?.parentTitle, "Refinancement du triplex")270        XCTAssertEqual(set.relations?.first?.title, "Offre de la banque")271        XCTAssertNotNil(set.relations?.first?.reason)272        XCTAssertTrue(set.summary?.hasPrefix("Rappeler la banque") ?? false)273274        let applied = try NextFixture.decode(InboxApplyResult.self, "ai-doc-parity", "inbox-apply")275        XCTAssertEqual(applied.applied, ["title", "summary", "tags", "relations"])276        XCTAssertEqual(applied.filed, false)277        XCTAssertEqual(applied.created?.summaryBlockId, "AW23RJ67ft")278        XCTAssertEqual(applied.created?.tagsAdded?.count, 3)279        XCTAssertEqual(applied.created?.edgeIds?.count, 1)280        let nothing = try NextFixture.error("ai-doc-parity", "inbox-apply-nothing")281        XCTAssertEqual(nothing.code, ParityCode.nothingToApply)282        XCTAssertFalse(nothing.isRouteMissing)283284        let selection = InboxSuggestionSelection(title: "T", tags: ["a"], destination: .init(section: "projects", parentId: .null), relations: [.init(pageId: "p", reason: "r")])285        XCTAssertEqual(String(decoding: try APIJSON.encoder().encode(selection), as: UTF8.self),286                       #"{"destination":{"parentId":null,"section":"projects"},"relations":[{"pageId":"p","reason":"r"}],"tags":["a"],"title":"T"}"#)287        XCTAssertTrue(InboxSuggestionSelection().isEmpty)288    }289}290
Files main
Menu

Theme