174 lines
4.7 KiB
TypeScript
174 lines
4.7 KiB
TypeScript
import { act, render, screen, waitFor } from '@testing-library/react';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { MemoryRouter, Outlet } from 'react-router-dom';
|
|
import App from '@/App';
|
|
import { useAuthStore } from '@/stores/auth';
|
|
import { useProviderStore } from '@/stores/providers';
|
|
import { useSettingsStore } from '@/stores/settings';
|
|
import { useUserSyncStore } from '@/stores/user-sync';
|
|
|
|
vi.mock('@/components/layout/MainLayout', () => ({
|
|
MainLayout: () => (
|
|
<div data-testid="main-layout">
|
|
<Outlet />
|
|
</div>
|
|
),
|
|
}));
|
|
|
|
vi.mock('@/pages/AiHardware', () => ({
|
|
AiHardware: () => <div>Robot workspace</div>,
|
|
}));
|
|
|
|
vi.mock('@/pages/ImageCanvas', () => ({
|
|
ImageCanvas: () => <div>Canvas workspace</div>,
|
|
}));
|
|
|
|
vi.mock('@/pages/Chat', () => ({
|
|
Chat: () => <div>Programming workspace</div>,
|
|
}));
|
|
|
|
vi.mock('@/pages/ModuleSelection', () => ({
|
|
ModuleSelection: () => <div>Module chooser</div>,
|
|
}));
|
|
|
|
vi.mock('@/pages/Settings', () => ({
|
|
Settings: () => <div>Global settings</div>,
|
|
}));
|
|
|
|
describe('App programming provider initialization gate', () => {
|
|
const initProviders = vi.fn();
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
window.electron.imageWorkspaceLocalDevelopment = false;
|
|
useSettingsStore.setState({
|
|
setupComplete: true,
|
|
init: vi.fn(),
|
|
});
|
|
useProviderStore.setState({ init: initProviders });
|
|
useAuthStore.setState({
|
|
initialized: true,
|
|
accessToken: 'access-token',
|
|
expiresAt: Date.now() + 60_000,
|
|
user: {
|
|
username: 'robot-tester',
|
|
userId: '1',
|
|
tenantId: null,
|
|
deptId: null,
|
|
authorities: [],
|
|
},
|
|
moduleAccess: {
|
|
programming: true,
|
|
design: true,
|
|
learning: true,
|
|
robot: true,
|
|
},
|
|
init: vi.fn(),
|
|
});
|
|
useUserSyncStore.setState({ bootstrap: vi.fn().mockResolvedValue(undefined) });
|
|
});
|
|
|
|
async function renderAt(pathname: string) {
|
|
render(
|
|
<MemoryRouter initialEntries={[pathname]}>
|
|
<App />
|
|
</MemoryRouter>,
|
|
);
|
|
await waitFor(() => expect(useSettingsStore.getState().init).toHaveBeenCalled());
|
|
}
|
|
|
|
it.each(['/ai-hardware', '/image-canvas', '/module-select'])(
|
|
'does not initialize programming providers at %s',
|
|
async (pathname) => {
|
|
await renderAt(pathname);
|
|
|
|
expect(initProviders).not.toHaveBeenCalled();
|
|
},
|
|
);
|
|
|
|
it('initializes programming providers at the explicit programming route', async () => {
|
|
await renderAt('/opencode-chat');
|
|
|
|
await waitFor(() => expect(initProviders).toHaveBeenCalledTimes(1));
|
|
});
|
|
|
|
it('waits for the startup module policy before initializing programming providers', async () => {
|
|
useAuthStore.setState({
|
|
initialized: false,
|
|
moduleAccess: {
|
|
programming: true,
|
|
design: true,
|
|
learning: true,
|
|
robot: true,
|
|
},
|
|
});
|
|
|
|
await renderAt('/opencode-chat');
|
|
|
|
expect(initProviders).not.toHaveBeenCalled();
|
|
|
|
act(() => {
|
|
useAuthStore.setState({
|
|
initialized: true,
|
|
moduleAccess: {
|
|
programming: false,
|
|
design: true,
|
|
learning: true,
|
|
robot: true,
|
|
},
|
|
});
|
|
});
|
|
|
|
expect(await screen.findByText('Module chooser')).toBeInTheDocument();
|
|
expect(initProviders).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each([
|
|
['/opencode-chat', 'programming'],
|
|
['/workbench/project-1', 'programming'],
|
|
['/chat', 'programming'],
|
|
['/image-canvas', 'design'],
|
|
['/image-prompts/example', 'design'],
|
|
['/learning', 'learning'],
|
|
['/learning/project/project-1', 'learning'],
|
|
['/ai-hardware', 'robot'],
|
|
['/ai-hardware/device-1', 'robot'],
|
|
] as const)('redirects disabled %s routes before mounting their module', async (pathname, accessKey) => {
|
|
useAuthStore.setState({
|
|
moduleAccess: {
|
|
programming: true,
|
|
design: true,
|
|
learning: true,
|
|
robot: true,
|
|
[accessKey]: false,
|
|
},
|
|
});
|
|
|
|
await renderAt(pathname);
|
|
|
|
expect(await screen.findByText('Module chooser')).toBeInTheDocument();
|
|
expect(screen.queryByTestId('main-layout')).not.toBeInTheDocument();
|
|
if (accessKey === 'programming') {
|
|
expect(initProviders).not.toHaveBeenCalled();
|
|
}
|
|
});
|
|
|
|
it('keeps global settings available when Programming is disabled', async () => {
|
|
useAuthStore.setState({
|
|
moduleAccess: {
|
|
programming: false,
|
|
design: true,
|
|
learning: true,
|
|
robot: true,
|
|
},
|
|
});
|
|
|
|
await renderAt('/settings');
|
|
|
|
expect(await screen.findByText('Global settings')).toBeInTheDocument();
|
|
expect(screen.queryByText('Module chooser')).not.toBeInTheDocument();
|
|
expect(screen.getByTestId('main-layout')).toBeInTheDocument();
|
|
await waitFor(() => expect(initProviders).toHaveBeenCalledTimes(1));
|
|
});
|
|
});
|