Files
archived-orca/src/shared/agent-session/agent-session-message-source.test.ts

82 lines
2.9 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest'
import { readAgentMessageSource, type AgentMessageSource } from './agent-session-message-source'
import { testOrcaSessionId } from '../orca-session-address-test-fixture'
const SESSION = '4a1f6c2e-8b3d-4e7a-9c15-0d2b6e8f1a37'
const FROM: AgentMessageSource = {
kind: 'agent',
senders: [
{
party: { address: 'term_a', terminalHandle: 'term_a', orcaSessionId: null },
name: 'Build tab',
agent: 'claude'
},
{
party: {
address: `orca_session_id:${SESSION}`,
terminalHandle: null,
orcaSessionId: testOrcaSessionId(SESSION)
},
name: null
}
],
orchestration: {
message: 'mail-notice',
mailbox: 'run:r1',
dispatchId: null,
messages: [{ messageId: 'm1', runId: 'r1', from: 'term_a' }]
}
}
describe("a message's sender, read through its one reader", () => {
it("reads a Dispatch's task back whole", () => {
const task: AgentMessageSource = {
...FROM,
orchestration: { message: 'task', runId: 'r1', taskId: 't1', dispatchId: 'ctx_1' }
}
expect(readAgentMessageSource(structuredClone(task))).toEqual(task)
const parsed = readAgentMessageSource({
...task,
senders: FROM.senders.map((sender) => ({ ...sender, relation: 'future' }))
})
expect(parsed).toEqual(task)
})
it("keeps a sender's agent this build does not know, and drops only an unreadable one", () => {
const withAgent = (agent: unknown) =>
readAgentMessageSource({ ...FROM, senders: [{ ...FROM.senders[0], agent }] })?.senders[0]
expect(withAgent('future-agent')?.agent).toBe('future-agent')
expect(withAgent('x'.repeat(200))).toEqual({ ...FROM.senders[0], agent: undefined })
expect(withAgent(7)?.name).toBe('Build tab')
})
it('keeps an agent an agent when its orchestration kind is newer than this build', () => {
const newer = { ...FROM, orchestration: { message: 'review', dispatchId: 'ctx_1' } }
expect(readAgentMessageSource(newer)).toEqual({ ...FROM, orchestration: null })
// A newer sender kind is still not the person.
expect(readAgentMessageSource({ ...FROM, kind: 'automation' })).toMatchObject({
kind: 'agent'
})
})
it('drops a sender it cannot read and bounds a name, keeping the rest', () => {
const read = readAgentMessageSource({
...FROM,
senders: [
{ party: { address: 'term_a', terminalHandle: 'term_a', orcaSessionId: 'term_a' } },
{ party: { address: 'term_b', terminalHandle: 'term_b', orcaSessionId: null }, name: 7 },
{
party: { address: 'term_c', terminalHandle: 'term_c', orcaSessionId: null },
name: `a‮b\n${'x'.repeat(300)}`
}
]
})
expect(read?.senders.map(({ party, name }) => [party.address, name?.length ?? null])).toEqual([
['term_b', null],
['term_c', 200]
])
expect(read?.senders[1]?.name?.startsWith('a b ')).toBe(true)
})
})