// The editors' rules: the guard on the folder — editors that never save — and // the change receipt's undo. // // Every date here is pinned to a fixed zone and clock; every name is invented. import CottonValues import XCTest @testable import CottonUI final class EditorsTests: XCTestCase { private let ist: Calendar = { var calendar = Calendar(identifier: .gregorian) calendar.timeZone = TimeZone(identifier: "Asia/Kolkata")! calendar.firstWeekday = 2 return calendar }() private let en = Locale(identifier: "en_US") private func at(_ iso: String) -> Date { ISO8601DateFormatter().date(from: iso)! } private func sampler() throws -> TableDefinition { EditorsSampler.definition } private func samplerRecords() throws -> [TableRecord] { EditorsSampler.records } // RULE: An editor never writes a record: nothing under CottonUI/Editors calls create, update, delete, reorder or link on the Records; only the change receipt calls undo, because the surface owns the write. func testEditorsNeverSave() throws { let folder = SourceSweep.iosSources.appendingPathComponent("CottonUI/Editors") let files = try FileManager.default.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil) .filter { $0.pathExtension == "swift" } XCTAssertFalse(files.isEmpty) let writes = try NSRegularExpression(pattern: #"\.(create|update|delete|reorder|link)\(table:"#) let undo = try NSRegularExpression(pattern: #"\.undo\(change:"#) for file in files { let source = SourceSweep.withoutComments(try String(contentsOf: file, encoding: .utf8)) let range = NSRange(source.startIndex..., in: source) XCTAssertNil(writes.firstMatch(in: source, range: range), "\(file.lastPathComponent) writes a record") if file.lastPathComponent != "ChangeReceipt.swift" { XCTAssertNil(undo.firstMatch(in: source, range: range), "\(file.lastPathComponent) undoes") } } } // ── The receipt ── func testUndoingTheReceiptsChangePutsTheValueBack() async throws { let records = FixtureRecords(definitions: [try sampler()], records: ["sampler": try samplerRecords()]) let change = try await records.update(table: "sampler", id: EditorsSampler.recordId, patch: ["count": .number(7)]) _ = try await records.undo(change: change.id) let back = try await records.get(table: "sampler", id: EditorsSampler.recordId) XCTAssertEqual(back?["count"], .number(2)) } }