-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathsingle-open-invariant.spec.ts
More file actions
256 lines (233 loc) · 8.47 KB
/
Copy pathsingle-open-invariant.spec.ts
File metadata and controls
256 lines (233 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// npx vitest run __tests__/single-open-invariant.spec.ts
import { describe, it, expect, vi, beforeEach } from "vitest"
import { type OutputChannel } from "vscode"
import { ClineProvider } from "../core/webview/ClineProvider"
import { TaskRegistry } from "../core/task/TaskRegistry"
import { TaskScheduler } from "../core/task/TaskScheduler"
import { type Task } from "../core/task/Task"
import { API } from "../extension/api"
import * as ProfileValidatorMod from "../shared/ProfileValidator"
type PrivateClineProviderMethods = {
createTask: (
this: unknown,
text?: string,
images?: string[],
parentTask?: Task,
options?: Parameters<ClineProvider["createTask"]>[3],
) => ReturnType<ClineProvider["createTask"]>
createTaskWithHistoryItem: (
this: unknown,
...args: Parameters<ClineProvider["createTaskWithHistoryItem"]>
) => ReturnType<ClineProvider["createTaskWithHistoryItem"]>
evictCurrentTask: (this: unknown) => ReturnType<ClineProvider["evictCurrentTask"]>
}
const privateClineProvider = ClineProvider.prototype as unknown as PrivateClineProviderMethods
// Mock Task class used by ClineProvider to avoid heavy startup
vi.mock("../core/task/Task", () => {
class TaskStub {
public taskId: string
public instanceId = "inst"
public parentTask?: unknown
public apiConfiguration: unknown
public rootTask?: unknown
constructor(opts: {
historyItem?: { id: string }
parentTask?: unknown
apiConfiguration?: unknown
onCreated?: (t: TaskStub) => void
}) {
this.taskId = opts.historyItem?.id ?? `task-${Math.random().toString(36).slice(2, 8)}`
this.parentTask = opts.parentTask
this.apiConfiguration = opts.apiConfiguration ?? { apiProvider: "anthropic" }
opts.onCreated?.(this)
}
start() {}
run() {
return Promise.resolve()
}
on() {}
off() {}
emit() {}
}
return { Task: TaskStub }
})
describe("Single-open-task invariant", () => {
beforeEach(() => {
vi.restoreAllMocks()
})
it("User-initiated create: closes existing before opening new", async () => {
// Allow profile
vi.spyOn(ProfileValidatorMod.ProfileValidator, "isProfileAllowed").mockReturnValue(true)
const removeClineFromStack = vi.fn().mockResolvedValue(undefined)
const addClineToStack = vi.fn().mockResolvedValue(undefined)
const existingTask = { taskId: "existing-1", abort: false, abandoned: false }
const registry = new TaskRegistry()
registry.push(existingTask as unknown as Task)
const provider = {
taskRegistry: registry,
taskScheduler: new TaskScheduler(),
getCurrentTask: vi.fn(() => existingTask),
taskHistoryStore: { get: vi.fn(() => undefined) },
markDelegatedChildInterrupted: vi.fn().mockResolvedValue(undefined),
get evictCurrentTask() {
return privateClineProvider.evictCurrentTask.bind(this)
},
setValues: vi.fn(),
getState: vi.fn().mockResolvedValue({
apiConfiguration: { apiProvider: "anthropic", consecutiveMistakeLimit: 0 },
organizationAllowList: "*",
enableCheckpoints: true,
checkpointTimeout: 60,
cloudUserInfo: null,
}),
removeClineFromStack,
addClineToStack,
setProviderProfile: vi.fn(),
log: vi.fn(),
getStateToPostToWebview: vi.fn(),
providerSettingsManager: { getModeConfigId: vi.fn(), listConfig: vi.fn() },
customModesManager: { getCustomModes: vi.fn().mockResolvedValue([]) },
taskCreationCallback: vi.fn(),
contextProxy: {
extensionUri: {},
setValue: vi.fn(),
getValue: vi.fn(),
setProviderSettings: vi.fn(),
getProviderSettings: vi.fn(() => ({})),
},
} as unknown as ClineProvider
await privateClineProvider.createTask.call(provider, "New task")
expect(removeClineFromStack).toHaveBeenCalledTimes(1)
expect(addClineToStack).toHaveBeenCalledTimes(1)
})
it("Subtask create: keeps existing task open when parentTask is provided", async () => {
vi.spyOn(ProfileValidatorMod.ProfileValidator, "isProfileAllowed").mockReturnValue(true)
const removeClineFromStack = vi.fn().mockResolvedValue(undefined)
const addClineToStack = vi.fn().mockResolvedValue(undefined)
const parentTask = { taskId: "parent-1", abort: false, abandoned: false }
const registry2 = new TaskRegistry()
registry2.push(parentTask as unknown as Task)
const provider = {
taskRegistry: registry2,
taskScheduler: new TaskScheduler(),
setValues: vi.fn(),
getState: vi.fn().mockResolvedValue({
apiConfiguration: { apiProvider: "anthropic", consecutiveMistakeLimit: 0 },
organizationAllowList: "*",
enableCheckpoints: true,
checkpointTimeout: 60,
cloudUserInfo: null,
}),
removeClineFromStack,
addClineToStack,
setProviderProfile: vi.fn(),
log: vi.fn(),
getStateToPostToWebview: vi.fn(),
providerSettingsManager: { getModeConfigId: vi.fn(), listConfig: vi.fn() },
customModesManager: { getCustomModes: vi.fn().mockResolvedValue([]) },
taskCreationCallback: vi.fn(),
contextProxy: {
extensionUri: {},
setValue: vi.fn(),
getValue: vi.fn(),
setProviderSettings: vi.fn(),
getProviderSettings: vi.fn(() => ({})),
},
} as unknown as ClineProvider
await privateClineProvider.createTask.call(provider, "Subtask", undefined, parentTask as unknown as Task)
expect(removeClineFromStack).not.toHaveBeenCalled()
expect(addClineToStack).toHaveBeenCalledTimes(1)
})
it("History resume path always closes current before rehydration (non-rehydrating case)", async () => {
const removeClineFromStack = vi.fn().mockResolvedValue(undefined)
const addClineToStack = vi.fn().mockResolvedValue(undefined)
const updateGlobalState = vi.fn().mockResolvedValue(undefined)
const provider = {
getCurrentTask: vi.fn(() => undefined), // ensure not rehydrating
taskHistoryStore: { get: vi.fn(() => undefined) },
markDelegatedChildInterrupted: vi.fn().mockResolvedValue(undefined),
get evictCurrentTask() {
return privateClineProvider.evictCurrentTask.bind(this)
},
removeClineFromStack,
addClineToStack,
updateGlobalState,
log: vi.fn(),
customModesManager: { getCustomModes: vi.fn().mockResolvedValue([]) },
providerSettingsManager: {
getModeConfigId: vi.fn().mockResolvedValue(undefined),
listConfig: vi.fn().mockResolvedValue([]),
},
getState: vi.fn().mockResolvedValue({
apiConfiguration: { apiProvider: "anthropic", consecutiveMistakeLimit: 0 },
enableCheckpoints: true,
checkpointTimeout: 60,
experiments: {},
cloudUserInfo: null,
taskSyncEnabled: false,
}),
// Methods used by createTaskWithHistoryItem for pending edit cleanup
getPendingEditOperation: vi.fn().mockReturnValue(undefined),
clearPendingEditOperation: vi.fn(),
context: { extension: { packageJSON: {} }, globalStorageUri: { fsPath: "/tmp" } },
contextProxy: {
extensionUri: {},
getValue: vi.fn(),
setValue: vi.fn(),
setProviderSettings: vi.fn(),
getProviderSettings: vi.fn(() => ({})),
},
postStateToWebview: vi.fn(),
} as unknown as ClineProvider
const historyItem = {
id: "hist-1",
number: 1,
ts: Date.now(),
task: "Task",
tokensIn: 0,
tokensOut: 0,
totalCost: 0,
workspace: "/tmp",
}
const task = await privateClineProvider.createTaskWithHistoryItem.call(provider, historyItem)
expect(task).toBeTruthy()
expect(removeClineFromStack).toHaveBeenCalledTimes(1)
expect(addClineToStack).toHaveBeenCalledTimes(1)
})
it("IPC StartNewTask path closes current before new task", async () => {
const removeClineFromStack = vi.fn().mockResolvedValue(undefined)
const createTask = vi.fn().mockResolvedValue({ taskId: "ipc-1" })
const provider = {
context: {} as unknown,
getCurrentTask: vi.fn(() => undefined),
taskHistoryStore: { get: vi.fn(() => undefined) },
markDelegatedChildInterrupted: vi.fn().mockResolvedValue(undefined),
get evictCurrentTask() {
return privateClineProvider.evictCurrentTask.bind(this)
},
removeClineFromStack,
postStateToWebview: vi.fn(),
postMessageToWebview: vi.fn(),
createTask,
getValues: vi.fn(() => ({})),
providerSettingsManager: { saveConfig: vi.fn() },
on: vi.fn((ev: unknown, cb: unknown) => {
if (ev === "taskCreated") {
// no-op for this test
}
return provider
}),
} as unknown as ClineProvider
const output = { appendLine: vi.fn() } as unknown as OutputChannel
const api = new API(output, provider, undefined, false)
const taskId = await api.startNewTask({
configuration: {},
text: "hello",
images: undefined,
newTab: false,
})
expect(taskId).toBe("ipc-1")
expect(removeClineFromStack).toHaveBeenCalledTimes(1)
expect(createTask).toHaveBeenCalled()
})
})