Inkspring / app · inkspring.ai / native-mac / Tests / InkspringTests / AnalyticsReportTests.swift
| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import Inkspring |
| 4 | |
| 5 | @Test func analyticsReportUsesSavedHistoryAndCountsOnlyConfirmedSteps() throws { |
| 6 | let now = try #require(scheduleDate("2026-11-11T16:00:00Z")) |
| 7 | let day = "2026-11-10" |
| 8 | func record(_ kind: String, _ fields: [String: Any], account: String = "local-account") -> WorkspaceRecord { |
| 9 | WorkspaceRecord(kind: kind, fields: fields.merging(["platform": "x", "accountId": account]) { current, _ in current }) |
| 10 | } |
| 11 | let observations = record("analytics-day", [ |
| 12 | "first": ["at": day + "T12:00:00Z", "followers": 100], |
| 13 | "last": ["at": day + "T16:00:00Z", "followers": 107, "accountPosts": 500] |
| 14 | ]) |
| 15 | let tracked = record("analytics-post", [ |
| 16 | "id": "tracked", "publishedAt": day + "T13:00:00Z", "observedAt": day + "T17:00:00Z", |
| 17 | "metrics": ["likes": 12, "replies": 0, "views": true] |
| 18 | ]) |
| 19 | let noMetrics = record("analytics-post", [ |
| 20 | "id": "missing", "publishedAt": day + "T14:00:00Z", "metrics": [:] |
| 21 | ]) |
| 22 | let excluded = record("analytics-post", [ |
| 23 | "id": "another", "publishedAt": day + "T15:00:00Z", "metrics": ["likes": 100] |
| 24 | ], account: "different-account") |
| 25 | let partial = record("publication", [ |
| 26 | "id": "thread", "status": "uncertain", "replyToId": "100", |
| 27 | "steps": [["id": "confirmed-step", "at": day + "T14:00:00Z"]] |
| 28 | ]) |
| 29 | let duplicate = record("publication", [ |
| 30 | "id": "duplicate", "status": "published", |
| 31 | "steps": [["id": "confirmed-step", "at": day + "T15:00:00Z"]] |
| 32 | ]) |
| 33 | let unconfirmed = record("publication", ["id": "unconfirmed", "status": "uncertain"]) |
| 34 | let records = [observations, tracked, noMetrics, excluded, partial, duplicate, unconfirmed] |
| 35 | let report = AnalyticsReport(records: records, platform: .x, accountID: "local-account", days: 7, sort: .engagement, now: now) |
| 36 | #expect(report.followers.map(\.count) == [100, 107]) |
| 37 | #expect(report.latestFollowers?.accountPosts == 500) |
| 38 | #expect(report.growth == 7) |
| 39 | #expect(report.posts.map(\.id) == ["tracked", "missing"]) |
| 40 | #expect(report.oldestPostAt == scheduleDate(day + "T13:00:00Z")) |
| 41 | #expect(report.posts.first?.observedAt == scheduleDate(day + "T17:00:00Z")) |
| 42 | #expect(report.total(.replies).value == 0) |
| 43 | #expect(report.total(.replies).covered == 1) |
| 44 | #expect(report.total(.views).value == nil) |
| 45 | #expect(report.confirmedCount == 1) |
| 46 | #expect(report.replyCount == 1) |
| 47 | #expect(report.activity.first?.count == 1) |
| 48 | #expect(AnalyticsReport.allConfirmed(records) == 1) |
| 49 | } |
| 50 | |
| 51 | @Test @MainActor func analyticsKeepsUnreportedShareCountsUnknown() async throws { |
| 52 | let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 53 | defer { try? FileManager.default.removeItem(at: directory) } |
| 54 | let workspace = Workspace(directory: directory) |
| 55 | let account = ManagedSocialAccount( |
| 56 | session: ManagedSocialSession(accessToken: "fixture", refreshToken: nil, managedTicket: nil, |
| 57 | expiresAt: 2_000_000_000_000, scopes: "tweet.read"), |
| 58 | id: "100", name: "Fixture", username: "fixture" |
| 59 | ) |
| 60 | let now = try #require(scheduleDate("2026-11-11T16:00:00Z")) |
| 61 | let transport = SocialTransport { request in |
| 62 | let published = "2026-11-10T12:00:00Z" |
| 63 | let posts: [[String: Any]] = [ |
| 64 | ["id": "1", "text": "No share metrics", "created_at": published, "public_metrics": ["like_count": 4]], |
| 65 | ["id": "2", "text": "Reported zero", "created_at": published, "public_metrics": ["repost_count": 0]], |
| 66 | ["id": "3", "text": "Only quotes available", "created_at": published, "public_metrics": ["quote_count": 3, "retweet_count": -1]], |
| 67 | ["id": "4", "text": "Unusable share total", "created_at": published, "public_metrics": ["quote_count": 9_007_199_254_740_991, "retweet_count": 1]] |
| 68 | ] |
| 69 | let data = try JSONSerialization.data(withJSONObject: ["data": posts]) |
| 70 | let url = try #require(request.url) |
| 71 | let response = try #require(HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)) |
| 72 | return (data, response) |
| 73 | } |
| 74 | let analytics = AccountAnalytics(workspace: workspace, transport: transport, accountOverride: account) |
| 75 | #expect(try await analytics.refresh(.x, now: now).isEmpty) |
| 76 | let report = AnalyticsReport(records: workspace.records, platform: .x, accountID: account.id, days: 7, sort: .shares, now: now) |
| 77 | #expect(report.posts.count == 4) |
| 78 | #expect(report.posts.prefix(2).map { $0.record["postId"] } == ["3", "2"]) |
| 79 | #expect(report.total(.shares).value == 3) |
| 80 | #expect(report.total(.shares).covered == 2) |
| 81 | #expect(report.posts.allSatisfy { $0.observedAt == now }) |
| 82 | } |