返回 DeepSeek-Reasonix
context-budget-card.test.ts
根目录 / desktop / frontend / src / __tests__ / context-budget-card.test.ts
1 // Run: tsx src/__tests__/context-budget-card.test.ts
2
3 import {
4 contextBudgetRecoveryKey,
5 contextBudgetSourceKey,
6 resolveContextBudget,
7 sharedContextPhysicalRemaining,
8 showsSharedContextOverflowRisk,
9 } from "../components/ContextBudgetCard";
10 import type { ContextInfo, ContextPanelInfo } from "../lib/types";
11
12 let passed = 0;
13 let failed = 0;
14
15 function eq(a: unknown, b: unknown, label: string) {
16 if (JSON.stringify(a) === JSON.stringify(b)) {
17 process.stdout.write(` PASS ${label}\n`);
18 passed += 1;
19 } else {
20 process.stdout.write(` FAIL ${label}: expected ${JSON.stringify(b)}, got ${JSON.stringify(a)}\n`);
21 failed += 1;
22 }
23 }
24
25 console.log("\ncontext budget card");
26
27 eq(contextBudgetSourceKey("official"), "context.budgetSourceOfficial", "official source key");
28 eq(contextBudgetSourceKey("learned"), "context.budgetSourceLearned", "learned source key");
29 eq(contextBudgetSourceKey("nope"), "context.budgetSourceUnknown", "unknown source key");
30 eq(contextBudgetRecoveryKey("proactive_clip"), "context.budgetRecoveryClip", "clip recovery");
31 eq(contextBudgetRecoveryKey("none"), "", "none recovery is empty");
32
33 const fromPanel = resolveContextBudget(undefined, {
34 contextBudget: { source: "official", windowTokens: 1000 },
35 } as ContextPanelInfo);
36 eq(fromPanel?.source, "official", "panel budget is a fallback without live context");
37
38 const fromContext = resolveContextBudget({
39 used: 1,
40 window: 2,
41 sessionTokens: 3,
42 contextBudget: { source: "learned" },
43 } as ContextInfo, null);
44 eq(fromContext?.source, "learned", "context info budget is used when panel omits it");
45
46 const stalePanel = { contextBudget: { promptTokens: 271100 } } as ContextPanelInfo;
47 eq(resolveContextBudget({ used: 179300, window: 1000000, sessionTokens: 0,
48 contextBudget: { promptTokens: 179300 } }, stalePanel)?.promptTokens, 179300, "live budget wins over stale panel");
49 eq(resolveContextBudget({ used: 0, window: 256000, sessionTokens: 0 }, stalePanel), undefined,
50 "rebound runtime with no budget does not resurrect old request");
51 eq(resolveContextBudget({ used: 10, window: 1000, sessionTokens: 0,
52 maintenance: { contextBudget: { promptTokens: 10 } } } as ContextInfo, stalePanel)?.promptTokens,
53 10, "live maintenance budget wins over stale panel");
54
55 eq(sharedContextPhysicalRemaining({ windowMode: "shared", physicalRemaining: -12 }), 0, "shared remaining is clamped");
56 eq(sharedContextPhysicalRemaining({ windowMode: "unknown", physicalRemaining: 99 }), undefined, "unknown provider has no asserted physical remainder");
57 eq(showsSharedContextOverflowRisk({ windowMode: "shared", physicalRemaining: 10, clipped: true }), true, "shared clipping explains overflow risk");
58 eq(showsSharedContextOverflowRisk({ windowMode: "shared", physicalRemaining: 0 }), true, "exhausted shared window explains overflow risk");
59 eq(showsSharedContextOverflowRisk({ windowMode: "unknown", physicalRemaining: -10 }), false, "unknown provider does not claim shared-window overflow");
60 eq(showsSharedContextOverflowRisk({ windowMode: "independent", physicalRemaining: -10, clipped: true }), false, "independent provider does not claim shared-window overflow");
61
62 if (failed > 0) {
63 process.exit(1);
64 }
65 console.log(` ${passed} passed`);
66
66 lines TYPESCRIPT