import * as assert from "assert"; import * as fs from "fs"; import * as path from "path"; import * as vscode from "vscode"; import type { CowritingApi } from "../../../src/extension"; const WS = process.env.E2E_WORKSPACE!; const settle = () => new Promise((r) => setTimeout(r, 400)); async function getApi(): Promise { const ext = vscode.extensions.getExtension("benstull.vscode-cowriting-plugin")!; const api = (await ext.activate()) as CowritingApi; assert.ok(api?.editFlow && api?.proposalController, "exports controllers"); return api; } async function freshDoc(rel: string, body: string): Promise<{ doc: vscode.TextDocument; key: string }> { const abs = path.join(WS, rel); fs.mkdirSync(path.dirname(abs), { recursive: true }); fs.writeFileSync(abs, body, "utf8"); const uri = vscode.Uri.file(abs); const doc = await vscode.workspace.openTextDocument(uri); await vscode.window.showTextDocument(doc); // INV-10: proposal rendering (F12 optimistic-apply, driven by // onDidChangeProposals) is gated on coediting (Task 4). await vscode.commands.executeCommand("cowriting.coeditDocument"); await settle(); return { doc, key: uri.toString() }; } // #46 SLICE-3 (accept): one "Accept all" gesture applies every pending proposal // on the current document through the existing F4 accept seam (INV-42) — batched, // re-anchor-safe (descending), orphan-skip + report. Host E2E, no LLM. suite("F12 SLICE-3 — accept-all (#46, INV-42)", () => { // PUC-6: N pending → all applied, text replaced, proposals cleared. test("acceptAll applies every pending proposal and reconstructs the document", async () => { const original = "# All\n\nFirst para alpha.\n\nSecond para beta.\n\nThird para gamma.\n"; const rewrite = "# All\n\nFirst para ALPHA.\n\nSecond para BETA.\n\nThird para GAMMA.\n"; const { doc } = await freshDoc("docs/f12-all.md", original); const api = await getApi(); const editFlow = api.editFlow; editFlow.setEditTurnForTest(async () => ({ replacement: rewrite, model: "sonnet", sessionId: "e2e-f12-all" })); const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "uppercase the nouns"); await settle(); assert.strictEqual(ids.length, 3, "three changed blocks → three pending proposals"); // Route through the real "Keep all" command (Task 8, PUC-2) — the doc is the // active editor from freshDoc, matching the top-of-file CodeLens's own target. await vscode.commands.executeCommand("cowriting.acceptAllProposals"); await settle(); await settle(); assert.strictEqual(doc.getText(), rewrite, "accept-all reconstructs the intended document"); assert.strictEqual(api.proposalController.listProposals(doc).length, 0, "all proposals cleared"); }); // PUC-6 / INV-42: an orphaned proposal is skipped (not force-applied) and the // resolvable ones still apply — reported via the {applied, skipped} tally. test("acceptAllProposals skips an orphaned proposal and applies the rest (report)", async () => { const original = "# Mix\n\nKeep alpha here.\n\nKeep gamma here.\n"; const rewrite = "# Mix\n\nKeep ALPHA here.\n\nKeep GAMMA here.\n"; const { doc } = await freshDoc("docs/f12-orphan.md", original); const api = await getApi(); const editFlow = api.editFlow; editFlow.setEditTurnForTest(async () => ({ replacement: rewrite, model: "sonnet", sessionId: "e2e-f12-orphan" })); const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "uppercase"); await settle(); assert.strictEqual(ids.length, 2, "two pending proposals"); // Orphan the FIRST block's proposal by mangling its target text in the buffer. const para = "Keep alpha here."; const start = doc.getText().indexOf(para); const edit = new vscode.WorkspaceEdit(); edit.replace( doc.uri, new vscode.Range(doc.positionAt(start), doc.positionAt(start + para.length)), "Totally different first paragraph now.", ); assert.ok(await vscode.workspace.applyEdit(edit), "mangling edit applied"); await settle(); const { applied, skipped } = await api.proposalController.acceptAllProposals(doc); assert.strictEqual(applied, 1, "the still-resolvable proposal applied"); assert.strictEqual(skipped, 1, "the orphaned proposal was skipped, not mangled"); assert.ok(doc.getText().includes("Keep GAMMA here."), "the resolvable block landed"); assert.ok( doc.getText().includes("Totally different first paragraph now."), "the orphaned block kept the operator's text (never force-applied)", ); assert.strictEqual(api.proposalController.listProposals(doc).length, 1, "the orphaned proposal remains pending"); }); // A single pending proposal still applies through the batch path (the // top-of-file "Keep all" CodeLens is hidden < 2 pending, Task 8 PUC-2, but the // command/seam handle any count). test("acceptAllProposals with one pending proposal applies it", async () => { const { doc } = await freshDoc("docs/f12-one.md", "# One\n\nThe only paragraph here.\n"); const api = await getApi(); const editFlow = api.editFlow; editFlow.setEditTurnForTest(async () => ({ replacement: "# One\n\nThe ONLY paragraph here.\n", model: "sonnet", sessionId: "e2e-f12-one", })); const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "uppercase only"); await settle(); assert.strictEqual(ids.length, 1, "one pending proposal"); const { applied, skipped } = await api.proposalController.acceptAllProposals(doc); assert.strictEqual(applied, 1); assert.strictEqual(skipped, 0); assert.strictEqual(doc.getText(), "# One\n\nThe ONLY paragraph here.\n"); }); // The command is registered + palette-guarded on markdown. test("cowriting.acceptAllProposals is registered and palette-guarded on markdown", async () => { const all = await vscode.commands.getCommands(true); assert.ok(all.includes("cowriting.acceptAllProposals"), "command registered"); const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "../../../../package.json"), "utf8")); const entry = (pkg.contributes.menus.commandPalette as Array<{ command: string; when?: string }>).find( (m) => m.command === "cowriting.acceptAllProposals", ); assert.ok(entry, "has a commandPalette entry"); assert.match(entry!.when ?? "", /editorLangId == markdown/, "guarded on markdown"); }); });