Skip to content

The agent that grows with you

test(desktop): the new-session test records gateway calls in a typed list

alt-glitch6f8f71b0c0f1
diff --git a/apps/desktop/src/app/session/hooks/new-session-first-message.test.tsx b/apps/desktop/src/app/session/hooks/new-session-first-message.test.tsx
index d45916da689..04d5134f57d 100644
--- a/apps/desktop/src/app/session/hooks/new-session-first-message.test.tsx
+++ b/apps/desktop/src/app/session/hooks/new-session-first-message.test.tsx
@@ -4,6 +4,7 @@ import { useEffect, useRef } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { ChatMessage } from '@/lib/chat-messages'
+import type * as GatewayStore from '@/store/gateway'
import { requestGatewayForProfile } from '@/store/gateway'
import {
$activeSessionId,
@@ -23,7 +24,7 @@ import { useSessionStateCache } from './use-session-state-cache'
// The row is default-profile owned, so its session RPCs ride the profile socket; route those to the fake.
vi.mock('@/store/gateway', async importOriginal => ({
- ...(await importOriginal<typeof import('@/store/gateway')>()),
+ ...(await importOriginal<typeof GatewayStore>()),
requestGatewayForProfile: vi.fn()
}))
@@ -31,6 +32,9 @@ const PROMPT = 'Plan my week.\n\nAbout me:\n- Call me Sid.'
type Options = Parameters<typeof useSessionActions>[0]
+// The test resumes the routed id itself, the step use-route-resume takes after the navigate.
+const stayOnRoute: Options['navigate'] = () => {}
+
interface Ready {
messagesOf: (runtimeId: string) => readonly ChatMessage[]
resume: ReturnType<typeof useSessionActions>['resumeSession']
@@ -48,6 +52,7 @@ function Harness({
const activeSessionId = useStore($activeSessionId)
const selectedStoredSessionId = useStore($selectedStoredSessionId)
const busyRef = useRef(false)
+ const creatingSessionRef = useRef(false)
const cache = useSessionStateCache({
activeSessionId,
@@ -62,12 +67,12 @@ function Harness({
activeSessionId,
activeSessionIdRef: cache.activeSessionIdRef,
busyRef,
- creatingSessionRef: useRef(false),
+ creatingSessionRef,
ensureSessionState: cache.ensureSessionState,
getRouteToken: () => 'new-session',
getRoutedStoredSessionId: () => null,
holdSessionTranscriptView: cache.holdSessionTranscriptView,
- navigate: vi.fn<Options['navigate']>(),
+ navigate: stayOnRoute,
requestGateway,
routedSessionId: null,
resetViewSync: cache.resetViewSync,
@@ -119,12 +124,15 @@ async function mount({ refuse = false } = {}) {
: {}
}
- const requestGateway = vi.fn(async <T,>(method: string): Promise<T> => {
- const answer = wire(method)
+ const calls: Parameters<Options['requestGateway']>[] = []
+
+ const requestGateway: Options['requestGateway'] = async <T,>(...call: Parameters<Options['requestGateway']>) => {
+ calls.push(call)
+ const answer = wire(call[0])
// SAFETY: each answer above is the wire shape of the method that asked for it.
return answer as T
- })
+ }
const rest = installRestBridge(request =>
request.path.endsWith('/messages') ? { messages: persisted, session_id: 'stored-new' } : {}
@@ -133,9 +141,9 @@ async function mount({ refuse = false } = {}) {
let ready!: Ready
render(<Harness onReady={value => (ready = value)} requestGateway={requestGateway} />)
await waitFor(() => expect(ready).toBeDefined())
- vi.mocked(requestGatewayForProfile).mockImplementation((_profile, method) => requestGateway(method))
+ vi.mocked(requestGatewayForProfile).mockImplementation((_profile, method, params) => requestGateway(method, params))
- return { ready, requestGateway, rest }
+ return { calls, ready, rest }
}
describe('submitTextToNewSession first message', () => {
@@ -153,7 +161,7 @@ describe('submitTextToNewSession first message', () => {
})
it('shows the submitted text as the first user message once the route opens the new chat', async () => {
- const { ready, requestGateway } = await mount()
+ const { calls, ready } = await mount()
await act(async () => {
await ready.submitNew(PROMPT)
@@ -167,7 +175,7 @@ describe('submitTextToNewSession first message', () => {
await waitFor(() => expect(userRows($messages.get())).toHaveLength(1))
expect($activeSessionId.get()).toBe('rt-new')
expect(userRows($messages.get())[0]?.parts).toEqual([expect.objectContaining({ text: PROMPT, type: 'text' })])
- expect(requestGateway).toHaveBeenCalledWith('prompt.submit', { session_id: 'rt-new', text: PROMPT })
+ expect(calls).toContainEqual(['prompt.submit', { session_id: 'rt-new', text: PROMPT }])
})
it('keeps one first message when the persisted transcript already has it', async () => {
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index 69bb9f93449..712eb332baf 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -1003,6 +1003,7 @@ export function useSessionActions({
}),
stored
)
+
// Submit the exact runtime id returned by session.create so this
// atomic path cannot fall back to a route token (#85590).
try {
@@ -1023,6 +1024,7 @@ export function useSessionActions({
throw error
}
+
navigate(sessionRoute(stored), { replace: true })
return { runtimeSessionId: created.session_id, sessionId: stored }