From fa26ba842cf412811646c97c50e2a0934a73048f Mon Sep 17 00:00:00 2001 From: arookieofc <2128194521@qq.com> Date: Sun, 1 Nov 2026 11:16:44 +0800 Subject: [PATCH] fix(tests): update code action tests for the interactive apply path (#638) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit applyCodeAction now resolves the picked action and executes it (lspResolveCodeAction → lspExecuteCodeAction) instead of calling lspApplyCodeAction, but refactorThis.test.ts and runKind.test.ts still mocked and asserted the old command, so main's frontend tests failed (4 tests). The mocks now follow the new commands; safeDelete.test.ts's negative assertion still holds. Co-authored-by: a-rookie-of-C-language <186315048+a-rookie-of-C-language@users.noreply.github.com> --- src/editor/refactor/refactorThis.test.ts | 13 ++++++++----- src/editor/refactor/runKind.test.ts | 15 +++++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/editor/refactor/refactorThis.test.ts b/src/editor/refactor/refactorThis.test.ts index e3b624d19..f49a316f3 100644 --- a/src/editor/refactor/refactorThis.test.ts +++ b/src/editor/refactor/refactorThis.test.ts @@ -14,9 +14,11 @@ const lspCodeActionQuery = vi.fn(async (request: number, _doc: number, _range: u ); const lspPrepareRename = vi.fn(async () => prepare); let onApply: (() => void) | null = null; -const lspApplyCodeAction = vi.fn(async () => { +// applyCodeAction resolves the picked action, then executes it (interactive command support). +const lspResolveCodeAction = vi.fn(async (_doc: number, action: CodeAction) => ok(action)); +const lspExecuteCodeAction = vi.fn(async () => { onApply?.(); - return ok(null); + return ok([]); }); const lspCodeActions = vi.fn(async () => ok([])); const cancelRequest = vi.fn(async () => undefined); @@ -31,7 +33,8 @@ vi.mock("@/platform/ipc", async (orig) => { ...actual.commands, lspCodeActionQuery, lspPrepareRename, - lspApplyCodeAction, + lspResolveCodeAction, + lspExecuteCodeAction, lspCodeActions, cancelRequest, languages, @@ -185,7 +188,7 @@ describe("Refactor This", () => { const items = (useLspPopup.getState().popup as { items: { label: string; run: () => void }[] }).items; items.find((i) => i.label === "Inline…")!.run(); await new Promise((r) => setTimeout(r, 0)); - expect(lspApplyCodeAction).toHaveBeenCalledOnce(); + expect(lspExecuteCodeAction).toHaveBeenCalledOnce(); items.find((i) => i.label === "Rename…")!.run(); expect(rename).toHaveBeenCalledWith(v); items.find((i) => i.label === "Safe Delete…")!.run(); @@ -240,7 +243,7 @@ describe("Refactor menu actions", () => { it("apply the single matching action, or offer a choice", async () => { query = answer([action("Move to a new file", "refactor.move.newFile")]); await runRefactoring(editor(), "move"); - expect(lspApplyCodeAction).toHaveBeenCalledOnce(); + expect(lspExecuteCodeAction).toHaveBeenCalledOnce(); expect(lspCodeActionQuery.mock.calls[0]![3]).toEqual(["refactor.move"]); closePopup(); diff --git a/src/editor/refactor/runKind.test.ts b/src/editor/refactor/runKind.test.ts index ab1b993a8..cbeb77e69 100644 --- a/src/editor/refactor/runKind.test.ts +++ b/src/editor/refactor/runKind.test.ts @@ -5,11 +5,17 @@ import type { CodeAction } from "@/bindings"; let answer: () => unknown = () => ({ status: "ok", data: [] }); const lspCodeActions = vi.fn(async () => answer()); -const lspApplyCodeAction = vi.fn(async () => ({ status: "ok", data: null })); +// applyCodeAction resolves the picked action, then executes it (interactive command support). +const lspResolveCodeAction = vi.fn(async (_doc: number, action: CodeAction) => ({ status: "ok", data: action })); +const lspExecuteCodeAction = vi.fn(async () => ({ status: "ok", data: [] })); vi.mock("@/platform/ipc", async (orig) => { const actual = await orig(); - return { ...actual, isTauri: () => true, commands: { ...actual.commands, lspCodeActions, lspApplyCodeAction } }; + return { + ...actual, + isTauri: () => true, + commands: { ...actual.commands, lspCodeActions, lspResolveCodeAction, lspExecuteCodeAction }, + }; }); const showHint = vi.fn(); vi.mock("@/editor/lsp/hint", () => ({ showHint })); @@ -32,7 +38,8 @@ let view: EditorView | null = null; afterEach(() => { view?.destroy(); showHint.mockClear(); - lspApplyCodeAction.mockClear(); + lspResolveCodeAction.mockClear(); + lspExecuteCodeAction.mockClear(); }); function run(fallback: (reason: "notReady" | "error" | "empty") => Promise) { @@ -56,7 +63,7 @@ describe("runKindAction fallback", () => { const fallback = vi.fn(async () => true); await run(fallback); expect(fallback).not.toHaveBeenCalled(); - expect(lspApplyCodeAction).toHaveBeenCalledOnce(); + expect(lspExecuteCodeAction).toHaveBeenCalledOnce(); }); it("falls back when the server is not ready or fails", async () => {