diff --git a/src/features/run/clearConsole.test.ts b/src/features/run/clearConsole.test.ts new file mode 100644 index 0000000000..2b582900cf --- /dev/null +++ b/src/features/run/clearConsole.test.ts @@ -0,0 +1,79 @@ +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import type { RunConfig } from "@/platform/ipc"; + +const ipc = vi.hoisted(() => ({ + runStart: vi.fn(async (_config: RunConfig) => ({ status: "ok", data: 1 })), + runStop: vi.fn(async () => ({ status: "ok", data: null })), +})); +vi.mock("@/platform/ipc", async (importOriginal) => { + const real = await importOriginal(); + return { ...real, isTauri: () => true, commands: { ...real.commands, ...ipc } }; +}); +vi.mock("@tauri-apps/api/core", async (importOriginal) => ({ + ...(await importOriginal()), + Channel: class { + onmessage: ((m: unknown) => void) | null = null; + }, +})); + +import { provideToolWindowService, type ToolWindowService } from "@/workbench/contributions"; +import { nodeDuration, nodeOutput, nodeStatus } from "./testing"; +import { clearConsole, resetRuns, runtime, startRun } from "./store"; + +const SEP = "\u0000"; +const config: RunConfig = { + id: "junit:CartTest", + name: "CartTest", + kind: "shell", + command: "true", + args: [], + cwd: null, + env: {}, + detected: false, + isTest: true, + debugAdapter: null, +}; +const cls = ["CartTest"]; +const total = ["CartTest", "total"]; + +beforeEach(() => provideToolWindowService({ show: vi.fn(), hide: vi.fn() } as unknown as ToolWindowService)); +afterEach(() => resetRuns()); + +async function testRun() { + const session = (await startRun(config, undefined, undefined, { prepared: true }))!; + const rt = runtime(session.id)!; + rt.console.append({ kind: "stdout", text: "before all\nprinted by total\n" }); + rt.tests!.apply([ + { type: "output", path: cls, line: "before all" }, + { type: "output", path: total, line: "printed by total" }, + { type: "result", path: total, status: "failed", durationMs: 12 }, + ]); + return { id: session.id, rt, tests: rt.tests! }; +} + +it("Clear All empties the full console and every test node's output, keeping the results (#6975)", async () => { + const { id, rt, tests } = await testRun(); + const leaf = tests.get(total.join(SEP))!; + const group = tests.get(cls.join(SEP))!; + expect(nodeOutput(group)).toEqual(["before all", "printed by total"]); + + clearConsole(id); + + expect(rt.console.text()).toBe(""); + expect(nodeOutput(leaf)).toEqual([]); + expect(nodeOutput(group)).toEqual([]); + expect(nodeStatus(leaf)).toBe("failed"); + expect(nodeDuration(group)).toBe(12); + expect(tests.roots.map((r) => r.children.map((c) => c.name))).toEqual([["total"]]); +}); + +it("output printed after Clear All shows in the console and on its test", async () => { + const { id, rt, tests } = await testRun(); + clearConsole(id); + rt.console.append({ kind: "stdout", text: "after clear\n" }); + tests.apply([{ type: "output", path: total, line: "after clear" }]); + + expect(rt.console.text()).toBe("after clear"); + expect(nodeOutput(tests.get(total.join(SEP))!)).toEqual(["after clear"]); + expect(nodeOutput(tests.get(cls.join(SEP))!)).toEqual(["after clear"]); +}); diff --git a/src/features/run/store.ts b/src/features/run/store.ts index e46c5c3e4d..e2f2497daa 100644 --- a/src/features/run/store.ts +++ b/src/features/run/store.ts @@ -1940,8 +1940,11 @@ export async function closeStdin(sessionId: string): Promise { } export function clearConsole(sessionId: string) { - runtimes.get(sessionId)?.console.clear(); - runtimes.get(sessionId)?.terminal?.clear(); + const rt = runtimes.get(sessionId); + rt?.console.clear(); + rt?.terminal?.clear(); + // A selected test shows its own captured output, so that goes too (as in IDEA). + rt?.tests?.clearOutput(); bump(sessionId); } diff --git a/src/features/run/testing/results.ts b/src/features/run/testing/results.ts index b8d570a632..7bb34e29cf 100644 --- a/src/features/run/testing/results.ts +++ b/src/features/run/testing/results.ts @@ -101,6 +101,12 @@ export class TestResults { this.version++; } + /** Console Clear All: drops every node's captured output; statuses, durations and the tree stay. */ + clearOutput() { + for (const n of this.byId.values()) n.output = []; + this.version++; + } + /** Finds a test by a suffix of its path (pytest failure headers name `Class.test`). */ findBySuffix(suffix: string[]): TestNode | undefined { let best: TestNode | undefined;