|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | +@testable import CodexBarCore |
| 4 | + |
| 5 | +struct CodexLineageLedgerTests { |
| 6 | + @Test |
| 7 | + func `transitive lineage counts copied observations once`() throws { |
| 8 | + let first = Self.observation(timestamp: "2026-07-09T12:00:00Z", input: 100, totalInput: 100) |
| 9 | + let second = Self.observation(timestamp: "2026-07-09T12:01:00Z", input: 50, totalInput: 150) |
| 10 | + let third = Self.observation(timestamp: "2026-07-09T12:02:00Z", input: 25, totalInput: 175) |
| 11 | + let documents = [ |
| 12 | + Self.document(owner: "root", observations: [first]), |
| 13 | + Self.document(owner: "child", metadata: "root", parent: "root", observations: [first, second]), |
| 14 | + Self.document(owner: "grandchild", metadata: "child", parent: "child", observations: [ |
| 15 | + first, |
| 16 | + second, |
| 17 | + third, |
| 18 | + ]), |
| 19 | + ] |
| 20 | + |
| 21 | + let report = try CodexLineageLedger.reconcile( |
| 22 | + documents: documents, |
| 23 | + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) |
| 24 | + |
| 25 | + #expect(report.utcDays["2026-07-09"]?.input == 175) |
| 26 | + #expect(report.localDays["2026-07-09"]?.input == 175) |
| 27 | + #expect(report.componentCount == 1) |
| 28 | + #expect(report.acceptedObservationCount == 3) |
| 29 | + #expect(report.duplicateObservationCount == 3) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + func `equal observations in disconnected lineages remain additive`() throws { |
| 34 | + let observation = Self.observation(timestamp: "2026-07-09T12:00:00Z", input: 100, totalInput: 100) |
| 35 | + let report = try CodexLineageLedger.reconcile( |
| 36 | + documents: [ |
| 37 | + Self.document(owner: "first", observations: [observation]), |
| 38 | + Self.document(owner: "second", observations: [observation]), |
| 39 | + ], |
| 40 | + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) |
| 41 | + |
| 42 | + #expect(report.utcDays["2026-07-09"]?.input == 200) |
| 43 | + #expect(report.componentCount == 2) |
| 44 | + #expect(report.acceptedObservationCount == 2) |
| 45 | + #expect(report.duplicateObservationCount == 0) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + func `unchanged state reemissions within a lineage count once`() throws { |
| 50 | + let observation = Self.observation(timestamp: "2026-07-09T12:00:00Z", input: 100, totalInput: 100) |
| 51 | + let report = try CodexLineageLedger.reconcile( |
| 52 | + documents: [Self.document(owner: "root", observations: [observation, observation, observation])], |
| 53 | + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) |
| 54 | + |
| 55 | + #expect(report.utcDays["2026-07-09"]?.input == 100) |
| 56 | + #expect(report.acceptedObservationCount == 1) |
| 57 | + #expect(report.duplicateObservationCount == 2) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + func `complete token state distinguishes observations within a lineage`() throws { |
| 62 | + let first = Self.observation( |
| 63 | + timestamp: "2026-07-09T12:00:00Z", |
| 64 | + input: 100, |
| 65 | + cached: 40, |
| 66 | + output: 10, |
| 67 | + totalInput: 100) |
| 68 | + let changedTotal = Self.observation( |
| 69 | + timestamp: "2026-07-09T12:01:00Z", |
| 70 | + input: 100, |
| 71 | + cached: 40, |
| 72 | + output: 10, |
| 73 | + totalInput: 200) |
| 74 | + let changedLast = Self.observation( |
| 75 | + timestamp: "2026-07-09T12:02:00Z", |
| 76 | + input: 125, |
| 77 | + cached: 50, |
| 78 | + output: 15, |
| 79 | + totalInput: 200) |
| 80 | + let report = try CodexLineageLedger.reconcile( |
| 81 | + documents: [ |
| 82 | + Self.document(owner: "root", observations: [first, changedTotal, changedLast]), |
| 83 | + ], |
| 84 | + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) |
| 85 | + |
| 86 | + #expect(report.utcDays["2026-07-09"] == .init(input: 325, cached: 130, output: 35)) |
| 87 | + #expect(report.acceptedObservationCount == 3) |
| 88 | + #expect(report.duplicateObservationCount == 0) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + func `UTC and local projections preserve their distinct day boundaries`() throws { |
| 93 | + let observation = Self.observation(timestamp: "2026-07-10T02:00:00Z", input: 100, totalInput: 100) |
| 94 | + let report = try CodexLineageLedger.reconcile( |
| 95 | + documents: [Self.document(owner: "root", observations: [observation])], |
| 96 | + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) |
| 97 | + |
| 98 | + #expect(report.utcDays["2026-07-10"]?.input == 100) |
| 99 | + #expect(report.localDays["2026-07-09"]?.input == 100) |
| 100 | + } |
| 101 | + |
| 102 | + @Test(arguments: [ |
| 103 | + ("archived-fork-33ce-3869", 15_309_178), |
| 104 | + ("live-fork-4d90-52bf", 26_801_911), |
| 105 | + ]) |
| 106 | + func `sanitized fork fixtures collapse copied prefixes and unchanged reemissions`( |
| 107 | + fixtureName: String, |
| 108 | + expectedTokens: Int) throws |
| 109 | + { |
| 110 | + let fixture = try SanitizedForkFamilyFixture.load(named: fixtureName) |
| 111 | + let parentMetadata = try fixture.sessionMetadata(named: "parent") |
| 112 | + let childMetadata = try fixture.sessionMetadata(named: "child") |
| 113 | + let parent = try fixture.events(named: "parent") |
| 114 | + let child = try fixture.events(named: "child") |
| 115 | + let documents = [ |
| 116 | + CodexLineageLedger.Document( |
| 117 | + ownerID: "parent-owner", |
| 118 | + metadataSessionID: parentMetadata.id, |
| 119 | + parentSessionID: parentMetadata.forkedFromID, |
| 120 | + observations: parent.map(Self.observation)), |
| 121 | + CodexLineageLedger.Document( |
| 122 | + ownerID: "child-owner", |
| 123 | + metadataSessionID: childMetadata.id, |
| 124 | + parentSessionID: childMetadata.forkedFromID, |
| 125 | + observations: child.map(Self.observation)), |
| 126 | + ] |
| 127 | + |
| 128 | + let report = try CodexLineageLedger.reconcile( |
| 129 | + documents: documents, |
| 130 | + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) |
| 131 | + let total = report.utcDays.values.reduce(0) { partial, totals in |
| 132 | + partial + totals.input + totals.output |
| 133 | + } |
| 134 | + |
| 135 | + #expect(total == expectedTokens) |
| 136 | + #expect(total < fixture.manifest.oracle.dedupedLastTokens) |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + func `document order does not change lineage totals or attribution`() throws { |
| 141 | + let copiedLater = Self.observation(timestamp: "2026-07-10T00:05:00Z", input: 100, totalInput: 100) |
| 142 | + let original = Self.observation(timestamp: "2026-07-09T23:55:00Z", input: 100, totalInput: 100) |
| 143 | + let root = Self.document(owner: "root", observations: [original]) |
| 144 | + let child = Self.document(owner: "child", parent: "root", observations: [copiedLater]) |
| 145 | + let timeZone = try #require(TimeZone(identifier: "America/New_York")) |
| 146 | + |
| 147 | + let forward = try CodexLineageLedger.reconcile(documents: [root, child], localTimeZone: timeZone) |
| 148 | + let reversed = try CodexLineageLedger.reconcile(documents: [child, root], localTimeZone: timeZone) |
| 149 | + |
| 150 | + #expect(forward == reversed) |
| 151 | + #expect(forward.utcDays["2026-07-09"]?.input == 100) |
| 152 | + #expect(forward.utcDays["2026-07-10"] == nil) |
| 153 | + } |
| 154 | + |
| 155 | + private static func document( |
| 156 | + owner: String, |
| 157 | + metadata: String? = nil, |
| 158 | + parent: String? = nil, |
| 159 | + observations: [CodexLineageLedger.Observation]) -> CodexLineageLedger.Document |
| 160 | + { |
| 161 | + .init( |
| 162 | + ownerID: owner, |
| 163 | + metadataSessionID: metadata, |
| 164 | + parentSessionID: parent, |
| 165 | + observations: observations) |
| 166 | + } |
| 167 | + |
| 168 | + private static func observation( |
| 169 | + timestamp: String, |
| 170 | + input: Int, |
| 171 | + cached: Int = 0, |
| 172 | + output: Int = 0, |
| 173 | + totalInput: Int) -> CodexLineageLedger.Observation |
| 174 | + { |
| 175 | + .init( |
| 176 | + timestamp: timestamp, |
| 177 | + last: .init(input: input, cached: cached, output: output), |
| 178 | + total: .init(input: totalInput, cached: cached, output: output)) |
| 179 | + } |
| 180 | + |
| 181 | + private static func observation( |
| 182 | + _ event: SanitizedForkFamilyFixture.TokenEvent) -> CodexLineageLedger.Observation |
| 183 | + { |
| 184 | + .init( |
| 185 | + timestamp: event.timestamp, |
| 186 | + last: .init( |
| 187 | + input: event.last.inputTokens, |
| 188 | + cached: event.last.cachedInputTokens, |
| 189 | + output: event.last.outputTokens), |
| 190 | + total: .init( |
| 191 | + input: event.total.inputTokens, |
| 192 | + cached: event.total.cachedInputTokens, |
| 193 | + output: event.total.outputTokens)) |
| 194 | + } |
| 195 | +} |
0 commit comments