// @vitest-environment node import { beforeEach, describe, expect, it, vi } from 'vitest'; const mocks = vi.hoisted(() => { let gatewayEventHandler: ((event: any) => void | Promise) | null = null; return { gatewayRpc: vi.fn(), hostApiFetch: vi.fn(), onGatewayEvent: vi.fn((callback: (event: any) => void | Promise) => { gatewayEventHandler = callback; return () => { gatewayEventHandler = null; }; }), emitGatewayEvent: async (event: any) => { await gatewayEventHandler?.(event); }, }; }); vi.mock('../src/lib/gateway-client', () => ({ gatewayRpc: mocks.gatewayRpc, onGatewayEvent: mocks.onGatewayEvent, })); vi.mock('../src/lib/host-api', () => ({ hostApiFetch: mocks.hostApiFetch, })); vi.mock('../src/stores/agents', () => ({ agentsStore: { init: vi.fn(async () => undefined), resolveMainSessionKey: vi.fn(() => 'agent:test:main'), getState: vi.fn(() => ({ defaultAgentId: 'test', defaultProviderAccountId: null, })), getAgentById: vi.fn(() => undefined), }, })); describe('chat store runtime refresh', () => { beforeEach(() => { vi.resetModules(); vi.clearAllMocks(); mocks.gatewayRpc.mockImplementation(async (method: string) => { if (method === 'session.list') { return ['agent:test:main']; } if (method === 'chat.history') { return []; } throw new Error(`Unexpected RPC method: ${method}`); }); mocks.hostApiFetch.mockResolvedValue({ messages: [], }); }); it('reloads current history when skills runtime changes', async () => { const { chatStore } = await import('../src/stores/chat'); await chatStore.init(); const initialHistoryCalls = mocks.gatewayRpc.mock.calls.filter( ([method]) => method === 'chat.history', ).length; await mocks.emitGatewayEvent({ type: 'runtime:changed', topics: ['skills'], syncedAt: new Date().toISOString(), }); const afterRefreshHistoryCalls = mocks.gatewayRpc.mock.calls.filter( ([method]) => method === 'chat.history', ).length; expect(afterRefreshHistoryCalls).toBe(initialHistoryCalls + 1); }); });