134 lines · 6.1 KB · SwiftRaw
import CottonValuesimport Foundationimport XCTest@testable import CottonUI@MainActorfinal class ClassroomTests: XCTestCase { private let ids = ["lesson", "class", "student", "grade"] // SwiftPM's macOS test host can place the resource bundle outside Bundle.main's search. private var fixtureRoot: URL { if let root = CottonUIResources.bundle?.resourceURL { return root } return SourceSweep.iosSources.appendingPathComponent("CottonUI/Resources") } // Read the shipped JSON itself; no Classroom model or drawing code exists. private func fixture(_ directory: String, _ id: String) throws -> Data { let root = fixtureRoot return try Data(contentsOf: root.appendingPathComponent("Fixtures/classroom/\(directory)/\(id).json")) } private func definitions() throws -> [TableDefinition] { try ids.map { try TableDefinition.decode(fixture("tables", $0)) } } private func rows() throws -> [String: [TableRecord]] { try Dictionary( uniqueKeysWithValues: ids.map { ($0, try JSONDecoder().decode([TableRecord].self, from: fixture("records", $0))) }) } /// RULE: Classroom fixtures give every table invented records with valid link targets and choices so the interpreter never needs a Classroom-specific adapter. func testFixturesAgreeWithDefinitions() throws { let tables = try rows() for definition in try definitions() { let records = try XCTUnwrap(tables[definition.id]) XCTAssertFalse(records.isEmpty) XCTAssertEqual(Set(records.map(\.id)).count, records.count, "ids are unique") for record in records { XCTAssertEqual(Set(record.values.keys), Set(definition.columns.map(\.key))) for column in definition.columns { if column.required { XCTAssertNotNil(record[column.key].text) } if column.type == .link { let target = try XCTUnwrap(tables[try XCTUnwrap(column.linkTo)]) XCTAssertTrue(target.contains { $0.id == record[column.key].text }) } if column.type == .choice { XCTAssertTrue(column.choices?.contains(record[column.key].text ?? "") == true) } } } } } /// RULE: Every additional list page resolves its links with one batch and preserves linked titles from earlier pages without per-row reads. func testLinkBatchesFollowPagination() async throws { let definitions = try definitions() let lesson = try XCTUnwrap(definitions.first { $0.id == "lesson" }) var rows = try rows() let sample = try XCTUnwrap(rows["lesson"]?.first) rows["lesson"] = (0..<62).map { index in var row = sample row.id = "paged-lesson-\(index)" return row } let store = ClassroomBatchRecords(FixtureRecords(definitions: definitions, records: rows)) let list = RecordListModel(definition: lesson, store: store, core: ListCoreFake(), seeds: ListSeeds()) await list.reload(project: nil, query: "") XCTAssertEqual(list.records.count, 50) XCTAssertTrue(list.hasMore) let firstTitles = list.linkTitles.byID await list.loadMore() XCTAssertEqual(list.records.count, 62) XCTAssertFalse(list.hasMore) let batches = await store.batchReads let gets = await store.singleReads XCTAssertEqual(batches, 2) XCTAssertEqual(gets, 0) for (id, title) in firstTitles { XCTAssertEqual(list.linkTitles.byID[id], title) } } /// RULE: Missing link targets have no invented title while other targets in the same batch still resolve. func testMissingLinkDoesNotHideOtherTitles() async throws { let definitions = try definitions() let store = FixtureRecords(definitions: definitions, records: try rows()) let titles = try await store.linkTitles(for: [ RecordLink(table: "class", id: "class-01"), RecordLink(table: "class", id: "removed-class"), ]) XCTAssertNotNil(titles.byID["class-01"]) XCTAssertNil(titles.byID["removed-class"]) }}private actor ClassroomBatchRecords: Records { private let inner: FixtureRecords private(set) var batchReads = 0 private(set) var singleReads = 0 init(_ inner: FixtureRecords) { self.inner = inner } func linkTitles(for links: [RecordLink]) async throws -> LinkTitles { batchReads += 1 return try await inner.linkTitles(for: links) } func tableDefinitions() async throws -> [TableDefinition] { try await inner.tableDefinitions() } func count(table: String) async throws -> Int { try await inner.count(table: table) } func list( table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue] ) async throws -> RecordPage { return try await inner.list(table: table, project: project, query: query, page: page, where: filter) } func get(table: String, id: String) async throws -> TableRecord? { singleReads += 1 return try await inner.get(table: table, id: id) } func create(table: String, data: [String: RecordValue]) async throws -> ChangeRecord { try await inner.create(table: table, data: data) } func update(table: String, id: String, patch: [String: RecordValue]) async throws -> ChangeRecord { try await inner.update(table: table, id: id, patch: patch) } func delete(table: String, id: String) async throws -> ChangeRecord { try await inner.delete(table: table, id: id) } func reorder(table: String, ids: [String]) async throws -> ChangeRecord { try await inner.reorder(table: table, ids: ids) } func link(table: String, id: String, column: String, target: String) async throws -> ChangeRecord { try await inner.link(table: table, id: id, column: column, target: target) } func undo(change: String) async throws -> ChangeRecord { try await inner.undo(change: change) }}