Cotton code
78 lines · 4.7 KB · SwiftRaw
import CottonKitimport CottonValuesimport Foundationimport XCTest@testable import CottonUI@MainActorfinal class DocumentsConversionTests: XCTestCase {  /// RULE: The list offers one way in — Add document, which opens the Files / Camera / Photos picker at /documents/import — and no second copy of it in a related list; a kept document opens its own page, and one the core did not name goes back to the list.  func testTheOneAddOpensThePickerAndAKeptDocumentOpensItsPage() throws {    XCTAssertEqual(DocumentImportRoute.opens(MediaCaptured(key: "k", recordId: "doc-1")), .documentsDocument(id: "doc-1"))    XCTAssertNil(DocumentImportRoute.opens(MediaCaptured(key: "k", recordId: "")))  }  /// RULE: A refusal from the core reads as its own sentence wherever a screen draws a caught error — never '(CottonKit.CottonCallError error 1.)'.  func testACallRefusalReadsAsItsSentence() {    XCTAssertEqual(CottonCallError(code: "not_found", message: "The file is not on the Mac.").localizedDescription, "The file is not on the Mac.")    XCTAssertFalse(CottonCallError(code: "x", message: "").localizedDescription.contains("CottonCallError"))  }  /// RULE: Exported bytes use the projected copy name, without allowing a projected path to escape the temporary share directory.  func testFileCopiesUseTheirProjectedName() {    XCTAssertEqual(      FileColumnContent.copyName("Example statement.pdf", fallback: "object.bin"),      "Example statement.pdf")    XCTAssertEqual(      FileColumnContent.copyName("../Example statement.pdf", fallback: "object.bin"),      "Example statement.pdf")    XCTAssertEqual(FileColumnContent.copyName(nil, fallback: "object.bin"), "object.bin")  }  /// RULE: A list filter matches a scalar or an element of a projected list, so a document spanning two years remains findable in both years.  func testFacetsMatchAListElement() {    XCTAssertTrue(RecordFilter.matches(.list([.text("2025"), .text("2026")]), .text("2025")))    XCTAssertTrue(RecordFilter.matches(.text("Example Bank"), .text("Example Bank")))    XCTAssertFalse(RecordFilter.matches(.list([.text("2025")]), .text("2026")))  }  /// RULE: A password input never appears in a value label or change receipt; the engine must also omit it from its returned record.  func testPasswordInputsDoNotEchoIntoReceipts() throws {    let column = ColumnDefinition(key: "password", type: .text, presentation: "password")    let words = try XCTUnwrap(ColumnWords.words(.text("invented-password"), column: column))    XCTAssertFalse(words.isEmpty)    XCTAssertFalse(words.contains("invented-password"), "a password echoed into a receipt")  }  /// RULE: Local previews never fetch; explicitly ensuring one file uses the engine seam and propagates a refusal.  func testFileStoreSeparatesLocalLookupFromEnsure() async throws {    let core = ScriptedCore([      CottonMethod.readMediaPath: .ok("{}"),      "media.ensure": .ok(#"{"path":"/tmp/example.pdf"}"#)    ])    let store = CoreAttachmentStore(core: core)    let local = await store.localURL(for: "example-key")    XCTAssertNil(local)    let calls = await core.calls    XCTAssertEqual(calls.map(\.method), [CottonMethod.readMediaPath])    let ensured = try await store.ensure(for: "example-key")    XCTAssertEqual(ensured?.path, "/tmp/example.pdf")    await core.answer("media.ensure", .refuse(code: "unreachable_server", message: "Unavailable"))    do { _ = try await store.ensure(for: "example-key"); XCTFail("must propagate refusal") } catch {}  }  /// RULE: Revision and search refreshes keep the current list and its rows; changing a facet constructs a different filtered list.  func testRefreshRetainsTheSameListModel() async throws {    let definition = try XCTUnwrap(TableDefinitions(FixtureDefinitions.load(      from: Bundle(url: SourceSweep.iosSources.appendingPathComponent("CottonUI/Resources"))).definitions)["document"])    let row = TableRecord(id: "example-document", values: [:])    let store = FixtureRecords(definitions: [definition], records: [definition.id: [row]])    let first = RecordListModel.retaining(nil, definition: definition, store: store, core: ListCoreFake(), archived: false, filter: [:])    await first.reload(project: nil, query: "")    let refreshed = RecordListModel.retaining(first, definition: definition, store: store, core: ListCoreFake(), archived: false, filter: [:])    XCTAssertTrue(refreshed === first)    XCTAssertEqual(refreshed.records.map(\.id), [row.id])    XCTAssertEqual(refreshed.phase, .loaded)    let filtered = RecordListModel.retaining(first, definition: definition, store: store, core: ListCoreFake(), archived: false, filter: ["issuer": .text("Example")])    XCTAssertFalse(filtered === first)  }}