Graphley / app / Tests / AnalyticsKitTests / PortfolioTests.swift
| 1 | import XCTest |
| 2 | @testable import AnalyticsKit |
| 3 | |
| 4 | final class PortfolioTests: XCTestCase { |
| 5 | func testWindowFamilyReplacesWholeFamilyDailyKeepsOtherDays() { |
| 6 | let y = DayKey.lastDays(2) |
| 7 | var all = [DataPoint(day: y[0], subject: "s", metric: .pageViews, breakdown: "page:/old", value: 5), |
| 8 | DataPoint(day: y[0], subject: "s", metric: .users, value: 10)] |
| 9 | WorkspaceStore.merge([DataPoint(day: y[1], subject: "s", metric: .pageViews, breakdown: "page:/new", value: 7), |
| 10 | DataPoint(day: y[1], subject: "s", metric: .users, value: 12)], into: &all) |
| 11 | XCTAssertFalse(all.contains { $0.breakdown == "page:/old" }) |
| 12 | XCTAssertTrue(all.contains { $0.breakdown == "page:/new" }) |
| 13 | XCTAssertEqual(all.filter { $0.metric == .users }.count, 2) |
| 14 | } |
| 15 | |
| 16 | func testSubsitesGetOwnViewAndParentExcludesThem() { |
| 17 | var ws = Workspace() |
| 18 | Portfolio.apply(to: &ws) |
| 19 | let views = GA4SiteView.views(for: ws.sites) |
| 20 | let services = try! XCTUnwrap(views.first { $0.site.id == "protomota-services" }) |
| 21 | let main = try! XCTUnwrap(views.first { $0.site.id == "protomota" }) |
| 22 | XCTAssertTrue(services.isSection) |
| 23 | XCTAssertEqual(services.property, main.property) |
| 24 | guard case .excluding(let sections) = main.scope else { return XCTFail("main should exclude its sections") } |
| 25 | XCTAssertEqual(Set(sections.map(\.prefix)), ["/services", "/experiments", "/projectatom"]) |
| 26 | let atom = try! XCTUnwrap(views.first { $0.site.id == "project-atom" }) |
| 27 | XCTAssertTrue(atom.isSection) |
| 28 | XCTAssertEqual(atom.property, main.property) |
| 29 | XCTAssertEqual(sections.first { $0.prefix == "/projectatom" }?.host, "projectatom.ai") |
| 30 | XCTAssertEqual(sections.first { $0.prefix == "/projectatom" }?.contentGroup, "project-atom") |
| 31 | let atomParts = (atom.filter?["orGroup"] as? [String: Any])?["expressions"] as? [[String: Any]] ?? [] |
| 32 | XCTAssertEqual(atomParts.compactMap { ($0["filter"] as? [String: Any])?["fieldName"] as? String }, ["pagePath", "hostName", "contentGroup"]) |
| 33 | XCTAssertNotNil(main.filter?["notExpression"]) |
| 34 | } |
| 35 | |
| 36 | func testSitesFollowPortfolioOrderEvenWhenAddedLater() { |
| 37 | var ws = Workspace() |
| 38 | ws.sites = [Site(id: "mine", name: "Mine", url: nil, ga4PropertyID: "1", colorIndex: 0), |
| 39 | Site(id: "skillfarm", name: "SkillFarm", url: nil, ga4PropertyID: "527940031", colorIndex: 0)] |
| 40 | Portfolio.apply(to: &ws) |
| 41 | let ids = ws.sites.map(\.id) |
| 42 | XCTAssertEqual(ids.firstIndex(of: "skillfarm"), ids.firstIndex(of: "glimmer").map { $0 + 1 }) |
| 43 | XCTAssertEqual(ids.last, "mine") |
| 44 | } |
| 45 | |
| 46 | func testPlatformMapping() { |
| 47 | XCTAssertEqual(AppStoreConnect.platform(device: "Desktop"), "Mac") |
| 48 | XCTAssertEqual(AppStoreConnect.platform(device: "Desktop", desktopCountsAs: "iPhone"), "iPhone") |
| 49 | XCTAssertEqual(AppStoreConnect.platform(device: "Apple Vision"), "Vision Pro") |
| 50 | XCTAssertEqual(AppStoreConnect.platform(device: "iPad"), "iPad") |
| 51 | } |
| 52 | |
| 53 | func testMigrateMergesPlatformsAndRatings() { |
| 54 | let apps = [StoreApp(id: "6746770199", name: "Jotley")].map { a -> StoreApp in var a = a; a.desktopCountsAs = "iPhone"; return a } |
| 55 | var points = [DataPoint(day: "2026-11-20", subject: "6746770199", metric: .appDownloads, breakdown: "platform:Desktop", value: 2), |
| 56 | DataPoint(day: "2026-11-20", subject: "6746770199", metric: .appDownloads, breakdown: "platform:iPhone", value: 3), |
| 57 | DataPoint(day: "2026-11-20", subject: "6746770199", metric: .rating, breakdown: "count", value: 9)] |
| 58 | Portfolio.migrate(&points, apps: apps) |
| 59 | XCTAssertEqual(points.first { $0.breakdown == "platform:iPhone" }?.value, 5) |
| 60 | XCTAssertEqual(points.first { $0.metric == .ratingCount }?.value, 9) |
| 61 | } |
| 62 | |
| 63 | func testReviewTodoKeyAndPriority() { |
| 64 | let r = Review(id: "123", appID: "6746770199", appName: "Jotley: Voice Notes", country: "us", stars: 2, title: "Crashes", text: "x", |
| 65 | version: "1.0", author: "a", date: "2026-11-20", firstSeen: "2026-11-21", url: "https://apps.apple.com") |
| 66 | let t = OpenTalonTodos.todo(for: r) |
| 67 | XCTAssertEqual(t.key, "appstore-review-6746770199-123") |
| 68 | XCTAssertEqual(t.board, "Jotley") |
| 69 | XCTAssertEqual(t.priority, "High") |
| 70 | XCTAssertEqual(t.due, "2026-11-24") |
| 71 | } |
| 72 | } |