mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-07 04:17:48 +08:00
61 lines
2.3 KiB
TypeScript
61 lines
2.3 KiB
TypeScript
import { act, cleanup, renderHook, waitFor } from "@testing-library/react";
|
|
import { afterEach, expect, test, vi } from "vitest";
|
|
|
|
vi.mock("@/lib/recovery-principal", () => ({
|
|
RECOVERY_PRINCIPAL_CHANGED: "test-principal-changed",
|
|
recoveryPrincipal: () => "user:test",
|
|
}));
|
|
const listWorkspaces = vi.fn();
|
|
|
|
vi.mock("@/lib/workspaces-api", () => ({
|
|
listWorkspaces: (...args: unknown[]) => listWorkspaces(...args),
|
|
}));
|
|
|
|
vi.mock("@/lib/session-events", () => ({
|
|
subscribeSessionChanges: () => () => {},
|
|
}));
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
listWorkspaces.mockReset();
|
|
vi.resetModules();
|
|
});
|
|
|
|
test("a remounted sidebar keeps the last workspace while it refreshes", async () => {
|
|
const rows = [
|
|
{
|
|
workspace_id: "ws-anglais",
|
|
display_name: "Anglais",
|
|
kind: "workspace",
|
|
status: "ready",
|
|
archived: false,
|
|
},
|
|
];
|
|
listWorkspaces.mockResolvedValueOnce(rows);
|
|
|
|
const { useChatWorkspaces } = await import("@/hooks/useChatWorkspaces");
|
|
const first = renderHook(() => useChatWorkspaces());
|
|
await waitFor(() => expect(first.result.current.workspaces).toHaveLength(1));
|
|
first.unmount();
|
|
|
|
// The collapse remounts the menu; its refresh is still in flight.
|
|
listWorkspaces.mockReturnValue(new Promise(() => {}));
|
|
const second = renderHook(() => useChatWorkspaces());
|
|
|
|
expect(second.result.current.workspaces[0]?.display_name).toBe("Anglais");
|
|
});
|
|
|
|
test('a superseded response cannot replace a newer workspace list after remount',async()=>{
|
|
const row=(display_name:string)=>[{workspace_id:display_name,display_name,kind:'workspace'}];
|
|
let settle:(value:unknown)=>void=()=>{};
|
|
listWorkspaces.mockReturnValueOnce(new Promise(resolve=>{settle=resolve;}));
|
|
const {useChatWorkspaces}=await import('@/hooks/useChatWorkspaces');
|
|
const a=renderHook(()=>useChatWorkspaces());a.unmount();
|
|
listWorkspaces.mockResolvedValueOnce(row('New workspace'));
|
|
const b=renderHook(()=>useChatWorkspaces());await waitFor(()=>expect(b.result.current.workspaces[0]?.display_name).toBe('New workspace'));
|
|
await act(async()=>{settle(row('Old workspace'));await Promise.resolve();});
|
|
b.unmount();listWorkspaces.mockReturnValueOnce(new Promise(()=>{}));
|
|
const c=renderHook(()=>useChatWorkspaces());
|
|
expect(c.result.current.workspaces[0]?.display_name).toBe('New workspace');
|
|
});
|