Calendarr already publishes an event snapshot into its App Group container for the widgets to render. That channel is what a second Mac app needs too, so this turns an internal detail into a documented contract rather than something a second app reverse-engineers and then drifts from. Foundation only — no SwiftUI, no UIKit, no AppKit — so one source compiles for iOS, Mac Catalyst and native macOS. Building against the native macOS SDK is the point: it fails loudly on a UIKit leak that a Catalyst build would accept. Three things earn their place here rather than being copied per app: CalendarrAppGroup resolves the container identifier per platform. macOS requires the Team ID prefix and iOS forbids it, and a mismatch fails silently — nil container, every read and write a no-op, widgets blank forever. CalendarKey normalises the two ways the server reports calendar_id (bare for CalDAV, "local-3" style for everything else). Normalising them differently is what made per-calendar filtering quietly do nothing for some sources. The snapshot schema is versioned, with the coverage window now explicit. That window is the easiest thing to get wrong downstream: outside it the snapshot carries no information, which is not the same as no events, and rendering an empty week for an uncovered date produces a screen that looks right and is false. covers() exists so consumers can tell the two apart. Reads return a named result rather than an optional. "Never ran", "signed out", "too new to read" and "unreadable" need different messages, and a bare nil is how the App Group misconfiguration used to hide. Writing is behind @_spi(Writer) so a read-only consumer cannot corrupt the shared cache. Writes post a Darwin notification, the one cross-process signal that works between two sandboxed apps without an extra entitlement. The six colour values stay flat on the wire instead of nesting under `theme`, which would read better. A v1 reader requires them flat, so nesting would have broken every older build on the first v2 write; `theme` is a computed view. Tests pin both directions of compatibility, since the two apps ship independently. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
173 lines
6.9 KiB
Swift
173 lines
6.9 KiB
Swift
import Foundation
|
|
import Testing
|
|
@testable import CalendarrCore
|
|
|
|
/// The wire format is a contract between apps that ship independently, so both
|
|
/// directions of compatibility need a test: a new reader must understand an old
|
|
/// file, and an old reader must survive a new one.
|
|
struct SnapshotSchemaTests {
|
|
|
|
private static func decoder() -> JSONDecoder {
|
|
let d = JSONDecoder(); d.dateDecodingStrategy = .iso8601; return d
|
|
}
|
|
private static func encoder() -> JSONEncoder {
|
|
let e = JSONEncoder(); e.dateEncodingStrategy = .iso8601; return e
|
|
}
|
|
|
|
/// A real v1 file: no schemaVersion, no coverage window, no isLoggedIn,
|
|
/// colours flat at the top level.
|
|
private static let v1Fixture = """
|
|
{
|
|
"writtenAt": "2026-08-10T12:00:00Z",
|
|
"events": [
|
|
{
|
|
"id": "abc",
|
|
"title": "Zahnarzt",
|
|
"start": "2026-08-11T09:00:00Z",
|
|
"end": "2026-08-11T10:00:00Z",
|
|
"isAllDay": false,
|
|
"colorHex": "#58B900",
|
|
"location": "Bern",
|
|
"calendarKey": "local:3"
|
|
}
|
|
],
|
|
"todayColorHex": "#6FB669",
|
|
"textColorHex": "#FFFFFF",
|
|
"backgroundColorHex": "#000000",
|
|
"lineColorHex": "#3B3B3D",
|
|
"primaryColorHex": "#58B900",
|
|
"accentColorHex": "#45A148",
|
|
"language": "de"
|
|
}
|
|
"""
|
|
|
|
@Test("A v1 file decodes, and its missing fields get sane defaults")
|
|
func decodesV1() throws {
|
|
let snap = try Self.decoder().decode(
|
|
CalendarrSnapshot.self, from: Data(Self.v1Fixture.utf8))
|
|
|
|
#expect(snap.schemaVersion == 1)
|
|
#expect(snap.events.count == 1)
|
|
#expect(snap.events[0].calendarKey == "local:3")
|
|
#expect(snap.language == "de")
|
|
#expect(snap.theme.today == "#6FB669")
|
|
#expect(snap.theme.accent == "#45A148")
|
|
|
|
// v1 never said whether anyone was signed in; assuming yes preserves the
|
|
// old behaviour of simply rendering whatever was cached.
|
|
#expect(snap.isLoggedIn)
|
|
#expect(snap.writerVersion == "unknown")
|
|
|
|
// The window is reconstructed from the policy v1 always used.
|
|
let cal = Calendar(identifier: .gregorian)
|
|
let dayStart = cal.startOfDay(for: snap.writtenAt)
|
|
#expect(snap.coverageStart == cal.date(byAdding: .day, value: -7, to: dayStart))
|
|
#expect(snap.coverageEnd == cal.date(byAdding: .day, value: 42, to: dayStart))
|
|
}
|
|
|
|
/// Older still: written before per-calendar filtering existed, so the events
|
|
/// carry no calendarKey at all.
|
|
private static let v1FixtureNoCalendarKey = """
|
|
{
|
|
"writtenAt": "2026-08-10T12:00:00Z",
|
|
"events": [
|
|
{
|
|
"id": "abc",
|
|
"title": "Zahnarzt",
|
|
"start": "2026-08-11T09:00:00Z",
|
|
"end": "2026-08-11T10:00:00Z",
|
|
"isAllDay": false,
|
|
"colorHex": "#58B900",
|
|
"location": "Bern"
|
|
}
|
|
],
|
|
"todayColorHex": "#6FB669",
|
|
"textColorHex": "#FFFFFF",
|
|
"backgroundColorHex": "#000000",
|
|
"lineColorHex": "#3B3B3D",
|
|
"language": "de"
|
|
}
|
|
"""
|
|
|
|
@Test("A v1 file that predates calendarKey and the newer colours still decodes")
|
|
func decodesV1WithoutCalendarKey() throws {
|
|
let snap = try Self.decoder().decode(
|
|
CalendarrSnapshot.self, from: Data(Self.v1FixtureNoCalendarKey.utf8))
|
|
#expect(snap.events[0].calendarKey == "")
|
|
// primary/accent were added later too, so they fall back to defaults.
|
|
#expect(snap.theme.primary == "#4285f4")
|
|
#expect(snap.theme.accent == "#ea4335")
|
|
}
|
|
|
|
@Test("v2 round-trips without losing anything")
|
|
func roundTripsV2() throws {
|
|
let start = Date(timeIntervalSince1970: 1_780_000_000)
|
|
let original = CalendarrSnapshot(
|
|
writtenAt: start,
|
|
coverageStart: start.addingTimeInterval(-7 * 86_400),
|
|
coverageEnd: start.addingTimeInterval(42 * 86_400),
|
|
isLoggedIn: true,
|
|
writerVersion: "3.5",
|
|
events: [SnapshotEvent(id: "x", title: "Termin", start: start,
|
|
end: start.addingTimeInterval(3600),
|
|
isAllDay: false, colorHex: "#58B900",
|
|
location: "", calendarKey: "local:1")],
|
|
theme: SnapshotTheme(today: "#1", text: "#2", background: "#3",
|
|
line: "#4", primary: "#5", accent: "#6"),
|
|
language: "ch")
|
|
|
|
let decoded = try Self.decoder().decode(
|
|
CalendarrSnapshot.self, from: Self.encoder().encode(original))
|
|
|
|
#expect(decoded.schemaVersion == CalendarrSnapshot.currentSchema)
|
|
#expect(decoded.writerVersion == "3.5")
|
|
#expect(decoded.language == "ch")
|
|
#expect(decoded.coverageStart == original.coverageStart)
|
|
#expect(decoded.coverageEnd == original.coverageEnd)
|
|
#expect(decoded.theme == original.theme)
|
|
#expect(decoded.events == original.events)
|
|
}
|
|
|
|
/// The reason the colours are stored flat rather than nested under `theme`:
|
|
/// a v1 reader requires them at the top level, so nesting them would have
|
|
/// broken every older build on the first v2 write.
|
|
@Test("v2 keeps the colour keys where a v1 reader expects them")
|
|
func v2StaysReadableByV1() throws {
|
|
let start = Date(timeIntervalSince1970: 1_780_000_000)
|
|
let snap = CalendarrSnapshot(
|
|
writtenAt: start, coverageStart: start, coverageEnd: start,
|
|
isLoggedIn: true, writerVersion: "3.5", events: [],
|
|
theme: SnapshotTheme(today: "#a", text: "#b", background: "#c",
|
|
line: "#d", primary: "#e", accent: "#f"),
|
|
language: "de")
|
|
|
|
let object = try JSONSerialization.jsonObject(
|
|
with: Self.encoder().encode(snap)) as? [String: Any]
|
|
|
|
for key in ["todayColorHex", "textColorHex", "backgroundColorHex",
|
|
"lineColorHex", "primaryColorHex", "accentColorHex",
|
|
"writtenAt", "events", "language"] {
|
|
#expect(object?[key] != nil, "v1 readers require \(key) at the top level")
|
|
}
|
|
#expect(object?["theme"] == nil, "theme must stay a computed view, not a stored key")
|
|
}
|
|
|
|
@Test("covers() separates 'no events' from 'no information'")
|
|
func coverageWindow() {
|
|
let now = Date(timeIntervalSince1970: 1_780_000_000)
|
|
let snap = CalendarrSnapshot(
|
|
writtenAt: now,
|
|
coverageStart: now.addingTimeInterval(-86_400),
|
|
coverageEnd: now.addingTimeInterval(86_400),
|
|
isLoggedIn: true, writerVersion: "3.5", events: [],
|
|
theme: SnapshotTheme(today: "", text: "", background: "",
|
|
line: "", primary: "", accent: ""),
|
|
language: "de")
|
|
|
|
#expect(snap.covers(now))
|
|
#expect(snap.covers(now.addingTimeInterval(-86_399)))
|
|
#expect(!snap.covers(now.addingTimeInterval(-90_000)))
|
|
#expect(!snap.covers(snap.coverageEnd)) // half-open: end is excluded
|
|
}
|
|
}
|