import CottonKit import Foundation import XCTest @testable import CottonUI #if os(iOS) /// Google Calendar in Days (CONTRACT §13): the core's answer read as the contract shapes it; Connect shows its notice /// first and stands only while nothing is connected; Disconnect says what left and asks Google again. @MainActor final class DaysGoogleTests: XCTestCase { private static let connected = """ {"line":"Bring in your Google calendars.","accounts":[{"email":"walk@example.test","says":"Last brought in today at 10:05 · 312 events","trouble":false, "disconnect":{"id":"google_disconnect","tool":"calendar","account":"walk@example.test","label":"Disconnect", "asks":"Disconnect walk@example.test? Days stops bringing in its events.","yes":"Disconnect"}}], "connect":null,"unavailable":null} """ private static let none = """ {"line":"Bring in your Google calendars.","accounts":[], "connect":{"id":"google_connect","tool":"calendar","label":"Connect Google Calendar", "notice":{"title":"Before Google asks","body":"Days reads your primary calendar's events.","go":"Continue to Google","notNow":"Not now"}}, "unavailable":null} """ private func model(_ core: GoogleCore) -> DaysModel { DaysModel(book: CoreDaysBook(core: core, clock: .fixed("2026-11-22", zone: TimeZone(identifier: "Asia/Kolkata")!)), core: core, clock: .fixed("2026-11-22", zone: TimeZone(identifier: "Asia/Kolkata")!)) } // RULE: a connected account is read with its address, how its events are coming and its Disconnect; Connect is // not drawn then, and the first day offers only Add. func testAConnectedAccountHidesConnect() async { let core = GoogleCore(["days.google": Self.connected]) let m = model(core) await m.readGoogle() XCTAssertEqual(m.google?.accounts.first?.email, "walk@example.test") XCTAssertEqual(m.google?.accounts.first?.disconnect?.account, "walk@example.test") XCTAssertNil(m.google?.connect) XCTAssertEqual(DaysGoogle.firstDayLine(connect: m.google?.connect), "empty.line_alone") let asked = await core.sent.first { $0.method == "days.google" } XCTAssertEqual(asked?.body["timezone"] as? String, "Asia/Kolkata", "asked in the phone's zone") } // RULE: Connect opens its notice before anything reaches Google; its act is the Calendar tool's own. func testConnectShowsTheNoticeFirst() async { let core = GoogleCore(["days.google": Self.none]) let m = model(core) await m.readGoogle() XCTAssertEqual(DaysGoogle.firstDayLine(connect: m.google?.connect), "empty.line") m.askToConnect() XCTAssertEqual(m.noticing?.notice.go, "Continue to Google") XCTAssertEqual(m.noticing?.notice.notNow, "Not now") XCTAssertEqual(m.google?.connectAct?.id, "google_connect") XCTAssertEqual(m.google?.connectAct?.tool, "calendar") let methods = await core.sent.map(\.method) XCTAssertFalse(methods.contains("tools.act"), "nothing is asked of Google before Continue") } // RULE: Disconnect names its account, says what left at Days' foot, and asks Google again. func testDisconnectSaysWhatLeftAndAsksAgain() async { let core = GoogleCore(["days.google": Self.none, "days.google_disconnect": #"{"account":"walk@example.test","removed":312,"said":"walk@example.test is disconnected. Its 312 events left Days."}"#]) let m = model(core) await m.disconnectGoogle("walk@example.test") let sent = await core.sent XCTAssertEqual(sent.first { $0.method == "days.google_disconnect" }?.body["account"] as? String, "walk@example.test") XCTAssertEqual(m.said, "walk@example.test is disconnected. Its 312 events left Days.") XCTAssertEqual(sent.last?.method, "days.google", "Google is asked again after Disconnect") XCTAssertNotNil(m.google?.connect) } // RULE: a phone not signed in reads why nothing can be connected, in place of Connect. func testSignedOutSaysWhyInPlaceOfConnect() async { let core = GoogleCore(["days.google": #"{"line":"x","accounts":[],"connect":null,"unavailable":"Sign in first, then connect Google Calendar."}"#]) let m = model(core) await m.readGoogle() XCTAssertNil(m.google?.connect) XCTAssertEqual(m.google?.unavailable, "Sign in first, then connect Google Calendar.") } // RULE: Google's page opens in the sign-in sheet, which catches the Worker's https callback the consent page // names (redirect_uri), and Days reads the Worker's page after asking it: connected by its title, else its line. func testTheSheetCatchesTheCallbackTheConsentPageNames() { let page = URL(string: "https://accounts.google.com/o/oauth2/v2/auth?client_id=x&redirect_uri=https%3A%2F%2Fdev.example.invalid%2Fv1%2Fgoogle%2Fcallback&state=db.abc")! let back = DaysGoogleSignIn.callback(of: page) XCTAssertEqual(back?.host, "dev.example.invalid") XCTAssertEqual(back?.path, "/v1/google/callback") XCTAssertNil(DaysGoogleSignIn.callback(of: URL(string: "https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=http%3A%2F%2F127.0.0.1%3A8080%2F")!), "no https callback, no sheet: Safari as before") XCTAssertNil(DaysGoogleSignIn.callback(of: URL(string: "https://accounts.google.com/o/oauth2/v2/auth?client_id=x")!)) } func testTheWorkersPageSaysWhetherItConnected() { let ok = "
Days now brings in the events of walk@example.test. You can close this page and go back to Days.
" XCTAssertEqual(DaysGoogleSignIn.read(ok), .connected) let unticked = "Calendar access was not ticked on Google's page. Start again from Days and leave it ticked.
" XCTAssertEqual(DaysGoogleSignIn.read(unticked), .refused("Calendar access was not ticked on Google's page. Start again from Days and leave it ticked.")) XCTAssertEqual(DaysGoogleSignIn.read(""), .closed) } } /// A core answering the Google methods (and every other with an empty object), saying what it was asked. private actor GoogleCore: CottonCalling { struct Sent: @unchecked Sendable { let method: String let body: [String: Any] } private let answers: [String: String] private(set) var sent: [Sent] = [] init(_ answers: [String: String]) { self.answers = answers } func callRaw(_ method: String, requestJSON: String) async throws -> String { sent.append(Sent(method: method, body: (try? JSONSerialization.jsonObject(with: Data(requestJSON.utf8)) as? [String: Any]) ?? [:])) if method == "days.span" { return #"{"ok":true,"data":{"today":"2026-11-22","days":[]}}"# } return #"{"ok":true,"data":\#(answers[method] ?? "{}")}"# } func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) async {} func close() async {} } #endif