дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 10:07:11 +02:00
After a remote backend disconnected or was replaced, its progress rows stayed in the window because the old connection could no longer deliver completion events. When the connection is lost or the session changes, the window now drops the backend's numeric task IDs, their pending delayed updates and the indexing row, and keeps client-side tasks. Co-authored-by: fenndooscar <[email protected]>
246 рядки
9.7 KiB
TypeScript
246 рядки
9.7 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { act, fireEvent, render, screen } from "@testing-library/react";
|
|
import { useRemote } from "@/platform/remote";
|
|
|
|
const cancelTaskCommand = vi.hoisted(() => vi.fn(async (_id: number) => true));
|
|
vi.mock("@/platform/ipc", async (orig) => {
|
|
const actual = await orig<typeof import("@/platform/ipc")>();
|
|
return { ...actual, commands: { ...actual.commands, cancelTask: cancelTaskCommand } };
|
|
});
|
|
|
|
const { emitLocalIdeEvent, isCancelled } = await import("@/platform/ipc");
|
|
const { cancelTask, installTaskEvents, queueTask, setIndexing, upsertTask, useTasks } = await import("./tasks");
|
|
const { BackgroundTasksHost, TaskStatus } = await import("./StatusBar");
|
|
|
|
const task = (id: string) => useTasks.getState().tasks.find((t) => t.id === id);
|
|
|
|
function deferred() {
|
|
let resolve!: () => void;
|
|
const promise = new Promise<void>((r) => (resolve = r));
|
|
return { promise, resolve };
|
|
}
|
|
|
|
afterEach(() => {
|
|
useTasks.setState({ tasks: [], indexing: null });
|
|
useRemote.setState({ session: null, lost: null });
|
|
vi.useRealTimers();
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("task queues", () => {
|
|
it("runs one task at a time; the others wait, listed as queued", async () => {
|
|
const first = deferred();
|
|
const started: string[] = [];
|
|
const a = queueTask("q1", { id: "a", title: "A" }, async () => {
|
|
started.push("a");
|
|
upsertTask({ id: "a", title: "A", message: null, fraction: null, done: false });
|
|
await first.promise;
|
|
return "a";
|
|
});
|
|
const b = queueTask("q1", { id: "b", title: "B" }, async () => {
|
|
started.push("b");
|
|
return "b";
|
|
});
|
|
expect(started).toEqual(["a"]);
|
|
expect(task("b")).toMatchObject({ queued: true, message: "Waiting for other tasks" });
|
|
first.resolve();
|
|
await expect(a).resolves.toBe("a");
|
|
await expect(b).resolves.toBe("b");
|
|
expect(started).toEqual(["a", "b"]);
|
|
});
|
|
|
|
it("cancelling a queued task drops it at once: its work never starts and its caller sees a cancellation", async () => {
|
|
const first = deferred();
|
|
const work = vi.fn(async () => "never");
|
|
const running = queueTask("q2", { id: "run", title: "Running" }, () => first.promise);
|
|
const queued = queueTask("q2", { id: "wait", title: "Waiting" }, work);
|
|
const next = queueTask("q2", { id: "next", title: "Next" }, async () => "next");
|
|
|
|
cancelTask("wait");
|
|
await expect(queued).rejects.toSatisfy(isCancelled);
|
|
expect(task("wait")).toBeUndefined();
|
|
|
|
first.resolve();
|
|
await running;
|
|
await expect(next).resolves.toBe("next");
|
|
expect(work).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("the queue's Cancel is gone once the work starts (the work brings its own)", async () => {
|
|
const first = deferred();
|
|
const second = deferred();
|
|
void queueTask("q3", { id: "x", title: "X" }, () => first.promise);
|
|
const y = queueTask("q3", { id: "y", title: "Y" }, () => second.promise);
|
|
first.resolve();
|
|
await vi.waitFor(() => expect(task("y")?.queued).toBe(false));
|
|
expect(task("y")?.cancel).toBeUndefined();
|
|
second.resolve();
|
|
await y;
|
|
});
|
|
});
|
|
|
|
describe("remote task lifetime", () => {
|
|
it.each(["disconnect", "replace"])("clears server tasks on %s", (transition) => {
|
|
vi.useFakeTimers();
|
|
const session = {
|
|
host: "Ubuntu",
|
|
root: "/project",
|
|
os: "linux",
|
|
target: { kind: "wsl" as const, distro: "Ubuntu" },
|
|
commands: [],
|
|
};
|
|
useRemote.setState({ session });
|
|
const uninstall = installTaskEvents();
|
|
try {
|
|
const backendTask = {
|
|
type: "task" as const,
|
|
title: "Find References",
|
|
message: null,
|
|
fraction: null,
|
|
done: false,
|
|
cancellable: false,
|
|
};
|
|
upsertTask({ ...backendTask, id: "client-task" });
|
|
emitLocalIdeEvent({ ...backendTask, id: 1 });
|
|
vi.advanceTimersByTime(500);
|
|
expect(useTasks.getState().tasks.map((t) => t.id)).toEqual(["client-task", 1]);
|
|
emitLocalIdeEvent({ ...backendTask, id: 2 });
|
|
setIndexing(true, 1, 1);
|
|
|
|
useRemote.setState(transition === "disconnect" ? { lost: session.host } : { session: { ...session } });
|
|
vi.advanceTimersByTime(500);
|
|
expect(useTasks.getState().tasks.map((t) => t.id)).toEqual(["client-task"]);
|
|
expect(useTasks.getState().indexing).toBeNull();
|
|
|
|
useRemote.setState({ session: { ...session }, lost: null });
|
|
emitLocalIdeEvent({ ...backendTask, id: 1, title: "New backend task" });
|
|
vi.advanceTimersByTime(500);
|
|
expect(useTasks.getState().tasks.map((t) => t.id)).toEqual(["client-task", 1]);
|
|
} finally {
|
|
uninstall();
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("cancelling", () => {
|
|
it.each([false, true])("keeps a replacement task when the old completion arrives (delayed: %s)", (delayed) => {
|
|
vi.useFakeTimers();
|
|
const uninstall = installTaskEvents();
|
|
const base = {
|
|
type: "task" as const,
|
|
title: "Finding references",
|
|
message: null,
|
|
fraction: null,
|
|
cancellable: false,
|
|
};
|
|
emitLocalIdeEvent({ ...base, id: 51, done: false });
|
|
if (delayed) vi.advanceTimersByTime(600);
|
|
emitLocalIdeEvent({ ...base, id: 52, done: false });
|
|
emitLocalIdeEvent({ ...base, id: 51, done: true });
|
|
vi.advanceTimersByTime(600);
|
|
expect(useTasks.getState().tasks.map((t) => t.id)).toEqual([52]);
|
|
emitLocalIdeEvent({ ...base, id: 52, done: true });
|
|
emitLocalIdeEvent({ ...base, id: 52, done: true });
|
|
vi.advanceTimersByTime(600);
|
|
expect(useTasks.getState().tasks).toEqual([]);
|
|
uninstall();
|
|
});
|
|
|
|
it("shows Cancelling… until the task ends, then removes it without the finished bar", () => {
|
|
vi.useFakeTimers();
|
|
const cancel = vi.fn();
|
|
upsertTask({ id: "sync", title: "Syncing", message: "Resolving", fraction: 0.3, done: false, cancel });
|
|
cancelTask("sync");
|
|
cancelTask("sync");
|
|
expect(cancel).toHaveBeenCalledOnce();
|
|
expect(task("sync")).toMatchObject({ cancelling: true, message: "Cancelling…" });
|
|
upsertTask({ id: "sync", title: "Syncing", message: "Downloading", fraction: 0.4, done: false, cancel });
|
|
expect(task("sync")?.message).toBe("Cancelling…");
|
|
upsertTask({ id: "sync", title: "Syncing", message: null, fraction: 1, done: true });
|
|
expect(task("sync")).toBeUndefined();
|
|
});
|
|
|
|
it("the popup's cancel buttons: working, Cancelling…, and disabled with the reason", () => {
|
|
const cancel = vi.fn();
|
|
upsertTask({ id: "dl", title: "Downloading JDK 25", message: null, fraction: null, done: false, cancel });
|
|
upsertTask({
|
|
id: "inst",
|
|
title: "Installing update",
|
|
message: null,
|
|
fraction: null,
|
|
done: false,
|
|
uncancellable: "This step can't be cancelled",
|
|
});
|
|
render(
|
|
<>
|
|
<TaskStatus />
|
|
<BackgroundTasksHost />
|
|
</>,
|
|
);
|
|
fireEvent.click(screen.getByRole("button", { name: /Background tasks/ }));
|
|
fireEvent.click(screen.getByRole("button", { name: "Cancel Downloading JDK 25" }));
|
|
expect(cancel).toHaveBeenCalledOnce();
|
|
expect(screen.getByRole("button", { name: "Cancelling…" })).toBeDisabled();
|
|
const off = screen.getByRole("button", { name: "This step can't be cancelled" });
|
|
expect(off).toBeDisabled();
|
|
expect(off.parentElement).toHaveAttribute("data-tooltip", "This step can't be cancelled");
|
|
});
|
|
|
|
it("cancelling the last task from the popup closes it", () => {
|
|
const cancel = vi.fn(() => upsertTask({ id: "dl", title: "Installing", message: null, fraction: 1, done: true }));
|
|
upsertTask({ id: "dl", title: "Installing", message: null, fraction: null, done: false, cancel });
|
|
render(
|
|
<>
|
|
<TaskStatus />
|
|
<BackgroundTasksHost />
|
|
</>,
|
|
);
|
|
fireEvent.click(screen.getByRole("button", { name: /Background tasks/ }));
|
|
expect(screen.getByRole("dialog", { name: "Background tasks" })).toBeVisible();
|
|
act(() => fireEvent.click(screen.getByRole("button", { name: "Cancel Installing" })));
|
|
expect(useTasks.getState().tasks).toEqual([]);
|
|
expect(screen.queryByRole("dialog", { name: "Background tasks" })).toBeNull();
|
|
expect(screen.queryByText("No background tasks")).toBeNull();
|
|
});
|
|
|
|
it("indexing says why it can't be stopped", () => {
|
|
setIndexing(true, 10, 20);
|
|
expect(task("indexing")).toMatchObject({ uncancellable: expect.any(String) });
|
|
expect(task("indexing")?.cancel).toBeUndefined();
|
|
});
|
|
|
|
it("backend tasks: cancellable ones cancel through cancel_task, the others say they can't", () => {
|
|
vi.useFakeTimers();
|
|
const uninstall = installTaskEvents();
|
|
const base = { type: "task" as const, message: null, fraction: null, done: false };
|
|
act(() => {
|
|
emitLocalIdeEvent({ ...base, id: 41, title: "Building workspace", cancellable: true });
|
|
emitLocalIdeEvent({ ...base, id: 42, title: "Loading", cancellable: false });
|
|
vi.advanceTimersByTime(600);
|
|
});
|
|
cancelTask(41);
|
|
expect(cancelTaskCommand).toHaveBeenCalledWith(41);
|
|
expect(useTasks.getState().tasks.find((t) => t.id === 42)).toMatchObject({
|
|
cancel: undefined,
|
|
uncancellable: "This task can't be cancelled",
|
|
});
|
|
uninstall();
|
|
});
|
|
});
|
|
|
|
it("shows progress and uncancellable tasks through an action without the status bar", async () => {
|
|
const { registerShellActions } = await import("./shellActions");
|
|
const { executeAction } = await import("@/platform/actions");
|
|
const dispose = registerShellActions();
|
|
try {
|
|
upsertTask({ id: "scan", title: "Scanning", message: "Reading files", fraction: 0.25, done: false });
|
|
render(<BackgroundTasksHost />);
|
|
await act(() => executeAction("view.backgroundTasks"));
|
|
expect(screen.getByRole("dialog", { name: "Background tasks" })).toHaveTextContent("25%");
|
|
expect(screen.getByText("Reading files")).toBeVisible();
|
|
fireEvent.keyDown(document, { key: "Escape" });
|
|
} finally {
|
|
dispose();
|
|
}
|
|
});
|