Skip to content

Commit 5494896

Browse files
author
Eric Wheeler
committed
test: update UI tests after task history migration
- Removed pass-through state tests from ContextProxy that no longer apply - Updated ClineProvider tests to use file-based history instead of global state - Modified ChatTextArea tests to use useTaskSearch hook instead of taskHistory prop - Completely rewrote useTaskSearch tests to use message-based architecture - Updated other tests to remove taskHistory references from mock states Signed-off-by: Eric Wheeler <[email protected]> test: Fix ClineProvider test by mocking extension context and taskHistory This commit fixes the failing test 'correctly identifies subtask scenario for issue #4602' by: 1. Adding necessary Vitest imports 2. Mocking getExtensionContext to return a mock context with globalStorageUri 3. Mocking taskHistory module to prevent file system operations during tests Signed-off-by: Eric Wheeler <[email protected]>
1 parent 1f08c7f commit 5494896

File tree

8 files changed

+428
-227
lines changed

8 files changed

+428
-227
lines changed

src/core/config/__tests__/ContextProxy.spec.ts

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -102,41 +102,6 @@ describe("ContextProxy", () => {
102102
const result = proxy.getGlobalState("apiProvider", "deepseek")
103103
expect(result).toBe("deepseek")
104104
})
105-
106-
it("should bypass cache for pass-through state keys", async () => {
107-
// Setup mock return value
108-
mockGlobalState.get.mockReturnValue("pass-through-value")
109-
110-
// Use a pass-through key (taskHistory)
111-
const result = proxy.getGlobalState("taskHistory")
112-
113-
// Should get value directly from original context
114-
expect(result).toBe("pass-through-value")
115-
expect(mockGlobalState.get).toHaveBeenCalledWith("taskHistory")
116-
})
117-
118-
it("should respect default values for pass-through state keys", async () => {
119-
// Setup mock to return undefined
120-
mockGlobalState.get.mockReturnValue(undefined)
121-
122-
// Use a pass-through key with default value
123-
const historyItems = [
124-
{
125-
id: "1",
126-
number: 1,
127-
ts: 1,
128-
task: "test",
129-
tokensIn: 1,
130-
tokensOut: 1,
131-
totalCost: 1,
132-
},
133-
]
134-
135-
const result = proxy.getGlobalState("taskHistory", historyItems)
136-
137-
// Should return default value when original context returns undefined
138-
expect(result).toBe(historyItems)
139-
})
140105
})
141106

142107
describe("updateGlobalState", () => {
@@ -150,33 +115,6 @@ describe("ContextProxy", () => {
150115
const storedValue = await proxy.getGlobalState("apiProvider")
151116
expect(storedValue).toBe("deepseek")
152117
})
153-
154-
it("should bypass cache for pass-through state keys", async () => {
155-
const historyItems = [
156-
{
157-
id: "1",
158-
number: 1,
159-
ts: 1,
160-
task: "test",
161-
tokensIn: 1,
162-
tokensOut: 1,
163-
totalCost: 1,
164-
},
165-
]
166-
167-
await proxy.updateGlobalState("taskHistory", historyItems)
168-
169-
// Should update original context
170-
expect(mockGlobalState.update).toHaveBeenCalledWith("taskHistory", historyItems)
171-
172-
// Setup mock for subsequent get
173-
mockGlobalState.get.mockReturnValue(historyItems)
174-
175-
// Should get fresh value from original context
176-
const storedValue = proxy.getGlobalState("taskHistory")
177-
expect(storedValue).toBe(historyItems)
178-
expect(mockGlobalState.get).toHaveBeenCalledWith("taskHistory")
179-
})
180118
})
181119

