Files
zclaw_openfang/tests/desktop/gatewayStore.test.ts
iven 07079293f4 feat(hands): restructure Hands UI with Chinese localization
Major changes:
- Add HandList.tsx component for left sidebar
- Add HandTaskPanel.tsx for middle content area
- Restructure Sidebar tabs: 分身/HANDS/Workflow
- Remove Hands tab from RightPanel
- Localize all UI text to Chinese
- Archive legacy OpenClaw documentation
- Add Hands integration lessons document
- Update feature checklist with new components

UI improvements:
- Left sidebar now shows Hands list with status icons
- Middle area shows selected Hand's tasks and results
- Consistent styling with Tailwind CSS
- Chinese status labels and buttons

Documentation:
- Create docs/archive/openclaw-legacy/ for old docs
- Add docs/knowledge-base/hands-integration-lessons.md
- Update docs/knowledge-base/feature-checklist.md
- Update docs/knowledge-base/README.md

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-14 23:16:32 +08:00

621 lines
22 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
const getStoredGatewayUrlMock = vi.fn(() => 'ws://127.0.0.1:18789');
const getStoredGatewayTokenMock = vi.fn(() => 'stored-token');
const setStoredGatewayUrlMock = vi.fn((url: string) => url);
const setStoredGatewayTokenMock = vi.fn((token: string) => token);
const getLocalDeviceIdentityMock = vi.fn(async () => ({
deviceId: 'device_local',
publicKeyBase64: 'public_key_base64',
}));
const syncAgentsMock = vi.fn();
const mockClient = {
connect: vi.fn(async () => {
mockClient.onStateChange?.('connected');
}),
disconnect: vi.fn(),
health: vi.fn(async () => ({ version: '2026.3.11' })),
chat: vi.fn(),
listClones: vi.fn(async () => ({
clones: [
{
id: 'clone_alpha',
name: 'Alpha',
role: '代码助手',
createdAt: '2026-03-13T00:00:00.000Z',
},
],
})),
createClone: vi.fn(async (opts: Record<string, unknown>) => ({
clone: {
id: 'clone_new',
name: opts.name,
createdAt: '2026-03-13T01:00:00.000Z',
},
})),
updateClone: vi.fn(async (id: string, updates: Record<string, unknown>) => ({
clone: {
id,
name: updates.name || 'Alpha',
role: updates.role,
createdAt: '2026-03-13T00:00:00.000Z',
updatedAt: '2026-03-13T01:30:00.000Z',
},
})),
deleteClone: vi.fn(async () => ({ ok: true })),
getUsageStats: vi.fn(async () => ({
totalSessions: 1,
totalMessages: 2,
totalTokens: 3,
byModel: {},
})),
getPluginStatus: vi.fn(async () => ({
plugins: [{ id: 'zclaw-ui', status: 'active', version: '0.1.0' }],
})),
getQuickConfig: vi.fn(async () => ({
quickConfig: {
gatewayUrl: 'ws://127.0.0.1:18789',
gatewayToken: '',
theme: 'light',
},
})),
saveQuickConfig: vi.fn(async (config: Record<string, unknown>) => ({
quickConfig: config,
})),
getWorkspaceInfo: vi.fn(async () => ({
path: '~/.openclaw/zclaw-workspace',
resolvedPath: 'C:/Users/test/.openclaw/zclaw-workspace',
exists: true,
fileCount: 4,
totalSize: 128,
})),
listSkills: vi.fn(async () => ({
skills: [{ id: 'builtin:translation', name: 'translation', path: 'C:/skills/translation/SKILL.md', source: 'builtin' }],
extraDirs: ['C:/extra-skills'],
})),
listChannels: vi.fn(async () => ({
channels: [{ id: 'feishu', type: 'feishu', label: '飞书 (Feishu)', status: 'active', accounts: 1 }],
})),
getFeishuStatus: vi.fn(async () => ({ configured: true, accounts: 1 })),
listScheduledTasks: vi.fn(async () => ({
tasks: [{ id: 'task_1', name: 'Daily Summary', schedule: '0 9 * * *', status: 'active' }],
})),
// OpenFang methods
listHands: vi.fn(async () => ({
hands: [
{ name: 'echo', description: 'Echo handler', status: 'active' },
{ name: 'notify', description: 'Notification handler', status: 'active' },
],
})),
triggerHand: vi.fn(async (name: string, params?: Record<string, unknown>) => ({
runId: `run_${name}_${Date.now()}`,
status: 'running',
})),
listWorkflows: vi.fn(async () => ({
workflows: [
{ id: 'wf_1', name: 'Data Pipeline', steps: 3 },
{ id: 'wf_2', name: 'Report Generator', steps: 5 },
],
})),
executeWorkflow: vi.fn(async (id: string, input?: Record<string, unknown>) => ({
runId: `wfrun_${id}_${Date.now()}`,
status: 'running',
})),
listTriggers: vi.fn(async () => ({
triggers: [
{ id: 'trig_1', type: 'webhook', enabled: true },
{ id: 'trig_2', type: 'schedule', enabled: false },
],
})),
getAuditLogs: vi.fn(async (opts?: { limit?: number; offset?: number }) => ({
logs: [
{ id: 'log_1', timestamp: '2026-03-13T10:00:00Z', action: 'hand.trigger', actor: 'user1', details: { hand: 'echo' } },
{ id: 'log_2', timestamp: '2026-03-13T11:00:00Z', action: 'workflow.execute', actor: 'user2', details: { workflow: 'wf_1' } },
],
})),
getSecurityStatus: vi.fn(async () => ({
layers: [
{ name: 'Device Authentication', enabled: true, description: 'Ed25519 device signature verification' },
{ name: 'JWT Tokens', enabled: true, description: 'Short-lived JWT for session management' },
{ name: 'RBAC', enabled: true, description: 'Role-based access control' },
{ name: 'Rate Limiting', enabled: true, description: 'Per-user rate limits' },
{ name: 'Input Validation', enabled: true, description: 'Z-schema input validation' },
{ name: 'Sandboxing', enabled: true, description: 'Code execution sandbox' },
{ name: 'Audit Logging', enabled: true, description: 'Merkle hash chain audit logs' },
{ name: 'Encryption at Rest', enabled: true, description: 'AES-256 encryption' },
{ name: 'Encryption in Transit', enabled: true, description: 'TLS 1.3 encryption' },
{ name: 'Secrets Management', enabled: true, description: 'Secure secrets storage' },
{ name: 'Permission Gates', enabled: true, description: 'Capability-based permissions' },
{ name: 'Content Filtering', enabled: false, description: 'Content moderation' },
{ name: 'PII Detection', enabled: false, description: 'PII scanning' },
{ name: 'Malware Scanning', enabled: false, description: 'File malware detection' },
{ name: 'Network Isolation', enabled: false, description: 'Container network isolation' },
{ name: 'HSM Integration', enabled: false, description: 'Hardware security module' },
],
})),
getCapabilities: vi.fn(async () => ({
capabilities: ['operator.read', 'operator.write', 'hand.trigger', 'workflow.execute'],
})),
updateOptions: vi.fn(),
onStateChange: undefined as undefined | ((state: string) => void),
onLog: undefined as undefined | ((level: string, message: string) => void),
};
vi.mock('../../desktop/src/lib/gateway-client', () => ({
DEFAULT_GATEWAY_URL: 'ws://127.0.0.1:4200/ws',
FALLBACK_GATEWAY_URLS: ['ws://127.0.0.1:4200/ws', 'ws://127.0.0.1:4201/ws'],
GatewayClient: class {},
getGatewayClient: () => mockClient,
getStoredGatewayUrl: () => getStoredGatewayUrlMock(),
getStoredGatewayToken: () => getStoredGatewayTokenMock(),
setStoredGatewayUrl: (url: string) => setStoredGatewayUrlMock(url),
setStoredGatewayToken: (token: string) => setStoredGatewayTokenMock(token),
getLocalDeviceIdentity: () => getLocalDeviceIdentityMock(),
}));
vi.mock('../../desktop/src/lib/tauri-gateway', () => ({
isTauriRuntime: () => false,
approveLocalGatewayDevicePairing: vi.fn(async () => ({ approved: false, requestId: null, deviceId: null })),
getLocalGatewayAuth: vi.fn(async () => ({ configPath: null, gatewayToken: null })),
getLocalGatewayStatus: vi.fn(async () => ({
supported: false,
cliAvailable: false,
runtimeSource: null,
runtimePath: null,
serviceLabel: null,
serviceLoaded: false,
serviceStatus: null,
configOk: false,
port: null,
portStatus: null,
probeUrl: null,
listenerPids: [],
error: null,
raw: {},
})),
getUnsupportedLocalGatewayStatus: () => ({
supported: false,
cliAvailable: false,
runtimeSource: null,
runtimePath: null,
serviceLabel: null,
serviceLoaded: false,
serviceStatus: null,
configOk: false,
port: null,
portStatus: null,
probeUrl: null,
listenerPids: [],
error: null,
raw: {},
}),
prepareLocalGatewayForTauri: vi.fn(async () => ({
configPath: null,
originsUpdated: false,
gatewayRestarted: false,
})),
restartLocalGateway: vi.fn(async () => undefined),
startLocalGateway: vi.fn(async () => undefined),
stopLocalGateway: vi.fn(async () => undefined),
}));
vi.mock('../../desktop/src/store/chatStore', () => ({
useChatStore: {
getState: () => ({
syncAgents: syncAgentsMock,
}),
},
}));
function resetClientMocks() {
mockClient.connect.mockClear();
mockClient.disconnect.mockClear();
mockClient.health.mockReset();
mockClient.chat.mockReset();
mockClient.listClones.mockReset();
mockClient.createClone.mockReset();
mockClient.updateClone.mockReset();
mockClient.deleteClone.mockReset();
mockClient.getUsageStats.mockReset();
mockClient.getPluginStatus.mockReset();
mockClient.getQuickConfig.mockReset();
mockClient.saveQuickConfig.mockReset();
mockClient.getWorkspaceInfo.mockReset();
mockClient.listSkills.mockReset();
mockClient.listChannels.mockReset();
mockClient.getFeishuStatus.mockReset();
mockClient.listScheduledTasks.mockReset();
// OpenFang mocks
mockClient.listHands.mockReset();
mockClient.triggerHand.mockReset();
mockClient.listWorkflows.mockReset();
mockClient.executeWorkflow.mockReset();
mockClient.listTriggers.mockReset();
mockClient.getAuditLogs.mockReset();
mockClient.updateOptions.mockClear();
mockClient.onStateChange = undefined;
mockClient.onLog = undefined;
mockClient.health.mockResolvedValue({ version: '2026.3.11' });
mockClient.listClones.mockResolvedValue({
clones: [
{
id: 'clone_alpha',
name: 'Alpha',
role: '代码助手',
createdAt: '2026-03-13T00:00:00.000Z',
},
],
});
mockClient.createClone.mockImplementation(async (opts: Record<string, unknown>) => ({
clone: {
id: 'clone_new',
name: opts.name,
createdAt: '2026-03-13T01:00:00.000Z',
},
}));
mockClient.updateClone.mockImplementation(async (id: string, updates: Record<string, unknown>) => ({
clone: {
id,
name: updates.name || 'Alpha',
role: updates.role,
createdAt: '2026-03-13T00:00:00.000Z',
updatedAt: '2026-03-13T01:30:00.000Z',
},
}));
mockClient.deleteClone.mockResolvedValue({ ok: true });
mockClient.getUsageStats.mockResolvedValue({
totalSessions: 1,
totalMessages: 2,
totalTokens: 3,
byModel: {},
});
mockClient.getPluginStatus.mockResolvedValue({
plugins: [{ id: 'zclaw-ui', status: 'active', version: '0.1.0' }],
});
mockClient.getQuickConfig.mockResolvedValue({
quickConfig: {
gatewayUrl: 'ws://127.0.0.1:18789',
gatewayToken: '',
theme: 'light',
},
});
mockClient.saveQuickConfig.mockImplementation(async (config: Record<string, unknown>) => ({
quickConfig: config,
}));
mockClient.getWorkspaceInfo.mockResolvedValue({
path: '~/.openclaw/zclaw-workspace',
resolvedPath: 'C:/Users/test/.openclaw/zclaw-workspace',
exists: true,
fileCount: 4,
totalSize: 128,
});
mockClient.listSkills.mockResolvedValue({
skills: [{ id: 'builtin:translation', name: 'translation', path: 'C:/skills/translation/SKILL.md', source: 'builtin' }],
extraDirs: ['C:/extra-skills'],
});
mockClient.listChannels.mockResolvedValue({
channels: [{ id: 'feishu', type: 'feishu', label: '飞书 (Feishu)', status: 'active', accounts: 1 }],
});
mockClient.getFeishuStatus.mockResolvedValue({ configured: true, accounts: 1 });
mockClient.listScheduledTasks.mockResolvedValue({
tasks: [{ id: 'task_1', name: 'Daily Summary', schedule: '0 9 * * *', status: 'active' }],
});
// OpenFang mock defaults
mockClient.listHands.mockResolvedValue({
hands: [
{ name: 'echo', description: 'Echo handler', status: 'active' },
{ name: 'notify', description: 'Notification handler', status: 'active' },
],
});
mockClient.triggerHand.mockImplementation(async (name: string) => ({
runId: `run_${name}_123`,
status: 'running',
}));
mockClient.listWorkflows.mockResolvedValue({
workflows: [
{ id: 'wf_1', name: 'Data Pipeline', steps: 3 },
],
});
mockClient.executeWorkflow.mockImplementation(async (id: string) => ({
runId: `wfrun_${id}_123`,
status: 'running',
}));
mockClient.listTriggers.mockResolvedValue({
triggers: [
{ id: 'trig_1', type: 'webhook', enabled: true },
],
});
mockClient.getAuditLogs.mockResolvedValue({
logs: [
{ id: 'log_1', timestamp: '2026-03-13T10:00:00Z', action: 'hand.trigger', actor: 'user1' },
],
});
}
describe('gatewayStore desktop flows', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.resetModules();
resetClientMocks();
});
it('loads post-connect data and syncs agents after a successful connection', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
await useGatewayStore.getState().connect('ws://127.0.0.1:18789', 'token-123');
const state = useGatewayStore.getState();
expect(mockClient.updateOptions).toHaveBeenCalledWith({
url: 'ws://127.0.0.1:18789',
token: 'token-123',
});
expect(mockClient.connect).toHaveBeenCalledTimes(1);
expect(state.connectionState).toBe('connected');
expect(state.gatewayVersion).toBe('2026.3.11');
expect(state.quickConfig.gatewayUrl).toBe('ws://127.0.0.1:18789');
expect(state.workspaceInfo?.resolvedPath).toBe('C:/Users/test/.openclaw/zclaw-workspace');
expect(state.pluginStatus).toHaveLength(1);
expect(state.skillsCatalog).toHaveLength(1);
expect(state.channels).toEqual([
{ id: 'feishu', type: 'feishu', label: '飞书 (Feishu)', status: 'active', accounts: 1 },
]);
expect(syncAgentsMock).toHaveBeenCalledWith([
{
id: 'clone_alpha',
name: 'Alpha',
role: '代码助手',
createdAt: '2026-03-13T00:00:00.000Z',
},
]);
expect(setStoredGatewayUrlMock).toHaveBeenCalledWith('ws://127.0.0.1:18789');
});
it('falls back to feishu probing with the correct chinese label when channels.list is unavailable', async () => {
mockClient.listChannels.mockRejectedValueOnce(new Error('channels.list unavailable'));
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
await useGatewayStore.getState().loadChannels();
expect(useGatewayStore.getState().channels).toEqual([
{ id: 'feishu', type: 'feishu', label: '飞书 (Feishu)', status: 'active', accounts: 1 },
]);
});
it('merges and persists quick config updates through the gateway store', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
useGatewayStore.setState({
quickConfig: {
agentName: 'Alpha',
theme: 'light',
gatewayUrl: 'ws://127.0.0.1:18789',
gatewayToken: 'old-token',
},
});
await useGatewayStore.getState().saveQuickConfig({
gatewayToken: 'new-token',
workspaceDir: 'C:/workspace-next',
});
expect(mockClient.saveQuickConfig).toHaveBeenCalledWith({
agentName: 'Alpha',
theme: 'light',
gatewayUrl: 'ws://127.0.0.1:18789',
gatewayToken: 'new-token',
workspaceDir: 'C:/workspace-next',
});
expect(setStoredGatewayTokenMock).toHaveBeenCalledWith('new-token');
expect(useGatewayStore.getState().quickConfig.workspaceDir).toBe('C:/workspace-next');
});
it('returns the updated clone and refreshes the clone list after update', async () => {
const initialClones = [
{
id: 'clone_alpha',
name: 'Alpha',
role: '代码助手',
createdAt: '2026-03-13T00:00:00.000Z',
},
];
const refreshedClones = [
{
id: 'clone_alpha',
name: 'Alpha Prime',
role: '架构助手',
createdAt: '2026-03-13T00:00:00.000Z',
updatedAt: '2026-03-13T01:30:00.000Z',
},
];
mockClient.listClones
.mockResolvedValueOnce({
clones: initialClones,
})
.mockResolvedValueOnce({
clones: refreshedClones,
});
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
await useGatewayStore.getState().loadClones();
const updated = await useGatewayStore.getState().updateClone('clone_alpha', {
name: 'Alpha Prime',
role: '架构助手',
});
expect(updated).toMatchObject({
id: 'clone_alpha',
name: 'Alpha Prime',
role: '架构助手',
});
expect(useGatewayStore.getState().clones).toEqual(refreshedClones);
});
});
describe('OpenFang actions', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.resetModules();
resetClientMocks();
});
it('loads hands from the gateway', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
await useGatewayStore.getState().loadHands();
expect(mockClient.listHands).toHaveBeenCalledTimes(1);
expect(useGatewayStore.getState().hands).toEqual([
{ name: 'echo', description: 'Echo handler', status: 'active' },
{ name: 'notify', description: 'Notification handler', status: 'active' },
]);
});
it('triggers a hand and returns the run result', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
const result = await useGatewayStore.getState().triggerHand('echo', { message: 'hello' });
expect(mockClient.triggerHand).toHaveBeenCalledWith('echo', { message: 'hello' });
expect(result).toMatchObject({
runId: 'run_echo_123',
status: 'running',
});
});
it('sets error when triggerHand fails', async () => {
mockClient.triggerHand.mockRejectedValueOnce(new Error('Hand not found'));
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
const result = await useGatewayStore.getState().triggerHand('nonexistent');
expect(result).toBeUndefined();
expect(useGatewayStore.getState().error).toBe('Hand not found');
});
it('loads workflows from the gateway', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
await useGatewayStore.getState().loadWorkflows();
expect(mockClient.listWorkflows).toHaveBeenCalledTimes(1);
expect(useGatewayStore.getState().workflows).toEqual([
{ id: 'wf_1', name: 'Data Pipeline', steps: 3 },
]);
});
it('executes a workflow and returns the run result', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
const result = await useGatewayStore.getState().executeWorkflow('wf_1', { input: 'data' });
expect(mockClient.executeWorkflow).toHaveBeenCalledWith('wf_1', { input: 'data' });
expect(result).toMatchObject({
runId: 'wfrun_wf_1_123',
status: 'running',
});
});
it('loads triggers from the gateway', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
await useGatewayStore.getState().loadTriggers();
expect(mockClient.listTriggers).toHaveBeenCalledTimes(1);
expect(useGatewayStore.getState().triggers).toEqual([
{ id: 'trig_1', type: 'webhook', enabled: true },
]);
});
it('loads audit logs from the gateway', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
await useGatewayStore.getState().loadAuditLogs({ limit: 50, offset: 0 });
expect(mockClient.getAuditLogs).toHaveBeenCalledWith({ limit: 50, offset: 0 });
expect(useGatewayStore.getState().auditLogs).toEqual([
{ id: 'log_1', timestamp: '2026-03-13T10:00:00Z', action: 'hand.trigger', actor: 'user1' },
]);
});
it('initializes OpenFang state with empty arrays', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
const state = useGatewayStore.getState();
expect(state.hands).toEqual([]);
expect(state.workflows).toEqual([]);
expect(state.triggers).toEqual([]);
expect(state.auditLogs).toEqual([]);
});
// === Security Tests ===
it('loads security status from the gateway', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
await useGatewayStore.getState().loadSecurityStatus();
expect(mockClient.getSecurityStatus).toHaveBeenCalledTimes(1);
const { securityStatus } = useGatewayStore.getState();
expect(securityStatus).not.toBeNull();
expect(securityStatus?.totalCount).toBe(16);
expect(securityStatus?.enabledCount).toBe(11);
expect(securityStatus?.layers).toHaveLength(16);
});
it('calculates security level correctly (critical for 14+ layers)', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
await useGatewayStore.getState().loadSecurityStatus();
const { securityStatus } = useGatewayStore.getState();
// 11/16 enabled = 68.75% = 'high' level
expect(securityStatus?.securityLevel).toBe('high');
});
it('identifies disabled security layers', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
await useGatewayStore.getState().loadSecurityStatus();
const { securityStatus } = useGatewayStore.getState();
const disabledLayers = securityStatus?.layers.filter(l => !l.enabled) || [];
expect(disabledLayers.length).toBe(5);
expect(disabledLayers.map(l => l.name)).toContain('Content Filtering');
expect(disabledLayers.map(l => l.name)).toContain('HSM Integration');
});
it('sets isLoading during loadHands', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
// Reset store state
useGatewayStore.setState({ hands: [], isLoading: false });
const loadPromise = useGatewayStore.getState().loadHands();
// Check isLoading was set to true at start
// (this might be false again by the time we check due to async)
await loadPromise;
// After completion, isLoading should be false
expect(useGatewayStore.getState().isLoading).toBe(false);
expect(useGatewayStore.getState().hands.length).toBeGreaterThan(0);
});
it('sets isLoading during loadWorkflows', async () => {
const { useGatewayStore } = await import('../../desktop/src/store/gatewayStore');
// Reset store state
useGatewayStore.setState({ workflows: [], isLoading: false });
await useGatewayStore.getState().loadWorkflows();
expect(useGatewayStore.getState().isLoading).toBe(false);
expect(useGatewayStore.getState().workflows.length).toBeGreaterThan(0);
});
});