@@ -7,7 +7,13 @@ import * as NodePath from "node:path"; |
7 | 7 | import { assert, describe, it } from "@effect/vitest"; |
8 | 8 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
9 | 9 | import { HostProcessEnvironment } from "@t3tools/shared/hostProcess"; |
10 | | -import { UsageDay, type UsageSummaryInput } from "@t3tools/contracts"; |
| 10 | +import { |
| 11 | + EnvironmentId, |
| 12 | + USAGE_CONTRACT_VERSION, |
| 13 | + UsageDay, |
| 14 | + type UsageSummaryInput, |
| 15 | +} from "@t3tools/contracts"; |
| 16 | +import { mergeUsage } from "@t3tools/shared/usageMerge"; |
11 | 17 | import * as Duration from "effect/Duration"; |
12 | 18 | import * as Deferred from "effect/Deferred"; |
13 | 19 | import * as Effect from "effect/Effect"; |
@@ -98,6 +104,50 @@ function totalOutputTokens(summary: { buckets: readonly { totals: { outputTokens |
98 | 104 | } |
99 | 105 |
|
100 | 106 | describe("UsageService", () => { |
| 107 | + it.live("counts a transcript home once when another environment uses a directory alias", () => |
| 108 | + Effect.gen(function* () { |
| 109 | + const { transcript, settings, home } = yield* setup; |
| 110 | + yield* Effect.promise(() => NodeFSP.writeFile(transcript, claudeLine(1, 5))); |
| 111 | + const alias = NodePath.join(home, "claude-alias"); |
| 112 | + yield* Effect.promise(() => |
| 113 | + NodeFSP.symlink(settings.providers.claudeAgent.homePath, alias, "junction"), |
| 114 | + ); |
| 115 | + |
| 116 | + const direct = yield* UsageService.make.pipe( |
| 117 | + Effect.provide(serviceLayers({ prefix: "usage-direct-home", home, settings })), |
| 118 | + ); |
| 119 | + const linked = yield* UsageService.make.pipe( |
| 120 | + Effect.provide( |
| 121 | + serviceLayers({ |
| 122 | + prefix: "usage-linked-home", |
| 123 | + home, |
| 124 | + settings: { |
| 125 | + providers: { ...settings.providers, claudeAgent: { homePath: alias } }, |
| 126 | + }, |
| 127 | + }), |
| 128 | + ), |
| 129 | + ); |
| 130 | + const directSummary = yield* direct.readSummary(WINDOW); |
| 131 | + const linkedSummary = yield* linked.readSummary(WINDOW); |
| 132 | + const merged = mergeUsage( |
| 133 | + [ |
| 134 | + { environmentId: EnvironmentId.make("direct"), label: "Direct", summary: directSummary }, |
| 135 | + { environmentId: EnvironmentId.make("linked"), label: "Linked", summary: linkedSummary }, |
| 136 | + ], |
| 137 | + USAGE_CONTRACT_VERSION, |
| 138 | + ); |
| 139 | + |
| 140 | + assert.strictEqual(merged.outputTokens, 5); |
| 141 | + assert.strictEqual(merged.sessions, 1); |
| 142 | + assert.deepStrictEqual( |
| 143 | + directSummary.sources.find((source) => source.fingerprint.provider === "claude") |
| 144 | + ?.fingerprint, |
| 145 | + linkedSummary.sources.find((source) => source.fingerprint.provider === "claude") |
| 146 | + ?.fingerprint, |
| 147 | + ); |
| 148 | + }).pipe(Effect.scoped), |
| 149 | + ); |
| 150 | + |
101 | 151 | it.live("reprices unchanged transcripts when custom prices are added, edited, or removed", () => |
102 | 152 | Effect.gen(function* () { |
103 | 153 | const { transcript, settings, home } = yield* setup; |
|