|
| 1 | +import * as assert from "assert"; |
| 2 | +import * as vscode from "vscode"; |
| 3 | +import * as summaryPipeline from "../../semantic/summaryPipeline"; |
| 4 | +import { |
| 5 | + registerDiscoveredCommands, |
| 6 | + initAiSummaries, |
| 7 | + runSummarisation, |
| 8 | + syncAndSummarise, |
| 9 | + type SummaryDeps, |
| 10 | +} from "../../summaryOrchestration"; |
| 11 | +import { ok, err } from "../../models/Result"; |
| 12 | +import { setAiSummariesTemporarilyDisabledForTests } from "../../aiSummaryState"; |
| 13 | +import { createMockTaskItem } from "../helpers/helpers"; |
| 14 | +import type { CommandItem } from "../../models/TaskItem"; |
| 15 | +import type { Result } from "../../models/Result"; |
| 16 | + |
| 17 | +function createTaskList(): CommandItem[] { |
| 18 | + return [createMockTaskItem({ id: "summary-task", label: "Summary Task", command: "echo hi" })]; |
| 19 | +} |
| 20 | + |
| 21 | +interface TestHarness { |
| 22 | + deps: { |
| 23 | + workspaceRoot: string; |
| 24 | + treeProvider: { getAllTasks: () => CommandItem[]; refresh: () => Promise<void> }; |
| 25 | + quickTasksProvider: { updateTasks: (tasks: CommandItem[]) => void }; |
| 26 | + }; |
| 27 | + getRefreshCount: () => number; |
| 28 | + getUpdatedTasks: () => number; |
| 29 | +} |
| 30 | + |
| 31 | +function createDeps(tasks: CommandItem[] = createTaskList()): TestHarness { |
| 32 | + let refreshCount = 0; |
| 33 | + let updatedTasks = 0; |
| 34 | + return { |
| 35 | + deps: { |
| 36 | + workspaceRoot: "/tmp/workspace", |
| 37 | + treeProvider: { |
| 38 | + getAllTasks: () => tasks, |
| 39 | + refresh: async () => { |
| 40 | + refreshCount += 1; |
| 41 | + await Promise.resolve(); |
| 42 | + }, |
| 43 | + }, |
| 44 | + quickTasksProvider: { |
| 45 | + updateTasks: (nextTasks: CommandItem[]) => { |
| 46 | + updatedTasks = nextTasks.length; |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + getRefreshCount: () => refreshCount, |
| 51 | + getUpdatedTasks: () => updatedTasks, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +function toSummaryDeps(value: TestHarness["deps"]): SummaryDeps { |
| 56 | + return value as SummaryDeps; |
| 57 | +} |
| 58 | + |
| 59 | +function patchSummariseAllTasks(impl: typeof summaryPipeline.summariseAllTasks): { restore: () => void } { |
| 60 | + const original = summaryPipeline.summariseAllTasks; |
| 61 | + Object.defineProperty(summaryPipeline, "summariseAllTasks", { configurable: true, value: impl }); |
| 62 | + return { |
| 63 | + restore: () => { |
| 64 | + Object.defineProperty(summaryPipeline, "summariseAllTasks", { configurable: true, value: original }); |
| 65 | + }, |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +function patchRegisterAllCommands(impl: typeof summaryPipeline.registerAllCommands): { restore: () => void } { |
| 70 | + const original = summaryPipeline.registerAllCommands; |
| 71 | + Object.defineProperty(summaryPipeline, "registerAllCommands", { configurable: true, value: impl }); |
| 72 | + return { |
| 73 | + restore: () => { |
| 74 | + Object.defineProperty(summaryPipeline, "registerAllCommands", { configurable: true, value: original }); |
| 75 | + }, |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +function patchInfoMessages(): { messages: string[]; restore: () => void } { |
| 80 | + const messages: string[] = []; |
| 81 | + const original = vscode.window.showInformationMessage; |
| 82 | + Object.defineProperty(vscode.window, "showInformationMessage", { |
| 83 | + configurable: true, |
| 84 | + value: async (message: string) => { |
| 85 | + messages.push(message); |
| 86 | + return await Promise.resolve(undefined); |
| 87 | + }, |
| 88 | + }); |
| 89 | + return { |
| 90 | + messages, |
| 91 | + restore: () => { |
| 92 | + Object.defineProperty(vscode.window, "showInformationMessage", { configurable: true, value: original }); |
| 93 | + }, |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +function patchErrorMessages(): { messages: string[]; restore: () => void } { |
| 98 | + const messages: string[] = []; |
| 99 | + const original = vscode.window.showErrorMessage; |
| 100 | + Object.defineProperty(vscode.window, "showErrorMessage", { |
| 101 | + configurable: true, |
| 102 | + value: async (message: string) => { |
| 103 | + messages.push(message); |
| 104 | + return await Promise.resolve(undefined); |
| 105 | + }, |
| 106 | + }); |
| 107 | + return { |
| 108 | + messages, |
| 109 | + restore: () => { |
| 110 | + Object.defineProperty(vscode.window, "showErrorMessage", { configurable: true, value: original }); |
| 111 | + }, |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +function patchExecuteCommand(): { calls: { command: string; args: unknown[] }[]; restore: () => void } { |
| 116 | + const calls: { command: string; args: unknown[] }[] = []; |
| 117 | + const original = vscode.commands.executeCommand; |
| 118 | + Object.defineProperty(vscode.commands, "executeCommand", { |
| 119 | + configurable: true, |
| 120 | + value: async (command: string, ...args: unknown[]) => { |
| 121 | + calls.push({ command, args }); |
| 122 | + return await Promise.resolve(undefined); |
| 123 | + }, |
| 124 | + }); |
| 125 | + return { |
| 126 | + calls, |
| 127 | + restore: () => { |
| 128 | + Object.defineProperty(vscode.commands, "executeCommand", { configurable: true, value: original }); |
| 129 | + }, |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +suite("Summary Orchestration E2E Tests", () => { |
| 134 | + teardown(() => { |
| 135 | + setAiSummariesTemporarilyDisabledForTests(true); |
| 136 | + }); |
| 137 | + |
| 138 | + test("registerDiscoveredCommands skips pipeline when there are no tasks", async () => { |
| 139 | + let called = false; |
| 140 | + const registerPatch = patchRegisterAllCommands(async () => { |
| 141 | + called = true; |
| 142 | + await Promise.resolve(); |
| 143 | + return ok(0); |
| 144 | + }); |
| 145 | + try { |
| 146 | + await registerDiscoveredCommands(toSummaryDeps(createDeps([]).deps)); |
| 147 | + } finally { |
| 148 | + registerPatch.restore(); |
| 149 | + } |
| 150 | + assert.strictEqual(called, false, "No-task registration should not call the DB registration pipeline"); |
| 151 | + }); |
| 152 | + |
| 153 | + test("runSummarisation refreshes views and reports count when enabled", async () => { |
| 154 | + setAiSummariesTemporarilyDisabledForTests(false); |
| 155 | + const summaryPatch = patchSummariseAllTasks(async () => { |
| 156 | + await Promise.resolve(); |
| 157 | + return ok(2); |
| 158 | + }); |
| 159 | + const infoPatch = patchInfoMessages(); |
| 160 | + const harness = createDeps(); |
| 161 | + try { |
| 162 | + await runSummarisation({ ...toSummaryDeps(harness.deps), modelSelectionMode: "automatic" }); |
| 163 | + } finally { |
| 164 | + infoPatch.restore(); |
| 165 | + summaryPatch.restore(); |
| 166 | + } |
| 167 | + assert.strictEqual(harness.getRefreshCount(), 1, "Successful summarisation should refresh the tree once"); |
| 168 | + assert.strictEqual(harness.getUpdatedTasks(), 1, "Successful summarisation should refresh quick tasks from the tree"); |
| 169 | + assert.ok( |
| 170 | + infoPatch.messages.includes("CommandTree: Summarised 2 commands"), |
| 171 | + "Successful summarisation should report the summarised command count", |
| 172 | + ); |
| 173 | + }); |
| 174 | + |
| 175 | + test("runSummarisation shows an error when the summary pipeline fails", async () => { |
| 176 | + setAiSummariesTemporarilyDisabledForTests(false); |
| 177 | + const summaryPatch = patchSummariseAllTasks(async (): Promise<Result<number, string>> => { |
| 178 | + await Promise.resolve(); |
| 179 | + return err("boom"); |
| 180 | + }); |
| 181 | + const errorPatch = patchErrorMessages(); |
| 182 | + try { |
| 183 | + await runSummarisation({ ...toSummaryDeps(createDeps().deps), modelSelectionMode: "automatic" }); |
| 184 | + } finally { |
| 185 | + errorPatch.restore(); |
| 186 | + summaryPatch.restore(); |
| 187 | + } |
| 188 | + assert.ok( |
| 189 | + errorPatch.messages.includes("CommandTree: Summary failed — boom"), |
| 190 | + "Failed summarisation should surface the pipeline error to the user", |
| 191 | + ); |
| 192 | + }); |
| 193 | + |
| 194 | + test("syncAndSummarise registers commands and summarises when enabled", async () => { |
| 195 | + setAiSummariesTemporarilyDisabledForTests(false); |
| 196 | + let registered = 0; |
| 197 | + let summarised = 0; |
| 198 | + const registerPatch = patchRegisterAllCommands(async () => { |
| 199 | + registered += 1; |
| 200 | + await Promise.resolve(); |
| 201 | + return ok(1); |
| 202 | + }); |
| 203 | + const summaryPatch = patchSummariseAllTasks(async () => { |
| 204 | + summarised += 1; |
| 205 | + await Promise.resolve(); |
| 206 | + return ok(0); |
| 207 | + }); |
| 208 | + const infoPatch = patchInfoMessages(); |
| 209 | + const harness = createDeps(); |
| 210 | + try { |
| 211 | + await syncAndSummarise(toSummaryDeps(harness.deps)); |
| 212 | + } finally { |
| 213 | + infoPatch.restore(); |
| 214 | + summaryPatch.restore(); |
| 215 | + registerPatch.restore(); |
| 216 | + } |
| 217 | + assert.strictEqual(harness.getRefreshCount(), 1, "syncAndSummarise should refresh the tree before syncing"); |
| 218 | + assert.strictEqual(registered, 1, "syncAndSummarise should register discovered commands"); |
| 219 | + assert.strictEqual(summarised, 1, "syncAndSummarise should trigger summarisation when enabled"); |
| 220 | + }); |
| 221 | + |
| 222 | + test("initAiSummaries sets the disabled context without starting summarisation", () => { |
| 223 | + const executePatch = patchExecuteCommand(); |
| 224 | + try { |
| 225 | + initAiSummaries(toSummaryDeps(createDeps().deps)); |
| 226 | + } finally { |
| 227 | + executePatch.restore(); |
| 228 | + } |
| 229 | + assert.deepStrictEqual( |
| 230 | + executePatch.calls, |
| 231 | + [{ command: "setContext", args: ["commandtree.aiSummariesEnabled", false] }], |
| 232 | + "Disabled init must only set the false context flag", |
| 233 | + ); |
| 234 | + }); |
| 235 | +}); |
0 commit comments