| 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 preferred"); |
| 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 | eq(sharedContextPhysicalRemaining({ windowMode: "shared", physicalRemaining: -12 }), 0, "shared remaining is clamped"); |
| 47 | eq(sharedContextPhysicalRemaining({ windowMode: "unknown", physicalRemaining: 99 }), undefined, "unknown provider has no asserted physical remainder"); |
| 48 | eq(showsSharedContextOverflowRisk({ windowMode: "shared", physicalRemaining: 10, clipped: true }), true, "shared clipping explains overflow risk"); |
| 49 | eq(showsSharedContextOverflowRisk({ windowMode: "shared", physicalRemaining: 0 }), true, "exhausted shared window explains overflow risk"); |
| 50 | eq(showsSharedContextOverflowRisk({ windowMode: "unknown", physicalRemaining: -10 }), false, "unknown provider does not claim shared-window overflow"); |
| 51 | eq(showsSharedContextOverflowRisk({ windowMode: "independent", physicalRemaining: -10, clipped: true }), false, "independent provider does not claim shared-window overflow"); |
| 52 | |
| 53 | if (failed > 0) { |
| 54 | process.exit(1); |
| 55 | } |
| 56 | console.log(` ${passed} passed`); |
| 57 |