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

ContextWindowMeter.logic.test.ts

159 lines4.6 KB

1import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { deriveProviderInstanceEntries } from "../../providerInstances";
4import {
5 hasAvailableCompactionProvider,
6 hasDismissedResumeCompaction,
7 resolveContextWindowModelDisplayName,
8 shouldReserveContextWindowMeter,
9} from "./ContextWindowMeter.logic";
10​
11function claudeProvider(input: {
12 instanceId: string;
13 continuationGroupKey: string;
14 enabled?: boolean;
15}): ServerProvider {
16 return {
17 instanceId: ProviderInstanceId.make(input.instanceId),
18 driver: ProviderDriverKind.make("claudeAgent"),
19 continuation: { groupKey: input.continuationGroupKey },
20 enabled: input.enabled ?? true,
21 installed: true,
22 version: null,
23 status: "ready",
24 auth: { status: "authenticated" },
25 checkedAt: "2026-08-24T12:00:00.000Z",
26 models: [],
27 slashCommands: [{ name: "compact", description: "" }],
28 skills: [],
29 };
31​
32describe("hasAvailableCompactionProvider", () => {
33 const originalInstanceId = ProviderInstanceId.make("claude_original");
34​
35 it("rejects a fallback in a different locked continuation group", () => {
36 const providers = deriveProviderInstanceEntries([
37 claudeProvider({
38 instanceId: originalInstanceId,
39 continuationGroupKey: "claude:home:/original",
40 enabled: false,
41 }),
42 claudeProvider({
43 instanceId: "claude_other",
44 continuationGroupKey: "claude:home:/other",
45 }),
46 ]);
47​
48 expect(
49 hasAvailableCompactionProvider({
50 providers,
51 driverKind: ProviderDriverKind.make("claudeAgent"),
52 instanceId: originalInstanceId,
53 lockedInstanceId: originalInstanceId,
54 }),
55 ).toBe(false);
56 });
57​
58 it("accepts an enabled fallback in the locked continuation group", () => {
59 const providers = deriveProviderInstanceEntries([
60 claudeProvider({
61 instanceId: originalInstanceId,
62 continuationGroupKey: "claude:home:/original",
63 enabled: false,
64 }),
65 claudeProvider({
66 instanceId: "claude_fallback",
67 continuationGroupKey: "claude:home:/original",
68 }),
69 ]);
70​
71 expect(
72 hasAvailableCompactionProvider({
73 providers,
74 driverKind: ProviderDriverKind.make("claudeAgent"),
75 instanceId: originalInstanceId,
76 lockedInstanceId: originalInstanceId,
77 }),
78 ).toBe(true);
79 });
80});
81​
82describe("resolveContextWindowModelDisplayName", () => {
83 it("uses the selected model from the exact provider instance", () => {
84 const primaryInstanceId = ProviderInstanceId.make("codex");
85 const selectedInstanceId = ProviderInstanceId.make("codex-work");
86 const modelOptionsByInstance = new Map([
87 [
88 primaryInstanceId,
89 [{ slug: "gpt-5.6-sol", name: "Primary profile model", shortName: "Primary" }],
90 ],
91 [selectedInstanceId, [{ slug: "gpt-5.6-sol", name: "GPT-5.6 Sol", shortName: "5.6 Sol" }]],
92 ]);
93​
94 expect(
95 resolveContextWindowModelDisplayName(
96 {
97 instanceId: selectedInstanceId,
98 model: "gpt-5.6-sol",
99 },
100 modelOptionsByInstance,
101 ),
102 ).toBe("GPT-5.6 Sol");
103 });
104});
105​
106describe("hasDismissedResumeCompaction", () => {
107 it("recognizes the native resume dialog's permanent dismissal", () => {
108 expect(
109 hasDismissedResumeCompaction([
110 {
111 kind: "user-input.resolved",
112 payload: {
113 answers: {
114 "This session is 2h 0m old and uses 250,000 tokens. Compact it before continuing?":
115 "Don't ask again",
116 },
117 },
118 },
119 ]),
120 ).toBe(true);
121 });
122​
123 it("ignores unrelated questions that end with Claude's compaction prompt", () => {
124 expect(
125 hasDismissedResumeCompaction([
126 {
127 kind: "user-input.resolved",
128 payload: {
129 answers: {
130 "The build cache is large. Compact it before continuing?": "Don't ask again",
131 },
132 },
133 },
134 ]),
135 ).toBe(false);
136 });
137});
138​
139describe("shouldReserveContextWindowMeter", () => {
140 const loadingStartedThread = {
141 meterEnabled: true,
142 detailLoading: true,
143 threadStarted: true,
144 providerReportsContextWindow: true,
145 };
146​
147 it("holds the meter's slot while a started thread's detail loads", () => {
148 expect(shouldReserveContextWindowMeter(loadingStartedThread)).toBe(true);
149 });
150​
151 it("reserves while the thread's provider is not in the catalog yet", () => {
152 expect(
153 shouldReserveContextWindowMeter({
154 ...loadingStartedThread,
155 providerReportsContextWindow: null,
156 }),
157 ).toBe(true);
158 });
159});