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("formats compact token values", () => {
36 expect(formatContextWindowTokens(1_500)).toBe("1.5k");
37 });
38});
39
40describe("live provider-turn usage (#8144)", () => {
41 it("prefers the provider's live report over compaction items", () => {
42 const snapshot = deriveLatestContextWindowSnapshot([], {
43 usedTokens: 42_000,
44 maxTokens: 200_000,
45 inputTokens: 40_000,
46 outputTokens: 2_000,
47 updatedAt: "2026-08-27T00:00:00.000Z",
48 });
49 expect(snapshot).not.toBeNull();
50 expect(snapshot?.usedTokens).toBe(42_000);
51 expect(snapshot?.maxTokens).toBe(200_000);
52 expect(snapshot?.remainingTokens).toBe(158_000);
53 expect(snapshot?.usedPercentage).toBe(21);
54 });
55
56 it("handles a report without a known context window", () => {
57 const snapshot = deriveLatestContextWindowSnapshot([], {
58 usedTokens: 42_000,
59 updatedAt: "2026-08-27T00:00:00.000Z",
60 });
61 expect(snapshot?.maxTokens).toBeNull();
62 expect(snapshot?.usedPercentage).toBeNull();
63 });
64});