fix(remote): simplify connection mutations and preserve host state

This commit is contained in:
Xubin Ren committed 2026-10-02 11:36:34 +08:00
1 parent 0ffdf86619
commit 2330bfc5a5
7 files changed
+96 -37

No files matched your search

+9 -3
View File
@@ -35,7 +35,10 @@ beforeEach(() => {
window.sessionStorage.clear();
mocks.read.mockReset().mockResolvedValue(directory);
mocks.request.mockReset().mockImplementation(async (action: string, payload: { id: string }) =>
action === "remote.connect" ? connections.find((item) => item.id === payload.id) : {});
action === "remote.connect" ? connections.find((item) => item.id === payload.id) : {
...directory, profiles: directory.profiles.map((profile) =>
profile.id === payload.id ? { ...profile, connected: false } : profile),
});
});
afterEach(() => { cleanup(); vi.useRealTimers(); vi.restoreAllMocks(); });
@@ -99,14 +102,17 @@ describe("host session lifecycle", () => {
expect(result.current.frames[0].error).toBe("instance_changed");
});
it("does not report a successful disconnect as failed when refreshing the list fails", async () => {
it("uses the disconnect result without a second directory read", async () => {
const { result } = renderHook(useHostSessions);
await ready(result, 0);
const reads = mocks.read.mock.calls.length;
mocks.read.mockRejectedValue(new Error("directory_unavailable"));
await act(async () => { await expect(result.current.disconnect(ids[0])).resolves.toBeUndefined(); });
expect(result.current.selected).toBeNull();
expect(result.current.frames).toHaveLength(0);
expect(result.current.directoryError).toBe(true);
expect(result.current.directory?.profiles.find((profile) => profile.id === ids[0])?.connected).toBe(false);
expect(mocks.read).toHaveBeenCalledTimes(reads);
expect(result.current.directoryError).toBe(false);
});
it("keeps the current host until the destination view loads, then switches warm without RPC", async () => {
+28
View File
@@ -67,6 +67,7 @@ beforeEach(async () => {
mocks.request.mockReset().mockImplementation(async (action: string) => {
if (action === "remote.connect") return connection;
if (action === "remote.save") return { id: profile.id };
if (action === "remote.disconnect") return { available: true, profiles: [profile] };
return {};
});
});
@@ -860,6 +861,33 @@ describe("remote instance UX", () => {
expect(mocks.request).toHaveBeenCalledWith("remote.disconnect", { id: profile.id }, 65_000);
});
it("keeps the current view when forgetting fails and closes it only after a successful retry", async () => {
view(); await openDirectory();
fireEvent.click(screen.getByRole("button", { name: "Team server ubuntu@example.test" }));
const frame = await readyRemote();
await openDirectory();
fireEvent.pointerDown(screen.getByRole("button", { name: "Manage Team server" }), { button: 0, ctrlKey: false });
fireEvent.click(await screen.findByRole("menuitem", { name: "Forget server" }));
const dialog = await screen.findByRole("dialog", { name: "Forget Team server?" });
mocks.request.mockImplementation(async (action: string) => {
if (action === "remote.remove") throw new Error("local_io_error");
return {};
});
fireEvent.click(within(dialog).getByRole("button", { name: "Forget server" }));
await within(dialog).findByRole("alert");
expect(readSelectedRemote()?.id).toBe(profile.id);
expect(frame).toBeInTheDocument();
expect(mocks.request.mock.calls.map(([action]) => action)).toEqual(["remote.connect", "remote.remove"]);
mocks.request.mockResolvedValue({ available: true, profiles: [] });
mocks.read.mockRejectedValue(new Error("directory_unavailable"));
fireEvent.click(within(dialog).getByRole("button", { name: "Forget server" }));
await waitFor(() => expect(frame).not.toBeInTheDocument());
expect(readSelectedRemote()).toBeNull();
expect(screen.queryByRole("button", { name: "Manage Team server" })).not.toBeInTheDocument();
expect(mocks.request.mock.calls.map(([action]) => action)).toEqual(["remote.connect", "remote.remove", "remote.remove"]);
});
it("refresh restores the selected remote, keeping the local shell hidden", async () => {
rememberSelectedRemote(connection);
view();