The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(desktop): one discriminating guard test per empty-page site
kshitijk4poore2f8a0731bf2
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.tsindex b869d233102..6ae91e66b32 100644--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts@@ -1,6 +1,7 @@import { act, cleanup, renderHook, waitFor } from '@testing-library/react'import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import type { ChatMessage } from '@/lib/chat-messages'import { createClientSessionState } from '@/lib/chat-runtime'import { sessionMessagesSignature } from '@/lib/session-signatures'import { $changeEventsAvailable, notifySessionsChanged, resetLiveSync } from '@/store/live-sync'@@ -26,6 +27,7 @@ import {import {type ActiveTranscriptRefreshDeps,+ hydrateStoredSessionTranscript,isTypingBurstActive,noteRendererKeyboardActivity,profileScopeForTranscriptSession,@@ -1138,27 +1140,23 @@ describe('isTypingBurstActive', () => {})})-describe('reconcileActiveTranscript with an empty persisted page', () => {+describe('an empty persisted page over a populated runtime', () => {// A backend respawn (or a state.db read racing the change event) answers a- // `sessions.changed` refresh with zero rows. That page is not proof the- // transcript is empty; accepting it blanks the view, flips the routed- // thread into its loading branch and re-runs the composer lifecycle.- function populatedFixture() {- const fixture = makeRefresh()-- fixture.state.messages = [- { id: 'user-1', parts: [{ text: 'question', type: 'text' }], role: 'user' },- { id: 'assistant-1', parts: [{ text: 'answer', type: 'text' }], role: 'assistant' }- ]- publishSessionState(ACTIVE_RUNTIME_ID, fixture.state)+ // refresh with zero rows. That page is not proof the transcript is empty;+ // accepting it blanks the view, flips the routed thread into its loading+ // branch and re-runs the composer lifecycle.+ const populated = (): ChatMessage[] => [+ { id: 'user-1', parts: [{ text: 'question', type: 'text' }], role: 'user' },+ { id: 'assistant-1', parts: [{ text: 'answer', type: 'text' }], role: 'assistant' }+ ]- return fixture- }+ const emptyPage = (sessionId = ACTIVE_STORED_ID) => ({ messages: [], session_id: sessionId })- const emptyPage = () => ({ messages: [], session_id: ACTIVE_STORED_ID })+ it('active pane: keeps the transcript and records no signature for the ignored page', async () => {+ const fixture = makeRefresh()- it('keeps a populated active transcript when the refresh reads zero rows', async () => {- const fixture = populatedFixture()+ fixture.state.messages = populated()+ publishSessionState(ACTIVE_RUNTIME_ID, fixture.state)vi.mocked(getLatestSessionMessages).mockResolvedValue(emptyPage() as never)await fixture.refresh()@@ -1168,49 +1166,80 @@ describe('reconcileActiveTranscript with an empty persisted page', () => {'user-1','assistant-1'])- })- it('does not record the ignored empty page as accepted, so the next usable page still lands', async () => {- const fixture = populatedFixture()- vi.mocked(getLatestSessionMessages).mockResolvedValueOnce(emptyPage() as never)+ // Once the runtime is genuinely empty the same empty page is authoritative+ // again. It would be deduped away had the ignored read left a signature.+ publishSessionState(ACTIVE_RUNTIME_ID, { ...fixture.state, messages: [] })await fixture.refresh()- vi.mocked(getLatestSessionMessages).mockResolvedValueOnce(transcript('a newer answer') as never)-- await fixture.refresh()-- const messages = fixture.states.get(ACTIVE_RUNTIME_ID)?.messages ?? []-expect(fixture.updateSessionState).toHaveBeenCalledTimes(1)- expect(messages.flatMap(message => message.parts.map(part => ('text' in part ? part.text : '')))).toContain(- 'a newer answer'- )})- it('still accepts an empty page for a session whose transcript is genuinely empty', async () => {+ it('active pane: a runtime bound to another stored session does not veto the requested page', async () => {const fixture = makeRefresh()- publishSessionState(ACTIVE_RUNTIME_ID, fixture.state)++ publishSessionState(+ ACTIVE_RUNTIME_ID,+ createClientSessionState('stored-other', [+ { id: 'other-user', parts: [{ text: 'elsewhere', type: 'text' }], role: 'user' }+ ])+ )vi.mocked(getLatestSessionMessages).mockResolvedValue(emptyPage() as never)await fixture.refresh()expect(fixture.updateSessionState).toHaveBeenCalledTimes(1)- expect(fixture.states.get(ACTIVE_RUNTIME_ID)?.messages).toEqual([])})- it('does not let a runtime bound to another stored session veto the requested page', async () => {- const fixture = makeRefresh()+ it('tile: keeps the transcript and records no signature for the ignored page', async () => {+ const runtimeId = 'runtime-tile'+ const storedId = 'stored-tile'+ const signatureRef = { current: new Map<string, string>() }- const foreign = createClientSessionState('stored-other', [- { id: 'other-user', parts: [{ text: 'elsewhere', type: 'text' }], role: 'user' }- ])+ $activeSessionId.set(ACTIVE_RUNTIME_ID)+ publishSessionState(runtimeId, createClientSessionState(storedId, populated()))+ vi.mocked(getLatestSessionMessages).mockResolvedValue(emptyPage(storedId) as never)- publishSessionState(ACTIVE_RUNTIME_ID, foreign)- vi.mocked(getLatestSessionMessages).mockResolvedValue(emptyPage() as never)+ const updateSessionState = vi.fn()- await fixture.refresh()+ await reconcileTileTranscriptsForTest({+ requestSequenceRef: { current: 0 },+ signatureRef,+ tiles: [{ runtimeId, storedSessionId: storedId }],+ updateSessionState+ })++ expect(updateSessionState).not.toHaveBeenCalled()+ expect(signatureRef.current.size).toBe(0)+ })++ it('post-turn hydrate: an empty page is not the answer, the next attempt is', async () => {+ vi.useFakeTimers()+ const fixture = makeRefresh()++ fixture.state.messages = populated()+ publishSessionState(ACTIVE_RUNTIME_ID, fixture.state)+ vi.mocked(getLatestSessionMessages)+ .mockResolvedValueOnce(emptyPage() as never)+ .mockResolvedValueOnce(transcript('a newer answer') as never)++ const hydrated = hydrateStoredSessionTranscript({+ attempts: 2,+ storedSessionId: ACTIVE_STORED_ID,+ runtimeSessionId: ACTIVE_RUNTIME_ID,+ storedProfile: 'default',+ updateSessionState: fixture.updateSessionState+ })++ await vi.advanceTimersByTimeAsync(250)+ await hydratedexpect(fixture.updateSessionState).toHaveBeenCalledTimes(1)+ expect(+ fixture.states+ .get(ACTIVE_RUNTIME_ID)+ ?.messages.flatMap(message => message.parts.map(part => ('text' in part ? part.text : '')))+ ).toContain('a newer answer')})})diff --git a/apps/desktop/src/store/transcript-tail.test.ts b/apps/desktop/src/store/transcript-tail.test.tsindex 29ab08e6836..cd201f71efb 100644--- a/apps/desktop/src/store/transcript-tail.test.ts+++ b/apps/desktop/src/store/transcript-tail.test.ts@@ -154,12 +154,4 @@ describe('recordTranscriptTail with an empty page', () => {expect(transcriptTailState('s1')).toMatchObject({ nextOffset: 10, possiblyTruncated: true })})-- it('still records an empty page when nothing was truncated before', () => {- recordTranscriptTail('s1', page(3))-- recordTranscriptTail('s1', page(0))-- expect(transcriptTailState('s1')).toMatchObject({ nextOffset: 0, possiblyTruncated: false })- })})