Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import * as DateTime from "effect/DateTime";
3import { deriveLatestContextWindowSnapshot, formatContextWindowTokens } from "./contextWindow";
4
5describe("V2 context window presentation", () => {
6 it("uses retained compaction token data when available", () => {
7 const snapshot = deriveLatestContextWindowSnapshot([
8 {
9 item: {
10 id: "compaction-1" as never,
11 threadId: "thread-1" as never,
12 runId: null,
13 nodeId: null,
14 providerThreadId: null,
15 providerTurnId: null,
16 nativeItemRef: null,
17 parentItemId: null,
18 ordinal: 1,
19 status: "completed",
20 title: null,
21 startedAt: null,
22 completedAt: null,
23 updatedAt: DateTime.makeUnsafe("2026-06-20T00:00:00.000Z"),
24 type: "compaction",
25 driver: null,
26 beforeTokenCount: 10_000,
27 afterTokenCount: 2_000,
28 },
29 },
30 ]);
31 expect(snapshot?.usedTokens).toBe(2_000);
32 expect(snapshot?.totalProcessedTokens).toBe(10_000);
33 });
34
35 it("prefers current provider usage and preserves ACP cost", () => {
36 const snapshot = deriveLatestContextWindowSnapshot([], undefined, {
37 contextUsage: {
38 usedTokens: 2_500,
39 maxTokens: 10_000,
40 cost: { amount: 0.42, currency: "USD" },
41 },
42 updatedAt: DateTime.makeUnsafe("2026-08-23T00:00:00.000Z"),
43 });
44
45 expect(snapshot).toMatchObject({
46 usedTokens: 2_500,
47 maxTokens: 10_000,
48 remainingTokens: 7_500,
49 usedPercentage: 25,
50 cost: { amount: 0.42, currency: "USD" },
51 });
52 });
53
54 it("formats compact token values", () => {
55 expect(formatContextWindowTokens(1_500)).toBe("1.5k");
56 });
57});
58
59describe("live provider-turn usage (#8144)", () => {
60 it("prefers the provider's live report over compaction items", () => {
61 const snapshot = deriveLatestContextWindowSnapshot([], {
62 usedTokens: 42_000,
63 maxTokens: 200_000,
64 inputTokens: 40_000,
65 outputTokens: 2_000,
66 updatedAt: "2026-08-27T00:00:00.000Z",
67 });
68 expect(snapshot).not.toBeNull();
69 expect(snapshot?.usedTokens).toBe(42_000);
70 expect(snapshot?.maxTokens).toBe(200_000);
71 expect(snapshot?.remainingTokens).toBe(158_000);
72 expect(snapshot?.usedPercentage).toBe(21);
73 });
74
75 it("handles a report without a known context window", () => {
76 const snapshot = deriveLatestContextWindowSnapshot([], {
77 usedTokens: 42_000,
78 updatedAt: "2026-08-27T00:00:00.000Z",
79 });
80 expect(snapshot?.maxTokens).toBeNull();
81 expect(snapshot?.usedPercentage).toBeNull();
82 });
83});