182120
describe("getSecret", () => {

src/core/task/__tests__/Task.spec.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,22 +198,6 @@ describe("Cline", () => {
198198
mockExtensionContext = {
199199
globalState: {
200200
get: vi.fn().mockImplementation((key: keyof GlobalState) => {
201-
if (key === "taskHistory") {
202-
return [
203-
{
204-
id: "123",
205-
number: 0,
206-
ts: Date.now(),
207-
task: "historical task",
208-
tokensIn: 100,
209-
tokensOut: 200,
210-
cacheWrites: 0,
211-
cacheReads: 0,
212-
totalCost: 0.001,
213-
},
214-
]
215-
}
216-
217201
return undefined
218202
}),
219203
update: vi.fn().mockImplementation((_key, _value) => Promise.resolve()),

src/core/webview/__tests__/ClineProvider.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// npx vitest core/webview/__tests__/ClineProvider.spec.ts
22

3+
import { describe, test, expect, beforeEach, afterEach, afterAll, vi, it } from "vitest"
34
import Anthropic from "@anthropic-ai/sdk"
45
import * as vscode from "vscode"
56
import axios from "axios"
@@ -20,6 +21,20 @@ import { ClineProvider } from "../ClineProvider"
2021
// Mock setup must come before imports
2122
vi.mock("../../prompts/sections/custom-instructions")
2223

24+
// Mock extension context
25+
vi.mock("../../../extension", () => ({
26+
getExtensionContext: vi.fn().mockReturnValue({
27+
globalStorageUri: { fsPath: "/mock/storage/path" },
28+
}),
29+
}))
30+
31+
// Mock task history module
32+
vi.mock("../../task-persistence/taskHistory", () => ({
33+
getHistoryItem: vi.fn().mockResolvedValue(undefined),
34+
setHistoryItems: vi.fn().mockResolvedValue(undefined),
35+
deleteHistoryItem: vi.fn().mockResolvedValue(undefined),
36+
}))
37+
2338
vi.mock("vscode")
2439

2540
vi.mock("p-wait-for", () => ({
@@ -147,6 +162,7 @@ vi.mock("vscode", () => ({
147162
showInformationMessage: vi.fn(),
148163
showWarningMessage: vi.fn(),
149164
showErrorMessage: vi.fn(),
165+
createTextEditorDecorationType: vi.fn().mockReturnValue({}),
150166
},
151167
workspace: {
152168
getConfiguration: vi.fn().mockReturnValue({
@@ -500,7 +516,6 @@ describe("ClineProvider", () => {
500516
const mockState: ExtensionState = {
501517
version: "1.0.0",
502518
clineMessages: [],
503-
taskHistory: [],
504519
shouldShowAnnouncement: false,
505520
apiConfiguration: {
506521
apiProvider: "openrouter",
@@ -735,7 +750,7 @@ describe("ClineProvider", () => {
735750
expect(state).toHaveProperty("alwaysAllowWrite")
736751
expect(state).toHaveProperty("alwaysAllowExecute")
737752
expect(state).toHaveProperty("alwaysAllowBrowser")
738-
expect(state).toHaveProperty("taskHistory")
753+
// taskHistory has been deprecated and removed from the global state
739754
expect(state).toHaveProperty("soundEnabled")
740755
expect(state).toHaveProperty("ttsEnabled")
741756
expect(state).toHaveProperty("diffEnabled")

src/core/webview/__tests__/webviewMessageHandler.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@ vi.mock("vscode", () => ({
3939
window: {
4040
showInformationMessage: vi.fn(),
4141
showErrorMessage: vi.fn(),
42+
createTextEditorDecorationType: vi.fn(() => ({
43+
key: "mock-decoration-type",
44+
})),
4245
},
4346
workspace: {
4447
workspaceFolders: [{ uri: { fsPath: "/mock/workspace" } }],
4548
},
49+
CodeActionKind: {
50+
QuickFix: "QuickFix",
51+
RefactorRewrite: "RefactorRewrite",
52+
},
4653
}))
4754

4855
vi.mock("../../../i18n", () => ({

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { defaultModeSlug } from "@roo/modes"
55
import { useExtensionState } from "@src/context/ExtensionStateContext"
66
import { vscode } from "@src/utils/vscode"
77
import * as pathMentions from "@src/utils/path-mentions"
8+
import { useTaskSearch } from "@src/components/history/useTaskSearch"
89

910
import ChatTextArea from "../ChatTextArea"
1011

@@ -16,6 +17,12 @@ vi.mock("@src/utils/vscode", () => ({
1617

1718
vi.mock("@src/components/common/CodeBlock")
1819
vi.mock("@src/components/common/MarkdownBlock")
20+
vi.mock("@src/components/history/useTaskSearch", () => ({
21+
useTaskSearch: vi.fn().mockReturnValue({
22+
tasks: [],
23+
loading: false,
24+
}),
25+
}))
1926
vi.mock("@src/utils/path-mentions", () => ({
2027
convertToMentionPath: vi.fn((path, cwd) => {
2128
// Simple mock implementation that mimics the real function's behavior
@@ -674,19 +681,23 @@ describe("ChatTextArea", () => {
674681
})
675682

676683
it("should use task history (oldest first) when no conversation messages exist", () => {
677-
const mockTaskHistory = [
678-
{ task: "First task", workspace: "/test/workspace" },
679-
{ task: "Second task", workspace: "/test/workspace" },
680-
{ task: "Third task", workspace: "/test/workspace" },
684+
const mockTaskItems = [
685+
{ id: "1", task: "First task", workspace: "/test/workspace", ts: 1000 },
686+
{ id: "2", task: "Second task", workspace: "/test/workspace", ts: 2000 },
687+
{ id: "3", task: "Third task", workspace: "/test/workspace", ts: 3000 },
681688
]
682689

690+
// Mock useTaskSearch to return the task items
691+
vi.mocked(useTaskSearch).mockReturnValue({
692+
tasks: mockTaskItems,
693+
loading: false,
694+
} as any)
683695
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
684696
filePaths: [],
685697
openedTabs: [],
686698
apiConfiguration: {
687699
apiProvider: "anthropic",
688700
},
689-
taskHistory: mockTaskHistory,
690701
clineMessages: [], // No conversation messages
691702
cwd: "/test/workspace",
692703
})
@@ -714,16 +725,20 @@ describe("ChatTextArea", () => {
714725
)
715726

716727
// Start with task history
728+
// Mock useTaskSearch to return the task items
729+
vi.mocked(useTaskSearch).mockReturnValue({
730+
tasks: [
731+
{ id: "1", task: "Task 1", workspace: "/test/workspace", ts: 1000 },
732+
{ id: "2", task: "Task 2", workspace: "/test/workspace", ts: 2000 },
733+
],
734+
loading: false,
735+
} as any)
717736
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
718737
filePaths: [],
719738
openedTabs: [],
720739
apiConfiguration: {
721740
apiProvider: "anthropic",
722741
},
723-
taskHistory: [
724-
{ task: "Task 1", workspace: "/test/workspace" },
725-
{ task: "Task 2", workspace: "/test/workspace" },
726-
],
727742
clineMessages: [],
728743
cwd: "/test/workspace",
729744
})
@@ -737,6 +752,11 @@ describe("ChatTextArea", () => {
737752
expect(setInputValue).toHaveBeenCalledWith("Task 1")
738753

739754
// Switch to conversation messages
755+
// Reset the useTaskSearch mock
756+
vi.mocked(useTaskSearch).mockReturnValue({
757+
tasks: [],
758+
loading: false,
759+
} as any)
740760
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
741761
filePaths: [],
742762
openedTabs: [],

webview-ui/src/components/history/__tests__/HistoryPreview.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ describe("HistoryPreview", () => {
8888
})
8989

9090
it("renders up to 3 tasks when tasks are available", () => {
91+
// Only return the first 3 tasks since the component has a limit of 3
9192
mockUseTaskSearch.mockReturnValue({
92-
tasks: mockTasks,
93+
tasks: mockTasks.slice(0, 3),
9394
loading: false,
9495
searchQuery: "",
9596
setSearchQuery: vi.fn(),

0 commit comments

Comments
 (0)