Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
// @vitest-environment jsdom
import { act, createElement } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, expect, it, vi } from 'vitest';
const h = vi.hoisted(() => ({ sheet: {} as any, invoke: vi.fn(), submit: vi.fn(), openLink: vi.fn(), created: vi.fn(), uuid: vi.fn(() => 'fixture-request-12345') }));
vi.mock('react-native', () => ({
StyleSheet: { create: (v: unknown) => v }, View: ({ children }: any) => createElement('div', {}, children), ScrollView: ({ children }: any) => createElement('div', {}, children),
Pressable: ({ onPress, children }: any) => createElement('button', { onClick: onPress }, children),
Switch: ({ accessibilityLabel, value, disabled, onValueChange }: any) => createElement('input', { type: 'checkbox', 'aria-label': accessibilityLabel, checked: value, disabled, onChange: (e: any) => onValueChange(e.target.checked) }),
}));
vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string, options?: Record<string, string>) => options ? `${key} ${Object.values(options).join(' ')}` : key, i18n: { language: 'en' } }) }));
vi.mock('expo-crypto', () => ({ randomUUID: () => h.uuid() }));
vi.mock('@/device-link/DeviceLinkContext', () => ({ useDeviceLink: () => ({ invoke: h.invoke, openLink: h.openLink }) }));
vi.mock('@/device-link/remoteResources', () => ({ invokeRemoteResourceAction: (...args: unknown[]) => h.submit(...args) }));
vi.mock('@/components/AppText', () => ({ Text: ({ children }: any) => createElement('span', {}, children), TextInput: ({ value, editable, onChangeText }: any) => createElement('input', { value, disabled: !editable, onInput: (e: any) => onChangeText(e.target.value), onChange() {} }) }));
vi.mock('@/components/MobilePrimitives', () => ({ MainWindowActionButton: ({ action }: any) => createElement('button', { onClick: action.onPress, disabled: action.disabled }, action.label) }));
vi.mock('@/session/CompanionSheet', () => ({ CompanionSheet: (props: any) => { h.sheet = props; return props.children; } }));
vi.mock('@/session/CompanionPortraitPicker', () => ({ randomCompanionPortrait: async () => 'original-portrait', CompanionPortraitPicker: ({ onChange }: any) => createElement('button', { onClick: () => onChange('chosen-existing-portrait') }, 'existing portrait picker') }));
vi.mock('@/theme', async () => ({ ...await import('@/theme/tokens'), useThemedStyles: () => ({}) }));
import { CompanionImportSheet } from '@/session/CompanionImportSheet';
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
let root: Root | undefined;
afterEach(() => { act(() => root?.unmount()); root = undefined; vi.clearAllMocks(); h.uuid.mockReset().mockReturnValue('fixture-request-12345'); });
it('uses the existing portrait picker and sends only the remaining selections through the host resource', async () => {
let imported = false;
const result = { requestId: 'fixture-request-12345', botId: 'bot', canonicalSessionId: 'chat', status: 'complete', checks: [] };
h.invoke.mockImplementation(async (_host: string, _channel: string, args: any[]) => {
const id = args[0].ref.id;
return { blocks: [{ primitive: 'companion-import', data: id === 'sources' ? { sources: [{ id: 'source', name: 'Ada', kind: 'hermes' }] } : id.startsWith('preview:') ? { preview: { id: 'preview', name: 'Ada', source: { id: 'source', name: 'Ada', kind: 'hermes' }, entries: [
{ id: 'personality', name: 'SOUL.md', category: 'personality', selected: true },
{ id: 'memory', name: 'USER.md', category: 'memory', selected: true },
{ id: 'unused', name: 'unused', category: 'skills', selected: false },
] } } : { result: imported ? result : null } }] };
});
h.submit.mockImplementation(async () => { imported = true; return { effects: [] }; });
const container = document.createElement('div'); root = createRoot(container);
await act(async () => root!.render(createElement(CompanionImportSheet, { visible: true, deviceId: 'host', deviceName: 'Mac', online: true, onClose() {}, onCreated: h.created })));
expect(h.sheet.contentScrolls).toBe(true);
const click = async (text: string) => { await act(async () => { const button = [...container.querySelectorAll('button')].find(button => button.textContent === text); expect(button).toBeDefined(); button!.click(); }); };
await click('Ada · Hermes');
await click('existing portrait picker');
await act(async () => (container.querySelector('[aria-label="devices.companionImport.memory"]') as HTMLInputElement).click());
await click('devices.companionImport.submit');
expect(h.submit.mock.calls[0]?.[2].input).toMatchObject({ entryIds: ['personality'], avatarImageBase64: 'chosen-existing-portrait' });
expect(h.created).toHaveBeenCalledExactlyOnceWith({ collectionId: 'teammates', kind: 'bot', id: 'bot' });
expect(container.textContent).not.toContain('devices.companionImport.open');
});
it.each(['IMPORT_NAME_EXISTS', 'INVALID_SELECTION', 'PROFILE_TEXT_TOO_LARGE', 'SOURCE_SNAPSHOT_TOO_LARGE', 'SOURCE_TOO_MANY_FILES', 'SOURCE_FILE_TOO_LARGE', 'SOURCE_ITEM_TOO_LARGE', 'SOURCE_LINK_OUTSIDE_FOLDER', 'SOURCE_LINK_CYCLE', 'SOURCE_NOT_REGULAR_FILE', 'SOURCE_CHANGED', 'INTERNAL'])('unlocks only definitive %s errors and preserves an ambiguous request', async code => {
h.uuid.mockReturnValueOnce('fixture-request-original').mockReturnValue('fixture-request-corrected');
h.invoke.mockImplementation(async (_host: string, _channel: string, args: any[]) => {
const id = args[0].ref.id;
return { blocks: [{ primitive: 'companion-import', data: id === 'sources' ? { sources: [{ id: 'source', name: 'Ada', kind: 'hermes' }] } : id.startsWith('preview:') ? { preview: { id: 'preview', name: 'Ada', source: { id: 'source', name: 'Ada', kind: 'hermes' }, entries: [
{ id: 'work', name: 'Work', category: 'connections', selected: false, exclusiveWith: ['personal'] },
{ id: 'personal', name: 'Personal', category: 'connections', selected: false, exclusiveWith: ['work'] },
] } } : { result: null } }] };
});
h.submit.mockRejectedValue(new Error(code === 'INTERNAL' ? '[INTERNAL] remote resource provider failed' : `[INVALID_PARAMS] ${code}`));
const container = document.createElement('div'); root = createRoot(container);
await act(async () => root!.render(createElement(CompanionImportSheet, { visible: true, deviceId: 'host', deviceName: 'Mac', online: true, onClose() {}, onCreated: h.created })));
const click = async (text: string) => { await act(async () => { const button = [...container.querySelectorAll('button')].find(button => button.textContent?.startsWith(text)); expect(button).toBeDefined(); button!.click(); }); };
await click('Ada · Hermes');
await click('devices.companionImport.connections');
await act(async () => (container.querySelector('[aria-label="Work"]') as HTMLInputElement).click());
await act(async () => (container.querySelector('[aria-label="Personal"]') as HTMLInputElement).click());
expect((container.querySelector('[aria-label="Work"]') as HTMLInputElement).checked).toBe(false);
await click('devices.companionImport.back');
await click('devices.companionImport.submit');
const input = container.querySelector('input:not([type="checkbox"])') as HTMLInputElement;
expect(input.disabled).toBe(code === 'INTERNAL');
if (code !== 'INTERNAL') {
await act(async () => {
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, 'Corrected name');
input.dispatchEvent(new Event('input', { bubbles: true }));
(container.querySelector('[aria-label="devices.companionImport.connections"]') as HTMLInputElement).click();
});
}
await click('devices.companionImport.submit');
const requests = h.submit.mock.calls.map(call => call[2].input);
expect(requests).toHaveLength(2);
expect(requests[0]).toMatchObject({ name: 'Ada', entryIds: ['personal'], requestId: 'fixture-request-original' });
expect(requests[1]).toMatchObject(code === 'INTERNAL' ? requests[0] : { name: 'Corrected name', entryIds: [], requestId: 'fixture-request-corrected' });
});
it.each(['PREVIEW_EXPIRED', 'SELECTION_CHANGED'])('refreshes source IDs and allows a new selection after %s', async code => {
let generation = 0;
h.uuid.mockReturnValueOnce('fixture-original-request').mockReturnValue('fixture-new-request');
h.invoke.mockImplementation(async (_host: string, _channel: string, args: any[]) => {
const id = args[0].ref.id;
return { blocks: [{ primitive: 'companion-import', data: id === 'sources' ? { sources: [{ id: `source-${++generation}`, name: 'Ada', kind: 'hermes' }] } : id.startsWith('preview:') ? { preview: { id: `preview-${generation}`, name: 'Ada', source: { id: `source-${generation}`, name: 'Ada', kind: 'hermes' }, entries: [] } } : { result: null } }] };
});
h.submit.mockRejectedValue(new Error(`[INVALID_PARAMS] ${code}`));
const container = document.createElement('div'); root = createRoot(container);
await act(async () => root!.render(createElement(CompanionImportSheet, { visible: true, deviceId: 'host', deviceName: 'Mac', online: true, onClose() {}, onCreated: h.created })));
const click = async (text: string) => { await act(async () => { const button = [...container.querySelectorAll('button')].find(button => button.textContent === text); expect(button).toBeDefined(); button!.click(); }); };
await click('Ada · Hermes'); await click('devices.companionImport.submit');
expect(generation).toBe(2);
await click('Ada · Hermes');
expect((container.querySelector('input') as HTMLInputElement).disabled).toBe(false);
await click('devices.companionImport.submit');
expect(h.submit.mock.calls.map(call => call[2].input)).toEqual([
expect.objectContaining({ requestId: 'fixture-original-request', previewId: 'preview-1' }),
expect.objectContaining({ requestId: 'fixture-new-request', previewId: 'preview-2' }),
]);
});
it('clears an earlier receipt after a name conflict and uses the existing editable form for a new request', async () => {
h.uuid.mockReturnValueOnce('fixture-original-request').mockReturnValue('fixture-renamed-request');
let hasReceipt = false;
h.invoke.mockImplementation(async (_host: string, _channel: string, args: any[]) => {
const id = args[0].ref.id;
return { blocks: [{ primitive: 'companion-import', data: id === 'sources' ? { sources: [{ id: 'source', name: 'Ada', kind: 'hermes' }] }
: id.startsWith('preview:') ? { preview: { id: 'preview', name: 'Ada', source: { id: 'source', name: 'Ada', kind: 'hermes' }, entries: [] } }
: { result: hasReceipt ? { requestId: 'fixture-original-request', botId: 'bot', status: 'needs-attention', checks: [] } : null } }] };
});
h.submit.mockReset().mockImplementationOnce(async () => { hasReceipt = true; return { effects: [] }; })
.mockImplementation(async () => { hasReceipt = false; throw new Error('[INVALID_PARAMS] IMPORT_NAME_EXISTS'); });
const container = document.createElement('div'); root = createRoot(container);
await act(async () => root!.render(createElement(CompanionImportSheet, { visible: true, deviceId: 'host', deviceName: 'Mac', online: true, onClose() {}, onCreated: h.created })));
const click = async (text: string) => { await act(async () => { const button = [...container.querySelectorAll('button')].find(button => button.textContent === text); expect(button).toBeDefined(); button!.click(); }); };
await click('Ada · Hermes');
await click('devices.companionImport.submit');
await click('devices.companionImport.retry');
const input = container.querySelector('input:not([type="checkbox"])') as HTMLInputElement;
expect(input.disabled).toBe(false);
await act(async () => {
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, 'Grace');
input.dispatchEvent(new Event('input', { bubbles: true }));
});
await click('devices.companionImport.submit');
const requests = h.submit.mock.calls.map(call => call[2].input);
expect(requests).toHaveLength(3);
expect(requests[1].requestId).toBe(requests[0].requestId);
expect(requests[2]).toMatchObject({ requestId: 'fixture-renamed-request', name: 'Grace' });
});
it('rejects an oversized action before freezing the selection and can submit after deselection', async () => {
const avatar = 'a'.repeat(55_000);
const entries = Array.from({ length: 400 }, (_, index) => ({ id: `entry-${index}-` + 'x'.repeat(30), name: `Entry ${index}`, category: 'memory', selected: true }));
h.invoke.mockImplementation(async (_host: string, _channel: string, args: any[]) => {
const id = args[0].ref.id;
return { blocks: [{ primitive: 'companion-import', data: id === 'sources' ? { sources: [{ id: 'source', name: 'Ada', kind: 'hermes' }] }
: id.startsWith('preview:') ? { preview: { id: 'preview', name: 'Ada', avatarImageBase64: avatar, source: { id: 'source', name: 'Ada', kind: 'hermes' }, entries } }
: { result: null } }] };
});
h.submit.mockReset().mockResolvedValue({ effects: [] });
const container = document.createElement('div'); root = createRoot(container);
await act(async () => root!.render(createElement(CompanionImportSheet, { visible: true, deviceId: 'host', deviceName: 'Mac', online: true, onClose() {}, onCreated: h.created })));
const click = async (text: string) => { await act(async () => { const button = [...container.querySelectorAll('button')].find(button => button.textContent === text); expect(button).toBeDefined(); button!.click(); }); };
await click('Ada · Hermes');
await click('devices.companionImport.submit');
expect(h.submit).not.toHaveBeenCalled();
expect(h.invoke.mock.calls.some(call => call[2][0].ref.id.startsWith('result:'))).toBe(false);
expect((container.querySelector('input:not([type="checkbox"])') as HTMLInputElement).disabled).toBe(false);
await act(async () => (container.querySelector('[aria-label="devices.companionImport.memory"]') as HTMLInputElement).click());
await click('devices.companionImport.submit');
expect(h.submit).toHaveBeenCalledTimes(1);
const request = h.submit.mock.calls[0]![2];
expect(request.input).toMatchObject({ entryIds: [], avatarImageBase64: avatar });
const { parseRemoteActionInvokeRequest, REMOTE_RESOURCE_PROTOCOL_VERSION } = await import('@cindy/device-link');
expect(parseRemoteActionInvokeRequest({ ...request, client: { protocolVersion: REMOTE_RESOURCE_PROTOCOL_VERSION, primitives: [] } })).not.toBeNull();
});
it('submits all 10,000 selected entries compactly without raising the transport budget', async () => {
const entries = Array.from({ length: 10_000 }, (_, index) => ({ id: `entry-${index}`, name: `Memory ${index}`, category: 'memory', selected: true }));
h.invoke.mockImplementation(async (_host: string, _channel: string, args: any[]) => {
const id = args[0].ref.id;
return { blocks: [{ primitive: 'companion-import', data: id === 'sources' ? { sources: [{ id: 'source', name: 'Ada', kind: 'hermes' }] }
: id.startsWith('preview:') ? { preview: { id: 'preview', selectionRanges: true, name: 'Ada', source: { id: 'source', name: 'Ada', kind: 'hermes' }, entries } }
: { result: null } }] };
});
h.submit.mockReset().mockResolvedValue({ effects: [] });
const container = document.createElement('div'); root = createRoot(container);
await act(async () => root!.render(createElement(CompanionImportSheet, { visible: true, deviceId: 'host', deviceName: 'Mac', online: true, onClose() {}, onCreated: h.created })));
const click = async (text: string) => { await act(async () => [...container.querySelectorAll('button')].find(button => button.textContent === text)!.click()); };
await click('Ada · Hermes'); await click('devices.companionImport.submit');
expect(h.submit.mock.calls[0]?.[2].input).toMatchObject({ entryIds: [], entryRanges: [[0, 9999]], deferSetup: true });
});
it('shows failed filenames and saved parts after a partial import without blocking chat or retry', async () => {
const result = { requestId: 'fixture-request-12345', botId: 'bot', canonicalSessionId: 'chat', saved: true, savedEntryIds: [], status: 'needs-attention',
checks: [{ entryId: 'memory', status: 'needs-attention', message: 'IMPORT_DISK_FULL', progress: { saved: 1, total: 3 } }] };
h.invoke.mockImplementation(async (_host: string, _channel: string, args: any[]) => {
const id = args[0].ref.id;
return { blocks: [{ primitive: 'companion-import', data: id === 'sources' ? { sources: [{ id: 'source', name: 'Ada', kind: 'hermes' }] }
: id.startsWith('preview:') ? { preview: { id: 'preview', name: 'Ada', source: { id: 'source', name: 'Ada', kind: 'hermes' }, entries: [{ id: 'memory', name: 'long-document.md', category: 'memory', selected: true }] } }
: { result } }] };
});
h.submit.mockReset().mockResolvedValue({ effects: [] });
const container = document.createElement('div'); root = createRoot(container);
await act(async () => root!.render(createElement(CompanionImportSheet, { visible: true, deviceId: 'host', deviceName: 'Mac', online: true, onClose() {}, onCreated: h.created })));
const click = async (text: string) => { await act(async () => { const button = [...container.querySelectorAll('button')].find(button => button.textContent?.startsWith(text)); expect(button).toBeDefined(); button!.click(); }); };
await click('Ada · Hermes'); await click('devices.companionImport.submit');
expect(container.textContent).toContain('devices.companionImport.partial');
expect(container.textContent).not.toContain('devices.companionImport.complete');
await click('devices.companionImport.details');
expect(container.textContent).toContain('long-document.md');
expect(container.textContent).toContain('devices.companionImport.partlySaved');
expect(container.textContent).toContain('devices.companionImport.diskFull');
expect(container.textContent).toContain('1 / 3');
expect(container.textContent).toContain('devices.companionImport.retry');
await click('devices.companionImport.open');
expect(h.created).toHaveBeenCalledWith({ collectionId: 'teammates', kind: 'bot', id: 'bot' });
});
it('submits sparse 100,000-entry choices using bounded actions with the original selection intact', async () => {
const entries = Array.from({ length: 100_000 }, (_, index) => ({ id: `entry-${index}`, name: `Memory ${index}`, category: 'memory', selected: index % 2 === 0 }));
h.invoke.mockImplementation(async (_host: string, _channel: string, args: any[]) => {
const id = args[0].ref.id;
return { blocks: [{ primitive: 'companion-import', data: id === 'sources' ? { sources: [{ id: 'source', name: 'Ada', kind: 'hermes' }] }
: id.startsWith('preview:') ? { preview: { id: 'preview', selectionRanges: true, selectionChunks: true, name: 'Ada', source: { id: 'source', name: 'Ada', kind: 'hermes' }, entries } }
: { result: null } }] };
});
h.submit.mockReset().mockResolvedValue({ effects: [] });
const container = document.createElement('div'); root = createRoot(container);
await act(async () => root!.render(createElement(CompanionImportSheet, { visible: true, deviceId: 'host', deviceName: 'Mac', online: true, onClose() {}, onCreated: h.created })));
const click = async (text: string) => { await act(async () => [...container.querySelectorAll('button')].find(button => button.textContent === text)!.click()); };
await click('Ada · Hermes'); await click('devices.companionImport.submit');
const { parseRemoteActionInvokeRequest, REMOTE_RESOURCE_PROTOCOL_VERSION } = await import('@cindy/device-link');
expect(h.submit.mock.calls.length).toBeGreaterThan(2);
let text = '';
for (const call of h.submit.mock.calls) {
const request = call[2];
expect(parseRemoteActionInvokeRequest({ ...request, client: { protocolVersion: REMOTE_RESOURCE_PROTOCOL_VERSION, primitives: [] } })).not.toBeNull();
text += request.input.selectionChunk.text;
}
expect(JSON.parse(text)).toMatchObject({ entryIds: [], entryRanges: entries.filter(entry => entry.selected).map(entry => { const index = Number(entry.id.slice(6)); return [index, index]; }), deferSetup: true });
});
it('advertises the bots category and shows bots with the handover note, owner alternatives and source-disabled bots', async () => {
h.invoke.mockImplementation(async (_host: string, _channel: string, args: any[]) => {
const id = args[0].ref.id;
return { blocks: [{ primitive: 'companion-import', data: id === 'sources' ? { sources: [{ id: 'source', name: 'Ada', kind: 'openclaw' }] } : id.startsWith('preview:') ? { preview: { id: 'preview', name: 'Ada', source: { id: 'source', name: 'Ada', kind: 'openclaw' }, entries: [
{ id: 'owner-a', name: 'Ada', category: 'bots', platform: 'discord', owner: '111111111111111111', selected: false, exclusiveWith: ['owner-b'] },
{ id: 'owner-b', name: 'Ada', category: 'bots', platform: 'discord', owner: '222222222222222222', selected: false, exclusiveWith: ['owner-a'] },
{ id: 'lark', name: 'Work', category: 'bots', platform: 'feishu', owner: 'ou_fixture', selected: false, enabled: false },
] } } : { result: null } }] };
});
const container = document.createElement('div'); root = createRoot(container);
await act(async () => root!.render(createElement(CompanionImportSheet, { visible: true, deviceId: 'host', deviceName: 'Mac', online: true, onClose() {}, onCreated: h.created })));
expect(h.invoke.mock.calls[0]![2][0].client.primitives).toContain('companion-import-bots-v1');
const click = async (text: string) => { await act(async () => { const button = [...container.querySelectorAll('button')].find(button => button.textContent?.startsWith(text)); expect(button).toBeDefined(); button!.click(); }); };
await click('Ada · OpenClaw');
expect(container.textContent).toContain('devices.companionImport.botsNote OpenClaw');
await click('devices.companionImport.bots');
// A short list needs no search field.
expect(container.querySelector('input:not([type="checkbox"])')).toBeNull();
expect(container.textContent).toContain('devices.companionImport.botOwner 222222222222222222');
expect(container.textContent).not.toContain('devices.companionImport.botOwner ou_fixture');
expect(container.textContent).toContain('devices.companionImport.sourceDisabled');
expect(container.querySelector('[aria-label="message.renderer.imPlatform.feishu · Work"]')).not.toBeNull();
});