Repository Public
1// SP Notes Core2// File: Tests/SPNotesAPITests/LiveNextKnowledgeTests.swift3// Description: Live tests of knowledge health, the brief, living blocks, semantic zoom, tags, ranged files with the4// resumable download, and the model catalogue against the SCRATCH backend. No route that calls a model5// is ever requested. Skipped without SPN_LIVE_URL / SPN_LIVE_TOKEN.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import CryptoKit10import Foundation11import XCTest12@testable import SPNotesAPI1314final class LiveNextKnowledgeTests: XCTestCase {15 func testKnowledgeHealth() async throws {16 let live = try await LiveNext.make()17 try await live.require(ServerFeature.health)18 let api = live.api19 let ghost = NotesAPI.newID(), lostReference = NotesAPI.newID()20 let content = RawJSON(string: #"{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Lien "},{"type":"mention","attrs":{"id":"\#(ghost)","label":"fantôme"}},{"type":"text","text":" et source "},{"type":"citation","attrs":{"refId":"\#(lostReference)","label":"Nul 1900"}}]}]}"#)21 let page = try await live.page(self, "santé", content: content)22 let late = try await api.createTaskIdempotent(TaskDraft(title: "Live · tâche en retard", dueAt: Date().addingTimeInterval(-10 * 86_400), pageId: page.id, id: NotesAPI.newID())).value23 addTeardownBlock { _ = try? await api.removeTask(id: late.id) }2425 let health = try await api.knowledgeHealth()26 XCTAssertEqual(health.inactiveDays, 30)27 XCTAssertGreaterThanOrEqual(health.score.pages, 1)28 let orphan = try XCTUnwrap(health.orphans.items.first { $0.id == page.id }, "a page without any accepted edge")29 XCTAssertEqual(orphan.title, page.title)30 XCTAssertTrue(health.orphans.ids.contains(page.id))31 let broken = try XCTUnwrap(health.brokenLinks.items.first { $0.pageId == page.id })32 XCTAssertEqual(broken.targetId, ghost)33 XCTAssertEqual(broken.kind, "page")34 XCTAssertEqual(broken.reason, "missing")35 XCTAssertNotNil(broken.blockId)36 let missing = try XCTUnwrap(health.missingReferences.items.first { $0.pageId == page.id })37 XCTAssertEqual(missing.refId, lostReference)38 let task = try XCTUnwrap(health.openTasks.items.first { $0.id == late.id })39 XCTAssertEqual(task.overdue, true)40 XCTAssertEqual(task.pageId, page.id)41 XCTAssertGreaterThanOrEqual(health.openTasks.overdue, 1)42 XCTAssertGreaterThanOrEqual(health.score.issues, 4)4344 let custom = try await api.knowledgeHealth(inactiveDays: 3650)45 XCTAssertEqual(custom.inactiveDays, 3650)46 }4748 func testBrief() async throws {49 let live = try await LiveNext.make()50 try await live.require(ServerFeature.brief)51 let api = live.api52 let project = try await live.page(self, "brief projet", section: .projects)53 let page = try await live.page(self, "brief page", parentId: project.id, section: .projects, content: LiveNext.document("Un deux trois."))54 let calendar = Calendar(identifier: .gregorian)55 let soon = try await api.createTaskIdempotent(TaskDraft(title: "Live · tâche bientôt", dueAt: Date().addingTimeInterval(2 * 86_400), pageId: page.id, id: NotesAPI.newID())).value56 addTeardownBlock { _ = try? await api.removeTask(id: soon.id) }5758 let brief = try await api.brief(timeZone: "America/Toronto")59 XCTAssertEqual(brief.timeZone, "America/Toronto")60 var toronto = calendar61 toronto.timeZone = try XCTUnwrap(TimeZone(identifier: "America/Toronto"))62 let today = toronto.dateComponents([.year, .month, .day], from: Date())63 XCTAssertEqual(brief.date, String(format: "%04d-%02d-%02d", today.year ?? 0, today.month ?? 0, today.day ?? 0), "the calendar day in the requested zone")64 let recent = try XCTUnwrap(brief.recentNotes.first { $0.id == page.id })65 XCTAssertEqual(recent.wordCount, 3)66 XCTAssertEqual(recent.type, "page")67 let active = try XCTUnwrap(brief.activeProjects.first { $0.id == project.id })68 XCTAssertEqual(active.pages, 2)69 XCTAssertEqual(active.openTasks, 1)70 let due = try XCTUnwrap(brief.tasks.dueSoon.first { $0.id == soon.id })71 XCTAssertEqual(due.pageTitle, page.title)72 XCTAssertEqual(brief.unfiledCaptures.count >= brief.unfiledCaptures.items.count, true)73 XCTAssertLessThanOrEqual(brief.linkSuggestions.items.count, 5)7475 let other = try await api.brief(timeZone: "Pacific/Auckland")76 XCTAssertEqual(other.timeZone, "Pacific/Auckland")77 let bad = await captureAPIError { try await api.brief(timeZone: "Mars/Olympus") }78 XCTAssertEqual(bad?.code, APIError.NextCode.badRequest)79 // `summarizeBrief()` is a paid model call: never exercised here.80 }8182 func testLivingBlocks() async throws {83 let live = try await LiveNext.make()84 try await live.require(ServerFeature.query)85 let api = live.api86 let tag = "live-query-\(NotesAPI.newID().prefix(8))"87 let target = try await live.page(self, "requête cible", content: LiveNext.document("Cible."))88 let mention = RawJSON(string: #"{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Voir "},{"type":"mention","attrs":{"id":"\#(target.id)","label":"cible"}}]}]}"#)89 let source = try await live.page(self, "requête source", content: mention)90 _ = try await api.setPageTags(pageID: source.id, tags: [tag])91 let task = try await api.createTaskIdempotent(TaskDraft(title: "Live · tâche de requête", priority: .high, pageId: source.id, id: NotesAPI.newID())).value92 addTeardownBlock { _ = try? await api.removeTask(id: task.id) }9394 let tasks = try await api.query(.tasks(LiveQueryFilter(status: "all", parentId: source.id)))95 XCTAssertEqual(tasks.source, LiveQuerySource.tasks)96 XCTAssertEqual(tasks.rows.map(\.id), [task.id])97 XCTAssertEqual(tasks.rows.first?.string("priority"), "high")98 XCTAssertEqual(tasks.rows.first?.pageId, source.id)99 XCTAssertEqual(tasks.total, 1)100 XCTAssertEqual(tasks.table.columns, tasks.columns.map(\.label))101 XCTAssertEqual(tasks.table.rows.first?.first, .text("Live · tâche de requête"))102103 let pages = try await api.query(.pages(LiveQueryFilter(tag: tag), limit: 5))104 XCTAssertEqual(pages.rows.map(\.id), [source.id])105 XCTAssertEqual(pages.rows.first?.strings("tags"), [tag])106 XCTAssertEqual(pages.limit, 5)107108 let backlinks = try await api.query(.backlinks(pageId: target.id))109 XCTAssertEqual(backlinks.rows.map(\.id), [source.id])110 XCTAssertEqual(backlinks.rows.first?.strings("via"), ["link"])111112 let references = try await api.query(.references(LiveQueryFilter(text: "no-such-reference-\(tag)")))113 XCTAssertEqual(references.source, LiveQuerySource.references)114 XCTAssertEqual(references.total, 0)115 XCTAssertTrue(references.rows.isEmpty)116 XCTAssertFalse(references.columns.isEmpty, "the columns are known even without rows")117118 // A database: its host page id is accepted; without rows the answer still names the database and its view.119 let database = try await live.page(self, "requête base", section: .databases, type: .database)120 let rows = try await api.query(.database(database.id))121 XCTAssertEqual(rows.source, LiveQuerySource.database)122 XCTAssertEqual(rows.name, database.title)123 XCTAssertNotNil(rows.databaseId)124 XCTAssertNotNil(rows.viewId)125 XCTAssertEqual(rows.total, 0)126 XCTAssertTrue(rows.columns.contains { $0.type == LiveQueryColumnType.page })127128 let noDatabase = await captureAPIError { try await api.query(LiveQuery(source: LiveQuerySource.database)) }129 XCTAssertEqual(noDatabase?.code, APIError.NextCode.badRequest)130 let unknown = await captureAPIError { try await api.query(.database(NotesAPI.newID())) }131 XCTAssertEqual(unknown?.isNotFound, true)132 let badSource = await captureAPIError { try await api.query(LiveQuery(source: "oracle")) }133 XCTAssertEqual(badSource?.status, 400)134 XCTAssertEqual(badSource?.code, APIError.NextCode.badRequest, "every error body has a code (default by status)")135 }136137 func testZoomAndTags() async throws {138 let live = try await LiveNext.make()139 try await live.require(ServerFeature.zoom)140 let api = live.api141 let content = RawJSON(string: #"{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Avant le titre. Suite."}]},{"type":"heading","attrs":{"level":2},"content":[{"type":"text","text":"Partie"}]},{"type":"paragraph","content":[{"type":"text","text":"Première phrase ! Deuxième phrase."}]}]}"#)142 let page = try await live.page(self, "zoom", content: content)143144 let outline = try await api.pageZoom(pageID: page.id)145 XCTAssertEqual(outline.level, 2)146 XCTAssertFalse(outline.ai)147 XCTAssertEqual(outline.pageId, page.id)148 XCTAssertEqual(outline.title, page.title)149 let sections = try XCTUnwrap(outline.sections)150 XCTAssertEqual(sections.map(\.heading), [nil, "Partie"])151 XCTAssertEqual(sections.map(\.level), [0, 2])152 XCTAssertEqual(sections[0].firstSentence, "Avant le titre.")153 XCTAssertEqual(sections[1].firstSentence, "Première phrase !")154 XCTAssertEqual(sections[1].blocks, 1)155156 let full = try await api.pageZoom(pageID: page.id, level: 3)157 XCTAssertEqual(full.level, 3)158 let stored = try await api.page(id: page.id)159 XCTAssertEqual(full.content, stored.page.content, "level 3 is the stored document")160 XCTAssertEqual(full.version, stored.page.version)161162 // Level 1 is the AI synopsis. A server that provides it would call a model: only a server WITHOUT the163 // feature is exercised here (cachedOnly on the dedicated route never spends).164 do {165 let cached = try await api.pageSynopsis(pageID: page.id, cachedOnly: true)166 XCTAssertEqual(cached.pageId, page.id)167 XCTAssertTrue([SynopsisStatus.missing, SynopsisStatus.ready, SynopsisStatus.empty].contains(cached.status))168 } catch let error as APIError {169 XCTAssertTrue(error.isRouteMissing, "without the AI workstream the route does not exist: \(error)")170 let level1 = await captureAPIError { try await api.pageZoom(pageID: page.id, level: 1) }171 XCTAssertEqual(level1?.status, 501)172 XCTAssertEqual(level1?.code, APIError.NextCode.notImplemented)173 }174 let badLevel = await captureAPIError { try await api.pageZoom(pageID: page.id, level: 7) }175 XCTAssertEqual(badLevel?.code, APIError.NextCode.badRequest)176177 // Tags: `pages` counts the live pages carrying the tag.178 let tag = "live-tag-\(NotesAPI.newID().prefix(8))"179 _ = try await api.setPageTags(pageID: page.id, tags: [tag])180 let tags = try await api.tags()181 XCTAssertEqual(tags.first { $0.name == tag }?.pages, 1)182 _ = try await api.trashPage(id: page.id)183 let afterTrash = try await api.tags()184 XCTAssertEqual(afterTrash.first { $0.name == tag }?.pages ?? 0, 0, "a trashed page no longer counts")185 // Zoom works on a trashed page.186 let trashed = try await api.pageZoom(pageID: page.id)187 XCTAssertEqual(trashed.sections?.count, 2)188 }189190 func testParentCycles() async throws {191 let live = try await LiveNext.make()192 let api = live.api193 guard try await api.meta().api >= 3 else { throw XCTSkip("a 2.x server does not check cycles") }194 let parent = try await live.page(self, "cycle parent")195 let child = try await live.page(self, "cycle enfant", parentId: parent.id)196 let cycle = await captureAPIError { try await api.patchPage(id: parent.id, PagePatch(parentId: .value(child.id))) }197 XCTAssertEqual(cycle?.status, 409)198 XCTAssertEqual(cycle?.code, APIError.NextCode.cycle)199 let unknown = await captureAPIError { try await api.patchPage(id: parent.id, PagePatch(parentId: .value(NotesAPI.newID()))) }200 XCTAssertEqual(unknown?.code, APIError.NextCode.badParent)201 let detail = try await api.page(id: parent.id)202 XCTAssertNil(detail.page.parentId, "nothing was written")203 }204205 func testRangesAndResumableDownload() async throws {206 let live = try await LiveNext.make()207 try await live.require(ServerFeature.filesRange)208 let api = live.api209 let directory = FileManager.default.temporaryDirectory.appendingPathComponent("spn-live-range-\(UUID().uuidString)")210 try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)211 addTeardownBlock { try? FileManager.default.removeItem(at: directory) }212213 // 300 kB of bytes unique to this run (the server stores a content once per checksum).214 var content = Data()215 var counter = 0216 let seed = UUID().uuidString217 while content.count < 300_000 {218 content.append(contentsOf: SHA256.hash(data: Data("\(seed)-\(counter)".utf8)))219 counter += 1220 }221 content = content.prefix(300_000)222 let checksum = SHA256.hash(data: content).map { String(format: "%02x", $0) }.joined()223 let source = directory.appendingPathComponent("live-range.bin")224 try content.write(to: source)225 let page = try await live.page(self, "fichiers")226 let uploaded = try await api.uploadAttachment(fileURL: source, id: NotesAPI.newID(), pageID: page.id, mimeType: "application/octet-stream").value227 addTeardownBlock { _ = try? await api.deleteAttachment(id: uploaded.id) }228 XCTAssertEqual(uploaded.size, 300_000)229230 // Ranges.231 let first = try await api.fileRange(id: uploaded.id, range: .bounded(0...9))232 XCTAssertEqual(first.status, 206)233 XCTAssertEqual(first.data, content.prefix(10))234 XCTAssertEqual(first.contentRange, ContentRange(range: 0...9, total: 300_000))235 XCTAssertEqual(first.etag, "\"\(checksum)\"", "ETag = sha256 of the whole content")236 let tail = try await api.fileRange(id: uploaded.id, range: .suffix(16))237 XCTAssertEqual(tail.data, content.suffix(16))238 XCTAssertEqual(tail.contentRange?.range, 299_984...299_999)239 let open = try await api.fileRange(id: uploaded.id, range: .from(299_000), ifRange: first.etag)240 XCTAssertEqual(open.status, 206)241 XCTAssertEqual(open.data, content.suffix(1000))242 let stale = try await api.fileRange(id: uploaded.id, range: .from(299_000), ifRange: "\"\(String(repeating: "0", count: 64))\"")243 XCTAssertEqual(stale.status, 200, "another ETag in If-Range: the whole file")244 XCTAssertEqual(stale.data, content)245 let beyond = try await api.fileRange(id: uploaded.id, range: .from(300_000))246 XCTAssertTrue(beyond.isUnsatisfiable)247 XCTAssertEqual(beyond.totalSize, 300_000)248 XCTAssertEqual(beyond.etag, first.etag)249250 // Resume from a partial file: only the missing bytes travel, and the result is verified.251 let destination = directory.appendingPathComponent("download.bin")252 let partial = directory.appendingPathComponent("download.bin.part")253 try content.prefix(123_456).write(to: partial)254 try Data((first.etag ?? "").utf8).write(to: NotesAPI.etagSidecarURL(forPartial: partial))255 let progress = RangeProgressLog()256 let resumed = try await api.downloadFile(id: uploaded.id, to: destination, resumingFrom: partial) { progress.add($0) }257 XCTAssertEqual(try Data(contentsOf: destination), content)258 XCTAssertEqual(resumed.resumedFrom, 123_456)259 XCTAssertFalse(resumed.restarted)260 XCTAssertTrue(resumed.verified)261 XCTAssertEqual(resumed.size, 300_000)262 XCTAssertEqual(resumed.etag, first.etag)263 XCTAssertEqual(resumed.suggestedFilename, "live-range.bin")264 XCTAssertEqual(progress.values.last, TransferProgress(completedBytes: 300_000, totalBytes: 300_000))265 XCTAssertGreaterThan(progress.values.first?.completedBytes ?? 0, 123_456)266267 // Already complete on disk: 416, nothing transferred.268 try content.write(to: partial)269 let complete = try await api.downloadFile(id: uploaded.id, to: destination, resumingFrom: partial, etag: first.etag)270 XCTAssertEqual(complete.resumedFrom, 300_000)271 XCTAssertTrue(complete.verified)272273 // Partial bytes of another content (wrong ETag): the server sends the whole file, the download restarts.274 try Data(repeating: 0x2A, count: 50_000).write(to: partial)275 let restarted = try await api.downloadFile(id: uploaded.id, to: destination, resumingFrom: partial, etag: "\"\(String(repeating: "1", count: 64))\"")276 XCTAssertTrue(restarted.restarted)277 XCTAssertEqual(restarted.resumedFrom, 0)278 XCTAssertTrue(restarted.verified)279 XCTAssertEqual(try Data(contentsOf: destination), content)280281 // From nothing, and the plain 1.x download still works.282 let fresh = try await api.downloadFile(id: uploaded.id, to: destination, resumingFrom: partial)283 XCTAssertEqual(fresh.resumedFrom, 0)284 XCTAssertFalse(fresh.restarted)285 XCTAssertTrue(fresh.verified)286 let plain = try await api.downloadFile(id: uploaded.id, to: directory.appendingPathComponent("plain.bin"))287 XCTAssertEqual(try Data(contentsOf: plain.fileURL), content)288289 // 3.0: a soft-deleted attachment is not served any more.290 _ = try await api.deleteAttachment(id: uploaded.id)291 let deleted = await captureAPIError { try await api.fileRange(id: uploaded.id, range: .bounded(0...9)) }292 XCTAssertEqual(deleted?.status, 404)293 let gone = await captureAPIError { try await api.downloadFile(id: uploaded.id, to: destination, resumingFrom: partial) }294 XCTAssertEqual(gone?.code, APIError.NextCode.notFound)295 }296297 func testModelCatalogAndAbsentAIRoutes() async throws {298 let live = try await LiveNext.make()299 let api = live.api300 // A read: no model is called.301 let catalog = try await api.aiModelCatalog()302 XCTAssertFalse(catalog.models.isEmpty)303 XCTAssertFalse(catalog.providers.isEmpty)304 XCTAssertTrue(catalog.models.allSatisfy { !$0.id.isEmpty })305 XCTAssertTrue(catalog.models.contains { $0.available == true })306 XCTAssertNotNil(catalog.taskLabels)307 let everything = try await api.aiModelCatalog(AIModelQuery(all: true))308 XCTAssertGreaterThanOrEqual(everything.models.count, catalog.models.count)309 if catalog.creators != nil {310 // A server with the AI workstream: every model names its creator and its route.311 XCTAssertTrue(catalog.models.allSatisfy { $0.creator != nil && $0.route != nil && $0.routeKind != nil })312 }313314 // Voice and smart database properties: read-only probes. On a foundation server the routes do not exist.315 do {316 let state = try await api.audioTranscription(attachmentID: NotesAPI.newID())317 XCTAssertEqual(state.status, TranscriptionStatus.none)318 } catch let error as APIError {319 XCTAssertTrue(error.isRouteMissing || error.isNotFound, "\(error)")320 }321 do {322 _ = try await api.databaseAIRuns(databaseID: NotesAPI.newID())323 } catch let error as APIError {324 XCTAssertTrue(error.isRouteMissing || error.isNotFound, "\(error)")325 }326 do {327 _ = try await api.databaseAIConfig(databaseID: NotesAPI.newID())328 } catch let error as APIError {329 XCTAssertTrue(error.isRouteMissing || error.isNotFound, "\(error)")330 }331 }332}333