25c5ecdf6e
Project CI / AI game creator shell Rust crates (pull_request) Successful in 2m58s
Project CI / AI game creator shell Rust lane 1/2 (pull_request) Successful in 6m34s
Project CI / AI game creator shell Rust lane 2/2 (pull_request) Successful in 5m29s
Project CI / Backend tests (pull_request) Failing after 4m38s
Project CI / Frontend tests (pull_request) Successful in 3m33s
Project CI / Repository checks (pull_request) Failing after 4m4s
Project CI / AI game creator shell web tests (pull_request) Successful in 3m13s
Project CI / Native shell tests (pull_request) Successful in 6m35s
- 78 处内联 window.__TAURI__ 赋值改为 installTauriRuntime,覆盖 appSurface 之外的 37 个测试文件 - 31 处 delete window.__TAURI__ / window.__TAURI__ = undefined 复位改为 resetTauriRuntime - 删除 recentProjectsHook、projectCreationDirectory、projectResourceLiveIntegration、useProjectResourceCardPreviews、resourceVersionReplacementModel、unityProjectOpen 里的本地 asTauriInvoke / asTauriListen 适配器与相关类型别名 - designProjectRestore 的 setup 参数由 ReturnType<typeof vi.fn> 收窄为具体 invoke 签名 - 迁移后仅 tests/tauriRuntimeFake.ts 直接读写 window.__TAURI__
112 lines
3.3 KiB
TypeScript
112 lines
3.3 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { act, cleanup, renderHook, waitFor } from '@testing-library/react';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { useDirectThreadChatSubscription } from '../src/view/project-development/chat/controller/useDirectThreadChatSubscription';
|
|
import { createTauriEventFake, type TauriEventFake } from './tauriEventFake';
|
|
import { resetTauriRuntime } from './tauriRuntimeFake';
|
|
|
|
const BOOTSTRAP = {
|
|
subscriptionId: 'direct-subscription-1',
|
|
lastCompletedItemId: null,
|
|
events: [],
|
|
};
|
|
|
|
let eventFake: TauriEventFake | null = null;
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
eventFake?.restore();
|
|
eventFake = null;
|
|
resetTauriRuntime();
|
|
delete (window as Window & { __TAURI_INTERNALS__?: unknown })
|
|
.__TAURI_INTERNALS__;
|
|
});
|
|
|
|
function installEventFake(
|
|
invoke: (command: string, args?: Record<string, unknown>) => Promise<unknown>,
|
|
) {
|
|
eventFake = createTauriEventFake({ invoke });
|
|
eventFake.install();
|
|
return eventFake;
|
|
}
|
|
|
|
describe('useDirectThreadChatSubscription', () => {
|
|
it('卸载时幂等注销 native subscriber', async () => {
|
|
const unsubscribeCalls: Record<string, unknown>[] = [];
|
|
const invoke = vi.fn(
|
|
async (command: string, args?: Record<string, unknown>) => {
|
|
if (command === 'subscribe_direct_project_thread') return BOOTSTRAP;
|
|
if (command === 'consume_direct_project_thread') return { events: [] };
|
|
if (command === 'unsubscribe_direct_project_thread') {
|
|
unsubscribeCalls.push(args ?? {});
|
|
}
|
|
return null;
|
|
},
|
|
);
|
|
installEventFake(invoke);
|
|
|
|
const { unmount } = renderHook(() =>
|
|
useDirectThreadChatSubscription({
|
|
enabled: true,
|
|
projectPath: 'C:/projects/demo',
|
|
}),
|
|
);
|
|
await waitFor(() =>
|
|
expect(invoke).toHaveBeenCalledWith('subscribe_direct_project_thread', {
|
|
projectPath: 'C:/projects/demo',
|
|
}),
|
|
);
|
|
|
|
unmount();
|
|
await act(async () => {
|
|
await Promise.resolve();
|
|
});
|
|
|
|
expect(unsubscribeCalls).toEqual([
|
|
{ subscriptionId: BOOTSTRAP.subscriptionId },
|
|
]);
|
|
});
|
|
|
|
it('bootstrap 迟到于卸载时仍注销已登记的 native subscriber', async () => {
|
|
let resolveBootstrap!: (value: typeof BOOTSTRAP) => void;
|
|
const unsubscribeCalls: Record<string, unknown>[] = [];
|
|
const invoke = vi.fn(
|
|
async (command: string, args?: Record<string, unknown>) => {
|
|
if (command === 'subscribe_direct_project_thread') {
|
|
return new Promise<typeof BOOTSTRAP>((resolve) => {
|
|
resolveBootstrap = resolve;
|
|
});
|
|
}
|
|
if (command === 'unsubscribe_direct_project_thread') {
|
|
unsubscribeCalls.push(args ?? {});
|
|
}
|
|
return { events: [] };
|
|
},
|
|
);
|
|
installEventFake(invoke);
|
|
|
|
const { unmount } = renderHook(() =>
|
|
useDirectThreadChatSubscription({
|
|
enabled: true,
|
|
projectPath: 'C:/projects/demo',
|
|
}),
|
|
);
|
|
await waitFor(() =>
|
|
expect(invoke).toHaveBeenCalledWith('subscribe_direct_project_thread', {
|
|
projectPath: 'C:/projects/demo',
|
|
}),
|
|
);
|
|
|
|
unmount();
|
|
await act(async () => {
|
|
resolveBootstrap(BOOTSTRAP);
|
|
await Promise.resolve();
|
|
});
|
|
|
|
expect(unsubscribeCalls).toEqual([
|
|
{ subscriptionId: BOOTSTRAP.subscriptionId },
|
|
]);
|
|
});
|
|
});
|