The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(desktop): harden runtime timer regressions
Jakub Wolniewicz6d3e85211995
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsxindex 56d47c9bc3f..01dabd6c778 100644--- a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx+++ b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx@@ -1732,6 +1732,57 @@ describe('resumeSession failure recovery', () => {expect(resumeParams).toMatchObject({ source: 'desktop', omit_messages: true })})+ it('captures a cold runtime anchor before session.resume settles and stores it in the runtime cache', async () => {+ const resumeStartedAt = 1_700_000_000_000+ const afterResumeSettles = 1_800_000_000_000+ const now = vi.spyOn(Date, 'now').mockReturnValue(resumeStartedAt)+ const resumeDeferred = deferred<never>()+ const sessionStateByRuntimeIdRef: MutableRefObject<Map<string, ClientSessionState>> = { current: new Map() }++ vi.mocked(getLatestSessionMessages).mockResolvedValue({ messages: [], session_id: 'stored-1' } as never)++ const requestGateway = vi.fn(async (method: string) => {+ if (method === 'session.resume') {+ return await resumeDeferred.promise+ }++ return {} as never+ })++ let resume: ((storedSessionId: string, replaceRoute?: boolean) => Promise<unknown>) | null = null+ render(+ <ResumeHarness+ onReady={ready => (resume = ready)}+ requestGateway={requestGateway}+ sessionStateByRuntimeIdRef={sessionStateByRuntimeIdRef}+ />+ )+ await waitFor(() => expect(resume).not.toBeNull())++ let resumePromise!: Promise<unknown>+ act(() => {+ resumePromise = resume!('stored-1', true)+ })+ await waitFor(() => expect(requestGateway).toHaveBeenCalledWith('session.resume', expect.any(Object)))++ // The visible timer is anchored before the delayed RPC resolves.+ expect($sessionStartedAt.get()).toBe(resumeStartedAt)+ now.mockReturnValue(afterResumeSettles)+ resumeDeferred.resolve({+ session_id: 'runtime-1',+ resumed: 'stored-1',+ messages: [],+ info: {}+ } as never)++ await act(async () => {+ await resumePromise+ })++ // The cache must retain the pre-await anchor, not a completion-time value.+ expect(sessionStateByRuntimeIdRef.current.get('runtime-1')?.runtimeStartedAt).toBe(resumeStartedAt)+ })+it('arms the failure latch when resume succeeds with an empty transcript for a non-empty stored session', async () => {setSessions([storedSession({ message_count: 4 })])@@ -4698,7 +4749,21 @@ describe('resumeSession warm-cache mapping integrity', () => {current: new Map([['rt-A', { ...clientState('stored-A'), runtimeStartedAt }]])}+ vi.mocked(getLatestSessionMessages).mockResolvedValue({ messages: [], session_id: 'stored-A' } as never)+const requestGateway = vi.fn(async (method: string) => {+ if (method === 'session.activate') {+ return {+ session_id: 'rt-A',+ session_key: 'stored-A',+ resumed: 'stored-A',+ message_count: 0,+ messages: [],+ running: false,+ info: {}+ } as never+ }+if (method === 'session.usage') {return { input: 0, output: 0, total: 0 } as never}diff --git a/apps/desktop/src/lib/chat-runtime.test.ts b/apps/desktop/src/lib/chat-runtime.test.tsindex b407780a552..d8ab7ef392e 100644--- a/apps/desktop/src/lib/chat-runtime.test.ts+++ b/apps/desktop/src/lib/chat-runtime.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it } from 'vitest'+import { describe, expect, it, vi } from 'vitest'import type { ChatMessage, ChatMessagePart } from '@/lib/chat-messages'import type { ComposerAttachment } from '@/store/composer'@@ -8,6 +8,7 @@ import {attachmentId,coalesceToolOnlyAssistants,coerceThinkingText,+ createClientSessionState,createToolMergeCache,messageCreatedAt,optimisticAttachmentRef,@@ -17,6 +18,19 @@ import {const DATA_URL = 'data:image/png;base64,iVBORw0KGgoAAAANS'const THUMB_URL = 'data:image/png;base64,dGh1bWI='+describe('createClientSessionState', () => {+ it('anchors a fresh runtime to its creation time', () => {+ const createdAt = 1_700_000_000_000+ const now = vi.spyOn(Date, 'now').mockReturnValue(createdAt)++ try {+ expect(createClientSessionState('stored-1').runtimeStartedAt).toBe(createdAt)+ } finally {+ now.mockRestore()+ }+ })+})+function attachment(overrides: Partial<ComposerAttachment> & Pick<ComposerAttachment, 'kind'>): ComposerAttachment {return { id: 'a', label: 'file.png', ...overrides }}