SPB Git

Repository Public

LiveRepositoryTests.swift 12.0 KB 153 lines swift

1// SP Notes Core2// File: Tests/SPNotesSyncTests/LiveRepositoryTests.swift3// Description: The repositories against the real scratch backend (never production; free routes only — no model is4//              called): a database written online and read back offline-style from the entity copy, with the local5//              evaluation of filters and sorts compared row for row with the server's own query; relations, concepts,6//              references; the cached read models. Skipped unless SPN_LIVE_URL and SPN_LIVE_TOKEN are set.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import Foundation11import SPNotesAPI12import SPNotesDocument13import SPNotesStore14import Testing15@testable import SPNotesSync16@testable import SPNotesTestSupport1718@Suite("Live: repositories against the scratch backend (2.0)")19struct LiveRepositoryTests {20    static var enabled: Bool {21        let env = ProcessInfo.processInfo.environment22        guard let url = env["SPN_LIVE_URL"].flatMap(URL.init(string:)), let host = url.host, env["SPN_LIVE_TOKEN"]?.isEmpty == false else { return false }23        return !host.hasSuffix("spboucher.ai")24    }2526    @Test("databases, graph, research and read models through the repositories; local view evaluation equals the server's query", .enabled(if: LiveRepositoryTests.enabled))27    func repositories() async throws {28        let env = ProcessInfo.processInfo.environment29        let directory = FileManager.default.temporaryDirectory.appendingPathComponent("spn-live-repos-\(UUID().uuidString)")30        try FileManager.default.createDirectory(at: directory.appendingPathComponent("files"), withIntermediateDirectories: true)31        defer { try? FileManager.default.removeItem(at: directory) }32        let store = try NotesStore(url: directory.appendingPathComponent("notes.sqlite"))33        let api = NotesAPI(client: HTTPClient(configuration: APIConfiguration(baseURL: URL(string: env["SPN_LIVE_URL"]!)!, userAgent: APIConfiguration.userAgent(appVersion: "2.0.0")),34                                              tokenStore: InMemoryTokenStore(token: env["SPN_LIVE_TOKEN"])))35        let engine = SyncEngine(store: store, api: api, files: TestFiles(root: directory.appendingPathComponent("files")))36        let repos = NotesRepositories(store: store, api: api, engine: nil)37        var pages: [String] = []38        var references: [String] = []39        var concepts: [String] = []40        do {41            #expect(await engine.sync() == .completed)42            #expect(await repos.features()?.has(ServerFeature.entities) == true)4344            // 1. a database: host page, properties, rows — all written online through the repository45            let tag = String(UUID().uuidString.prefix(6))46            let host = try await api.createPage(PageDraft(id: NotesAPI.newID(), title: "Live · base \(tag)", section: .databases, type: .database)).value.id47            pages.append(host)48            let database = try await repos.databases.refresh(id: host)49            let price = try await repos.databases.createProperty(databaseId: database.id, DatabasePropertyDraft(name: "Prix", type: "number"))50            let state = try await repos.databases.createProperty(databaseId: database.id, DatabasePropertyDraft(name: "État", type: "select",51                config: RawJSON(string: #"{"options":[{"id":"optlive01","name":"Loué","color":"green"},{"id":"optlive02","name":"Vacant","color":"red"}]}"#)))52            let bought = try await repos.databases.createProperty(databaseId: database.id, DatabasePropertyDraft(name: "Achat", type: "date"))53            let seen = try await repos.databases.createProperty(databaseId: database.id, DatabasePropertyDraft(name: "Inspecté", type: "checkbox"))54            let data: [(String, String)] = [55                ("252, montée Dalton", #"{"\#(price.id)":565000,"\#(state.id)":"optlive02","\#(bought.id)":"2026-03-15","\#(seen.id)":true}"#),56                ("99, rue de Magog", #"{"\#(price.id)":447000,"\#(state.id)":"optlive01","\#(bought.id)":"2025-11-02"}"#),57                ("255, rue Laurel", #"{"\#(price.id)":430000,"\#(state.id)":"optlive01","\#(seen.id)":false}"#),58                ("Terrain", #"{"\#(price.id)":80000}"#),59                ("Sans prix", #"{"\#(bought.id)":"2026-07-01","\#(seen.id)":true}"#),60            ]61            for (title, values) in data {62                let row = try await repos.databases.createRow(databaseId: database.id, DatabaseRowDraft(title: title, values: RawJSON(string: values)))63                pages.append(row.pageId)64            }65            // 2. the feed brings the same entities; the local copy answers without the network66            #expect(await engine.sync() == .completed)67            let local = try #require(try await repos.databases.database(id: host))68            #expect(local.properties.count == 5 && local.properties.first?.type == "title")69            #expect(try await repos.databases.localRows(databaseId: database.id).count == 5)7071            // 3. the local evaluation equals the server's own query, rule by rule and sort by sort72            typealias F = SPNotesAPI.DatabaseFilter73            let cases: [(String, DatabaseViewFilter?, [DatabaseViewSort], String?)] = [74                ("no filter", nil, [], nil),75                ("number ≥", .rule(property: price.id, op: "gte", value: 430000), [.init(property: price.id, descending: true)], nil),76                ("select eq by name", .rule(property: state.id, op: "eq", value: "Loué"), [.init(property: price.id)], nil),77                ("select neq", .rule(property: state.id, op: "neq", value: "optlive01"), [], nil),78                ("empty", .rule(property: price.id, op: "empty", value: nil), [], nil),79                ("checkbox", .rule(property: seen.id, op: "eq", value: true), [.init(property: local.properties[0].id, descending: true)], nil),80                ("date before", .rule(property: bought.id, op: "before", value: "2026-03-15"), [], nil),81                ("date on", .rule(property: bought.id, op: "on", value: "2026-03-15"), [], nil),82                ("title contains", .rule(property: local.properties[0].id, op: "contains", value: "RUE"), [.init(property: bought.id)], nil),83                ("and / or", .all([.rule(property: price.id, op: "lt", value: 500000), .any([.rule(property: state.id, op: "eq", value: "optlive01"), .rule(property: state.id, op: "empty", value: nil)])]),84                 [.init(property: state.id), .init(property: price.id, descending: true)], nil),85                ("sort by date desc, empty last", nil, [.init(property: bought.id, descending: true)], nil),86                ("sort by select then title", nil, [.init(property: state.id, descending: true), .init(property: local.properties[0].id)], nil),87                ("search", nil, [], "vacant"),88                ("search number", nil, [], "447"),89            ]90            for (name, filter, sorts, search) in cases {91                let mine = try await repos.databases.evaluateLocally(databaseId: database.id, query: DatabaseViewQuery(filter: filter, sorts: sorts, search: search))92                let body = RawJSON(string: JSONValue.object(["filter": filter?.topLevelJSON ?? .null, "sorts": .array(sorts.map(\.json)), "search": search.map(JSONValue.string) ?? .null]93                    .filter { !($0.key == "search" && $0.value.isNull) }).canonicalString())94                let answer = try await api.client.decode(DatabaseQueryResult.self, .json(.post, "/api/databases/\(database.id)/query", body: body))95                #expect(mine.isExact, "\(name): evaluable on the device")96                #expect(mine.rows.map(\.title) == answer.rows.map { $0.title ?? "" }, "\(name): local \(mine.rows.map(\.title)) server \(answer.rows.map { $0.title ?? "" })")97                #expect(mine.total == answer.total, "\(name): same total")98            }99            let online = try await repos.databases.rows(databaseId: database.id)100            #expect(online.source == .server && online.rows.count == 5)101102            // 4. relations and concepts: online writes land in the local copy; a duplicate is a typed error103            let relation = try await repos.graph.createRelation(RelationDraft(fromId: pages[1], toId: pages[2], relType: RelationType.supports, note: "live"))104            #expect(try await repos.graph.localRelations(touching: pages[1]).contains { $0.id == relation.id })105            do { _ = try await repos.graph.createRelation(RelationDraft(fromId: pages[1], toId: pages[2], relType: RelationType.supports)); Issue.record("expected relation_exists") }106            catch let error as RepositoryError { #expect(error.code == APIError.NextCode.relationExists) }107            #expect(try await repos.graph.reject(relationId: relation.id).status == DecisionStatus.rejected)108            try await repos.graph.deleteRelation(id: relation.id)109            try await repos.graph.deleteRelation(id: relation.id)     // already gone: not an error for the app110            #expect(try await repos.graph.localRelations(touching: pages[1]).allSatisfy { $0.id != relation.id })111            let concept = try await repos.graph.createConcept(name: "Live · concept \(tag)", pageIds: [pages[1]])112            concepts.append(concept.id)113            #expect(try await repos.graph.localConcepts().contains { $0.id == concept.id })114            #expect(try await repos.graph.concept(id: concept.id).pageLinks?.count == 1)115            #expect(try await repos.graph.view(.local(pageId: pages[1])).value.nodes.contains { $0.entityId == concept.id })116117            // 5. references118            let reference = try await repos.research.create(ReferenceDraft(pageId: pages[1], title: "Live · référence \(tag)", authors: ["Boucher, S.-P."], year: 2026))119            references.append(reference.id)120            #expect(try await repos.research.localReferences(pageId: pages[1]).map(\.id) == [reference.id])121            #expect(try await repos.research.resolve(ids: [reference.id])[reference.id]?.citeKey == reference.citeKey)122            #expect(try await repos.research.resolveLocally(ids: [reference.id]).count == 1)123124            // 6. read models, then the same from the cache125            let health = try await repos.knowledge.health()126            #expect(health.source == .network && health.value.score.pages > 0)127            #expect(try await repos.knowledge.health(policy: .cacheOnly).value == health.value)128            #expect(try await repos.knowledge.brief().value.date.isEmpty == false)129            #expect(try await repos.knowledge.zoom(pageId: pages[1]).value.level == 2)130            let rows = try await repos.knowledge.query(LiveQuery(source: LiveQuerySource.database, databaseId: host, limit: 10))131            #expect(rows.value.total == 5)132            let journal = try await repos.timeline.events(JournalQuery(pageId: pages[1], limit: 20))133            #expect(journal.events.contains { $0.kind == EventKind.relationCreated } || journal.events.contains { $0.kind.hasPrefix("relation.") })134            #expect(try await repos.timeline.events(JournalQuery(pageId: pages[1], limit: 20), policy: .cacheOnly).events.map(\.id) == journal.events.map(\.id))135            #expect(try await repos.timeline.timeMachine(pageId: pages[1], at: Date()).value.pages.first?.existed == true)136            let search = try await repos.search.search("Dalton")137            #expect(search.results.contains { $0.title.contains("Dalton") } || search.usedServer)138            // The entity sync and the online writes agree: after one more cycle nothing is pending and nothing was duplicated.139            #expect(await engine.sync() == .completed)140            #expect(try await store.entityOpCount() == 0)141            #expect(try await store.dirtyEntities().isEmpty)142            #expect(try await repos.databases.localRows(databaseId: database.id).count == 5)143        } catch {144            Issue.record("live scenario failed: \(error)")145        }146        for id in concepts { _ = try? await api.deleteConcept(id: id) }147        for id in references { _ = try? await api.deleteReference(id: id) }148        for id in pages.reversed() { _ = try? await api.trashPage(id: id); _ = try? await api.purgePage(id: id) }149        await engine.stop()150        await store.close()151    }152}153
Files main
Menu

Theme