test(native-chat): read sent requests through checked helpers instead of casts

This commit is contained in:
Brennan Benson
2026-10-04 03:48:38 -07:00
parent 0f7a1eb4c7
commit ee4dbf021c
2 changed files with 34 additions and 6 deletions
@@ -183,6 +183,24 @@ vi.mock('./NativeChatQuestionCard', () => ({
import { NativeChatStructuredSession } from './NativeChatStructuredSession'
import { getStructuredAgentSessionOutbox } from './structured-agent-session-outbox-storage'
/** The first text block a send request carried, read without trusting its shape. */
function sentText(params: unknown): string | undefined {
if (typeof params !== 'object' || params === null || !('body' in params)) {
return undefined
}
const { body } = params
if (typeof body !== 'object' || body === null || !('blocks' in body)) {
return undefined
}
const [first] = Array.isArray(body.blocks) ? body.blocks : []
return typeof first === 'object' &&
first !== null &&
'text' in first &&
typeof first.text === 'string'
? first.text
: undefined
}
describe('NativeChatStructuredSession delivery', () => {
afterEach(() => {
cleanup()
@@ -480,9 +498,7 @@ describe('NativeChatStructuredSession delivery', () => {
send?.('second', [])
})
await waitFor(() => expect(mocks.call).toHaveBeenCalledTimes(2), { timeout: 5000 })
const texts = mocks.call.mock.calls.map(
(call) => (call[2] as { body: { blocks: { text: string }[] } }).body.blocks[0]?.text
)
const texts = mocks.call.mock.calls.map((call) => sentText(call[2]))
expect(texts).toEqual(['first', 'second'])
expect(screen.queryByText('Message delivery is unconfirmed.')).toBeNull()
expect(screen.queryByRole('button', { name: /Retry/ })).toBeNull()
@@ -132,6 +132,20 @@ function refusedResult(code: AgentSessionWireRefusalCode) {
return { ok: false, refusal: { code, message: code } }
}
/** The operation id a request carried, read without trusting its shape. */
function sentOperationId(params: unknown): string | undefined {
if (typeof params !== 'object' || params === null || !('envelope' in params)) {
return undefined
}
const { envelope } = params
return typeof envelope === 'object' &&
envelope !== null &&
'clientOperationId' in envelope &&
typeof envelope.clientOperationId === 'string'
? envelope.clientOperationId
: undefined
}
describe('useStructuredAgentSessionOutbox', () => {
let randomUuidSequence = 0
@@ -592,9 +606,7 @@ describe('useStructuredAgentSessionOutbox', () => {
})
await waitFor(() => expect(result.current.outbox).toHaveLength(0))
const ids = mocks.call.mock.calls.map(
(call) => (call[2] as { envelope: { clientOperationId: string } }).envelope.clientOperationId
)
const ids = mocks.call.mock.calls.map((call) => sentOperationId(call[2]))
// The first went once; its row says it was not sent, and its text is not handed back.
expect(ids.filter((id) => id === firstId)).toHaveLength(1)
expect(ids).toHaveLength(2)