Skip to content

Commit 04f6422

Browse files
committed
test(delegation): exercise cross-provider adapters
1 parent a8656a7 commit 04f6422

1 file changed

Lines changed: 140 additions & 32 deletions

File tree

‎src/__tests__/ClineProvider.history-resume-delegation.spec.ts‎

Lines changed: 140 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { describe, it, expect, vi, beforeEach } from "vitest"
44
import * as vscode from "vscode"
5-
import { RooCodeEventName } from "@roo-code/types"
5+
import { providerIdentifiers, RooCodeEventName } from "@roo-code/types"
66
import type { ClineMessage, HistoryItem } from "@roo-code/types"
77

88
import type { ApiMessage } from "../core/task-persistence"
@@ -92,6 +92,37 @@ function makeTaskHistoryStoreStub(
9292
}
9393
}
9494

95+
function makeStatefulTaskHistoryStore(...items: HistoryItem[]) {
96+
const itemMap = new Map(items.map((item) => [item.id, item]))
97+
98+
return {
99+
get: vi.fn((id: string) => itemMap.get(id)),
100+
invalidate: vi.fn().mockResolvedValue(undefined),
101+
atomicUpdatePair: vi.fn(
102+
async (
103+
firstId: string,
104+
secondId: string,
105+
firstUpdater: (item: HistoryItem) => HistoryItem,
106+
secondUpdater: (item: HistoryItem) => HistoryItem,
107+
) => {
108+
const first = itemMap.get(firstId)
109+
const second = itemMap.get(secondId)
110+
if (!first || !second) throw new Error(`Missing history item for atomic pair: ${firstId}, ${secondId}`)
111+
itemMap.set(firstId, firstUpdater(first))
112+
itemMap.set(secondId, secondUpdater(second))
113+
return [itemMap.get(firstId), itemMap.get(secondId)]
114+
},
115+
),
116+
atomicReadAndUpdate: vi.fn(async (id: string, updater: (item: HistoryItem) => HistoryItem) => {
117+
const item = itemMap.get(id)
118+
if (!item) throw new Error(`Missing history item: ${id}`)
119+
const updated = updater(item)
120+
itemMap.set(id, updated)
121+
return [updated]
122+
}),
123+
}
124+
}
125+
95126
describe("History resume delegation - parent metadata transitions", () => {
96127
beforeEach(() => {
97128
vi.clearAllMocks()
@@ -1110,12 +1141,16 @@ describe("History resume delegation - parent metadata transitions", () => {
11101141
expect(parentInstance.resumeAfterDelegation).not.toHaveBeenCalled()
11111142
})
11121143

1113-
it("releases another provider's parent transition after resume invocation", async () => {
1144+
it("serializes real cross-provider completion and redelegation through resume invocation", async () => {
11141145
let releaseResume!: () => void
11151146
const resumeBlocked = new Promise<void>((resolve) => {
11161147
releaseResume = resolve
11171148
})
1118-
const parentInstance = {
1149+
let releaseChildRun!: () => void
1150+
const childRunBlocked = new Promise<void>((resolve) => {
1151+
releaseChildRun = resolve
1152+
})
1153+
const parentInstanceA = {
11191154
taskId: "parent-shared-transition",
11201155
abort: false,
11211156
abandoned: false,
@@ -1124,64 +1159,137 @@ describe("History resume delegation - parent metadata transitions", () => {
11241159
overwriteApiConversationHistory: vi.fn().mockResolvedValue(undefined),
11251160
}
11261161
const parentItem = {
1127-
id: parentInstance.taskId,
1162+
id: parentInstanceA.taskId,
11281163
status: "delegated",
11291164
awaitingChildId: "child-c1",
1165+
delegatedToId: "child-c1",
11301166
childIds: ["child-c1"],
11311167
ts: 1,
11321168
task: "Parent",
11331169
tokensIn: 0,
11341170
tokensOut: 0,
11351171
totalCost: 0,
1136-
}
1137-
const taskHistoryStore = makeTaskHistoryStoreStub({ id: "child-c1", status: "active" }, parentItem)
1138-
let currentTask: object | undefined = { taskId: "child-c1" }
1172+
mode: "code",
1173+
} as HistoryItem
1174+
const taskHistoryStore = makeStatefulTaskHistoryStore(parentItem, {
1175+
id: "child-c1",
1176+
status: "active",
1177+
parentTaskId: parentItem.id,
1178+
} as HistoryItem)
1179+
let currentTaskA: object | undefined = { taskId: "child-c1" }
11391180
let runScheduledContinuation!: () => Promise<void>
1181+
let settleScheduledContinuation!: () => void
1182+
const scheduledContinuationSettled = new Promise<void>((resolve) => {
1183+
settleScheduledContinuation = resolve
1184+
})
1185+
let scheduledContinuation: Promise<void> | undefined
1186+
const emitA = vi.fn()
11401187
const providerA = makeProviderStub({
11411188
contextProxy: { globalStorageUri: { fsPath: "/tmp" } },
1142-
getTaskWithId: vi.fn().mockResolvedValue({ historyItem: parentItem }),
1143-
emit: vi.fn(),
1144-
getCurrentTask: vi.fn(() => currentTask),
1189+
getTaskWithId: vi.fn(async (id: string) => ({ historyItem: taskHistoryStore.get(id)! })),
1190+
emit: emitA,
1191+
getCurrentTask: vi.fn(() => currentTaskA),
11451192
removeClineFromStack: vi.fn(async () => {
1146-
currentTask = undefined
1193+
currentTaskA = undefined
11471194
}),
1148-
createTaskWithHistoryItem: vi.fn(async () => (currentTask = parentInstance)),
1195+
createTaskWithHistoryItem: vi.fn(async () => (currentTaskA = parentInstanceA)),
11491196
taskScheduler: {
11501197
schedule: vi.fn((_task, run) => {
1151-
runScheduledContinuation = run
1152-
return new Promise<void>(() => {})
1198+
runScheduledContinuation = () => (scheduledContinuation ??= run())
1199+
return scheduledContinuationSettled
11531200
}),
11541201
},
11551202
taskHistoryStore,
11561203
})
1157-
const providerB = makeProviderStub({ taskHistoryStore })
1158-
const commitC2 = vi.fn()
1204+
const parentInstanceB = {
1205+
taskId: parentItem.id,
1206+
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, anthropicApiKey: "task-local-key" },
1207+
getTaskMode: vi.fn().mockResolvedValue("code"),
1208+
getTaskApiConfigName: vi.fn().mockResolvedValue("task-local-profile"),
1209+
flushPendingToolResultsToHistory: vi.fn().mockResolvedValue(true),
1210+
retrySaveApiConversationHistory: vi.fn(),
1211+
}
1212+
const childC2 = {
1213+
taskId: "child-c2",
1214+
run: vi.fn(async () => {
1215+
expect(taskHistoryStore.get(parentItem.id)).toMatchObject({
1216+
status: "delegated",
1217+
awaitingChildId: "child-c2",
1218+
})
1219+
await childRunBlocked
1220+
}),
1221+
}
1222+
let currentTaskB: object | undefined = parentInstanceB
1223+
let childRun: Promise<void> | undefined
1224+
const removeParentB = vi.fn(async () => {
1225+
currentTaskB = undefined
1226+
})
1227+
const createChildC2 = vi.fn(async () => {
1228+
currentTaskB = childC2
1229+
return childC2
1230+
})
1231+
const providerB = makeProviderStub({
1232+
taskHistoryStore,
1233+
getCurrentTask: vi.fn(() => currentTaskB),
1234+
removeClineFromStack: removeParentB,
1235+
createTask: createChildC2,
1236+
taskScheduler: {
1237+
schedule: vi.fn((_task, run) => (childRun = run())),
1238+
},
1239+
emit: vi.fn(),
1240+
log: vi.fn(),
1241+
isViewLaunched: false,
1242+
})
11591243

11601244
vi.mocked(readTaskMessages).mockResolvedValue([])
11611245
vi.mocked(readApiMessages).mockResolvedValue([])
11621246
await ClineProvider.prototype.reopenParentFromDelegation.call(providerA, {
1163-
parentTaskId: parentInstance.taskId,
1247+
parentTaskId: parentItem.id,
11641248
childTaskId: "child-c1",
11651249
completionResultSummary: "C1 done",
11661250
})
1167-
const providerBTransition = (
1168-
ClineProvider.prototype as unknown as {
1169-
runDelegationTransition: (
1170-
this: ClineProvider,
1171-
parentTaskId: string,
1172-
fn: () => Promise<void>,
1173-
) => Promise<void>
1174-
}
1175-
).runDelegationTransition.call(providerB, parentInstance.taskId, async () => commitC2())
1251+
expect(taskHistoryStore.get("child-c1")).toMatchObject({ status: "completed" })
1252+
expect(taskHistoryStore.get(parentItem.id)).toMatchObject({
1253+
status: "active",
1254+
completedByChildId: "child-c1",
1255+
awaitingChildId: undefined,
1256+
})
1257+
1258+
const providerBTransition = ClineProvider.prototype.delegateParentAndOpenChild.call(providerB, {
1259+
parentTaskId: parentItem.id,
1260+
message: "Run C2",
1261+
initialTodos: [],
1262+
mode: "code",
1263+
})
11761264

11771265
await Promise.resolve()
1178-
expect(commitC2).not.toHaveBeenCalled()
1179-
const scheduledContinuation = runScheduledContinuation()
1180-
await vi.waitFor(() => expect(parentInstance.resumeAfterDelegation).toHaveBeenCalledTimes(1))
1181-
await vi.waitFor(() => expect(commitC2).toHaveBeenCalledTimes(1))
1266+
expect(removeParentB).not.toHaveBeenCalled()
1267+
expect(createChildC2).not.toHaveBeenCalled()
1268+
expect(taskHistoryStore.atomicReadAndUpdate).not.toHaveBeenCalled()
1269+
1270+
const continuationRun = runScheduledContinuation()
1271+
await vi.waitFor(() => expect(parentInstanceA.resumeAfterDelegation).toHaveBeenCalledTimes(1))
1272+
await expect(providerBTransition).resolves.toBe(childC2)
1273+
expect(removeParentB).toHaveBeenCalledTimes(1)
1274+
expect(createChildC2).toHaveBeenCalledTimes(1)
1275+
expect(taskHistoryStore.atomicReadAndUpdate).toHaveBeenCalledTimes(1)
1276+
expect(taskHistoryStore.get(parentItem.id)).toMatchObject({
1277+
status: "delegated",
1278+
awaitingChildId: "child-c2",
1279+
delegatedToId: "child-c2",
1280+
childIds: ["child-c1", "child-c2"],
1281+
})
1282+
await vi.waitFor(() => expect(childC2.run).toHaveBeenCalledTimes(1))
1283+
expect(parentInstanceA.resumeAfterDelegation).toHaveBeenCalledTimes(1)
1284+
expect(emitA).not.toHaveBeenCalledWith(RooCodeEventName.TaskDelegationResumed, parentItem.id, "child-c1")
1285+
1286+
releaseChildRun()
1287+
await childRun
11821288
releaseResume()
1183-
await scheduledContinuation
1184-
await providerBTransition
1289+
await continuationRun
1290+
settleScheduledContinuation()
1291+
await scheduledContinuationSettled
1292+
expect(emitA).toHaveBeenCalledWith(RooCodeEventName.TaskDelegationResumed, parentItem.id, "child-c1")
11851293
})
11861294

11871295
it("allows resumed work to re-enter the same parent transition queue", async () => {

0 commit comments

Comments
 (0)