import Foundation import Testing @testable import OwnerMetricsKit private let session = String(repeating: "A", count: 43) private let payload = Data(""" { "since": "2026-11-23", "through": "2026-11-29", "rows": [ {"kind":"website","id":"projectatom","day":"2026-11-25","count":8}, {"kind":"website","id":"protomota","day":"2026-11-25","count":2}, {"kind":"website","id":"projectatom","day":"2026-11-29","count":3} ] } """.utf8) @Test func authenticatedReadUsesFixedNoOriginEndpointAndComputesUTCWindow() async throws { let client = MetricsClient { r in #expect(r.url?.absoluteString == "https://projectatom.ai/api/visits?private=graphley&days=7") #expect(r.httpMethod == "GET") #expect(r.value(forHTTPHeaderField: "Authorization") == "Bearer " + session) #expect(r.value(forHTTPHeaderField: "Origin") == nil) #expect(r.value(forHTTPHeaderField: "Cookie") == nil) #expect(r.httpBody == nil) return .init(code: 200, contentType: "application/json; charset=utf-8", data: payload) } let result = try await client.read(session: session, window: .week) #expect(result.total(.website) == 13) #expect(result.daily(.website).map(\.count) == [0,0,10,0,0,0,3]) #expect(result.properties(.website).map(\.title) == ["Project ATOM", "Protomota"]) #expect(!result.hasData(.app)) #expect(result.daily(.app).allSatisfy { $0.count == 0 }) } @Test func rejectsAbsentOrMalformedSessionBeforeAnyNetwork() async { let client = MetricsClient { _ in Issue.record("Invalid sessions must not be sent") return .init(code: 500, contentType: nil, data: Data()) } let oldViewerToken = "test-graphley-read-token-32-characters" for invalid in ["", "short", session + "A", session.dropLast() + "=", session.dropLast() + "\n", " " + session.dropLast(), oldViewerToken] { await #expect(throws: MetricsError.unauthorizedOrDisabled) { try await client.read(session: String(invalid), window: .week) } } } @Test func mapsDisabledOrUnauthorizedWithoutExposingResponseData() async { for code in [401,403,404] { let client = MetricsClient { _ in .init(code: code, contentType: "application/json", data: payload) } await #expect(throws: MetricsError.unauthorizedOrDisabled) { try await client.read(session: session, window: .week) } } } @Test func rejectsWrongContentTypeRedirectOrOversizedData() async { let unsupported = MetricsClient { _ in .init(code: 200, contentType: "text/html", data: payload) } await #expect(throws: MetricsError.invalidResponse) { try await unsupported.read(session: session, window: .week) } let redirected = MetricsClient { _ in .init(code: 302, contentType: nil, data: Data()) } await #expect(throws: MetricsError.unavailable) { try await redirected.read(session: session, window: .week) } let oversized = MetricsClient { _ in .init(code: 200, contentType: "application/json", data: Data(count: MetricsClient.limit + 1)) } await #expect(throws: MetricsError.invalidResponse) { try await oversized.read(session: session, window: .week) } } @Test func rejectsInvalidDayNegativeCountsAndDuplicateRows() throws { let format = { (rows: String) in Data(""" {"since":"2026-11-23","through":"2026-11-29","rows":[\(rows)]} """.utf8) } let invalid: [String] = [ #"{ "kind":"app","id":"graphley","day":"2026-11-29","count":-3 }"#, #"{ "kind":"app","id":"graphley","day":"2026-11-30","count":3 }"#, #"{ "kind":"app","id":"https://projectatom.ai/path","day":"2026-11-29","count":3 }"#, #"{ "kind":"app","id":"graphley","day":"2026-11-29","count":3 }, {"kind":"app","id":"graphley","day":"2026-11-29","count":1}"#, #"{ "kind":"app","id":"graphley","day":"2026-11-29","count":9223372036854775807 }, {"kind":"app","id":"jobley","day":"2026-11-29","count":1}"#, ] for rows in invalid { #expect(throws: MetricsError.invalidResponse) { try OwnerMetricsSnapshot.validated(format(rows), window: .week) } } } @Test func successfullyDecodesEmptyAndSeparateAppCounts() throws { let empty = try OwnerMetricsSnapshot.validated(Data(""" {"since":"2026-11-23","through":"2026-11-29","rows":[]} """.utf8), window: .week) #expect(!empty.hasData(.website)) #expect(!empty.hasData(.app)) let apps = try OwnerMetricsSnapshot.validated(Data(""" {"since":"2026-11-23","through":"2026-11-29","rows":[{"kind":"app","id":"jobley","day":"2026-11-29","count":5}]} """.utf8), window: .week) #expect(apps.hasData(.app)) #expect(!apps.hasData(.website)) #expect(apps.total(.app) == 5) }