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?.diffViewController, "exports editFlow + diffView"); return api; } /** Create + open a fresh markdown doc under WS, returning the doc + its uri key. * Also enters coediting (INV-10/Task 2): the baseline is established only on * entry, not merely on open. */ 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); await vscode.commands.executeCommand("cowriting.coeditDocument"); await settle(); return { doc, key: uri.toString() }; } // F11/F12 host E2E (no LLM): EditFlow.runEditAndPropose is the turn→proposal(s) // cut shared by every Ask-Claude entry point (comments-first ask, editDocument, // editSelection). Originally exercised via the now-sunset review webview's // toolbar (Task 8 deleted that webview and its message-routing seam, // `receiveMessage`/`pinBaseline` webview-message coverage duplicates // diffView.test.ts / baselineRouter.test.ts's `markReviewed` tests) — these // tests drive `runEditAndPropose` directly, the same programmatic seam the // deleted toolbar itself called into. suite("F11/F12 — EditFlow document/selection edit turns (host E2E, no LLM)", () => { // SLICE-1 reachability: the orphaned pin command gets a real palette `when`. // Renamed cowriting.pinDiffBaseline → cowriting.markReviewed (Task 2, D14). test("markReviewed is reachable from the command palette (when: editorLangId == markdown)", async () => { 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.markReviewed", ); assert.ok(entry, "markReviewed has a commandPalette entry"); assert.notStrictEqual(entry!.when, "false", "it is no longer hidden (when:false)"); assert.match(entry!.when ?? "", /editorLangId == markdown/, "guarded on markdown"); }); // #47 (was INV-37): Edit Document now cuts at BLOCK granularity — two word // changes in ONE paragraph are ONE block proposal (INV-39 supersedes INV-37's // per-word cut). Full block coverage lives in f12Review.test.ts. test("runEditAndPropose(document) — two word edits in one paragraph → ONE block proposal (PUC-4, INV-39)", async () => { const { doc, key } = await freshDoc( "docs/f11doc.md", "# F11 doc\n\nThe quick brown fox jumps over the lazy dog.\n", ); const api = await getApi(); const editFlow = api.editFlow; // Stub the host edit turn (no LLM in CI): rewrite two distinct words. editFlow.setEditTurnForTest(async () => ({ replacement: "# F11 doc\n\nThe quick RED fox jumps over the lazy CAT.\n", model: "sonnet", sessionId: "e2e-f11-doc", })); const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "swap brown→RED and dog→CAT"); await settle(); assert.strictEqual(ids.length, 1, "two changed words in one block → ONE block proposal (INV-39)"); const views = api.proposalController.listProposals(doc); const view = views.find((v) => v.id === ids[0]); assert.ok(view, "the returned proposal id is a live pending proposal"); assert.strictEqual( view!.replacement, "The quick RED fox jumps over the lazy CAT.", "the block proposal carries the whole rewritten paragraph", ); // F12 (INV-48): EditorProposalController optimistically applies the proposed // text, so after settle the buffer has the replacement. The proposal is still // pending (not finalized) until Accept. assert.ok(doc.getText().includes("RED fox") && doc.getText().includes("lazy CAT"), "F12 optimistic-apply: proposed text in buffer"); void key; }); // SLICE-4: Edit Selection → one single-range proposal over the resolved block-union. test("runEditAndPropose(range) with a stubbed turn → exactly one proposal over the resolved range (PUC-3, INV-37)", async () => { const body = "# F11 sel\n\nThe target paragraph Claude will rewrite.\n\nAnother untouched paragraph.\n"; const { doc, key } = await freshDoc("docs/f11sel.md", body); const api = await getApi(); const editFlow = api.editFlow; const target = "The target paragraph Claude will rewrite."; const start = doc.getText().indexOf(target); const end = start + target.length; editFlow.setEditTurnForTest(async (_instruction, text) => { assert.strictEqual(text, target, "the turn receives exactly the selected source range"); return { replacement: "The REWRITTEN paragraph from Claude.", model: "sonnet", sessionId: "e2e-f11-sel" }; }); const ids = await editFlow.runEditAndPropose(doc, { kind: "range", start, end }, "rewrite this paragraph"); await settle(); assert.strictEqual(ids.length, 1, "a selection yields exactly one proposal"); const views = api.proposalController.listProposals(doc); const view = views.find((v) => v.id === ids[0]); assert.ok(view, "the proposal is live"); assert.strictEqual(view!.replacement, "The REWRITTEN paragraph from Claude.", "carries the turn replacement"); assert.strictEqual(view!.replaced, target, "replaces exactly the selected range"); // F12 (INV-48): the EditorProposalController optimistically applies, so the buffer // now has the replacement. `view.replaced` still records the original target text. assert.ok(doc.getText().includes("The REWRITTEN paragraph from Claude."), "F12 optimistic-apply: proposed text in buffer"); void key; }); // SLICE-4: a no-op turn (Claude returns the input unchanged) produces no proposal. test("runEditAndPropose(range) where Claude returns the selection unchanged → no proposal", async () => { const { doc } = await freshDoc("docs/f11noop.md", "# noop\n\nLeave me exactly as I am.\n"); const api = await getApi(); const editFlow = api.editFlow; const target = "Leave me exactly as I am."; const start = doc.getText().indexOf(target); editFlow.setEditTurnForTest(async (_i, text) => ({ replacement: text, model: "sonnet", sessionId: "e2e-noop" })); const ids = await editFlow.runEditAndPropose(doc, { kind: "range", start, end: start + target.length }, "no change"); assert.strictEqual(ids.length, 0, "an unchanged replacement proposes nothing"); }); // SLICE-3 (review follow-up): a document rewrite that INSERTS text → an // acceptable proposal (not a born-orphaned zero-width hunk), and accepting all // hunks of a multi-hunk rewrite lands the intended document. test("document rewrite with an insertion → acceptable proposals; accept-all reaches the rewrite", async () => { const original = "# F11 accept\n\nThe brown fox sleeps.\n"; const rewrite = "# F11 accept\n\nThe brown fox QUIETLY sleeps today.\n"; const { doc } = await freshDoc("docs/f11accept.md", original); const api = await getApi(); const editFlow = api.editFlow; editFlow.setEditTurnForTest(async () => ({ replacement: rewrite, model: "sonnet", sessionId: "e2e-accept" })); const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "expand the sentence"); await settle(); assert.ok(ids.length >= 1, "the rewrite produced at least one proposal"); // Every proposal must be acceptable (the insertion-anchoring fix): accept each. for (const id of ids) { const ok = await api.proposalController.acceptById("docs/f11accept.md", id); assert.ok(ok, `proposal ${id} is acceptable (not born-orphaned)`); await settle(); } assert.strictEqual(doc.getText(), rewrite, "accepting all hunks reconstructs the intended rewrite"); assert.strictEqual(api.proposalController.listProposals(doc).length, 0, "no proposals left pending"); }); // SLICE-3: the document-scoped command stays registered for #42 reuse (now behind // the unified `cowriting.edit`). editDocument is hidden from the palette; the // markdown-guarded user-facing palette command is `cowriting.edit`. test("cowriting.editDocument stays registered; cowriting.edit is the markdown-guarded palette command", async () => { const all = await vscode.commands.getCommands(true); assert.ok(all.includes("cowriting.editDocument"), "editDocument command registered"); assert.ok(all.includes("cowriting.edit"), "unified edit command registered"); const palette = JSON.parse(fs.readFileSync(path.join(__dirname, "../../../../package.json"), "utf8")).contributes .menus.commandPalette as Array<{ command: string; when?: string }>; // editDocument is hidden (routed via cowriting.edit). const docEntry = palette.find((m) => m.command === "cowriting.editDocument"); assert.strictEqual(docEntry?.when, "false", "editDocument hidden from the palette"); // cowriting.edit is the user-facing, markdown-guarded entry. const editEntry = palette.find((m) => m.command === "cowriting.edit"); assert.ok(editEntry, "cowriting.edit has a commandPalette entry"); assert.match(editEntry!.when ?? "", /editorLangId == markdown/, "guarded on markdown"); }); });