committed by
GitHub
Unverified
parent
87ab12849c
commit
4ff6861042
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const invokeIpcMock = vi.fn();
|
||||
const hostApiFetchMock = vi.fn();
|
||||
const gatewayStoreGetStateMock = vi.fn();
|
||||
const clearHistoryPoll = vi.fn();
|
||||
const enrichWithCachedImages = vi.fn((messages) => messages);
|
||||
const enrichWithToolResultFiles = vi.fn((messages) => messages);
|
||||
@@ -30,6 +31,12 @@ vi.mock('@/lib/host-api', () => ({
|
||||
hostApiFetch: (...args: unknown[]) => hostApiFetchMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('@/stores/gateway', () => ({
|
||||
useGatewayStore: {
|
||||
getState: () => gatewayStoreGetStateMock(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('@/stores/chat/helpers', () => ({
|
||||
clearHistoryPoll: (...args: unknown[]) => clearHistoryPoll(...args),
|
||||
enrichWithCachedImages: (...args: unknown[]) => enrichWithCachedImages(...args),
|
||||
@@ -83,8 +90,13 @@ function makeHarness(initial?: Partial<ChatLikeState>) {
|
||||
describe('chat history actions', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
vi.resetModules();
|
||||
vi.useRealTimers();
|
||||
invokeIpcMock.mockResolvedValue({ success: true, result: { messages: [] } });
|
||||
hostApiFetchMock.mockResolvedValue({ messages: [] });
|
||||
gatewayStoreGetStateMock.mockReturnValue({
|
||||
status: { state: 'running', port: 18789, connectedAt: Date.now() },
|
||||
});
|
||||
});
|
||||
|
||||
it('uses cron session fallback when gateway history is empty', async () => {
|
||||
@@ -156,10 +168,158 @@ describe('chat history actions', () => {
|
||||
await actions.loadHistory();
|
||||
|
||||
expect(h.read().messages.map((message) => message.content)).toEqual(['still here']);
|
||||
expect(h.read().error).toBe('Error: Gateway unavailable');
|
||||
expect(h.read().error).toBe('Gateway unavailable');
|
||||
expect(h.read().loading).toBe(false);
|
||||
});
|
||||
|
||||
it('retries the first foreground startup history load after a timeout and then succeeds', async () => {
|
||||
vi.useFakeTimers();
|
||||
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
const { createHistoryActions } = await import('@/stores/chat/history-actions');
|
||||
const h = makeHarness({
|
||||
currentSessionKey: 'agent:main:main',
|
||||
});
|
||||
const actions = createHistoryActions(h.set as never, h.get as never);
|
||||
gatewayStoreGetStateMock.mockReturnValue({
|
||||
status: { state: 'running', port: 18789, connectedAt: Date.now() - 40_000 },
|
||||
});
|
||||
|
||||
invokeIpcMock
|
||||
.mockResolvedValueOnce({ success: false, error: 'RPC timeout: chat.history' })
|
||||
.mockResolvedValueOnce({
|
||||
success: true,
|
||||
result: {
|
||||
messages: [
|
||||
{ role: 'assistant', content: 'restored after retry', timestamp: 1000 },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const loadPromise = actions.loadHistory();
|
||||
await vi.runAllTimersAsync();
|
||||
await loadPromise;
|
||||
|
||||
expect(invokeIpcMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'gateway:rpc',
|
||||
'chat.history',
|
||||
{ sessionKey: 'agent:main:main', limit: 200 },
|
||||
35_000,
|
||||
);
|
||||
expect(invokeIpcMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'gateway:rpc',
|
||||
'chat.history',
|
||||
{ sessionKey: 'agent:main:main', limit: 200 },
|
||||
35_000,
|
||||
);
|
||||
expect(h.read().messages.map((message) => message.content)).toEqual(['restored after retry']);
|
||||
expect(h.read().error).toBeNull();
|
||||
expect(warnSpy).toHaveBeenCalledWith(
|
||||
'[chat.history] startup retry scheduled',
|
||||
expect.objectContaining({
|
||||
sessionKey: 'agent:main:main',
|
||||
attempt: 1,
|
||||
errorKind: 'timeout',
|
||||
}),
|
||||
);
|
||||
warnSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('stops retrying once the load no longer belongs to the active session', async () => {
|
||||
vi.useFakeTimers();
|
||||
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
const { createHistoryActions } = await import('@/stores/chat/history-actions');
|
||||
const h = makeHarness({
|
||||
currentSessionKey: 'agent:main:main',
|
||||
});
|
||||
const actions = createHistoryActions(h.set as never, h.get as never);
|
||||
|
||||
invokeIpcMock.mockImplementationOnce(async () => {
|
||||
h.set({
|
||||
currentSessionKey: 'agent:main:other',
|
||||
loading: false,
|
||||
messages: [{ role: 'assistant', content: 'other session', timestamp: 1001 }],
|
||||
});
|
||||
return { success: false, error: 'RPC timeout: chat.history' };
|
||||
});
|
||||
|
||||
await actions.loadHistory();
|
||||
|
||||
expect(invokeIpcMock).toHaveBeenCalledTimes(1);
|
||||
expect(h.read().currentSessionKey).toBe('agent:main:other');
|
||||
expect(h.read().messages.map((message) => message.content)).toEqual(['other session']);
|
||||
expect(h.read().error).toBeNull();
|
||||
expect(warnSpy).not.toHaveBeenCalled();
|
||||
warnSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('surfaces a final error only after startup retry budget is exhausted', async () => {
|
||||
vi.useFakeTimers();
|
||||
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
const { createHistoryActions } = await import('@/stores/chat/history-actions');
|
||||
const h = makeHarness({
|
||||
currentSessionKey: 'agent:main:main',
|
||||
});
|
||||
const actions = createHistoryActions(h.set as never, h.get as never);
|
||||
|
||||
invokeIpcMock.mockResolvedValue({
|
||||
success: false,
|
||||
error: 'RPC timeout: chat.history',
|
||||
});
|
||||
|
||||
const loadPromise = actions.loadHistory();
|
||||
await vi.runAllTimersAsync();
|
||||
await loadPromise;
|
||||
|
||||
expect(invokeIpcMock).toHaveBeenCalledTimes(2);
|
||||
expect(h.read().messages).toEqual([]);
|
||||
expect(h.read().error).toBe('RPC timeout: chat.history');
|
||||
expect(warnSpy).toHaveBeenCalledWith(
|
||||
'[chat.history] startup retry exhausted',
|
||||
expect.objectContaining({
|
||||
sessionKey: 'agent:main:main',
|
||||
}),
|
||||
);
|
||||
warnSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('does not retry quiet history refreshes', async () => {
|
||||
const { createHistoryActions } = await import('@/stores/chat/history-actions');
|
||||
const h = makeHarness({
|
||||
currentSessionKey: 'agent:main:main',
|
||||
});
|
||||
const actions = createHistoryActions(h.set as never, h.get as never);
|
||||
|
||||
invokeIpcMock.mockResolvedValue({
|
||||
success: false,
|
||||
error: 'RPC timeout: chat.history',
|
||||
});
|
||||
|
||||
await actions.loadHistory(true);
|
||||
|
||||
expect(invokeIpcMock).toHaveBeenCalledTimes(1);
|
||||
expect(h.read().error).toBeNull();
|
||||
});
|
||||
|
||||
it('does not retry non-retryable startup failures', async () => {
|
||||
const { createHistoryActions } = await import('@/stores/chat/history-actions');
|
||||
const h = makeHarness({
|
||||
currentSessionKey: 'agent:main:main',
|
||||
});
|
||||
const actions = createHistoryActions(h.set as never, h.get as never);
|
||||
|
||||
invokeIpcMock.mockResolvedValue({
|
||||
success: false,
|
||||
error: 'Validation failed: bad session key',
|
||||
});
|
||||
|
||||
await actions.loadHistory();
|
||||
|
||||
expect(invokeIpcMock).toHaveBeenCalledTimes(1);
|
||||
expect(h.read().error).toBe('Validation failed: bad session key');
|
||||
});
|
||||
|
||||
it('filters out system messages from loaded history', async () => {
|
||||
const { createHistoryActions } = await import('@/stores/chat/history-actions');
|
||||
const h = makeHarness();
|
||||
|
||||
Reference in New Issue
Block a user