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 } from 'react-dom/client';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
vi.mock('expo-crypto', () => ({ randomUUID: vi.fn(() => 'history-request') }));
const h = vi.hoisted(() => ({
router: { replace: vi.fn(), push: vi.fn() }, generation: 1,
params: { collectionId: 'teammates', resourceId: 'bot-1', resourceKind: 'bot', deviceId: 'mac', deviceName: 'Mac' },
translation: { t: (key: string) => key, i18n: { language: 'en' } },
read: vi.fn(), action: vi.fn(), upsert: vi.fn(), push: null as null | ((device: string, payload: any) => void),
link: { openLink: vi.fn(), connectionEpoch: 1, status: 'online', invoke: vi.fn(), onRemoteResourceChanged: vi.fn(), subscribe: vi.fn(), unsubscribe: vi.fn() },
}));
vi.mock('react-native', async () => {
const { createElement: el } = await import('react');
const view = ({ children, testID }: any) => el('div', { 'data-testid': testID }, children);
return { View: view, ActivityIndicator: () => null, StyleSheet: { create: (s: any) => s }, AppState: { currentState: 'active', addEventListener: () => ({ remove() {} }) } };
});
vi.mock('expo-router', async () => {
const { useEffect } = await import('react');
return { Redirect: ({ href }: { href: string }) => { useEffect(() => h.router.replace(href), [href]); return null; }, useFocusEffect: (effect: () => void | (() => void)) => useEffect(effect, [effect]), useRouter: () => h.router, useLocalSearchParams: () => h.params };
});
vi.mock('@/i18n', () => ({ i18n: { t: (key: string) => key } }));
vi.mock('react-i18next', () => ({ useTranslation: () => h.translation }));
vi.mock('react-native-safe-area-context', async () => { const { createElement: el } = await import('react'); return { SafeAreaView: ({ children }: any) => el('div', {}, children) }; });
vi.mock('@/components/AppText', () => ({ Text: 'span' }));
vi.mock('@/components/RemoteCompanionAvatar', () => ({ RemoteCompanionAvatar: () => null }));
vi.mock('@/session/CompanionProfileSheet', async () => {
const { createElement: el } = await import('react');
return { CompanionProfileSheet: ({ visible, resource, onClose, onClosed, onOpenSearch }: any) => visible
? el('div', {}, el('button', { 'data-testid': 'profile-settings', onClick: () => { onClose(); onClosed(); } }, resource.ref.id), onOpenSearch ? el('button', { 'data-testid': 'profile-history', onClick: () => { onOpenSearch(); onClosed(); } }, 'History') : null)
: null };
});
vi.mock('@/components/MobilePrimitives', async () => {
const { createElement: el } = await import('react');
return { MainWindowEmptyState: ({ copy }: any) => el('span', {}, copy), MainWindowActionButton: ({ action }: any) => el('button', { onClick: action.onPress, 'data-testid': action.testID }, action.label) };
});
vi.mock('@/platform/chrome', () => ({ SimpleStackHeader: () => null, simpleScreenSafeAreaEdges: () => [] }));
vi.mock('@/auth/AuthContext', () => ({ useAuth: () => ({ accountGeneration: h.generation }) }));
vi.mock('@/device-link/DeviceLinkContext', () => ({ useDeviceLink: () => h.link }));
vi.mock('@/device-link/remoteStatus', async () => ({ ...await vi.importActual<typeof import('@/device-link/remoteStatus')>('@/device-link/remoteStatus'), formatRemoteError: String }));
vi.mock('@/device-link/focusedTopicSubscription', () => ({ startFocusedTopicSubscription: () => () => {} }));
vi.mock('@/device-link/remoteResources', async (importOriginal) => ({ ...await importOriginal<typeof import('../device-link/remoteResources')>(), getRemoteResource: h.read, invokeRemoteResourceAction: h.action }));
vi.mock('@/session/remoteSessionStore', () => ({ remoteSessionStore: { getSessionDeviceId: () => null, upsertDeviceSession: h.upsert } }));
vi.mock('@/theme', () => ({ useThemedStyles: () => ({}), useTheme: () => ({ colors: {} }) }));
vi.mock('@/utils/backGuard', () => ({ goBackGuarded: vi.fn() }));
import Screen from '../../app/resources/[collectionId]/[resourceId]';
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
const resource = (stage: string) => ({ ref: { collectionId: 'teammates', kind: 'bot', id: 'bot-1' }, display: { title: 'Mimi' }, revision: stage, links: stage === 'ready' ? [{ rel: 'conversation', target: { kind: 'session', sessionId: 'chat-1' } }] : [], blocks: [{ id: 'invitation', primitive: 'status', fallbackMarkdown: '', data: { stage } }], actions: stage === 'failed' ? [{ id: 'opaque-retry', label: 'Retry' }] : [] });
let container: HTMLDivElement;
let root: ReturnType<typeof createRoot>;
beforeEach(() => {
vi.clearAllMocks(); h.link.openLink.mockResolvedValue(undefined); h.generation = 1; h.params.collectionId = 'teammates';
h.link.onRemoteResourceChanged.mockImplementation(callback => { h.push = callback; return () => { h.push = null; }; });
h.link.invoke.mockResolvedValue({ id: 'chat-1', source: 'bot' });
container = document.createElement('div'); root = createRoot(container);
});
afterEach(() => { act(() => root.unmount()); });
it('keeps preparation in place and opens the canonical chat only after the host is ready', async () => {
h.read.mockResolvedValue(resource('skills'));
await act(async () => root.render(createElement(Screen)));
expect(container.textContent).toContain('devices.companions.invitation.skills');
expect(h.router.replace).not.toHaveBeenCalled();
h.read.mockResolvedValue(resource('ready'));
await act(async () => h.push!('mac', { collectionId: 'teammates' }));
expect(h.upsert).toHaveBeenCalledWith('mac', 'Mac', { id: 'chat-1', source: 'bot' });
expect(h.router.replace).toHaveBeenCalledWith(expect.objectContaining({ pathname: '/sessions/[sessionId]' }));
});
it('retries failed preparation through the advertised action without creating another teammate', async () => {
h.read.mockResolvedValue(resource('failed'));
h.action.mockImplementation(async () => { h.read.mockResolvedValue(resource('skills')); return { effects: [] }; });
await act(async () => root.render(createElement(Screen)));
await act(async () => container.querySelector<HTMLButtonElement>('[data-testid="remoteResourceResolver.retryInvitation"]')!.click());
expect(h.action).toHaveBeenCalledWith(h.link.invoke, { deviceId: 'mac', deviceName: 'Mac' }, expect.objectContaining({ actionId: 'opaque-retry', resourceRef: resource('failed').ref }), 'en');
expect(container.textContent).toContain('devices.companions.invitation.skills');
});
it.each(['welcome', 'failed'])('opens settings while preparation is %s and defers navigation until editing ends', async stage => {
h.read.mockResolvedValue(resource(stage));
await act(async () => root.render(createElement(Screen)));
await act(async () => container.querySelector<HTMLButtonElement>('[data-testid="remoteResourceResolver.settings"]')!.click());
expect(container.querySelector('[data-testid="profile-settings"]')?.textContent).toBe('bot-1');
h.read.mockResolvedValue(resource('ready'));
await act(async () => h.push!('mac', { collectionId: 'teammates' }));
expect(h.router.replace).not.toHaveBeenCalled();
await act(async () => container.querySelector<HTMLButtonElement>('[data-testid="profile-settings"]')!.click());
expect(h.router.replace).toHaveBeenCalledWith(expect.objectContaining({ pathname: '/sessions/[sessionId]' }));
expect(h.action).not.toHaveBeenCalled();
});
it('ignores a late ready response from the previous account', async () => {
let finish!: (value: unknown) => void;
h.read.mockImplementationOnce(() => new Promise(resolve => { finish = resolve; })).mockResolvedValue(resource('failed'));
await act(async () => root.render(createElement(Screen)));
h.generation = 2;
await act(async () => root.render(createElement(Screen)));
await act(async () => finish(resource('ready')));
expect(h.router.replace).not.toHaveBeenCalled();
expect(h.upsert).not.toHaveBeenCalled();
});
it('keeps settings open when a ready chat finishes loading during a model edit', async () => {
h.read.mockResolvedValue(resource('failed'));
await act(async () => root.render(createElement(Screen)));
let finish!: (value: unknown) => void;
h.link.invoke.mockImplementationOnce(() => new Promise(resolve => { finish = resolve; }));
h.read.mockResolvedValue(resource('ready'));
await act(async () => h.push!('mac', { collectionId: 'teammates' }));
await act(async () => container.querySelector<HTMLButtonElement>('[data-testid="remoteResourceResolver.settings"]')!.click());
await act(async () => finish({ id: 'chat-1', source: 'bot' }));
expect(h.router.replace).not.toHaveBeenCalled();
expect(container.querySelector('[data-testid="profile-settings"]')).not.toBeNull();
await act(async () => container.querySelector<HTMLButtonElement>('[data-testid="profile-settings"]')!.click());
expect(h.router.replace).toHaveBeenCalledWith(expect.objectContaining({ pathname: '/sessions/[sessionId]' }));
});
it('keeps a failed preparation on screen with an inline notice when its retry fails', async () => {
h.read.mockResolvedValue(resource('failed'));
h.action.mockRejectedValue(new Error('[DEVICE_UNRESPONSIVE] mac'));
await act(async () => root.render(createElement(Screen)));
expect(container.textContent).toContain('devices.companions.invitation.failed');
expect(container.textContent).not.toContain('devices.companions.invitation.background');
await act(async () => container.querySelector<HTMLButtonElement>('[data-testid="remoteResourceResolver.retryInvitation"]')!.click());
expect(container.querySelector('[data-testid="remoteResourceResolver.preparation"]')).not.toBeNull();
expect(container.textContent).toContain('devices.companions.invitation.retryFailed');
expect(container.textContent).not.toContain('DEVICE_UNRESPONSIVE');
});
it('reassures the user while preparing and shows the live stage beside the spinner', async () => {
h.read.mockResolvedValue(resource('profile'));
await act(async () => root.render(createElement(Screen)));
expect(container.textContent).toContain('devices.companions.invitation.background');
expect(container.querySelector('[data-testid="remoteResourceResolver.preparationStage"]')?.textContent).toBe('devices.companions.invitation.profile');
});
it('redirects stale routine links without reading or subscribing to the host', async () => {
h.params.collectionId = 'routines';
await act(async () => root.render(createElement(Screen)));
expect(h.router.replace).toHaveBeenCalledWith('/devices');
expect(h.read).not.toHaveBeenCalled();
expect(h.action).not.toHaveBeenCalled();
expect(h.link.onRemoteResourceChanged).not.toHaveBeenCalled();
});
it('opens existing history after the failed teammate settings close', async () => {
h.read.mockResolvedValue({ ...resource('failed'), links: resource('ready').links });
await act(async () => root.render(createElement(Screen)));
await act(async () => (container.querySelector('[data-testid="remoteResourceResolver.settings"]') as HTMLButtonElement).click());
expect(h.router.push).not.toHaveBeenCalled();
await act(async () => (container.querySelector('[data-testid="profile-history"]') as HTMLButtonElement).click());
expect(h.router.push).toHaveBeenCalledWith(expect.objectContaining({ pathname: '/sessions/[sessionId]', params: expect.objectContaining({ sessionId: 'chat-1', deviceId: 'mac', openSearch: '1', openSearchRequest: 'history-request' }) }));
});