187 lines
7.0 KiB
TypeScript
187 lines
7.0 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
import { MemoryRouter, Route, Routes } from 'react-router-dom';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { MainLayout } from '@/components/layout/MainLayout';
|
|
import { codingWorkspaceStore } from '@/stores/coding-workspace';
|
|
import { useProjectConfigStore } from '@/stores/project-config';
|
|
import { useSettingsStore } from '@/stores/settings';
|
|
import { createCodingProjectConfigV2 } from '@electron/coding-projects/project-config';
|
|
import type { CodingProjectAgent } from '@/types/coding-project';
|
|
|
|
const learningIpcMock = vi.hoisted(() => vi.fn());
|
|
|
|
const readyAgent: CodingProjectAgent = {
|
|
id: 'agent-1',
|
|
avatarId: 'avatar-01',
|
|
roleName: '项目智能体',
|
|
name: 'Builder',
|
|
builtIn: false,
|
|
enabled: true,
|
|
model: { accountId: 'account-1', modelId: 'model-1', thinkingLevel: 'off' },
|
|
modelResolution: 'resolved',
|
|
skillIds: [],
|
|
responsibility: {
|
|
mission: '完成项目工作',
|
|
owns: [],
|
|
boundaries: [],
|
|
collaborators: [],
|
|
principles: [],
|
|
},
|
|
prompt: '',
|
|
archivedAt: null,
|
|
pinned: false,
|
|
createdAt: '2026-09-06T00:00:00.000Z',
|
|
updatedAt: '2026-09-06T00:00:00.000Z',
|
|
};
|
|
|
|
vi.mock('@/components/layout/Sidebar', () => ({
|
|
Sidebar: ({ sidebarCollapsedOverride }: { sidebarCollapsedOverride?: boolean }) => (
|
|
<aside data-testid="sidebar-stub" data-sidebar-collapsed={sidebarCollapsedOverride === undefined ? 'unset' : String(sidebarCollapsedOverride)} />
|
|
),
|
|
}));
|
|
vi.mock('@/components/layout/TitleBar', () => ({
|
|
TitleBar: ({ integrated = false, overlay = false, pageTitle, sidebarCollapsedOverride }: { integrated?: boolean; overlay?: boolean; pageTitle?: string; sidebarCollapsedOverride?: boolean }) => (
|
|
<header
|
|
data-testid="titlebar-stub"
|
|
data-integrated={integrated ? 'true' : 'false'}
|
|
data-overlay={overlay ? 'true' : 'false'}
|
|
data-page-title={pageTitle ?? ''}
|
|
data-sidebar-collapsed={sidebarCollapsedOverride === undefined ? 'unset' : String(sidebarCollapsedOverride)}
|
|
/>
|
|
),
|
|
}));
|
|
|
|
function LearningRouteContent() {
|
|
learningIpcMock();
|
|
return <div data-testid="route-content" />;
|
|
}
|
|
|
|
function renderLayout(path: string) {
|
|
return render(
|
|
<MemoryRouter initialEntries={[path]}>
|
|
<Routes>
|
|
<Route element={<MainLayout />}>
|
|
<Route path="*" element={<LearningRouteContent />} />
|
|
</Route>
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
);
|
|
}
|
|
|
|
describe('MainLayout module isolation', () => {
|
|
beforeEach(() => {
|
|
learningIpcMock.mockReset();
|
|
const project = {
|
|
id: 'local-project',
|
|
path: '/tmp/local-project',
|
|
name: 'local-project',
|
|
createdAt: '',
|
|
updatedAt: '',
|
|
lastOpenedAt: '',
|
|
};
|
|
codingWorkspaceStore.setState({ activeProject: project });
|
|
useProjectConfigStore.setState({
|
|
configsByProjectId: {
|
|
[project.id]: { ...createCodingProjectConfigV2(), initialized: false },
|
|
},
|
|
});
|
|
useSettingsStore.setState({ sidebarCollapsed: false });
|
|
});
|
|
|
|
it('does not overlay the local project initialization gate on AI painting', () => {
|
|
renderLayout('/image-canvas');
|
|
|
|
expect(screen.getByTestId('route-content')).toBeVisible();
|
|
expect(screen.queryByTestId('project-initialization-gate')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('uses the full-height workspace shell for AI painting', () => {
|
|
renderLayout('/image-canvas');
|
|
|
|
expect(screen.getByTestId('main-content')).toHaveClass('basis-0', 'h-full', 'overflow-hidden', 'p-0');
|
|
expect(screen.getByTestId('main-content')).not.toHaveClass('p-5', 'sm:p-6');
|
|
expect(screen.getByTestId('titlebar-stub')).toHaveAttribute('data-overlay', 'true');
|
|
});
|
|
|
|
it('keeps the user sidebar preference while moving the inspiration title into the title bar', () => {
|
|
renderLayout('/image-prompts');
|
|
|
|
expect(screen.getByTestId('titlebar-stub')).toHaveAttribute('data-page-title', '获取灵感');
|
|
expect(screen.getByTestId('titlebar-stub')).toHaveAttribute('data-overlay', 'false');
|
|
expect(screen.getByTestId('titlebar-stub')).toHaveAttribute('data-sidebar-collapsed', 'unset');
|
|
expect(screen.getByTestId('sidebar-stub')).toHaveAttribute('data-sidebar-collapsed', 'unset');
|
|
expect(useSettingsStore.getState().sidebarCollapsed).toBe(false);
|
|
});
|
|
|
|
it('does not overlay the local project initialization gate on AI hardware', () => {
|
|
const load = vi.fn().mockResolvedValue(undefined);
|
|
useProjectConfigStore.setState({ load });
|
|
renderLayout('/ai-hardware');
|
|
|
|
expect(screen.getByTestId('route-content')).toBeVisible();
|
|
expect(screen.queryByTestId('project-initialization-gate')).not.toBeInTheDocument();
|
|
expect(load).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('keeps the local project initialization gate for AI programming routes', () => {
|
|
renderLayout('/chat');
|
|
|
|
expect(screen.getByTestId('project-initialization-gate')).toBeVisible();
|
|
expect(screen.getByTestId('titlebar-stub')).toHaveAttribute('data-overlay', 'false');
|
|
});
|
|
|
|
it('keeps the gate when a legacy config says initialized but has no usable Agent', () => {
|
|
const project = codingWorkspaceStore.getState().activeProject!;
|
|
useProjectConfigStore.setState({
|
|
configsByProjectId: {
|
|
[project.id]: { ...createCodingProjectConfigV2(), initialized: true, agents: [] },
|
|
},
|
|
});
|
|
|
|
renderLayout('/chat');
|
|
|
|
expect(screen.getByTestId('project-initialization-gate')).toBeVisible();
|
|
expect(screen.getByText('项目还差一步')).toBeVisible();
|
|
});
|
|
|
|
it('allows programming routes when the project has a fully configured Agent', () => {
|
|
const project = codingWorkspaceStore.getState().activeProject!;
|
|
useProjectConfigStore.setState({
|
|
configsByProjectId: {
|
|
[project.id]: { ...createCodingProjectConfigV2(), initialized: true, agents: [readyAgent] },
|
|
},
|
|
});
|
|
|
|
renderLayout('/chat');
|
|
|
|
expect(screen.queryByTestId('project-initialization-gate')).not.toBeInTheDocument();
|
|
expect(screen.getByTestId('route-content')).toBeVisible();
|
|
});
|
|
|
|
it('keeps the canonical plugin workspace outside the local project initialization gate', () => {
|
|
renderLayout('/plugins');
|
|
|
|
expect(screen.getByTestId('route-content')).toBeVisible();
|
|
expect(screen.queryByTestId('project-initialization-gate')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('keeps the programming workspace padding compensation separate from painting', () => {
|
|
renderLayout('/chat');
|
|
|
|
expect(screen.getByTestId('main-content')).toHaveClass('basis-0', 'overflow-hidden', 'p-0', 'sm:p-6');
|
|
});
|
|
|
|
it.each(['/learning', '/learning/project/project-1'])(
|
|
'keeps the project catalog route %s in the standard integrated shell',
|
|
(path) => {
|
|
renderLayout(path);
|
|
|
|
expect(screen.getByTestId('sidebar-stub')).toBeVisible();
|
|
expect(screen.getByTestId('titlebar-stub')).toHaveAttribute('data-integrated', 'true');
|
|
expect(screen.getByTestId('main-content')).toHaveClass('p-5', 'sm:p-6');
|
|
expect(screen.getByTestId('route-content')).toBeVisible();
|
|
expect(screen.queryByTestId('learning-profile-sync-error-gate')).not.toBeInTheDocument();
|
|
},
|
|
);
|
|
});
|