Graphley / app · graphley.ai / Tests / OwnerMetricsKitTests / OwnerMetricsTests.swift
| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import OwnerMetricsKit |
| 4 | |
| 5 | private let session = String(repeating: "A", count: 43) |
| 6 | private let payload = Data(""" |
| 7 | { |
| 8 | "since": "2026-11-23", |
| 9 | "through": "2026-11-29", |
| 10 | "rows": [ |
| 11 | {"kind":"website","id":"projectatom","day":"2026-11-25","count":8}, |
| 12 | {"kind":"website","id":"protomota","day":"2026-11-25","count":2}, |
| 13 | {"kind":"website","id":"projectatom","day":"2026-11-29","count":3} |
| 14 | ] |
| 15 | } |
| 16 | """.utf8) |
| 17 | |
| 18 | @Test func authenticatedReadUsesFixedNoOriginEndpointAndComputesUTCWindow() async throws { |
| 19 | let client = MetricsClient { r in |
| 20 | #expect(r.url?.absoluteString == "https://projectatom.ai/api/visits?private=graphley&days=7") |
| 21 | #expect(r.httpMethod == "GET") |
| 22 | #expect(r.value(forHTTPHeaderField: "Authorization") == "Bearer " + session) |
| 23 | #expect(r.value(forHTTPHeaderField: "Origin") == nil) |
| 24 | #expect(r.value(forHTTPHeaderField: "Cookie") == nil) |
| 25 | #expect(r.httpBody == nil) |
| 26 | return .init(code: 200, contentType: "application/json; charset=utf-8", data: payload) |
| 27 | } |
| 28 | let result = try await client.read(session: session, window: .week) |
| 29 | #expect(result.total(.website) == 13) |
| 30 | #expect(result.daily(.website).map(\.count) == [0,0,10,0,0,0,3]) |
| 31 | #expect(result.properties(.website).map(\.title) == ["Project ATOM", "Protomota"]) |
| 32 | #expect(!result.hasData(.app)) |
| 33 | #expect(result.daily(.app).allSatisfy { $0.count == 0 }) |
| 34 | } |
| 35 | |
| 36 | @Test func rejectsAbsentOrMalformedSessionBeforeAnyNetwork() async { |
| 37 | let client = MetricsClient { _ in |
| 38 | Issue.record("Invalid sessions must not be sent") |
| 39 | return .init(code: 500, contentType: nil, data: Data()) |
| 40 | } |
| 41 | let oldViewerToken = "test-graphley-read-token-32-characters" |
| 42 | for invalid in ["", "short", session + "A", session.dropLast() + "=", session.dropLast() + "\n", " " + session.dropLast(), oldViewerToken] { |
| 43 | await #expect(throws: MetricsError.unauthorizedOrDisabled) { try await client.read(session: String(invalid), window: .week) } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | @Test func mapsDisabledOrUnauthorizedWithoutExposingResponseData() async { |
| 48 | for code in [401,403,404] { |
| 49 | let client = MetricsClient { _ in .init(code: code, contentType: "application/json", data: payload) } |
| 50 | await #expect(throws: MetricsError.unauthorizedOrDisabled) { try await client.read(session: session, window: .week) } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Test func rejectsWrongContentTypeRedirectOrOversizedData() async { |
| 55 | let unsupported = MetricsClient { _ in .init(code: 200, contentType: "text/html", data: payload) } |
| 56 | await #expect(throws: MetricsError.invalidResponse) { try await unsupported.read(session: session, window: .week) } |
| 57 | let redirected = MetricsClient { _ in .init(code: 302, contentType: nil, data: Data()) } |
| 58 | await #expect(throws: MetricsError.unavailable) { try await redirected.read(session: session, window: .week) } |
| 59 | let oversized = MetricsClient { _ in .init(code: 200, contentType: "application/json", data: Data(count: MetricsClient.limit + 1)) } |
| 60 | await #expect(throws: MetricsError.invalidResponse) { try await oversized.read(session: session, window: .week) } |
| 61 | } |
| 62 | |
| 63 | @Test func rejectsInvalidDayNegativeCountsAndDuplicateRows() throws { |
| 64 | let format = { (rows: String) in Data(""" |
| 65 | {"since":"2026-11-23","through":"2026-11-29","rows":[\(rows)]} |
| 66 | """.utf8) } |
| 67 | let invalid: [String] = [ |
| 68 | #"{ "kind":"app","id":"graphley","day":"2026-11-29","count":-3 }"#, |
| 69 | #"{ "kind":"app","id":"graphley","day":"2026-11-30","count":3 }"#, |
| 70 | #"{ "kind":"app","id":"https://projectatom.ai/path","day":"2026-11-29","count":3 }"#, |
| 71 | #"{ "kind":"app","id":"graphley","day":"2026-11-29","count":3 }, {"kind":"app","id":"graphley","day":"2026-11-29","count":1}"#, |
| 72 | #"{ "kind":"app","id":"graphley","day":"2026-11-29","count":9223372036854775807 }, {"kind":"app","id":"jobley","day":"2026-11-29","count":1}"#, |
| 73 | ] |
| 74 | for rows in invalid { |
| 75 | #expect(throws: MetricsError.invalidResponse) { try OwnerMetricsSnapshot.validated(format(rows), window: .week) } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Test func successfullyDecodesEmptyAndSeparateAppCounts() throws { |
| 80 | let empty = try OwnerMetricsSnapshot.validated(Data(""" |
| 81 | {"since":"2026-11-23","through":"2026-11-29","rows":[]} |
| 82 | """.utf8), window: .week) |
| 83 | #expect(!empty.hasData(.website)) |
| 84 | #expect(!empty.hasData(.app)) |
| 85 | let apps = try OwnerMetricsSnapshot.validated(Data(""" |
| 86 | {"since":"2026-11-23","through":"2026-11-29","rows":[{"kind":"app","id":"jobley","day":"2026-11-29","count":5}]} |
| 87 | """.utf8), window: .week) |
| 88 | #expect(apps.hasData(.app)) |
| 89 | #expect(!apps.hasData(.website)) |
| 90 | #expect(apps.total(.app) == 5) |
| 91 | } |