Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.3 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';

const resetGatewayMuxRegistry = vi.hoisted(() => vi.fn());
const storeResets = vi.hoisted(() => [] as Array<() => void>);
const expertiseReset = vi.hoisted(() => vi.fn());
const mcpAppReset = vi.hoisted(() => vi.fn());
const pageReset = vi.hoisted(() => vi.fn());
const resourcePermissionReset = vi.hoisted(() => vi.fn());
const sandboxReset = vi.hoisted(() => vi.fn());
const statsReset = vi.hoisted(() => vi.fn());
const workReset = vi.hoisted(() => vi.fn());
const fakeStore = vi.hoisted(() => () => ({
  getState: () => {
    const reset = vi.fn();
    storeResets.push(reset);
    return { reset };
  },
}));

vi.mock('react-dom', () => ({ unstable_batchedUpdates: (fn: () => void) => fn() }));
vi.mock('@/store/chat/slices/agentRun/actions/transports/gateway/muxRegistry', () => ({
  resetGatewayMuxRegistry,
}));
vi.mock('@/store/agent', () => ({ useAgentStore: fakeStore() }));
vi.mock('@/store/agentGroup', () => ({ useAgentGroupStore: fakeStore() }));
vi.mock('@/store/agentQuota', () => ({ useAgentQuotaStore: fakeStore() }));
vi.mock('@/store/chat', () => ({ useChatStore: fakeStore() }));
vi.mock('@/store/discover', () => ({ useDiscoverStore: fakeStore() }));
vi.mock('@/store/document', () => ({ useDocumentStore: fakeStore() }));
vi.mock('@/store/eval', () => ({ useEvalStore: fakeStore() }));
vi.mock('@/store/expertise', () => ({ expertiseActions: { reset: expertiseReset } }));
vi.mock('@/store/file', () => ({ useFileStore: fakeStore() }));
vi.mock('@/store/home', () => ({ useHomeStore: fakeStore() }));
vi.mock('@/store/image', () => ({ useImageStore: fakeStore() }));
vi.mock('@/store/library', () => ({ useKnowledgeBaseStore: fakeStore() }));
vi.mock('@/store/mcpApp/action', () => ({ mcpAppActions: { reset: mcpAppReset } }));
vi.mock('@/store/mention', () => ({ useMentionStore: fakeStore() }));
vi.mock('@/store/notebook', () => ({ useNotebookStore: fakeStore() }));
vi.mock('@/store/page', () => ({ pageActions: { reset: pageReset } }));
vi.mock('@/store/resourcePermission', () => ({
  resourcePermissionActions: { reset: resourcePermissionReset },
}));
vi.mock('@/store/sandbox', () => ({ sandboxActions: { reset: sandboxReset } }));
vi.mock('@/store/stats', () => ({ statsActions: { reset: statsReset } }));
vi.mock('@/store/session', () => ({ useSessionStore: fakeStore() }));
vi.mock('@/store/task', () => ({ useTaskStore: fakeStore() }));
vi.mock('@/store/tool', () => ({ useToolStore: fakeStore() }));
vi.mock('@/store/user', () => ({ useUserStore: fakeStore() }));
vi.mock('@/store/userMemory', () => ({ useUserMemoryStore: fakeStore() }));
vi.mock('@/store/video', () => ({ useVideoStore: fakeStore() }));
vi.mock('@/store/work', () => ({ workActions: { reset: workReset } }));

describe('stores.reset', () => {
  beforeEach(() => {
    resetGatewayMuxRegistry.mockClear();
    storeResets.length = 0;
    expertiseReset.mockClear();
    mcpAppReset.mockClear();
    pageReset.mockClear();
    resourcePermissionReset.mockClear();
    sandboxReset.mockClear();
    statsReset.mockClear();
    workReset.mockClear();
  });

  it('tears down the gateway mux registry before wiping the user-data stores', async () => {
    const { stores } = await import('./userDataStores');
    const order: string[] = [];
    resetGatewayMuxRegistry.mockImplementation(() => order.push('mux'));

    stores.reset();

    expect(resetGatewayMuxRegistry).toHaveBeenCalledTimes(1);
    expect(storeResets).toHaveLength(19);
    // MCP App views, panels and pending model context belong to the old user
    expect(mcpAppReset).toHaveBeenCalledTimes(1);
    expect(pageReset).toHaveBeenCalledTimes(1);
    // The rule sheet and agent portraits must not leak into the next identity.
    expect(expertiseReset).toHaveBeenCalledTimes(1);
    // Confirmed manage grants and the memory-only collaborator roster must not
    // outlive the data context, even when the next one reuses the scope string.
    expect(resourcePermissionReset).toHaveBeenCalledTimes(1);
    expect(sandboxReset).toHaveBeenCalledTimes(1);
    // Another identity must not paint the previous one's usage numbers.
    expect(statsReset).toHaveBeenCalledTimes(1);
    expect(workReset).toHaveBeenCalledTimes(1);
    for (const reset of storeResets) expect(reset).toHaveBeenCalledTimes(1);
    expect(order[0]).toBe('mux');
  });
});