Cotton code
70 lines · 2.8 KB · SwiftRaw
// Send: one local write, and the chips written down beside it.//// The optimistic-cache cases (put a `temp-` row in on send, swap it on success,// filter it out on error) have no subject here: this app has no query cache, a// write answers with `tables` and the screens re-read (design P5). What is left// is the pair of facts that matter, and the two this file had to prove for itself.import Foundationimport XCTest@testable import CottonKitfinal class CaptureServiceTests: XCTestCase {    private let key = "@cotton:pendingLinks:v1:u1:ws-1"    private struct Kit {        let core: ScriptedCore        let store: MemoryLinkStore        let service: CaptureService    }    private func makeKit(answer: ScriptedCore.Reply = .ok(#"{"id":"i1"}"#)) -> Kit {        let core = ScriptedCore([CottonMethod.captureCreate: answer])        let store = MemoryLinkStore()        let links = PendingLinks(            userId: "u1",            workspaceId: "ws-1",            store: store,            source: ScriptedLinkSource(),            writer: RecordingLinkWriter(),            now: { Date(timeIntervalSince1970: 1_785_542_400) },            report: { _ in }        )        return Kit(core: core, store: store, service: CaptureService(core: core, links: links))    }    func testRemembersBothKindsOfChipAgainstTheRowThatLanded() async throws {        let kit = makeKit()        _ = try await kit.service.capture(            CaptureRequest(text: "lunch with Meera", projectIds: ["pr-1"]),            personIds: ["per-1", "per-2"]        )        let stored = kit.store.intents(key)        XCTAssertEqual(stored.count, 1)        XCTAssertEqual(stored.first?.inboxItemId, "i1")        XCTAssertEqual(stored.first?.projectIds, ["pr-1"])        XCTAssertEqual(stored.first?.personIds, ["per-1", "per-2"])    }    func testARefusalIsThrownAndRemembersNothing() async {        // The previous app rolled its optimistic row back on error; the host        // does that by keeping its draft when this throws. What matters here is the        // half that IS this layer's: a capture that did not land has no id to hang        // an intent on, and inventing one would leave a chip waiting a week for a        // record that was never going to exist.        let kit = makeKit(answer: .refuse(code: "bad_request", message: "no"))        do {            _ = try await kit.service.capture(                CaptureRequest(text: "x", projectIds: ["pr-1"]),                personIds: ["per-1"]            )            XCTFail("a refused capture must throw")        } catch {            XCTAssertEqual((error as? CottonCallError)?.code, "bad_request")        }        XCTAssertFalse(kit.store.has(key))    }    // ── The pictures on a capture (store schema 36) ─────────────────────}