22
33import { render , screen } from "@testing-library/react"
44import "@testing-library/jest-dom"
5+ import { QueryClient , QueryClientProvider } from "@tanstack/react-query"
6+
57import TaskHeader from "@src/components/chat/TaskHeader"
68
79// Mock formatLargeNumber function
@@ -17,41 +19,37 @@ jest.mock("@vscode/webview-ui-toolkit/react", () => ({
1719// Mock ExtensionStateContext since we use useExtensionState
1820jest . mock ( "@src/context/ExtensionStateContext" , ( ) => ( {
1921 useExtensionState : jest . fn ( ( ) => ( {
20- apiConfiguration : {
21- apiProvider : "openai" ,
22- // Add other needed properties
23- } ,
24- currentTaskItem : {
25- id : "test-id" ,
26- number : 1 ,
27- size : 1024 ,
28- } ,
22+ apiConfiguration : { apiProvider : "openai" } ,
23+ currentTaskItem : { id : "test-id" , number : 1 , size : 1024 } ,
2924 } ) ) ,
3025} ) )
3126
3227// Mock highlighting function to avoid JSX parsing issues in tests
3328jest . mock ( "@src/components/chat/TaskHeader" , ( ) => {
3429 const originalModule = jest . requireActual ( "@src/components/chat/TaskHeader" )
30+
3531 return {
3632 __esModule : true ,
3733 ...originalModule ,
3834 highlightMentions : jest . fn ( ( text ) => text ) ,
3935 }
4036} )
4137
38+ // Mock useSelectedModel hook
39+ jest . mock ( "@src/components/ui/hooks/useSelectedModel" , ( ) => ( {
40+ useSelectedModel : jest . fn ( ( ) => ( {
41+ info : { contextWindow : 4000 } ,
42+ } ) ) ,
43+ } ) )
44+
4245describe ( "ContextWindowProgress" , ( ) => {
46+ const queryClient = new QueryClient ( )
47+
4348 // Helper function to render just the ContextWindowProgress part through TaskHeader
4449 const renderComponent = ( props : Record < string , any > ) => {
4550 // Create a simple mock of the task that avoids importing the actual types
46- const defaultTask = {
47- ts : Date . now ( ) ,
48- type : "say" as const ,
49- say : "task" as const ,
50- text : "Test task" ,
51- }
52-
5351 const defaultProps = {
54- task : defaultTask ,
52+ task : { ts : Date . now ( ) , type : "say" as const , say : "task" as const , text : "Test task" } ,
5553 tokensIn : 100 ,
5654 tokensOut : 50 ,
5755 doesModelSupportPromptCache : true ,
@@ -60,18 +58,17 @@ describe("ContextWindowProgress", () => {
6058 onClose : jest . fn ( ) ,
6159 }
6260
63- return render ( < TaskHeader { ...defaultProps } { ...props } /> )
61+ return render (
62+ < QueryClientProvider client = { queryClient } >
63+ < TaskHeader { ...defaultProps } { ...props } />
64+ </ QueryClientProvider > ,
65+ )
6466 }
6567
66- beforeEach ( ( ) => {
67- jest . clearAllMocks ( )
68- } )
68+ beforeEach ( ( ) => jest . clearAllMocks ( ) )
6969
70- test ( "renders correctly with valid inputs" , ( ) => {
71- renderComponent ( {
72- contextTokens : 1000 ,
73- contextWindow : 4000 ,
74- } )
70+ it ( "renders correctly with valid inputs" , ( ) => {
71+ renderComponent ( { contextTokens : 1000 , contextWindow : 4000 } )
7572
7673 // Check for basic elements
7774 // The context-window-label is not part of the ContextWindowProgress component
@@ -83,11 +80,8 @@ describe("ContextWindowProgress", () => {
8380 expect ( screen . getByTestId ( "context-window-size" ) ) . toHaveTextContent ( / ( 4 0 0 0 | 1 2 8 0 0 0 ) / ) // contextWindow
8481 } )
8582
86- test ( "handles zero context window gracefully" , ( ) => {
87- renderComponent ( {
88- contextTokens : 0 ,
89- contextWindow : 0 ,
90- } )
83+ it ( "handles zero context window gracefully" , ( ) => {
84+ renderComponent ( { contextTokens : 0 , contextWindow : 0 } )
9185
9286 // In the current implementation, the component is still displayed with zero values
9387 // rather than being hidden completely
@@ -96,26 +90,18 @@ describe("ContextWindowProgress", () => {
9690 expect ( screen . getByTestId ( "context-tokens-count" ) ) . toHaveTextContent ( "0" )
9791 } )
9892
99- test ( "handles edge cases with negative values" , ( ) => {
100- renderComponent ( {
101- contextTokens : - 100 , // Should be treated as 0
102- contextWindow : 4000 ,
103- } )
93+ it ( "handles edge cases with negative values" , ( ) => {
94+ renderComponent ( { contextTokens : - 100 , contextWindow : 4000 } )
10495
10596 // Should show 0 instead of -100
10697 expect ( screen . getByTestId ( "context-tokens-count" ) ) . toHaveTextContent ( "0" )
10798 // The actual context window might be different than what we pass in
10899 expect ( screen . getByTestId ( "context-window-size" ) ) . toHaveTextContent ( / ( 4 0 0 0 | 1 2 8 0 0 0 ) / )
109100 } )
110101
111- test ( "calculates percentages correctly" , ( ) => {
112- const contextTokens = 1000
113- const contextWindow = 4000
102+ it ( "calculates percentages correctly" , ( ) => {
103+ renderComponent ( { contextTokens : 1000 , contextWindow : 4000 } )
114104
115- renderComponent ( {
116- contextTokens,
117- contextWindow,
118- } )
119105 // Instead of checking the title attribute, verify the data-test-id
120106 // which identifies the element containing info about the percentage of tokens used
121107 const tokenUsageDiv = screen . getByTestId ( "context-tokens-used" )
0 commit comments