93 lines · 5.5 KB · SwiftRaw
import CottonKitimport CottonValuesimport Foundationimport XCTest@testable import CottonUI@MainActorfinal class RecordFilePickTests: XCTestCase { private let envelope = #"{"command":"choose","answer":{"platform_action":"pick_file","accept":["public.plain-text"],"then":{"method":"records.create","arguments":{"table":"note","database":"captured-database","data":{"file":"$path","name":"$name","body":"literal $path suffix"}}}}}"# private func pick() throws -> RecordFilePick { let outcome = try JSONDecoder().decode(CommandOutcome.self, from: Data(envelope.utf8)) let change = try outcome.change(table: "example", record: "example-row") guard case .platform(.pickFile(let pick)) = change.opened else { throw ActionRefused("Missing file pick") } XCTAssertNil(change.said) return pick } /// RULE: A file effect may continue only records.create for a shipped /// engine; exact placeholder values are substituted, and the captured /// database and every other literal survive a navigation change. func testContinuationIsClosedAndPreservesCapturedArguments() throws { let pick = try pick() let args = pick.arguments(file: URL(fileURLWithPath: "/synthetic/export.txt")) XCTAssertEqual(args["database"], .text("captured-database")) guard case .object(let data) = args["data"] else { return XCTFail("No create data") } XCTAssertEqual(data["file"], .text("/synthetic/export.txt")) XCTAssertEqual(data["name"], .text("export.txt")) XCTAssertEqual(data["body"], .text("literal $path suffix")) for bad in [envelope.replacingOccurrences(of: "records.create", with: "devices.revoke"), envelope.replacingOccurrences(of: "public.plain-text", with: ""), envelope.replacingOccurrences(of: "captured-database", with: ""), envelope.replacingOccurrences(of: "\"file\":\"$path\"", with: "\"file\":\"/arbitrary/path\"")] { XCTAssertThrowsError(try JSONDecoder().decode(CommandOutcome.self, from: Data(bad.utf8))) } } /// RULE: The platform continuation is one native create with its captured /// scope, never a Swift adapter write; only a returned durable record may /// be opened, and a response for a different destination is refused. func testContinuationCallsNativeOnceAndOpensOnlyItsReturnedRecord() async throws { let pick = try pick() let reply = #"{"change":{"id":"created-change","record_id":"made-row","undoable":true},"record":{"id":"made-row","table":"note","data":{"body":"Imported"}}}"# let core = ScriptedCore(["records.create": .ok(reply)]) let records = CoreRecords(core: core, bundle: try TestResources.bundle()) let file = URL(fileURLWithPath: "/synthetic/export.txt") let changed = try await records.continueFilePick(pick, file: file) XCTAssertEqual(changed.created, RecordLink(table: "note", id: "made-row")) let calls = await core.calls XCTAssertEqual(calls.map(\.method), ["records.create"]) let request = try JSONDecoder().decode([String: RecordValue].self, from: Data(try XCTUnwrap(calls.first).requestJSON.utf8)) XCTAssertEqual(request, pick.arguments(file: file)) await core.answer("records.create", .ok(reply.replacingOccurrences(of: "\"table\":\"note\"", with: "\"table\":\"todo\""))) do { _ = try await records.continueFilePick(pick, file: file) XCTFail("A response for another destination must refuse") } catch is DecodingError {} let unknown = try JSONDecoder().decode(CommandOutcome.self, from: Data(envelope.replacingOccurrences(of: "\"table\":\"note\"", with: "\"table\":\"not-shipped\"").utf8)) guard case .platform(.pickFile(let invalid)) = try unknown.change(table: "example", record: "example-row").opened else { return XCTFail("No pick") } let before = await core.calls.count do { _ = try await records.continueFilePick(invalid, file: file) XCTFail("An unshipped destination must refuse before any call") } catch is ActionRefused {} let after = await core.calls.count XCTAssertEqual(after, before) } /// RULE: Cancellation causes no native continuation; native refusal is /// propagated, and the chosen file's access remains held throughout the /// awaited continuation and is released even when that call refuses. func testCancellationAndRefusalPreserveTheFileCapabilityLifetime() async throws { let pick = try pick() var calls = 0 let cancelled = try await RecordFileEffects.perform(pick, choose: { _ in nil }, access: { _ in XCTFail("Access after cancellation"); return {} }, run: { _ in calls += 1; return ChangeRecord(id: "unexpected", byline: "You", before: nil, after: nil, undoable: false, reason: nil) }) XCTAssertNil(cancelled) XCTAssertEqual(calls, 0) var held = false do { _ = try await RecordFileEffects.perform(pick, choose: { _ in URL(fileURLWithPath: "/synthetic/export.txt") }, access: { _ in held = true; return { held = false } }, run: { _ in XCTAssertTrue(held) await Task.yield() XCTAssertTrue(held) calls += 1 throw ActionRefused("Native copy refused") }) XCTFail("A refused copy must fail") } catch let refused as ActionRefused { XCTAssertEqual(refused.sentence, "Native copy refused") } XCTAssertFalse(held) XCTAssertEqual(calls, 1) }}