Files
Genarrative/apps/ai-game-creator-shell/tests/errorReporting.test.ts
T
k88936 876529e668 删除 Project Supervisor 前端链路并将项目对话收敛到 DirectProject 与立项策划
- 删除 ProjectSupervisorView、SupervisorChatOnlyView、ProjectWorkspaceChatPane、AgentConversationOverlay、DeveloperProjectPanels、DeveloperRuntimePanels 与 features/agent-runtime/panels.tsx
- 删除 Supervisor 独立调试窗口:windows.rs 的 supervisor_chat_window_url / open_project_supervisor_chat_window、main.rs 的 invoke 注册、?supervisor-chat 与 ?agent-chat 前端入口、developer.json capability 和对应 Rust 用例
- 删除工作台壳的开发者 Agent 面板:DeveloperAgentPanel、useDeveloperAgentPanel、useDeveloperAgentState、developerAgentControls
- App.tsx 删除只服务退役面板的 state/ref/effect/handler(agentConversation*、文件/记忆/资产/画板/预览面板处理、commandLog、llmConfigStatus、editorBaseUrl、回放历史与 trace 面板状态、selectedAgent 等)以及由此产生的空分支,并把 requestRuntimeConfigOpen 接回本地 RuntimeConfigDialog
- 立项策划独立成模块:Design Agent 与 Planning V2 的容器和表现移到 view/project-development/planning/(PlanningChatView、PlanningUserInputCard、GddApprovalCard、DesignAgentSurface、PlanningLaneRuntimeStrip、planningLane、planningSessionV2、planningSessionContract)
- 改名到中性概念:ProjectSupervisorComponentProps→ProjectChatComponentProps、WorkspaceLauncherShellProps.ProjectSupervisor→ProjectChat、ProjectDevelopmentView.supervisor→chat、orchestrationMode→agentDockVisible、initialSupervisorMessageClaims→initialTurnClaims、ProjectManifestSnapshotSource 'supervisor'→'chat'、CSS project-supervisor-*/project-planning-*→project-chat-*
- 删除 PROJECT_SUPERVISOR_AGENT_ID 与 PROJECT_SUPERVISOR_PLAN_SOURCE 常量
- 工作台壳自己订阅 game-creator-manifest-invalidated,用 revision→清单→revision 配对读(source: asset-event)并入 currentProjectContext,且不重新检查项目目录;测试夹具改为按监听器集合广播该事件
- 删除只钉住退役界面的 appSurface 用例(旧调试窗口、开发者面板、Agent 对话浮层、运行历史面板)
- 同步文档:ADR、DirectProject 聊天模块抽离实施计划与里程碑、Provider 推理里程碑、策划会话 RuntimeV2、AI 游戏创作实施计划,并在 decision-log 新增 2026-09-19 条
- 删除随退役面板失去调用方的模块与导出:projectSummaryCommands.ts(1054 行旧 Supervisor 斜杠命令处理器)、AGENT_RUN_HISTORY_* 常量、agent-runtime/model.ts 与 project-summary/agentPresentation.ts 里的死导出,以及 agentRunTrace / memoryCommands / projectCommandPolicy 中无调用方的校验与解析函数
- 更新 scripts/check-config.mjs 与 scripts/check-native-shells.mjs 的窗口与命令守卫
2026-09-19 21:59:44 +08:00

233 lines
7.3 KiB
TypeScript

