SPB Git

Repository Public

LiveSmokeTests.swift 12.1 KB 209 lines swift

1// SP Notes Core2// File: Tests/SPNotesSyncTests/LiveSmokeTests.swift3// Description: Smoke test of the synchronisation engine against the real TEST backend (never production): two engines with two4//              temporary stores — create, sync, concurrent edits merged, one conflict and its resolutions, an attachment round5//              trip. Skipped unless SPN_LIVE_URL and SPN_LIVE_TOKEN are set; every page it creates is trashed at the end.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9//10// Run:11//   ssh -f -N -L 8482:localhost:8482 M4M64c12//   SPN_LIVE_URL=http://localhost:8482 SPN_LIVE_TOKEN=<dev token> SPN_ONLY=Sync swift test --scratch-path .build-synctest --filter LiveSmoke13import Foundation14import SPNotesAPI15import SPNotesStore16import Testing17@testable import SPNotesSync18@testable import SPNotesTestSupport1920private enum Live {21    static func value(_ name: String) -> String? {22        guard let value = ProcessInfo.processInfo.environment[name], !value.isEmpty else { return nil }23        return value24    }25    static var url: URL? { value("SPN_LIVE_URL").flatMap(URL.init(string:)) }26    static var token: String? { value("SPN_LIVE_TOKEN") }27    /// Configured, and not pointing at production.28    static var enabled: Bool {29        guard let url, token != nil, let host = url.host else { return false }30        return !host.hasSuffix("spboucher.ai")31    }32}3334/// A device talking to the real test server.35private final class LiveDevice: @unchecked Sendable {36    let directory: URL37    let files: TestFiles38    let store: NotesStore39    let api: NotesAPI40    let engine: SyncEngine4142    init(_ name: String) throws {43        directory = FileManager.default.temporaryDirectory.appendingPathComponent("spn-live-\(name)-\(UUID().uuidString)")44        try FileManager.default.createDirectory(at: directory.appendingPathComponent("files"), withIntermediateDirectories: true)45        files = TestFiles(root: directory.appendingPathComponent("files"))46        store = try NotesStore(url: directory.appendingPathComponent("notes.sqlite"))47        let client = HTTPClient(configuration: APIConfiguration(baseURL: Live.url!, userAgent: APIConfiguration.userAgent(appVersion: "0.0.1")), tokenStore: InMemoryTokenStore(token: Live.token))48        api = NotesAPI(client: client)49        engine = SyncEngine(store: store, api: api, files: files)50    }5152    deinit { try? FileManager.default.removeItem(at: directory) }5354    func texts(_ id: String) async throws -> [String] {55        guard let page = try await store.page(id: id) else { throw StoreError.notFound("page \(id)") }56        return Doc.texts(page.content)57    }5859    func setBlocks(_ id: String, _ blocks: [Block]) async throws {60        let content = Doc.data(blocks)61        let digest = ContentDigest(content: content)62        _ = try await store.updateContent(id: id, content: content, plainText: digest.plainText, wordCount: digest.wordCount, preview: digest.preview)63    }6465    func edit(_ id: String, block: String, text: String) async throws {66        guard let page = try await store.page(id: id) else { throw StoreError.notFound("page \(id)") }67        var blocks = Doc.blocks(page.content)68        guard let index = blocks.firstIndex(where: { $0.id == block }) else { throw StoreError.notFound("block \(block)") }69        blocks[index].text = text70        try await setBlocks(id, blocks)71    }72}7374@Suite("Live smoke test (TEST server)")75struct LiveSmokeTests {76    @Test("two devices against the real test server: create, merge, conflict and resolutions, attachment", .enabled(if: Live.enabled))77    func twoDevices() async throws {78        let a = try LiveDevice("A")79        let b = try LiveDevice("B")80        let tag = "spn-sync-live-" + UUID().uuidString.prefix(8).lowercased()81        var created: [String] = []82        var attachments: [String] = []8384        do {85            // 1. create on A, receive on B86            let digest = ContentDigest(content: Doc.data([("la", "alpha"), ("lb", "beta"), ("lc", "gamma")]))87            let note = try await a.store.createPage(NewPage(title: "\(tag) note", content: Doc.data([("la", "alpha"), ("lb", "beta"), ("lc", "gamma")]),88                                                            plainText: digest.plainText, wordCount: digest.wordCount, preview: digest.preview, tags: [tag]))89            let id = note.id90            created.append(id)91            #expect(await a.engine.sync() == .completed)92            #expect(await b.engine.sync() == .completed)93            #expect(try await b.texts(id) == ["alpha", "beta", "gamma"])94            #expect(try await b.store.page(id: id)?.tags == [tag])95            #expect(try await a.store.page(id: id)?.isDirty == false)96            let first = try await a.api.page(id: id)97            #expect(first.page.version == 1)9899            // 2. edits of different blocks on both devices → merged, no conflict100            try await a.edit(id, block: "la", text: "alpha by A")101            try await b.edit(id, block: "lc", text: "gamma by B")102            #expect(await a.engine.sync() == .completed)103            #expect(await b.engine.sync() == .completed)104            #expect(await a.engine.sync() == .completed)105            #expect(try await a.texts(id) == ["alpha by A", "beta", "gamma by B"])106            #expect(try await b.texts(id) == ["alpha by A", "beta", "gamma by B"])107            #expect(try await b.store.conflicts().isEmpty)108            #expect(Doc.texts(try await a.api.page(id: id).page.content.data) == ["alpha by A", "beta", "gamma by B"])109110            // 3. the same block on both devices → conflict on B, resolved by keeping both111            try await a.edit(id, block: "lb", text: "beta by A")112            try await b.edit(id, block: "lb", text: "beta by B")113            #expect(await a.engine.sync() == .completed)114            #expect(await b.engine.sync() == .completed)115            let conflict = try #require(try await b.store.conflict(pageId: id))116            #expect(Doc.texts(conflict.server.content) == ["alpha by A", "beta by A", "gamma by B"])117            #expect(try await b.texts(id) == ["alpha by A", "beta by B", "gamma by B"], "local text intact")118            #expect(Doc.texts(try await a.api.page(id: id).page.content.data) == ["alpha by A", "beta by A", "gamma by B"], "server not overwritten")119            let copy = try #require(try await b.engine.resolveConflict(pageId: id, choice: .keepBoth))120            created.append(copy)121            #expect(await b.engine.sync() == .completed)122            #expect(await a.engine.sync() == .completed)123            for device in [a, b] {124                #expect(try await device.texts(id) == ["alpha by A", "beta by A", "gamma by B"])125                #expect(try await device.texts(copy) == ["alpha by A", "beta by B", "gamma by B"])126                #expect(try await device.store.page(id: copy)?.title == "\(tag) note (conflict copy)")127            }128129            // 4. another conflict, resolved by keeping the local text: the other text stays in the server history130            try await a.edit(id, block: "lb", text: "beta, second round by A")131            try await b.edit(id, block: "lb", text: "beta, second round by B")132            #expect(await a.engine.sync() == .completed)133            #expect(await b.engine.sync() == .completed)134            #expect(try await b.store.conflict(pageId: id) != nil)135            _ = try await b.engine.resolveConflict(pageId: id, choice: .keepMine)136            #expect(await b.engine.sync() == .completed)137            #expect(await a.engine.sync() == .completed)138            #expect(try await a.texts(id) == ["alpha by A", "beta, second round by B", "gamma by B"])139            let versions = try await a.api.pageVersions(pageID: id)140            var kept = false141            for summary in versions {142                let snapshot = try await a.api.pageVersion(pageID: id, version: summary.version)143                if Doc.texts(snapshot.content.data).contains("beta, second round by A") { kept = true }144            }145            #expect(kept, "the discarded text is in the server history")146147            // 5. a lost acknowledgement cannot be injected here, but its recovery path can be exercised: the device forgets that its148            //    push went through (same state as after a lost answer) and must settle without a conflict or a second write.149            try await a.edit(id, block: "la", text: "alpha, third round by A")150            let before = try #require(try await a.store.page(id: id))151            _ = try await a.api.patchPage(id: id, PagePatch(title: before.title, content: RawJSON(data: before.content), properties: RawJSON(data: before.properties), baseVersion: before.serverVersion))152            #expect(await a.engine.sync() == .completed)153            #expect(try await a.store.conflicts().isEmpty)154            #expect(try await a.store.page(id: id)?.isDirty == false)155            #expect(try await a.api.page(id: id).page.version == before.serverVersion + 1)156157            // 6. attachment round trip158            let bytes = Data([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] + (0..<4096).map { UInt8(truncatingIfNeeded: $0 &* 7) })159            let attachmentId = UUID().uuidString.lowercased()160            let relative = a.files.relativePath(id: attachmentId, filename: "\(tag).png")161            try FileManager.default.createDirectory(at: a.files.url(relativePath: relative).deletingLastPathComponent(), withIntermediateDirectories: true)162            try bytes.write(to: a.files.url(relativePath: relative))163            _ = try await a.store.addLocalAttachment(id: attachmentId, pageId: id, filename: "\(tag).png", mime: "image/png", size: Int64(bytes.count), sha256: nil, localPath: relative)164            attachments.append(attachmentId)165            #expect(await a.engine.sync() == .completed)166            #expect(try await a.store.attachment(id: attachmentId)?.uploaded == true)167            #expect(await b.engine.sync() == .completed)168            let received = try #require(try await b.store.attachment(id: attachmentId))169            #expect(received.pageId == id)170            #expect(received.state == .local, "pictures are downloaded automatically")171            if let path = received.localPath { #expect(try Data(contentsOf: b.files.url(relativePath: path)) == bytes) }172173            // 7. trash from B (through the queue), seen by A174            _ = try await b.store.trash(id: copy)175            #expect(await b.engine.sync() == .completed)176            #expect(await a.engine.sync() == .completed)177            #expect(try await a.store.page(id: copy)?.deletedAt != nil)178            #expect(await a.engine.currentStatus().pending == 0)179            #expect(await b.engine.currentStatus().pending == 0)180181            // 8. the note is trashed on B while A edits it: the edit waits (the server refuses writes in its trash), then a restore182            //    from A lets it through in the same call183            _ = try await b.store.trash(id: id)184            #expect(await b.engine.sync() == .completed)185            try await a.edit(id, block: "lc", text: "gamma, typed while in the trash")186            #expect(await a.engine.sync() == .completed)187            let waiting = try #require(try await a.store.page(id: id))188            #expect(waiting.deletedAt != nil && waiting.isDirty && waiting.syncError == nil)189            #expect(try await a.api.pageHead(id: id).deletedAt != nil)190            _ = try await a.store.restore(id: id)191            #expect(await a.engine.sync() == .completed)192            let restored = try await a.api.page(id: id).page193            #expect(restored.deletedAt == nil)194            #expect(Doc.texts(restored.content.data).last == "gamma, typed while in the trash")195            #expect(try await a.store.page(id: id)?.isDirty == false)196            #expect(await b.engine.sync() == .completed)197            #expect(try await b.texts(id).last == "gamma, typed while in the trash")198        } catch {199            Issue.record("live scenario failed: \(error)")200        }201202        // clean up: everything created here goes to the trash (the test database is never reset)203        for id in attachments { _ = try? await a.api.deleteAttachment(id: id) }204        for id in created { _ = try? await a.api.trashPage(id: id) }205        await a.engine.stop(); await b.engine.stop()206        await a.store.close(); await b.store.close()207    }208}209
Files main
Menu

Theme