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, cleanup, render, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { ReactNode } from 'react';
import type { GhostInstallConsentRequest } from '../../../shared/ghostInstallConsent';
import { GhostInstallConsentHost } from '../GhostInstallConsentHost';
const mocks = vi.hoisted(() => ({ confirm: vi.fn() }));
vi.mock('@/components/ui/confirm-dialog-provider', () => ({
useConfirmDialog: () => ({ confirm: mocks.confirm }),
}));
vi.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string, args?: Record<string, unknown>) =>
args && Object.keys(args).length > 0 ? `${key}:${JSON.stringify(args)}` : key,
}),
}));
type Push = (payload: GhostInstallConsentRequest, ownerStamp?: unknown) => void;
const HOST = {
key: 'network:host:upload.weather.test',
kind: 'network' as const,
labelKey: 'networkHost',
labelArgs: { host: 'upload.weather.test' },
};
describe('GhostInstallConsentHost', () => {
let push: Push | undefined;
let dismiss: ((payload: { requestId: string }) => void) | undefined;
const resolveInstallConsent = vi.fn();
beforeEach(() => {
mocks.confirm.mockReset().mockResolvedValue(true);
resolveInstallConsent.mockReset().mockResolvedValue({ handled: true });
(window as unknown as { electronAPI: unknown }).electronAPI = {
ghosts: {
onInstallConsentRequest: (callback: Push) => {
push = callback;
return () => {};
},
onInstallConsentDismissed: (callback: typeof dismiss) => {
dismiss = callback;
return () => {};
},
resolveInstallConsent,
},
};
});
afterEach(() => {
cleanup();
delete (window as unknown as { electronAPI?: unknown }).electronAPI;
});
it('shows the added permissions of an update and returns the answer to Main', async () => {
render(<GhostInstallConsentHost />);
await act(async () => {
push?.({
requestId: 'consent-1',
initiator: 'user',
origin: 'custom-market',
originLabel: 'Team market',
facts: {
kind: 'update', ghostId: 'weather-chip', name: 'Weather', version: '2.0.0',
previousVersion: '1.0.0', added: [HOST], removed: [], unchangedCount: 1,
builtinOauthClientChanged: false,
},
});
});
await waitFor(() => expect(resolveInstallConsent).toHaveBeenCalledWith('consent-1', true));
const options = mocks.confirm.mock.calls[0][0] as {
title: string;
description: string;
confirmText: string;
content: ReactNode;
};
expect(options.title).toBe('settings.ghosts.installConsent.updateTitle:{"name":"Weather"}');
expect(options.description).toContain('"from":"1.0.0"');
expect(options.description).toContain('originCustomMarket');
expect(options.confirmText).toBe('settings.ghosts.installConsent.confirmUpdate');
render(<>{options.content}</>);
expect(screen.getByText('settings.ghosts.perm.networkHost:{"host":"upload.weather.test"}')).toBeTruthy();
expect(screen.getByText('settings.ghosts.installConsent.addedBadge')).toBeTruthy();
});
it('shows first-use capability consent without claiming a package update or source', async () => {
render(<GhostInstallConsentHost />);
await act(async () => push?.({
requestId:'tasks', purpose:'task-capability', initiator:'user', origin:'local-file',
facts:{kind:'update',ghostId:'p',name:'Plugin',version:'1',previousVersion:'1',
added:[{key:'agent:tasks',kind:'agent',labelKey:'agentTasks',detailKey:'agentTasksDetail'}],
removed:[],unchangedCount:2,builtinOauthClientChanged:false},
}));
const options = mocks.confirm.mock.calls[0][0];
expect(options.title).toBe('Plugin');
expect(options.description).toBe('settings.ghosts.installConsent.addedTitle');
expect(options.confirmText).toBe('settings.ghosts.connections.confirmAllow');
render(<>{options.content}</>);
expect(screen.getByText('settings.ghosts.perm.agentTasksDetail')).toBeTruthy();
expect(screen.queryByText('settings.ghosts.installConsent.updateDescription')).toBeNull();
await waitFor(()=>expect(resolveInstallConsent).toHaveBeenCalledWith('tasks',true));
});
// Shared same-ID packages need a distinct replacement action and accurate
// state impact even with no added permissions (unit: real Host, fake Main).
it.each([false, true])('explains shared replacement without changing source rules (sourceChanged=%s)', async sourceChanged => {
render(<GhostInstallConsentHost />);
await act(async () => push?.({
requestId: 'share-replace', purpose: 'share-replace', initiator: 'user', origin: 'local-file',
target: { namespace: null, sourceChanged, otherCopies: 0 },
facts: { kind: 'update', ghostId: 'weather-chip', name: 'Weather', version: '1.0.0',
previousVersion: '1.0.0', added: [], removed: [], unchangedCount: 1, builtinOauthClientChanged: false },
}));
const options = mocks.confirm.mock.calls[0][0];
expect(options.title).toBe('sharing.localReplaceTitle:{"name":"Weather"}');
expect(options.confirmText).toBe('sharing.localReplaceButton');
render(<>{options.content}</>);
expect(screen.getByText('sharing.localReplaceNotice')).toBeTruthy();
expect(screen.queryByText('sharing.localReplaceRetainsState') !== null).toBe(!sourceChanged);
expect(screen.queryByText('settings.ghosts.installConsent.sourceChanged') !== null).toBe(sourceChanged);
await waitFor(() => expect(resolveInstallConsent).toHaveBeenCalledWith('share-replace', true));
});
it('answers cancel when Main dismisses the pending dialog', async () => {
let seenSignal: AbortSignal | undefined;
mocks.confirm.mockImplementation(
(_options: unknown, signal: AbortSignal) =>
new Promise<boolean>((resolve) => {
seenSignal = signal;
signal.addEventListener('abort', () => resolve(false));
}),
);
render(<GhostInstallConsentHost />);
await act(async () => {
push?.({
requestId: 'consent-2',
initiator: 'user',
origin: 'local-file',
facts: { kind: 'install', ghostId: 'weather-chip', name: 'Weather', version: '1.0.0', permissions: [] },
});
});
await waitFor(() => expect(mocks.confirm).toHaveBeenCalledTimes(1));
await act(async () => {
dismiss?.({ requestId: 'consent-2' });
});
expect(seenSignal?.aborted).toBe(true);
await waitFor(() => expect(resolveInstallConsent).toHaveBeenCalledWith('consent-2', false));
});
});