/** @vitest-environment jsdom */
import { afterEach, describe, expect, it, vi } from 'vitest';
type FakeErrorEvent = {
eventId: string;
fingerprint: string;
source: string;
message: string;
stack?: string;
occurredAt: string;
count: number;
};
const fakeRustQueue = vi.hoisted(() => {
const events = new Map<string, FakeErrorEvent>();
let sequence = 0;
return {
events,
get sequence() {
return sequence;
},
next() {
sequence += 1;
return sequence;
},
reset() {
events.clear();
sequence = 0;
},
};
});
vi.mock('@tauri-apps/api/core', () => ({
invoke: vi.fn(async (command: string, args?: Record<string, unknown>) => {
if (command === 'report_client_error') {
const fingerprint = `${args?.source ?? 'client'}|${args?.action ?? ''}|${args?.page ?? ''}|${args?.message ?? ''}`;
const existing = fakeRustQueue.events.get(fingerprint);
if (existing) {
existing.count += 1;
return existing;
}
const event = {
eventId: `rust-error-${fakeRustQueue.next()}`,
fingerprint,
source: String(args?.source ?? 'client'),
message: String(args?.message ?? ''),
stack: typeof args?.stack === 'string' ? args.stack : undefined,
occurredAt: '1',
count: 1,
};
if (fakeRustQueue.events.size >= 100) {
const oldest = fakeRustQueue.events.keys().next().value;
if (oldest) fakeRustQueue.events.delete(oldest);
}
fakeRustQueue.events.set(fingerprint, event);
return event;
}
if (command === 'get_pending_error_reports')
return [...fakeRustQueue.events.values()];
if (command === 'ack_error_reports') {
for (const event of fakeRustQueue.events.values()) {
const eventIds = Array.isArray(args?.eventIds) ? args.eventIds : [];
if (eventIds.includes(event.eventId))
fakeRustQueue.events.delete(event.fingerprint);
}
return undefined;
}
return undefined;
}),
}));
vi.mock('../src/services/clientAuth', () => ({
getStoredAuthAccessToken: vi.fn(() => 'test-token'),
}));
vi.mock('../src/services/clientHttp', () => ({
fetchClientHttp: vi.fn(),
getClientServerBaseUrl: vi.fn(() => 'https://example.test'),
}));
import { invoke } from '@tauri-apps/api/core';
import { fetchClientHttp } from '../src/services/clientHttp';
import {
ackClientErrorEventsWithRetry,
captureAgentRuntimeError,
captureClientError,
getPendingClientErrorEvents,
getStableErrorReportSubmissionId,
installWebviewLogBridge,
markClientErrorEventsSubmitted,
resetClientErrorEventsForTests,
shouldCaptureClientError,
submitErrorReportBatch,
subscribeClientErrorEvents,
} from '../src/services/errorReporting';
describe('客户端错误报告池', () => {
afterEach(() => {
fakeRustQueue.reset();
resetClientErrorEventsForTests();
vi.clearAllMocks();
});
it('按 fingerprint 合并重复错误并累计次数', async () => {
const first = await captureClientError(new Error('重复错误'), {
source: 'test',
});
const second = await captureClientError(new Error('重复错误'), {
source: 'test',
});
expect(second.eventId).toBe(first.eventId);
expect(await getPendingClientErrorEvents()).toHaveLength(1);
expect((await getPendingClientErrorEvents())[0]?.count).toBe(2);
});
it('使用统一上下文采集 Agent Runtime 错误', async () => {
await captureAgentRuntimeError(
new Error('kind=codex-app-server-other fingerprint=dynamic chars=12'),
'planning-agent-v2',
);
expect(invoke).toHaveBeenCalledWith('report_client_error', {
source: 'agent-runtime',
message: 'kind=codex-app-server-other fingerprint=dynamic chars=12',
stack: expect.any(String),
action: 'agent-runtime',
page: 'planning-agent-v2',
});
});
it('限制当前进程错误池最多保留 100 条', async () => {
for (let index = 0; index < 101; index += 1) {
await captureClientError(new Error(`错误 ${index}`), { source: 'test' });
}
expect(await getPendingClientErrorEvents()).toHaveLength(100);
});
it('提交成功后只清除本次提交的事件', async () => {
const first = await captureClientError(new Error('第一个错误'), {
source: 'test',
});
const second = await captureClientError(new Error('第二个错误'), {
source: 'test',
});
vi.mocked(fetchClientHttp).mockResolvedValue(
new Response(JSON.stringify({ data: { batchId: 'batch-1' } }), {
status: 200,
}),
);
await submitErrorReportBatch({ events: [first], logs: [] });
await markClientErrorEventsSubmitted([first]);
expect(await getPendingClientErrorEvents()).toEqual([second]);
});
it('同一批事件复用稳定 submissionId', async () => {
const event = await captureClientError(new Error('可重试错误'), {
source: 'test',
});
const firstId = getStableErrorReportSubmissionId([event]);
const secondId = getStableErrorReportSubmissionId([event]);
expect(secondId).toBe(firstId);
});
it('本地 ack 失败时自动重试且不向调用方抛错', async () => {
vi.useFakeTimers();
const event = await captureClientError(new Error('ack 重试错误'), {
source: 'test',
});
vi.mocked(invoke)
.mockRejectedValueOnce(new Error('暂时不可用'))
.mockResolvedValueOnce(undefined);
const pending = ackClientErrorEventsWithRetry([event]);
await vi.advanceTimersByTimeAsync(500);
await pending;
expect(invoke).toHaveBeenCalledWith('ack_error_reports', {
eventIds: [event.eventId],
});
expect(
vi
.mocked(invoke)
.mock.calls.filter(([command]) => command === 'ack_error_reports'),
).toHaveLength(2);
vi.useRealTimers();
});
it('未登录时拒绝提交且不发请求', async () => {
const auth = await import('../src/services/clientAuth');
vi.mocked(auth.getStoredAuthAccessToken).mockReturnValue('');
await expect(
submitErrorReportBatch({ events: [], logs: [] }),
).rejects.toThrow('请先登录');
expect(fetchClientHttp).not.toHaveBeenCalled();
});
it('只采集网络错误、408 和 5xx', () => {
expect(shouldCaptureClientError({ status: 400 })).toBe(false);
expect(shouldCaptureClientError({ status: 401 })).toBe(false);
expect(shouldCaptureClientError({ status: 429 })).toBe(false);
expect(shouldCaptureClientError({ status: 408 })).toBe(true);
expect(shouldCaptureClientError({ status: 503 })).toBe(true);
expect(shouldCaptureClientError({ networkError: true })).toBe(true);
});
it('将 WebView console 输出写入普通文本日志 command', async () => {
const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => {});
const uninstall = installWebviewLogBridge();
console.info('hello', { count: 1 });
await Promise.resolve();
expect(invoke).toHaveBeenCalledWith('append_application_log', {
level: 'info',
source: 'webview',
message: 'hello {"count":1}',
});
uninstall();
consoleInfo.mockRestore();
});
it('浏览器预览中订阅错误事件不会产生未处理拒绝', async () => {
delete window.__TAURI__;
const unsubscribe = subscribeClientErrorEvents(vi.fn());
unsubscribe();
await Promise.resolve();
});
});