Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageMerge.test.ts

750 lines23.9 KB

1import {
2 USAGE_CONTRACT_VERSION,
3 USAGE_MERGE_COMPATIBLE_SINCE,
4 type EnvironmentId,
5 type UsageBucket,
6 type UsageDay,
7 type UsageProviderKind,
8 UsageSummary,
9} from "@t3tools/contracts";
10import { describe, expect, it } from "vite-plus/test";
11import * as Schema from "effect/Schema";
12​
13import { isModelCostUnknown, mergeUsage, type EnvironmentUsage } from "./usageMerge.ts";
14​
15const decodeSummary = Schema.decodeUnknownSync(UsageSummary);
16​
17function bucket(overrides: Partial<UsageBucket> = {}): UsageBucket {
18 return {
19 day: "2026-08-07" as UsageDay,
20 provider: "claude",
21 model: "claude-fable-5",
22 totals: {
23 uncachedInputTokens: 100,
24 cachedInputTokens: 1000,
25 cacheCreationTokens: 10,
26 outputTokens: 50,
27 reasoningTokens: 0,
28 },
29 costUsd: 10,
30 cacheSavingsUsd: 2,
31 costSource: "modelPriced",
32 records: 5,
33 unpricedRecords: 0,
34 sessions: 1,
35 ...overrides,
36 };
38​
39function summary(
40 buckets: readonly UsageBucket[],
41 sources: readonly {
42 provider: UsageProviderKind;
43 hostId: string;
44 homePath: string;
45 volumeId?: string;
46 physicalSourceId?: string;
47 distinctSessions?: number;
48 }[],
49 contractVersion: number = USAGE_CONTRACT_VERSION,
50): UsageSummary {
51 return {
52 contractVersion,
53 readAt: "2026-08-07T00:00:00.000Z",
54 timeZone: "UTC",
55 sinceDay: "2026-08-01" as UsageDay,
56 untilDay: "2026-08-31" as UsageDay,
57 buckets,
58 sources: sources.map((source) => ({
59 fingerprint: {
60 hostId: source.hostId,
61 provider: source.provider,
62 resolvedHomePath: source.homePath,
63 volumeId: source.volumeId ?? `vol-${source.hostId}`,
64 ...(source.physicalSourceId === undefined
65 ? {}
66 : { physicalSourceId: source.physicalSourceId }),
67 },
68 status: "ok" as const,
69 scannedFiles: 1,
70 skippedFiles: 0,
71 malformedRecords: 0,
72 distinctSessions: source.distinctSessions ?? 1,
73 message: null,
74 })),
75 pricing: { status: "fresh", source: "litellm", fetchedAt: null, knownModels: 10 },
76 scanDurationMs: 1,
77 };
79​
80function environment(id: string, usageSummary: UsageSummary): EnvironmentUsage {
81 return { environmentId: id as EnvironmentId, label: id, summary: usageSummary };
83​
84describe("mergeUsage", () => {
85 it("counts a Cursor account once across servers while retaining each server's other providers", () => {
86 const account = {
87 provider: "cursor" as const,
88 hostId: "cursor.com",
89 homePath: "cursor-account:account-hash",
90 volumeId: "account-hash",
91 };
92 const merged = mergeUsage(
93 [
94 environment(
95 "mac",
96 summary([bucket({ provider: "cursor", sourcePath: account.homePath })], [account]),
97 ),
98 environment(
99 "linux",
100 summary(
101 [
102 bucket({ provider: "cursor", sourcePath: account.homePath }),
103 bucket({ provider: "opencode", sourcePath: "/opencode" }),
104 ],
105 [account, { provider: "opencode", hostId: "linux", homePath: "/opencode" }],
106 ),
107 ),
108 ],
109 USAGE_CONTRACT_VERSION,
110 );
111 expect(
112 merged.providers.map((provider) => [provider.provider, provider.costUsd]).sort(),
113 ).toEqual([
114 ["cursor", 10],
115 ["opencode", 10],
116 ]);
117 expect(merged.duplicateSources).toHaveLength(1);
118 });
119​
120 it("counts a shared transcript directory once", () => {
121 const shared = { provider: "claude" as const, hostId: "mac", homePath: "/home/theo/.claude" };
122 const merged = mergeUsage(
123 [
124 environment("env-a", summary([bucket()], [shared])),
125 environment("env-b", summary([bucket()], [shared])),
126 ],
127 USAGE_CONTRACT_VERSION,
128 );
129​
130 expect(merged.costUsd).toBe(10);
131 expect(merged.records).toBe(5);
132 expect(merged.sessions).toBe(1);
133 expect(merged.duplicateSources).toHaveLength(1);
134 expect(merged.contributingEnvironments).toEqual(["env-a"]);
135 });
136​
137 it("drops only the duplicated provider, keeping the environment's other one", () => {
138 const sharedClaude = {
139 provider: "claude" as const,
140 hostId: "mac",
141 homePath: "/home/theo/.claude",
142 };
143 const merged = mergeUsage(
144 [
145 environment("env-a", summary([bucket()], [sharedClaude])),
146 environment(
147 "env-b",
148 summary(
149 [bucket(), bucket({ provider: "codex", model: "gpt-5.6-sol", costUsd: 4 })],
150 [sharedClaude, { provider: "codex", hostId: "mac", homePath: "/home/theo/.codex" }],
151 ),
152 ),
153 ],
154 USAGE_CONTRACT_VERSION,
155 );
156​
157 expect(merged.costUsd).toBe(14);
158 expect(merged.providers.map((provider) => provider.provider).sort()).toEqual([
159 "claude",
160 "codex",
161 ]);
162 expect(merged.sessions).toBe(2);
163 expect(
164 Object.fromEntries(
165 merged.providers.map((provider) => [provider.provider, provider.sessions]),
166 ),
167 ).toEqual({ claude: 1, codex: 1 });
168 });
169​
170 it("counts overlapping provider roots once while keeping each environment's unique root", () => {
171 const source = (homePath: string) => ({
172 provider: "opencode" as const,
173 hostId: "host",
174 homePath,
175 });
176 const usage = (sourcePath: string, costUsd: number) =>
177 bucket({ provider: "opencode", sourcePath, costUsd });
178 const merged = mergeUsage(
179 [
180 environment(
181 "env-a",
182 summary([usage("/shared", 10), usage("/a", 2)], [source("/shared"), source("/a")]),
183 ),
184 environment(
185 "env-b",
186 summary([usage("/shared", 10), usage("/b", 3)], [source("/shared"), source("/b")]),
187 ),
188 ],
189 USAGE_CONTRACT_VERSION,
190 );
191 expect(merged.costUsd).toBe(15);
192 expect(merged.sessions).toBe(3);
193 });
194​
195 it("keeps transcript directories that differ by a trailing space distinct after a wire round trip", () => {
196 const shared = {
197 provider: "pi" as const,
198 hostId: "host",
199 homePath: "/sessions",
200 volumeId: "1:10",
201 };
202 const unique = { ...shared, homePath: "/sessions ", volumeId: "1:11" };
203 const usage = (sourcePath: string, uncachedInputTokens: number) =>
204 bucket({
205 provider: "pi",
206 model: "gpt-5.4",
207 sourcePath,
208 totals: {
209 uncachedInputTokens,
210 cachedInputTokens: 0,
211 cacheCreationTokens: 0,
212 outputTokens: 0,
213 reasoningTokens: 0,
214 },
215 });
216 const sharedBucket = usage(shared.homePath, 10);
217 const encodeSummary = Schema.encodeSync(UsageSummary);
218 const roundTrip = (value: UsageSummary) => decodeSummary(encodeSummary(value));
219 const second = roundTrip(summary([sharedBucket, usage(unique.homePath, 50)], [shared, unique]));
220 const merged = mergeUsage(
221 [environment("a", roundTrip(summary([sharedBucket], [shared]))), environment("b", second)],
222 USAGE_CONTRACT_VERSION,
223 );
224​
225 expect(merged.totalTokens).toBe(60);
226 expect(second.buckets.map((entry) => entry.sourcePath)).toEqual([
227 shared.homePath,
228 unique.homePath,
229 ]);
230 expect(second.sources.map((entry) => entry.fingerprint.resolvedHomePath)).toEqual([
231 shared.homePath,
232 unique.homePath,
233 ]);
234 });
235​
236 it("uses the newest scan when environments share the same transcript directory", () => {
237 const source = { provider: "claude" as const, hostId: "mac", homePath: "/home/theo/.claude" };
238 const environments = [
239 environment("env-a", summary([bucket({ costUsd: 4, records: 2 })], [source])),
240 environment("env-b", {
241 ...summary([bucket()], [source]),
242 readAt: "2026-08-07T01:00:00.000Z",
243 }),
244 ];
245​
246 for (const ordered of [environments, environments.toReversed()]) {
247 const merged = mergeUsage(ordered, USAGE_CONTRACT_VERSION);
248 expect(merged.costUsd).toBe(10);
249 expect(merged.records).toBe(5);
250 expect(merged.sessions).toBe(1);
251 expect(merged.contributingEnvironments).toEqual(["env-b"]);
252 expect(merged.duplicateSources).toEqual(["env-a: /home/theo/.claude"]);
253 }
254 });
255​
256 it("prefers a complete scan over a newer partial scan of the same directory", () => {
257 const source = { provider: "claude" as const, hostId: "mac", homePath: "/home/theo/.claude" };
258 const incomplete = summary([bucket({ costUsd: 4, records: 2 })], [source]);
259 const partial = environment("new", {
260 ...incomplete,
261 readAt: "2026-08-07T01:00:00.000Z",
262 sources: incomplete.sources.map((entry) => ({ ...entry, status: "partial" as const })),
263 });
264 const complete = environment("old", summary([bucket()], [source]));
265​
266 for (const ordered of [
267 [partial, complete],
268 [complete, partial],
269 ]) {
270 const merged = mergeUsage(ordered, USAGE_CONTRACT_VERSION);
271 expect(merged.costUsd).toBe(10);
272 expect(merged.contributingEnvironments).toEqual(["old"]);
273 expect(merged.duplicateSources).toEqual(["new: /home/theo/.claude"]);
274 }
275 expect(mergeUsage([partial], USAGE_CONTRACT_VERSION).costUsd).toBe(4);
276 });
277​
278 it("keeps new cells from a later partial scan without recounting older cells", () => {
279 const source = { provider: "claude" as const, hostId: "mac", homePath: "/home/theo/.claude" };
280 const complete = environment(
281 "old",
282 summary([bucket()], [source], USAGE_MERGE_COMPATIBLE_SINCE),
283 );
284 const partialSummary = summary(
285 [
286 bucket({ sourcePath: source.homePath, costUsd: 4, records: 2 }),
287 bucket({
288 day: "2026-08-08" as UsageDay,
289 sourcePath: source.homePath,
290 costUsd: 3,
291 records: 1,
292 }),
293 ],
294 [{ ...source, distinctSessions: 2 }],
295 );
296 const partial = environment("new", {
297 ...partialSummary,
298 readAt: "2026-08-08T01:00:00.000Z",
299 sources: partialSummary.sources.map((entry) => ({ ...entry, status: "partial" as const })),
300 });
301​
302 for (const ordered of [
303 [complete, partial],
304 [partial, complete],
305 ]) {
306 const merged = mergeUsage(ordered, USAGE_CONTRACT_VERSION);
307 expect(merged.costUsd).toBe(13);
308 expect(merged.records).toBe(6);
309 expect(merged.sessions).toBe(2);
310 expect(merged.daily.map(({ day, costUsd }) => [day, costUsd])).toEqual([
311 ["2026-08-07", 10],
312 ["2026-08-08", 3],
313 ]);
314 expect(merged.contributingEnvironments).toEqual(
315 ordered.map(({ environmentId }) => environmentId),
316 );
317 expect(merged.duplicateSources).toEqual(["new: /home/theo/.claude"]);
318 }
319​
320 const filtered = environment("new", {
321 ...partial.summary,
322 modelFilter: { exclude: [{ model: "claude-fable-5" }] },
323 });
324 const merged = mergeUsage([complete, filtered], USAGE_CONTRACT_VERSION);
325 expect(merged.costUsd).toBe(10);
326 expect(merged.sessions).toBe(1);
327 });
328​
329 it("retains a complete cell when a larger partial cell may have skipped old records", () => {
330 const source = { provider: "claude" as const, hostId: "mac", homePath: "/home/theo/.claude" };
331 const complete = environment("old", summary([bucket()], [source]));
332 const partialSummary = summary(
333 [
334 bucket({
335 costUsd: 4,
336 records: 6,
337 totals: {
338 uncachedInputTokens: 80,
339 cachedInputTokens: 500,
340 cacheCreationTokens: 10,
341 outputTokens: 30,
342 reasoningTokens: 0,
343 },
344 }),
345 ],
346 [{ ...source, distinctSessions: 2 }],
347 );
348 const partial = environment("new", {
349 ...partialSummary,
350 readAt: "2026-08-07T01:00:00.000Z",
351 sources: partialSummary.sources.map((entry) => ({ ...entry, status: "partial" as const })),
352 });
353​
354 const merged = mergeUsage([complete, partial], USAGE_CONTRACT_VERSION);
355 expect(merged.costUsd).toBe(10);
356 expect(merged.totalTokens).toBe(1160);
357 expect(merged.records).toBe(5);
358 expect(merged.sessions).toBe(1);
359 expect(merged.contributingEnvironments).toEqual(["old"]);
360 });
361​
362 it("identifies an environment reporting an older contract version", () => {
363 const merged = mergeUsage(
364 [
365 environment(
366 "env-a",
367 summary([bucket()], [{ provider: "claude", hostId: "mac", homePath: "/a" }]),
368 ),
369 environment(
370 "env-b",
371 summary(
372 [bucket()],
373 [{ provider: "claude", hostId: "linux", homePath: "/b" }],
374 USAGE_MERGE_COMPATIBLE_SINCE - 1,
375 ),
376 ),
377 ],
378 USAGE_CONTRACT_VERSION,
379 );
380​
381 expect(merged.costUsd).toBe(10);
382 expect(merged.contractMismatches).toEqual([
383 {
384 environmentId: "env-b",
385 direction: "serverBehind",
386 contractVersion: USAGE_MERGE_COMPATIBLE_SINCE - 1,
387 },
388 ]);
389 });
390​
391 it("identifies an environment reporting a newer contract version", () => {
392 const merged = mergeUsage(
393 [
394 environment(
395 "env-a",
396 summary(
397 [bucket()],
398 [{ provider: "claude", hostId: "mac", homePath: "/a" }],
399 USAGE_CONTRACT_VERSION + 1,
400 ),
401 ),
402 ],
403 USAGE_CONTRACT_VERSION,
404 );
405​
406 expect(merged.costUsd).toBe(0);
407 expect(merged.contractMismatches).toEqual([
408 {
409 environmentId: "env-a",
410 direction: "clientBehind",
411 contractVersion: USAGE_CONTRACT_VERSION + 1,
412 },
413 ]);
414 });
415​
416 it("keeps the previous compatible contract version so additive provider expansions still merge", () => {
417 const merged = mergeUsage(
418 [
419 environment(
420 "env-a",
421 summary(
422 [bucket({ costUsd: 10 })],
423 [{ provider: "claude", hostId: "mac", homePath: "/a" }],
424 ),
425 ),
426 environment(
427 "env-b",
428 summary(
429 [bucket({ costUsd: 4, provider: "codex", model: "gpt-5.6-sol" })],
430 [{ provider: "codex", hostId: "linux", homePath: "/b" }],
431 USAGE_CONTRACT_VERSION - 1,
432 ),
433 ),
434 ],
435 USAGE_CONTRACT_VERSION,
436 );
437​
438 expect(merged.costUsd).toBe(14);
439 expect(merged.contractMismatches).toEqual([]);
440 });
441​
442 it("keeps known usage when newer providers and bucket variants cannot be decoded", () => {
443 const known = summary([bucket()], [{ provider: "claude", hostId: "mac", homePath: "/a" }]);
444 const decoded = decodeSummary({
445 ...known,
446 buckets: [
447 ...known.buckets,
448 { ...bucket(), provider: "future-provider", costUsd: 100 },
449 { ...bucket(), costSource: "future-pricing", costUsd: 200 },
450 ],
451 sources: [
452 ...known.sources,
453 {
454 ...known.sources[0],
455 fingerprint: {
456 ...known.sources[0]?.fingerprint,
457 provider: "future-provider",
458 },
459 },
460 ],
461 });
462 expect(decoded).toEqual(known);
463 const merged = mergeUsage([environment("env-a", decoded)], USAGE_CONTRACT_VERSION);
464 expect(merged.costUsd).toBe(10);
465 expect(merged.totalTokens).toBe(1160);
466 expect(merged.contractMismatches).toEqual([]);
467 });
468​
469 it("normalizes model names during decoding before grouping usage", () => {
470 const decoded = decodeSummary(
471 summary(
472 [
473 bucket({ provider: "codex", model: " gpt-5 " }),
474 bucket({ provider: "codex", model: "gpt-5" }),
475 ],
476 [{ provider: "codex", hostId: "mac", homePath: "/a" }],
477 ),
478 );
479​
480 expect(decoded.buckets.map((entry) => entry.model)).toEqual(["gpt-5", "gpt-5"]);
481 const merged = mergeUsage([environment("env-a", decoded)], USAGE_CONTRACT_VERSION);
482 expect(merged.models).toHaveLength(1);
483 expect(merged.models[0]).toMatchObject({ model: "gpt-5", costUsd: 20, totalTokens: 2320 });
484 });
485​
486 it("marks a model with no known rates as unpriced rather than free", () => {
487 const merged = mergeUsage(
488 [
489 environment(
490 "env-a",
491 summary(
492 [
493 bucket({ costUsd: 75 }),
494 bucket({
495 provider: "codex",
496 model: "unknown-model",
497 costUsd: 0,
498 costSource: "unpriced",
499 unpricedRecords: 5,
500 }),
501 ],
502 [
503 { provider: "claude", hostId: "mac", homePath: "/a/.claude" },
504 { provider: "codex", hostId: "mac", homePath: "/a/.codex" },
505 ],
506 ),
507 ),
508 ],
509 USAGE_CONTRACT_VERSION,
510 );
511​
512 expect(merged.models.find((model) => model.model === "unknown-model")?.unpricedRecords).toBe(5);
513 expect(merged.models.filter(isModelCostUnknown).map((model) => model.model)).toEqual([
514 "unknown-model",
515 ]);
516 });
517​
518 it("splits cost by category and speed, counting older servers as unsplit standard cost", () => {
519 const merged = mergeUsage(
520 [
521 environment(
522 "env-a",
523 summary(
524 [
525 bucket({
526 costUsd: 10,
527 categoryCostUsd: { input: 1, cacheRead: 2, cacheWrite: 3, output: 4 },
528 fastCostUsd: 6,
529 speedPremiumUsd: 3,
530 }),
531 bucket({
532 provider: "codex",
533 model: "unknown-model",
534 costUsd: 0,
535 costSource: "unpriced",
536 unpricedRecords: 5,
537 }),
538 bucket({
539 provider: "codex",
540 model: "partly-reported",
541 costUsd: 0,
542 unpricedRecords: 4,
543 }),
544 ],
545 [
546 { provider: "claude", hostId: "mac", homePath: "/a/.claude" },
547 { provider: "codex", hostId: "mac", homePath: "/a/.codex" },
548 ],
549 ),
550 ),
551 environment(
552 "env-b",
553 summary(
554 [bucket({ costUsd: 5 })],
555 [{ provider: "claude", hostId: "linux", homePath: "/b/.claude" }],
556 USAGE_MERGE_COMPATIBLE_SINCE,
557 ),
558 ),
559 ],
560 USAGE_CONTRACT_VERSION,
561 );
562​
563 expect(merged.categoryCost).toEqual({
564 input: 1,
565 cacheRead: 2,
566 cacheWrite: 3,
567 output: 4,
568 unsplit: 5,
569 });
570 expect(merged.speedCost).toEqual({ standard: 9, fast: 6, ultrafast: 0, premium: 3 });
571 expect(merged.models.map(({ model, unpricedTokens }) => [model, unpricedTokens])).toEqual([
572 ["claude-fable-5", 0],
573 ["unknown-model", 1160],
574 ["partly-reported", 928],
575 ]);
576 });
577​
578 it("keeps two machines apart when hostname and home path collide", () => {
579 const shape = { provider: "claude" as const, hostId: "mac", homePath: "/Users/theo/.claude" };
580 const merged = mergeUsage(
581 [
582 environment("env-a", summary([bucket()], [{ ...shape, volumeId: "16777220:1234" }])),
583 environment("env-b", summary([bucket()], [{ ...shape, volumeId: "16777221:9999" }])),
584 ],
585 USAGE_CONTRACT_VERSION,
586 );
587​
588 expect(merged.costUsd).toBe(20);
589 expect(merged.duplicateSources).toHaveLength(0);
590 });
591​
592 describe("one directory seen from both sides of a Windows and WSL boundary", () => {
593 // Fingerprints the reporter captured for one NTFS directory, read natively by
594 // Windows and through the drvfs mount by WSL. Path and dev:ino both differ.
595 const physicalSourceId = "physical-claude-projects";
596 const windows = {
597 provider: "claude" as const,
598 hostId: "MainPC",
599 homePath: "C:\\Users\\user\\.claude\\projects",
600 volumeId: "1280555009:3659174697951285",
601 physicalSourceId,
602 distinctSessions: 142,
603 };
604 const wsl = {
605 provider: "claude" as const,
606 hostId: "MainPC",
607 homePath: "/mnt/c/Users/user/.claude/projects",
608 volumeId: "112:3659174697951287",
609 physicalSourceId,
610 distinctSessions: 142,
611 };
612​
613 it("counts it once, whichever scan is newer", () => {
614 const environments = [
615 environment(
616 "windows",
617 summary([bucket({ sourcePath: windows.homePath, records: 22433 })], [windows]),
618 ),
619 environment("wsl", {
620 ...summary([bucket({ sourcePath: wsl.homePath, records: 22432 })], [wsl]),
621 readAt: "2026-08-07T00:05:00.000Z",
622 }),
623 ];
624​
625 for (const ordered of [environments, environments.toReversed()]) {
626 const merged = mergeUsage(ordered, USAGE_CONTRACT_VERSION);
627 expect(merged.sessions).toBe(142);
628 expect(merged.records).toBe(22432);
629 expect(merged.costUsd).toBe(10);
630 expect(merged.contributingEnvironments).toEqual(["wsl"]);
631 expect(merged.duplicateSources).toEqual([`windows: ${windows.homePath}`]);
632 }
633 });
634​
635 it("keeps only new cells from a later partial scan through the other side", () => {
636 const complete = environment(
637 "windows",
638 summary([bucket({ sourcePath: windows.homePath })], [windows]),
639 );
640 const partialSummary = summary(
641 [
642 bucket({ sourcePath: wsl.homePath, costUsd: 4, records: 2 }),
643 bucket({ day: "2026-08-08" as UsageDay, sourcePath: wsl.homePath, costUsd: 3 }),
644 ],
645 [{ ...wsl, distinctSessions: 143 }],
646 );
647 const partial = environment("wsl", {
648 ...partialSummary,
649 readAt: "2026-08-08T01:00:00.000Z",
650 sources: partialSummary.sources.map((entry) => ({ ...entry, status: "partial" as const })),
651 });
652​
653 const merged = mergeUsage([complete, partial], USAGE_CONTRACT_VERSION);
654 expect(merged.costUsd).toBe(13);
655 expect(merged.sessions).toBe(143);
656 expect(merged.daily.map(({ day, costUsd }) => [day, costUsd])).toEqual([
657 ["2026-08-07", 10],
658 ["2026-08-08", 3],
659 ]);
660 });
661​
662 it("counts it once when one server reaches it through two mounts", () => {
663 const secondMount = { ...wsl, homePath: "/mnt/win/Users/user/.claude/projects" };
664 const merged = mergeUsage(
665 [
666 environment(
667 "wsl",
668 summary(
669 [
670 bucket({ sourcePath: wsl.homePath }),
671 bucket({
672 day: "2026-08-08" as UsageDay,
673 sourcePath: secondMount.homePath,
674 costUsd: 3,
675 }),
676 ],
677 [wsl, secondMount],
678 ),
679 ),
680 ],
681 USAGE_CONTRACT_VERSION,
682 );
683​
684 expect(merged.costUsd).toBe(10);
685 expect(merged.sessions).toBe(142);
686 expect(merged.daily.map(({ day }) => day)).toEqual(["2026-08-07"]);
687 expect(merged.duplicateSources).toEqual([`wsl: ${secondMount.homePath}`]);
688 });
689​
690 it("still counts a shared directory once when only one server reports physical identity", () => {
691 const { physicalSourceId: _omitted, ...legacy } = windows;
692 const merged = mergeUsage(
693 [
694 environment("desktop", summary([bucket({ sourcePath: windows.homePath })], [windows])),
695 environment("standalone", summary([bucket({ sourcePath: legacy.homePath })], [legacy])),
696 ],
697 USAGE_CONTRACT_VERSION,
698 );
699​
700 expect(merged.costUsd).toBe(10);
701 expect(merged.sessions).toBe(142);
702 expect(merged.duplicateSources).toHaveLength(1);
703 });
704​
705 it("counts it once when two desktop installs with different scopes read it", () => {
706 const merged = mergeUsage(
707 [
708 environment("packaged", summary([bucket({ sourcePath: windows.homePath })], [windows])),
709 environment(
710 "dev-build",
711 summary(
712 [bucket({ sourcePath: windows.homePath })],
713 [{ ...windows, physicalSourceId: "physical-other-scope" }],
714 ),
715 ),
716 ],
717 USAGE_CONTRACT_VERSION,
718 );
719​
720 expect(merged.costUsd).toBe(10);
721 expect(merged.sessions).toBe(142);
722 expect(merged.duplicateSources).toHaveLength(1);
723 });
724 });
725​
726 it("totals sessions from per-directory distinct counts, not per-bucket sums", () => {
727 const merged = mergeUsage(
728 [
729 environment(
730 "env-a",
731 summary(
732 [bucket({ day: "2026-08-06" as UsageDay }), bucket({ day: "2026-08-07" as UsageDay })],
733 [
734 {
735 provider: "claude",
736 hostId: "mac",
737 homePath: "/a/.claude",
738 distinctSessions: 1,
739 },
740 ],
741 ),
742 ),
743 ],
744 USAGE_CONTRACT_VERSION,
745 );
746​
747 expect(merged.sessions).toBe(1);
748 expect(merged.providers[0]?.sessions).toBe(1);
749 });
750});