Fix workspace navigation regression on web (#772)

Centralize workspace navigation behind a small route-derived API so the workspace shell reads active selection from the current Expo Router route instead of a separately synchronized store.

Remove the stale layout-driven active-workspace write that regressed web after 02e5b564.
This commit is contained in:
Mohamed Boudra
2026-05-06 13:22:31 +08:00
committed by GitHub
parent 0eacc98e17
commit 868479c493
26 changed files with 221 additions and 956 deletions
@@ -1,271 +1,78 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
/**
* @vitest-environment jsdom
*/
import { renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
const asyncStorageMock = vi.hoisted(() => {
const storage = new Map<string, string>();
return {
storage,
getItem: vi.fn(async (key: string): Promise<string | null> => storage.get(key) ?? null),
setItem: vi.fn(async (key: string, value: string): Promise<void> => {
storage.set(key, value);
}),
};
});
vi.mock("@react-native-async-storage/async-storage", () => ({
default: asyncStorageMock,
const routerMock = vi.hoisted(() => ({
dismissTo: vi.fn(),
}));
const pathnameState = vi.hoisted(() => ({
value: "/",
}));
const localParamsState = vi.hoisted(() => ({
value: {} as { serverId?: string | string[]; workspaceId?: string | string[] },
}));
vi.mock("@/constants/platform", () => ({
isWeb: true,
vi.mock("expo-router", () => ({
router: routerMock,
useLocalSearchParams: () => localParamsState.value,
usePathname: () => pathnameState.value,
}));
const LAST_WORKSPACE_ROUTE_SELECTION_STORAGE_KEY = "paseo:last-workspace-route-selection";
import {
navigateToLastWorkspace,
navigateToWorkspace,
useActiveWorkspaceSelection,
} from "@/stores/navigation-active-workspace-store";
function createDeferred<T>() {
let resolve!: (value: T) => void;
const promise = new Promise<T>((nextResolve) => {
resolve = nextResolve;
});
return { promise, resolve };
}
function installWindowStub(pathname: string) {
const windowStub = {
location: {
href: "",
origin: "http://localhost",
pathname: "",
search: "",
hash: "",
},
history: {
pushState: vi.fn((_state: unknown, _title: string, url: string) => {
updateLocation(url);
}),
replaceState: vi.fn((_state: unknown, _title: string, url: string) => {
updateLocation(url);
}),
},
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
};
function updateLocation(url: string) {
const next = new URL(url, windowStub.location.origin);
windowStub.location = {
href: next.href,
origin: next.origin,
pathname: next.pathname,
search: next.search,
hash: next.hash,
};
}
updateLocation(pathname);
vi.stubGlobal("window", windowStub);
return windowStub;
}
function createNavigationRef(serverId: string, workspaceId: string) {
return {
current: {
getCurrentRoute: () => ({
params: { serverId, workspaceId },
}),
},
};
}
function createNavigationPathRef(path: string) {
return {
current: {
getCurrentRoute: () => ({
path,
}),
},
};
}
function createNavigationPathWithParamsRef(path: string, serverId: string, workspaceId: string) {
return {
current: {
getCurrentRoute: () => ({
path,
params: { serverId, workspaceId },
}),
},
};
}
describe("navigation active workspace store", () => {
describe("workspace navigation", () => {
beforeEach(() => {
vi.resetModules();
asyncStorageMock.storage.clear();
asyncStorageMock.getItem.mockReset();
asyncStorageMock.setItem.mockReset();
asyncStorageMock.getItem.mockImplementation(
async (key: string): Promise<string | null> => asyncStorageMock.storage.get(key) ?? null,
);
asyncStorageMock.setItem.mockImplementation(
async (key: string, value: string): Promise<void> => {
asyncStorageMock.storage.set(key, value);
},
);
routerMock.dismissTo.mockReset();
pathnameState.value = "/";
localParamsState.value = {};
});
afterEach(() => {
vi.unstubAllGlobals();
it("reports when no last workspace is known", () => {
expect(navigateToLastWorkspace()).toBe(false);
});
it("keeps explicit web workspace activation ahead of a stale navigation route sync", async () => {
installWindowStub("/h/server-1/workspace/workspace-a");
const store = await import("@/stores/navigation-active-workspace-store");
it("navigates to a workspace route", () => {
navigateToWorkspace("server-1", "workspace-a");
store.syncNavigationActiveWorkspace(createNavigationRef("server-1", "workspace-a"));
expect(store.getNavigationActiveWorkspaceSelection()).toEqual({
serverId: "server-1",
workspaceId: "workspace-a",
});
store.activateNavigationWorkspaceSelection(
{ serverId: "server-1", workspaceId: "workspace-b" },
{ updateBrowserHistory: true, historyMode: "push" },
);
store.syncNavigationActiveWorkspace(createNavigationRef("server-1", "workspace-a"));
expect(store.getNavigationActiveWorkspaceSelection()).toEqual({
serverId: "server-1",
workspaceId: "workspace-b",
});
expect(routerMock.dismissTo).toHaveBeenCalledWith("/h/server-1/workspace/workspace-a");
});
it("clears a stale browser workspace when navigation sync reports a non-workspace route", async () => {
installWindowStub("/h/server-1/workspace/workspace-a");
const store = await import("@/stores/navigation-active-workspace-store");
it("reads the active workspace from the current route", () => {
pathnameState.value = "/h/server-1/workspace/workspace-a";
store.syncNavigationActiveWorkspace(createNavigationRef("server-1", "workspace-a"));
expect(store.getNavigationActiveWorkspaceSelection()).toEqual({
serverId: "server-1",
workspaceId: "workspace-a",
});
expect(store.getLastNavigationWorkspaceRouteSelection()).toEqual({
serverId: "server-1",
workspaceId: "workspace-a",
});
const { result } = renderHook(() => useActiveWorkspaceSelection());
store.syncNavigationActiveWorkspace(createNavigationPathRef("/h/server-1/sessions"));
expect(store.getNavigationActiveWorkspaceSelection()).toBeNull();
expect(store.getLastNavigationWorkspaceRouteSelection()).toEqual({
expect(result.current).toEqual({
serverId: "server-1",
workspaceId: "workspace-a",
});
});
it("clears stale workspace params when navigation sync reports a non-workspace path", async () => {
installWindowStub("/h/server-1/workspace/workspace-a");
const store = await import("@/stores/navigation-active-workspace-store");
store.syncNavigationActiveWorkspace(createNavigationRef("server-1", "workspace-a"));
expect(store.getNavigationActiveWorkspaceSelection()).toEqual({
it("falls back to workspace route params during cold route mount", () => {
localParamsState.value = {
serverId: "server-1",
workspaceId: "workspace-a",
});
workspaceId: "b64_L3RtcC9wYXNlby1taXNzaW5nLXdvcmtzcGFjZQ",
};
store.syncNavigationActiveWorkspace(
createNavigationPathWithParamsRef("/h/server-1/sessions", "server-1", "workspace-a"),
);
const { result } = renderHook(() => useActiveWorkspaceSelection());
expect(store.getNavigationActiveWorkspaceSelection()).toBeNull();
});
it("uses a one-shot workspace route override when returning to a retained shell", async () => {
installWindowStub("/h/server-1/workspace/workspace-a");
const store = await import("@/stores/navigation-active-workspace-store");
store.syncNavigationActiveWorkspace(
createNavigationPathRef("/h/server-1/workspace/workspace-a"),
);
store.overrideNextNavigationWorkspaceRouteSelection({
expect(result.current).toEqual({
serverId: "server-1",
workspaceId: "workspace-b",
});
store.syncNavigationActiveWorkspace(
createNavigationPathRef("/h/server-1/workspace/workspace-a"),
);
expect(store.getNavigationActiveWorkspaceSelection()).toEqual({
serverId: "server-1",
workspaceId: "workspace-b",
});
store.syncNavigationActiveWorkspace(
createNavigationPathRef("/h/server-1/workspace/workspace-a"),
);
expect(store.getNavigationActiveWorkspaceSelection()).toEqual({
serverId: "server-1",
workspaceId: "workspace-a",
workspaceId: "/tmp/paseo-missing-workspace",
});
});
it("hydrates the last workspace route selection from storage on startup", async () => {
const storedSelection = createDeferred<string | null>();
asyncStorageMock.getItem.mockReturnValue(storedSelection.promise);
installWindowStub("/open-project");
it("navigates to the last workspace observed by the route reader", () => {
pathnameState.value = "/h/server-1/workspace/workspace-a";
renderHook(() => useActiveWorkspaceSelection());
const store = await import("@/stores/navigation-active-workspace-store");
expect(store.getIsLastNavigationWorkspaceRouteSelectionLoaded()).toBe(false);
const hydration = store.hydrateLastNavigationWorkspaceRouteSelection();
storedSelection.resolve(JSON.stringify({ serverId: "server-1", workspaceId: "workspace-a" }));
await hydration;
expect(asyncStorageMock.getItem).toHaveBeenCalledWith(
LAST_WORKSPACE_ROUTE_SELECTION_STORAGE_KEY,
);
expect(store.getLastNavigationWorkspaceRouteSelection()).toEqual({
serverId: "server-1",
workspaceId: "workspace-a",
});
expect(store.getIsLastNavigationWorkspaceRouteSelectionLoaded()).toBe(true);
});
it("hydrates empty and corrupt workspace route storage as null", async () => {
installWindowStub("/open-project");
let store = await import("@/stores/navigation-active-workspace-store");
await store.hydrateLastNavigationWorkspaceRouteSelection();
expect(store.getLastNavigationWorkspaceRouteSelection()).toBeNull();
expect(store.getIsLastNavigationWorkspaceRouteSelectionLoaded()).toBe(true);
vi.resetModules();
asyncStorageMock.getItem.mockResolvedValueOnce("{not json");
store = await import("@/stores/navigation-active-workspace-store");
await store.hydrateLastNavigationWorkspaceRouteSelection();
expect(store.getLastNavigationWorkspaceRouteSelection()).toBeNull();
expect(store.getIsLastNavigationWorkspaceRouteSelectionLoaded()).toBe(true);
});
it("persists valid last workspace route selections", async () => {
installWindowStub("/h/server-1/workspace/workspace-a");
const store = await import("@/stores/navigation-active-workspace-store");
await store.hydrateLastNavigationWorkspaceRouteSelection();
store.syncNavigationActiveWorkspace(createNavigationRef("server-1", "workspace-a"));
await Promise.resolve(); // flush the fire-and-forget setItem microtask
vi.resetModules();
const freshStore = await import("@/stores/navigation-active-workspace-store");
await freshStore.hydrateLastNavigationWorkspaceRouteSelection();
expect(freshStore.getLastNavigationWorkspaceRouteSelection()).toEqual({
serverId: "server-1",
workspaceId: "workspace-a",
});
expect(freshStore.getIsLastNavigationWorkspaceRouteSelectionLoaded()).toBe(true);
expect(navigateToLastWorkspace()).toBe(true);
expect(routerMock.dismissTo).toHaveBeenCalledWith("/h/server-1/workspace/workspace-a");
});
});