// Sync is said in the engine's words: under a cap, at the top of a database's // page, and nowhere in words made here. import CottonKit import XCTest @testable import CottonUI final class DatabaseSyncLineTests: XCTestCase { private func standing(_ json: String) throws -> DatabaseSyncLine { try JSONDecoder().decode(DatabaseSyncLine.self, from: Data(json.utf8)) } private let syncing = #"{"state":"syncing","words":"syncing · 62%","page":"Syncing · 62% · 41,000 changes to go","percent":62,"toGo":41000,"bytesToGo":null,"since":null}"# private let copying = #"{"state":"copying","words":"copying · 400 MB to go","page":"Copying your database · 400 MB to go","percent":20,"toGo":null,"bytesToGo":400000000,"since":null}"# private let offline = #"{"state":"offline","words":"offline","page":"","percent":null,"toGo":null,"bytesToGo":null,"since":1791782160000}"# private let updateApp = #"{"state":"updateApp","words":"update app","page":"Update the app to sync again","percent":null,"toGo":null,"bytesToGo":null,"since":null}"# private let moving = #"{"state":"moving","words":"","page":"","percent":null,"toGo":null,"bytesToGo":null,"since":null}"# /// RULE: The line under a database's cap is the engine's own string and nothing made here — "syncing · N%", "copying · N MB to go", "offline" or "update app" — and with none of them the people line stands; a Mac and a phone say the same words (sync programme, lane A, 12 Oct 2026). func testTheCapSaysTheEnginesWordsOrThePeopleLine() throws { for json in [syncing, copying, offline, updateApp] { let line = try standing(json) XCTAssertEqual(line.capLine, line.words) XCTAssertFalse(line.words.isEmpty) } XCTAssertNil(try standing(moving).capLine, "fewer than 2,000 changes move without a word") XCTAssertNil(DatabaseSyncLine.current.capLine) } /// RULE: The cap turns while rows or bytes move — a few changes, a catch-up, a file copy — and stands still when nothing can: level, offline, and waiting for an update, which alone is drawn in red. func testTheCapTurnsOnlyWhileSomethingMoves() throws { for json in [moving, syncing, copying] { XCTAssertTrue(try standing(json).turns, json) } for json in [offline, updateApp] { XCTAssertFalse(try standing(json).turns, json) } XCTAssertFalse(DatabaseSyncLine.current.turns) XCTAssertTrue(try standing(updateApp).refuses) XCTAssertFalse(try standing(offline).refuses) } /// RULE: A device AHEAD of its serving Mac (the Mac's file on an older schema) carries the engine's own cap words, in red and still; its page line names the Mac only when this device knows it, says another person's Mac apart from ours, and never repeats the Mac's own "schemaVersion must be N" (phone 254, 12 Oct 2026). func testADeviceAheadOfItsMacSaysWhichMacToUpdate() throws { let ahead = try standing(#"{"state":"macBehind","words":"update your Mac","page":"","percent":null,"toGo":null,"bytesToGo":null,"since":null}"#) XCTAssertEqual(ahead.capLine, ahead.words) XCTAssertTrue(ahead.refuses) XCTAssertFalse(ahead.turns) let named = try XCTUnwrap(ahead.pageLine(mac: "mac-c", thisDevice: "This phone")) let unnamed = try XCTUnwrap(ahead.pageLine(mac: nil, thisDevice: "This phone")) let theirs = try XCTUnwrap(ahead.pageLine(mac: nil, theirs: true, thisDevice: "This phone")) XCTAssertTrue(named.contains("mac-c"), "the Mac to update is named when known") XCTAssertFalse(unnamed.contains("mac-c")) XCTAssertTrue(named.contains("This phone") && unnamed.contains("This phone"), "the device says which device is ahead") XCTAssertNotEqual(theirs, unnamed, "another person's Mac is said apart from ours") for line in [named, unnamed, theirs] { XCTAssertFalse(line.contains("schemaVersion"), line) } } /// RULE: The top of a database's page says sync in ONE slot, in the engine's own sentence, which is empty when the database is current; only the offline line is composed here, because the time of day (on the reader's clock, with the date once it is not today) and the Mac's name are the app's to know; a database joined at another person's Mac (read.databases' `theirs`) never carries a name this device does not have. func testThePageSaysTheEnginesSentenceAndIsEmptyWhenCurrent() throws { let kolkata = try XCTUnwrap(TimeZone(identifier: "Asia/Kolkata")) // 12 Oct 2026 10:46 IST, asked at 11:05 the same morning. let now = Date(timeIntervalSince1970: 1_791_783_300) for json in [syncing, copying, updateApp] { let line = try standing(json) XCTAssertEqual(line.pageLine(mac: "mac-c"), line.page, json) } XCTAssertNil(DatabaseSyncLine.current.pageLine(mac: "mac-c"), "current: the slot is empty") XCTAssertNil(try standing(moving).pageLine(mac: "mac-c")) let silent = try standing(offline) let today = try XCTUnwrap(silent.pageLine(mac: "mac-c", now: now, timeZone: kolkata)) XCTAssertTrue(today.contains("10:46"), "the reader's clock, not UTC's 05:16") XCTAssertTrue(today.contains("mac-c")) let noMac = try XCTUnwrap(silent.pageLine(mac: nil, now: now, timeZone: kolkata)) XCTAssertTrue(noMac.contains("10:46")) XCTAssertFalse(noMac.contains("mac-c")) // Another person's database: the Mac that serves it has no name // here, and none is borrowed from the household's. let theirs = try XCTUnwrap(silent.pageLine(mac: nil, theirs: true, now: now, timeZone: kolkata)) XCTAssertNotEqual(theirs, noMac) XCTAssertEqual( silent.pageLine(mac: "mac-c", theirs: true, now: now, timeZone: kolkata), theirs, "never a name we do not have") let listed = try JSONDecoder().decode( [HeldDatabase].self, from: Data(#"[{"databaseId":"shed","name":"Shed","kind":"shared","theirs":true},{"databaseId":"garden","name":"Garden","kind":"shared"}]"#.utf8)) XCTAssertEqual(listed.map(\.theirs), [true, false], "only the list's own word marks a database as theirs") // A day on, a bare "10:46" would read as this morning: the date joins it. let dayOn = try XCTUnwrap(silent.pageLine(mac: "mac-c", now: now + 86_400, timeZone: kolkata)) XCTAssertTrue(dayOn.contains("10:46")) XCTAssertNotEqual(dayOn, today) var never = silent never.since = nil let neverLine = try XCTUnwrap(never.pageLine(mac: "mac-c")) XCTAssertTrue(neverLine.contains("mac-c")) XCTAssertFalse(neverLine.contains("10:46"), "no time when there never was one") } /// RULE: The thin bar under the page's sync line draws a catch-up's or a file copy's share done, and nothing in any other state — it is the only bar while the app is usable. func testTheBarDrawsOnlyAShareDone() throws { XCTAssertEqual(try standing(syncing).fraction, 0.62) XCTAssertEqual(try standing(copying).fraction, 0.2) for json in [offline, updateApp, moving] { XCTAssertNil(try standing(json).fraction, json) } XCTAssertNil(DatabaseSyncLine.current.fraction) } /// RULE: A pass's frame and the status read carry the standing whole (`sync`, and `syncs` by database), so the sheet and the page say where sync stands from launch, not from the next pass that ends; a core that says none changes nothing. func testTheStandingIsReadOffTheFrameAndTheStatusRead() throws { let frame = try JSONDecoder().decode(JSONValue.self, from: Data(syncing.utf8)) let line = try XCTUnwrap(DatabaseSyncLine(frame)) XCTAssertEqual(line, try standing(syncing)) XCTAssertNil(DatabaseSyncLine(nil)) XCTAssertNil(DatabaseSyncLine(.object(["words": .string("syncing")])), "no state is no standing") let status = try JSONDecoder().decode( SyncStatusRead.self, from: Data(#"{"connected":true,"workspaceId":"db-own","pending":0,"canReachServer":true,"sync":\#(offline),"syncs":{"db-own":\#(offline),"db-garden":\#(syncing)}}"#.utf8)) XCTAssertEqual(status.sync?.state, .offline) XCTAssertEqual(status.sync?.since, Date(timeIntervalSince1970: 1_791_782_160)) XCTAssertEqual(status.syncs["db-garden"]?.capLine, try standing(syncing).words) let bare = try JSONDecoder().decode(SyncStatusRead.self, from: Data(#"{"connected":false}"#.utf8)) XCTAssertNil(bare.sync) XCTAssertEqual(bare.syncs, [:]) } }