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(remote-desktop): 兼容 Windows CRLF 源码截取

Dashd1cd11ba6980
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/hostCommands.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/hostCommands.test.ts
index d34c60f778..62ddae11ed 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/hostCommands.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/hostCommands.test.ts
@@ -3,13 +3,15 @@ import { transpileModule, ScriptTarget } from 'typescript';
import { afterEach, expect, it, vi } from 'vitest';
import { parseDesktopIceReply } from '@cindy/device-link';
const source = readFileSync(new URL('../index.ts', import.meta.url), 'utf8');
-function between(start: string, end: string) {
+function between(source: string, start: string, end: string) {
+ // Git may check out this TypeScript source with CRLF on Windows.
+ source = source.replace(/\r\n/g, '\n');
const from = source.indexOf(start),
to = source.indexOf(end, from);
if (from < 0 || to < 0) throw new Error('Host command source missing');
return source.slice(from, to);
}
-function harness() {
+function harness(sourceText = source) {
const host = { isDestroyed: () => false, send: vi.fn() },
stopVideo = vi.fn();
let reply!: (event: object, id: string, value: unknown) => void;
@@ -31,8 +33,8 @@ function harness() {
},
};
const code = `let pending:any=null, videoAttempt='attempt';
- ${between('function requestHost(', 'async function ice(')}
- ${between('ipcMain.handle(DESKTOP_LOCAL.REPLY,', 'ipcMain.handle(\n DESKTOP_LOCAL.INPUT,')}
+ ${between(sourceText, 'function requestHost(', 'async function ice(')}
+ ${between(sourceText, 'ipcMain.handle(DESKTOP_LOCAL.REPLY,', 'ipcMain.handle(\n DESKTOP_LOCAL.INPUT,')}
return requestHost;`;
const js = transpileModule(code, { compilerOptions: { target: ScriptTarget.ES2022 } }).outputText;
const request = new Function(...Object.keys(deps), js)(...Object.values(deps));
@@ -44,27 +46,36 @@ function harness() {
};
}
afterEach(() => vi.useRealTimers());
-it.each(['ice', 'offer'])('limits %s timeout cleanup to the correct resource', async (op) => {
- vi.useFakeTimers();
- const h = harness();
- const result = h.request({ id: 'first', op }, 4000);
- const rejected = expect(result).rejects.toThrow('DESKTOP_VIDEO_TIMEOUT');
- await vi.advanceTimersByTimeAsync(4000);
- await rejected;
- expect(h.stopVideo).toHaveBeenCalledTimes(op === 'offer' ? 1 : 0);
- const next = h.request({ id: 'next', op: 'offer' }, 10_000);
- expect(() => h.reply('first', 'late')).toThrow('PERMISSION_DENIED');
- h.reply('next', 'answer');
- await expect(next).resolves.toBe('answer');
-});
-it('rejects a concurrent command and malformed ICE without stopping healthy video', async () => {
- vi.useFakeTimers();
- const h = harness();
- const result = h.request({ id: 'ice', op: 'ice' }, 4000);
- await expect(h.request({ id: 'offer', op: 'offer' }, 4000)).rejects.toThrow('DESKTOP_VIDEO_BUSY');
- expect(h.host.send).toHaveBeenCalledTimes(1);
- h.reply('ice', { attemptId: 'wrong', candidates: [], next: 0, complete: true });
- await expect(result).rejects.toThrow('DESKTOP_VIDEO_STOPPED');
- expect(h.stopVideo).not.toHaveBeenCalled();
- expect(vi.getTimerCount()).toBe(0);
-});
+const lineEndings = ['\n', '\r\n'];
+it.each(['ice', 'offer'].flatMap((op) => lineEndings.map((lineEnding) => ({ op, lineEnding }))))(
+ 'limits $op timeout cleanup with $lineEnding source',
+ async ({ op, lineEnding }) => {
+ vi.useFakeTimers();
+ const h = harness(source.replace(/\r?\n/g, lineEnding));
+ const result = h.request({ id: 'first', op }, 4000);
+ const rejected = expect(result).rejects.toThrow('DESKTOP_VIDEO_TIMEOUT');
+ await vi.advanceTimersByTimeAsync(4000);
+ await rejected;
+ expect(h.stopVideo).toHaveBeenCalledTimes(op === 'offer' ? 1 : 0);
+ const next = h.request({ id: 'next', op: 'offer' }, 10_000);
+ expect(() => h.reply('first', 'late')).toThrow('PERMISSION_DENIED');
+ h.reply('next', 'answer');
+ await expect(next).resolves.toBe('answer');
+ },
+);
+it.each(lineEndings)(
+ 'rejects concurrent commands and malformed ICE without stopping video (%j source)',
+ async (lineEnding) => {
+ vi.useFakeTimers();
+ const h = harness(source.replace(/\r?\n/g, lineEnding));
+ const result = h.request({ id: 'ice', op: 'ice' }, 4000);
+ await expect(h.request({ id: 'offer', op: 'offer' }, 4000)).rejects.toThrow(
+ 'DESKTOP_VIDEO_BUSY',
+ );
+ expect(h.host.send).toHaveBeenCalledTimes(1);
+ h.reply('ice', { attemptId: 'wrong', candidates: [], next: 0, complete: true });
+ await expect(result).rejects.toThrow('DESKTOP_VIDEO_STOPPED');
+ expect(h.stopVideo).not.toHaveBeenCalled();
+ expect(vi.getTimerCount()).toBe(0);
+ },
+);