24e5dda022
Keep Q&A in a separate persistent conversation with scoped evidence reads, no production tools and immutable delivery files. Add the completed-board UI, recovery handling and regression coverage.
162 lines
7.6 KiB
TypeScript
162 lines
7.6 KiB
TypeScript
// @vitest-environment happy-dom
|
|
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
import { afterEach, expect, it, vi } from 'vitest';
|
|
import { CodexPcbDock } from '@ziroeda/designer/src/codex-pcb/CodexPcbDock.js';
|
|
|
|
vi.mock('@ziroeda/designer/src/codex-pcb/DeliveryModel3D.js', () => ({
|
|
DeliveryModel3D: () => null,
|
|
}));
|
|
|
|
const healthy = { ok: true, status: 'ready', checked_at: 100, checks: [] };
|
|
const unavailable = { ok: false, status: 'unavailable', checked_at: 100,
|
|
checks: [{ name: 'eda_bridge', ok: false, message: 'BRIDGE_NOT_READY: command-space-missing' }] };
|
|
const providers = { default_provider: 'openai', providers: [
|
|
{ id: 'openai', label: 'OpenAI', model: 'test-model', effort: 'high', configured: true },
|
|
] };
|
|
function response(url: string, fallback: unknown): Response {
|
|
return Response.json(url.includes('/readiness') ? healthy : url.endsWith('/providers') ? providers : fallback);
|
|
}
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
localStorage.clear();
|
|
window.history.replaceState(null, '', '/');
|
|
vi.unstubAllEnvs();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it('checks environment on project entry while keeping task history lazy until opened', async () => {
|
|
const fetch = vi.fn(async (url: string) => response(url, []));
|
|
vi.stubGlobal('fetch', fetch);
|
|
render(<CodexPcbDock />);
|
|
expect(screen.queryByLabelText('AI PCB 全屏工作台')).toBeNull();
|
|
expect(fetch.mock.calls.map(([url]) => url)).toEqual(['/pcb-api/readiness']);
|
|
fireEvent.click(screen.getByRole('button', { name: /Codex AI 自动绘板/ }));
|
|
expect(screen.getByLabelText('AI PCB 全屏工作台')).toBeTruthy();
|
|
await waitFor(() => expect(fetch.mock.calls.some(([url]) => url === '/pcb-api/sessions')).toBe(true));
|
|
fireEvent.click(screen.getByRole('button', { name: '返回编辑器' }));
|
|
expect(screen.queryByLabelText('AI PCB 全屏工作台')).toBeNull();
|
|
});
|
|
|
|
it('preserves the local disable switch even with an explicit AI URL and saved session', () => {
|
|
vi.stubEnv('VITE_ENABLE_PCB_AI', 'false');
|
|
window.history.replaceState(null, '', '/?ai=1');
|
|
localStorage.setItem('vibemake.pcb2.persistent-session', 'pcb-saved');
|
|
const fetch = vi.fn();
|
|
vi.stubGlobal('fetch', fetch);
|
|
render(<CodexPcbDock />);
|
|
expect(screen.queryByLabelText('AI PCB 全屏工作台')).toBeNull();
|
|
expect(screen.queryByRole('button', { name: /Codex AI 自动绘板/ })).toBeNull();
|
|
expect(fetch).not.toHaveBeenCalled();
|
|
expect(localStorage.getItem('vibemake.pcb2.persistent-session')).toBe('pcb-saved');
|
|
});
|
|
|
|
it('uses the new session protocol without replaying the legacy task outbox', async () => {
|
|
window.history.replaceState(null, '', '/?ai=1');
|
|
const queued = JSON.stringify({ action: 'create', request_id: 'old-request', payload: {} });
|
|
localStorage.setItem('vibemake.pcb-service.outbox', queued);
|
|
const task = {
|
|
id: 'pcb-new',
|
|
request: '测试电源板',
|
|
state: 'planning',
|
|
busy: true,
|
|
cursor: 0,
|
|
created_at: 0,
|
|
};
|
|
const fetch = vi.fn(
|
|
async (url: string, init?: RequestInit) =>
|
|
response(url, init?.method === 'POST' || url.endsWith('/sessions/pcb-new') ? task : []),
|
|
);
|
|
vi.stubGlobal('fetch', fetch);
|
|
render(<CodexPcbDock />);
|
|
fireEvent.change(screen.getByLabelText('描述你的设计需求'), { target: { value: '测试电源板设计' } });
|
|
await waitFor(() => expect((screen.getByRole('button', { name: '发送需求 →' }) as HTMLButtonElement).disabled).toBe(false));
|
|
fireEvent.click(screen.getByRole('button', { name: '发送需求 →' }));
|
|
await waitFor(() =>
|
|
expect(fetch.mock.calls.some(([, init]) => init?.method === 'POST')).toBe(true),
|
|
);
|
|
const [url, init] = fetch.mock.calls.find(([, init]) => init?.method === 'POST')!;
|
|
expect(url).toBe('/pcb-api/sessions');
|
|
expect(JSON.parse(String(init?.body))).toEqual({ text: '测试电源板设计', provider: 'openai' });
|
|
expect(
|
|
fetch.mock.calls.every(([url]) => !url.includes('/v1/') && !url.includes('/commands')),
|
|
).toBe(true);
|
|
expect(localStorage.getItem('vibemake.pcb-service.outbox')).toBe(queued);
|
|
});
|
|
|
|
it('keeps approval disabled while an unsubmitted proposal comment exists', async () => {
|
|
window.history.replaceState(null, '', '/?ai=1');
|
|
localStorage.setItem('vibemake.pcb2.persistent-session', 'pcb-review');
|
|
const task = {
|
|
id: 'pcb-review',
|
|
request: '测试电源板设计',
|
|
state: 'review',
|
|
busy: false,
|
|
cursor: 0,
|
|
created_at: 0,
|
|
proposal: {
|
|
title: '电源板方案',
|
|
markdown: '已完成待审核的方案。',
|
|
revision: 2,
|
|
hash: 'current-hash',
|
|
},
|
|
};
|
|
const fetch = vi.fn(
|
|
async (url: string) =>
|
|
response(url, url.endsWith('/sessions') ? [task] : url.includes('/events?') ? [] : task),
|
|
);
|
|
vi.stubGlobal('fetch', fetch);
|
|
render(<CodexPcbDock />);
|
|
const approve = await screen.findByRole('button', { name: '确认方案并开始' });
|
|
await waitFor(() => expect((approve as HTMLButtonElement).disabled).toBe(false));
|
|
fireEvent.change(screen.getByLabelText('补充要求或修改方案'), {
|
|
target: { value: '请增加一个状态灯' },
|
|
});
|
|
expect((approve as HTMLButtonElement).disabled).toBe(true);
|
|
fireEvent.click(approve);
|
|
expect(fetch.mock.calls.every(([url]) => !url.endsWith('/approve'))).toBe(true);
|
|
});
|
|
|
|
it('warns immediately on project entry even before opening the AI panel', async () => {
|
|
const fetch = vi.fn(async () => Response.json(unavailable));
|
|
vi.stubGlobal('fetch', fetch);
|
|
render(<CodexPcbDock/>);
|
|
await screen.findByText('绘板环境未就绪,暂不能生成');
|
|
expect(screen.queryByLabelText('AI PCB 全屏工作台')).toBeNull();
|
|
expect(fetch.mock.calls.length).toBe(1);
|
|
fireEvent.click(screen.getByRole('button', { name: '查看检查结果' }));
|
|
expect(await screen.findByText('BRIDGE_NOT_READY: command-space-missing')).toBeTruthy();
|
|
});
|
|
|
|
it.each(['checking', 'unavailable'])('prevents generation while runtime is %s', async status => {
|
|
window.history.replaceState(null, '', '/?ai=1');
|
|
const fetch = vi.fn(async (url: string) => url.includes('/readiness')
|
|
? Response.json({ ...unavailable, status }) : response(url, []));
|
|
vi.stubGlobal('fetch', fetch);
|
|
render(<CodexPcbDock/>);
|
|
fireEvent.change(screen.getByLabelText('描述你的设计需求'), { target: { value: '测试电源板设计' } });
|
|
await screen.findByRole('option', { name: /OpenAI · test-model/ });
|
|
const create = screen.getByRole('button', { name: '发送需求 →' }) as HTMLButtonElement;
|
|
expect(create.disabled).toBe(true);
|
|
fireEvent.click(create);
|
|
expect(fetch.mock.calls.filter(([url]) => url.endsWith('/sessions')).length).toBe(1); // GET only
|
|
});
|
|
|
|
it('enables generation only after a successful manual recheck', async () => {
|
|
window.history.replaceState(null, '', '/?ai=1');
|
|
const fetch = vi.fn(async (url: string, _init?: RequestInit) => {
|
|
if (url.endsWith('/readiness')) return Response.json(unavailable);
|
|
if (url.endsWith('/readiness/check')) return Response.json(healthy);
|
|
return response(url, []);
|
|
});
|
|
vi.stubGlobal('fetch', fetch);
|
|
render(<CodexPcbDock/>);
|
|
fireEvent.change(screen.getByLabelText('描述你的设计需求'), { target: { value: '测试电源板设计' } });
|
|
const retry = await screen.findByRole('button', { name: '重新检查环境' });
|
|
expect((screen.getByRole('button', { name: '发送需求 →' }) as HTMLButtonElement).disabled).toBe(true);
|
|
fireEvent.click(retry);
|
|
await waitFor(() => expect((screen.getByRole('button', { name: '发送需求 →' }) as HTMLButtonElement).disabled).toBe(false));
|
|
expect(fetch.mock.calls.some(([url, init]) => url.endsWith('/readiness/check') && init?.method === 'POST')).toBe(true);
|
|
expect(fetch.mock.calls.some(([url, init]) => url.endsWith('/sessions') && init?.method === 'POST')).toBe(false);
|
|
});
|