Cotton code
51 lines · 3.9 KB · SwiftRaw
import XCTest@testable import CottonUIfinal class ChartFaceTests: XCTestCase {    /// RULE: An hourly chart refreshes at its own civil-hour boundary and distinguishes repeated daylight-saving hours; UTC boundaries must not delay an IST chart by half an hour.    func testHourlyRefreshUsesTheChartsTimezone() throws {        let parser = ISO8601DateFormatter()        func key(_ at: String, _ zone: String?) throws -> String {            ChartFace.periodKey(try XCTUnwrap(parser.date(from: at)), chart: TileChart(kind: .bar, x: "at", y: "count", by: .hour, timezone: zone))        }        XCTAssertNotEqual(try key("2026-10-14T10:29:59Z", "Asia/Kolkata"), try key("2026-10-14T10:30:00Z", "Asia/Kolkata"))        XCTAssertEqual(try key("2026-10-14T10:59:59Z", "Asia/Kolkata"), try key("2026-10-14T11:00:00Z", "Asia/Kolkata"))        XCTAssertNotEqual(try key("2026-10-14T18:29:59Z", "Asia/Kolkata"), try key("2026-10-14T18:30:00Z", "Asia/Kolkata"))        XCTAssertNotEqual(try key("2026-11-01T05:30:00Z", "America/New_York"), try key("2026-11-01T06:30:00Z", "America/New_York"))        XCTAssertNotEqual(try key("2026-10-14T10:59:59Z", nil), try key("2026-10-14T11:00:00Z", nil))    }    /// RULE: An hourly chart keeps its civil-hour column and timezone through Swift decoding, so the app draws the core's hourly buckets without changing their meaning.    func testAnHourlyChartKeepsItsClockAndDecodesItsSeries() throws {        let chart = try JSONDecoder().decode(TileChart.self, from: Data(#"{"kind":"bar","x":"hour_at","y":"landings","by":"hour","hour":"hour","timezone":"Asia/Kolkata","periods":48}"#.utf8))        XCTAssertEqual(chart.by, .hour)        XCTAssertEqual(chart.hour, "hour")        XCTAssertEqual(chart.timezone, "Asia/Kolkata")        XCTAssertEqual(try JSONDecoder().decode(TileChart.self, from: JSONEncoder().encode(chart)), chart)        let series = try JSONDecoder().decode(ChartSeries.self, from: Data(#"{"kind":"bar","by":"hour","x":"hour_at","y":"landings","buckets":[{"start":"2026-10-14T00:00:00+05:30","value":3}],"total":3}"#.utf8))        XCTAssertEqual(series.by, .hour)        XCTAssertEqual(series.current?.start, "2026-10-14T00:00:00+05:30")    }    /// RULE: A chart face declared in any table's definition — a kit tool's table here, no code of its own — decodes and round-trips, and is the tool as a whole; the face only places the core's buckets: bars standing on the bottom edge, the largest full height, an empty period with none, oldest to newest left to right.    func testAChartFaceOnAKitTableDrawsTheCoresSeries() throws {        let tile = try JSONDecoder().decode(TileLayout.self, from: Data(#"""        {"face":"chart","chart":{"kind":"bar","x":"delivery_on","y":"count","by":"week","where":{"status":["new","delivered"]}}}        """#.utf8))        XCTAssertEqual(tile.face, .chart)        XCTAssertEqual(tile.chart?.by, .week)        XCTAssertEqual(tile.chart?.filter?["status"], .list([.text("new"), .text("delivered")]))        XCTAssertEqual(try JSONDecoder().decode(TileLayout.self, from: JSONEncoder().encode(tile)), tile)        XCTAssertFalse(BoardTile.opensRecords(.chart), "the chart is the tool as a whole")        let series = try JSONDecoder().decode(ChartSeries.self, from: Data(#"""        {"kind":"bar","by":"week","x":"delivery_on","y":"count","buckets":[{"start":"2026-09-28","value":2},{"start":"2026-10-05","value":0},{"start":"2026-10-12","value":4}],"total":6}        """#.utf8))        let bars = series.bars(in: CGSize(width: 90, height: 40), gap: 0)        XCTAssertEqual(bars.map(\.minX), [0, 30, 60])        XCTAssertEqual(bars.map(\.height), [20, 0, 40])        XCTAssertEqual(bars.map(\.maxY), [40, 40, 40], "every bar stands on the bottom edge")        XCTAssertEqual(series.current?.value, 4, "today's period is the window's last")        XCTAssertEqual(series.plotted(in: CGSize(width: 90, height: 40)).map(\.y), [20, 40, 0])    }}