дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 09:41:34 +02:00
Renaming or moving a class through a refactoring closed the editor of the old file and never opened the new one: workspace edits applied through Documents::apply_workspace_edit_as reported only the affected paths, so the editor saw just the watcher's removal. WorkspaceEditApplied now carries the edit's file moves, and WorkspaceHistory makes open editors follow them on apply, Undo and Redo using the Project view's helper (moved to src/editor/fileMoves.ts), which now waits for the old tab to close before reopening and settles the record before reopening on Undo/Redo. Co-authored-by: ntbang0901 <[email protected]> Co-authored-by: ntbang0901 <[email protected]>
72 рядки
2.7 KiB
TypeScript
72 рядки
2.7 KiB
TypeScript
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
|
import { emitLocalIdeEvent } from "@/platform/ipc";
|
|
import { provideNotificationService, type NotificationService } from "@/workbench/contributions";
|
|
import { useProject } from "@/platform/project";
|
|
import { installWorkspaceEditUndo } from "./workspaceUndo";
|
|
|
|
const mocks = vi.hoisted(() => ({ undo: vi.fn(), synced: vi.fn(), history: vi.fn(() => () => {}) }));
|
|
vi.mock("@/editor/api", () => ({
|
|
whenAllSynced: mocks.synced,
|
|
workspaceEditHistory: mocks.undo,
|
|
onWorkspaceEditHistory: mocks.history,
|
|
}));
|
|
|
|
let stop: () => void;
|
|
let notices: Parameters<NotificationService["notify"]>[0][];
|
|
|
|
beforeEach(() => {
|
|
mocks.undo.mockReset().mockResolvedValue(undefined);
|
|
mocks.synced.mockReset().mockResolvedValue(undefined);
|
|
notices = [];
|
|
provideNotificationService({
|
|
notify: (notice) => {
|
|
notices.push(notice);
|
|
},
|
|
});
|
|
stop = installWorkspaceEditUndo();
|
|
});
|
|
|
|
afterEach(() => stop());
|
|
|
|
it("waits for pending typing before undoing the entire backend transaction", async () => {
|
|
let release: () => void = () => {};
|
|
mocks.synced.mockReturnValue(
|
|
new Promise<void>((resolve) => {
|
|
release = resolve;
|
|
}),
|
|
);
|
|
emitLocalIdeEvent({ type: "workspaceEditApplied", id: 17, paths: [], moves: [], byAgent: false });
|
|
const action = notices[0]!.actions![0]!;
|
|
expect(action.label).toBe("Undo");
|
|
const pending = action.run();
|
|
expect(mocks.undo).not.toHaveBeenCalled();
|
|
release();
|
|
await pending;
|
|
expect(mocks.undo).toHaveBeenCalledExactlyOnceWith(17);
|
|
});
|
|
|
|
it("reports a refused undo instead of discarding newer work", async () => {
|
|
mocks.undo.mockRejectedValue(new Error("a.txt changed"));
|
|
emitLocalIdeEvent({ type: "workspaceEditApplied", id: 18, paths: [], moves: [], byAgent: false });
|
|
await notices[0]!.actions![0]!.run();
|
|
expect(notices[1]).toMatchObject({ level: "error", title: "Cannot undo refactoring", body: "a.txt changed" });
|
|
});
|
|
|
|
it("does not apply an old notification's id to a different project", async () => {
|
|
emitLocalIdeEvent({ type: "workspaceEditApplied", id: 19, paths: [], moves: [], byAgent: false });
|
|
const previous = useProject.getState().project;
|
|
useProject.setState({ project: { ...previous, root: "/different-project" } as NonNullable<typeof previous> });
|
|
try {
|
|
await notices[0]!.actions![0]!.run();
|
|
expect(mocks.undo).not.toHaveBeenCalled();
|
|
expect(notices[1]).toMatchObject({ level: "error", body: "The project for this refactoring is no longer open" });
|
|
} finally {
|
|
useProject.setState({ project: previous });
|
|
}
|
|
});
|
|
|
|
it("does not announce an agent's edit as a refactoring", () => {
|
|
emitLocalIdeEvent({ type: "workspaceEditApplied", id: 20, paths: [], moves: [], byAgent: true });
|
|
expect(notices).toEqual([]);
|
|
});
|