Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit 51e062c

Browse files
committed
test: improve percentage calculation test with proper mocks and assertions
1 parent d56b3da commit 51e062c

1 file changed

Lines changed: 49 additions & 4 deletions

File tree

webview-ui/src/components/chat/__tests__/TaskHeader.spec.tsx

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ vi.mock("@roo/array", () => ({
9191
},
9292
}))
9393

94+
// Create a variable to hold the mock model info for useSelectedModel
95+
let mockModelInfo: { contextWindow: number; maxTokens: number } | undefined = undefined
96+
97+
// Mock useSelectedModel hook
98+
vi.mock("@/components/ui/hooks/useSelectedModel", () => ({
99+
useSelectedModel: () => ({
100+
provider: "anthropic",
101+
id: "test-model",
102+
info: mockModelInfo,
103+
isLoading: false,
104+
isError: false,
105+
}),
106+
}))
107+
108+
// Mock getModelMaxOutputTokens from @roo/api
109+
let mockMaxOutputTokens = 0
110+
vi.mock("@roo/api", () => ({
111+
getModelMaxOutputTokens: () => mockMaxOutputTokens,
112+
}))
113+
94114
describe("TaskHeader", () => {
95115
const defaultProps: TaskHeaderProps = {
96116
task: { type: "say", ts: Date.now(), text: "Test task", images: [] },
@@ -409,6 +429,19 @@ describe("TaskHeader", () => {
409429
// This represents the percentage of AVAILABLE input space used,
410430
// not the percentage of the total context window.
411431

432+
beforeEach(() => {
433+
// Set up mock model with known contextWindow
434+
mockModelInfo = { contextWindow: 1000, maxTokens: 200 }
435+
// Set up mock for getModelMaxOutputTokens to return reservedForOutput
436+
mockMaxOutputTokens = 200
437+
})
438+
439+
afterEach(() => {
440+
// Reset mocks
441+
mockModelInfo = undefined
442+
mockMaxOutputTokens = 0
443+
})
444+
412445
it("should calculate percentage based on available input space, not total context window", () => {
413446
// With the formula: contextTokens / (contextWindow - reservedForOutput) * 100
414447
// If contextTokens = 200, contextWindow = 1000, reservedForOutput = 200
@@ -419,10 +452,22 @@ describe("TaskHeader", () => {
419452

420453
renderTaskHeader({ contextTokens: 200 })
421454

422-
// Look for the percentage text
423-
// The exact value depends on the mocked model info
424-
// Since we don't have a detailed mock, we just verify the component renders
425-
expect(screen.getByText("Test task")).toBeInTheDocument()
455+
// The percentage should be rendered in the collapsed header state
456+
// Verify that 25% is displayed (correct formula) and NOT 40% (old incorrect formula)
457+
expect(screen.getByText("25%")).toBeInTheDocument()
458+
expect(screen.queryByText("40%")).not.toBeInTheDocument()
459+
})
460+
461+
it("should handle edge case when available input space is zero", () => {
462+
// When contextWindow equals reservedForOutput, available space is 0
463+
// The percentage should be 0 to avoid division by zero
464+
mockModelInfo = { contextWindow: 200, maxTokens: 200 }
465+
mockMaxOutputTokens = 200
466+
467+
renderTaskHeader({ contextTokens: 100 })
468+
469+
// Should show 0% when available input space is 0
470+
expect(screen.getByText("0%")).toBeInTheDocument()
426471
})
427472
})
428473
})

0 commit comments

Comments
 (0)