Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

test(desktop): preserve import startup and empty slash behavior

MagicLizie4466fe2b6bd
diff --git a/apps/desktop/src/renderer/__tests__/sessionImportAutoScan.test.ts b/apps/desktop/src/renderer/__tests__/sessionImportAutoScan.test.ts
new file mode 100644
index 0000000000..08ebc2d987
--- /dev/null
+++ b/apps/desktop/src/renderer/__tests__/sessionImportAutoScan.test.ts
@@ -0,0 +1,107 @@
+// @vitest-environment jsdom
+
+import { StrictMode, createElement } from 'react';
+import { act, cleanup, render, screen } from '@testing-library/react';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { SessionImportSection } from '@/components/settings/SessionImportSection';
+
+vi.mock('react-i18next', () => {
+ const t = (key: string) => key;
+ return { useTranslation: () => ({ t }) };
+});
+vi.mock('@/lib/toast', () => ({ toast: { error: vi.fn(), success: vi.fn() } }));
+// The sharing wizard is a separate entry; keep the scanned-list UI real.
+vi.mock('@/components/settings/SessionShareImportWizard', () => ({
+ SessionShareImportWizard: () => null,
+}));
+
+type ScanResult = Awaited<ReturnType<typeof window.electronAPI.localDb.sessionImport.scan>>;
+const scan = vi.fn<typeof window.electronAPI.localDb.sessionImport.scan>();
+
+function scanResult(title: string): ScanResult {
+ return {
+ sources: { codexHomes: [], claudeRoots: [] },
+ candidates: [{
+ key: 'codex:' + title,
+ source: 'codex',
+ id: title,
+ title,
+ cwd: '',
+ updatedAt: '2026-10-16T00:00:00.000Z',
+ archived: false,
+ workspaceKind: 'dialogue',
+ sidebarBucket: 'dialogue',
+ projectDir: null,
+ }],
+ rejected: { codex: 0, claude: 0, existing: 0 },
+ currentProjectDirs: [],
+ };
+}
+
+function pendingScan() {
+ let resolve!: (value: ScanResult) => void;
+ const promise = new Promise<ScanResult>((done) => { resolve = done; });
+ return { promise, resolve };
+}
+
+beforeEach(() => {
+ scan.mockReset();
+ vi.stubGlobal('electronAPI', { localDb: { sessionImport: { scan } } });
+});
+
+afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+});
+
+// PR #6196 / engineering-conventions §3.2: replace the old source-text checks
+// with the real component's mount and cache-reentry behavior. SettingsView's
+// tests stub this component, so they cannot catch either regression. Restore
+// this module's original unit file: mount once with a pending scan; then remount
+// after one result, while its refresh is pending. No other component shares
+// this module cache or lifecycle, so these cases cannot join its parent tests.
+describe('SessionImportSection automatic scans', () => {
+ it('scans immediately once on mount and displays the result without a click', async () => {
+ const request = pendingScan();
+ scan.mockReturnValueOnce(request.promise);
+
+ render(createElement(StrictMode, null, createElement(SessionImportSection)));
+
+ expect(scan).toHaveBeenCalledExactlyOnceWith(undefined);
+ expect((screen.getByRole('button', { name: 'settings.sessionImport.scan' }) as HTMLButtonElement).disabled).toBe(true);
+
+ await act(async () => {
+ request.resolve(scanResult('Found on mount'));
+ await request.promise;
+ });
+
+ expect(screen.getByText('Found on mount')).toBeTruthy();
+ expect(scan).toHaveBeenCalledTimes(1);
+ expect((screen.getByRole('button', { name: 'settings.sessionImport.scan' }) as HTMLButtonElement).disabled).toBe(false);
+ });
+
+ it('keeps cached candidates visible on reentry until the automatic refresh finishes', async () => {
+ const initialScan = Promise.resolve(scanResult('Cached candidate'));
+ scan.mockReturnValueOnce(initialScan);
+ const first = render(createElement(SessionImportSection));
+ await act(async () => { await initialScan; });
+ expect(screen.getByText('Cached candidate')).toBeTruthy();
+ first.unmount();
+
+ const refresh = pendingScan();
+ scan.mockReturnValueOnce(refresh.promise);
+ render(createElement(SessionImportSection));
+
+ expect(scan).toHaveBeenCalledTimes(2);
+ expect(screen.getByText('Cached candidate')).toBeTruthy();
+
+ await act(async () => {
+ refresh.resolve(scanResult('Fresh candidate'));
+ await refresh.promise;
+ });
+
+ expect(screen.getByText('Fresh candidate')).toBeTruthy();
+ expect(screen.queryByText('Cached candidate')).toBeNull();
+ });
+});
diff --git a/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx b/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx
index 241ebe6aaf..b1b4261dc9 100644
--- a/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx
@@ -61,7 +61,7 @@ vi.mock('../NewGoalDialog', () => ({ NewGoalDialog: () => null }));
vi.mock('../FolderPickerPopover', () => ({ FolderPickerPopover: () => null, addRecentFolder: vi.fn() }));
vi.mock('../AtMentionPanel', () => ({ AtMentionPanel: (props: ComponentProps<typeof AtMentionPanel>) => { h.atPanel = props; return null; } }));
vi.mock('@/lib/atResourceService', async (original) => ({ ...await original<typeof import('@/lib/atResourceService')>(), scanAtResources: h.scan }));
-vi.mock('../SlashCommandPalette', () => ({ SlashCommandPalette: () => null }));
+vi.mock('../SlashCommandPalette', () => ({ SlashCommandPalette: ({ query }: { query: string }) => <div data-testid="test-slash-palette" data-query={query} /> }));
vi.mock('@/voice-input/VoiceInputPointerHintLayer', () => ({ VoiceInputPointerHintLayer: ({ children }: { children: import('react').ReactNode }) => <>{children}</> }));
vi.mock('@/voice-input/VoiceInputStatusNotice', () => ({ VoiceInputStatusNotice: () => null }));
vi.mock('@/voice-input/useVoiceInput', () => ({ useVoiceInput: (editor: Editor | null) => {
@@ -174,6 +174,30 @@ it('dismisses a preview on Enter without sending or changing the hidden draft',
expect(screen.queryByText('preview suggestion')).toBeNull();
});
+// The removed source guard did not prove the editor consumed the key. Mount the
+// real composer with send enabled: an unmatched slash must close its palette
+// without submitting or changing the draft. Reuse this editor/host harness.
+it.each(['Enter', 'Tab'])('consumes %s for an empty slash result without sending or editing', async key => {
+ h.remoteProviders = [sshNativeCodexProvider([sshModel('remote-only')])];
+ const view = renderComposer({ sessionId: 'slash-empty', remoteHostId: 'builder',
+ runtimeAgentKind: 'codex', vendorKey: 'codex', initialModel: 'remote-only',
+ initialProviderId: 'openai', initialEffort: 'low' });
+ await waitFor(() => expect(h.editor).not.toBeNull());
+ const text = '/zzzz-no-such-command';
+ await act(async () => {
+ h.editor!.commands.setContent('<p>' + text + '</p>');
+ h.editor!.commands.setTextSelection(text.length + 1);
+ });
+ await waitFor(() => expect(screen.getByTestId('test-slash-palette').getAttribute('data-query')).toBe(text.slice(1)));
+ expect((screen.getByRole('button', { name: 'newChat.sendButton.send' }) as HTMLButtonElement).disabled).toBe(false);
+ let allowed = true;
+ await act(async () => { allowed = fireEvent.keyDown(h.editor!.view.dom, { key, code: key }); });
+ expect(allowed).toBe(false);
+ expect(view.props.onSend).not.toHaveBeenCalled();
+ expect(h.editor!.getText()).toBe(text);
+ expect(screen.queryByTestId('test-slash-palette')).toBeNull();
+});
+
it('does not let a pending local resource scan repopulate an SSH composer', async () => {
let finish!: (value: Awaited<ReturnType<typeof scanAtResources>>) => void;
h.scan.mockReturnValue(new Promise(resolve => { finish = resolve; }));
diff --git a/docs/dev-rules/desktop-unit-test-performance.md b/docs/dev-rules/desktop-unit-test-performance.md
index c883809cda..6a27f6202e 100644
--- a/docs/dev-rules/desktop-unit-test-performance.md
+++ b/docs/dev-rules/desktop-unit-test-performance.md
@@ -78,6 +78,11 @@ Linux 及 package script 的启动方式不变。回归测试通过真实 runner
模块重载检测同样按 Vitest 绑定追踪解构、赋值与 namespace 别名,并检查 hook 及其
本地准备/清理函数;同名参数和非 Vitest 对象不会误判。既有账号用量初始化回归
改为带明确理由的单次直接重载,原 34 项通过(约 0.7 秒),不新增逐用例重载。
+导入设置恢复真实组件挂载回归,验证首次自动扫描与重入时先显示缓存、再更新结果,
+2 项单文件命令约 2.2 秒。slash 无匹配结果的 Enter/Tab 回归并入既有 ChatInput 文件,
+经真实编辑器键盘入口确认阻止发送、保留正文并关闭面板;整文件 8 项通过,冷启动
+16.64 秒(收集 14.86 秒、执行 0.83 秒)。该文件已有重型编辑器依赖,继续复用同一
+harness,未新增重复环境或放宽超时。
### 2026-10-16 实验与落地
@@ -122,11 +127,12 @@ Linux 及 package script 的启动方式不变。回归测试通过真实 runner
和设计契约,已恢复 30 个完整文件,并从 `homeUsageDashboardSource.test.ts` 保留账号隔离的 3 条
契约(含同步主干时保留的 `botChatSurface.test.ts`)。后续逐文件复核另保留分享脱敏的两条契约、
侧栏折叠时通知订阅归属的一条契约。随后将 Mobile 混合文件逐项行为化、合并必要原生契约,
- 最终相对原始基线删除 54 个文件(包含已迁入其他文件的覆盖),
+ 最终相对当前 main 删除 52 个测试文件(包含已迁入其他文件的覆盖),
不能沿用最初的「488 条无效用例」结论。
`FileTreeView.horizontalScroll.test.tsx` 真实挂载组件并断言可见宽度,已恢复。
IM 启动补全、空草稿水合与语音切换、预览误发送、远程扫描旧回包、图片标注误关闭、
- Orca 隐藏/可见时的通知生命周期,改由真实生产入口或组件行为回归覆盖。
+ Orca 隐藏/可见时的通知生命周期、导入设置自动扫描与 slash 空结果键盘保护,
+ 改由真实生产入口或组件行为回归覆盖。
原生源码检测包含 Swift、C/C++、Rust 等;必要的跨原生协议、权限与生命周期契约进入
required guard,执行脚本和 JSON/二进制资源不作为源码字面量断言。
- 初始审计的 207 个 PARTIAL 文件(2,874 条用例,其中约 991 条是静态或重复接线候选)与续跑、设备链、