Files
orca/src/main/runtime/orchestration/formatter.test.ts
T
Jinwoo Hong 2b42de1f52 fix(orchestration): wake coordinators with mail pointers (#12988)
Wake idle Run coordinators with durable orchestration mail pointers while keeping message payloads in the store until check consumes them. Preserve waiter, Cursor, restart, real Codex title, and PTY replacement behavior.\n\nPart of #12953.
2026-08-06 22:50:09 -07:00

198 lines
7.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { formatMessageBanner, formatMessagePointer, formatMessagesForInjection } from './formatter'
import type { MessageRow } from './types'
function makeMessage(overrides: Partial<MessageRow> = {}): MessageRow {
return {
id: 'msg_test1',
run_id: 'run_test',
from_handle: 'term_abc123',
to_handle: 'term_coord',
subject: 'Auth API implementation complete',
body: 'All endpoints implemented. Tests passing.',
type: 'worker_done',
priority: 'normal',
thread_id: null,
payload: null,
read: 0,
sequence: 1,
created_at: '2026-01-01T00:00:00Z',
delivered_at: null,
sender_pane_key: null,
...overrides
}
}
describe('formatMessageBanner', () => {
it('formats a normal priority message without a priority tag', () => {
const banner = formatMessageBanner(makeMessage())
expect(banner).toContain('From: TERM_ABC123 (term_abc123)')
expect(banner).toContain('(worker_done)')
expect(banner).not.toContain('[URGENT]')
expect(banner).not.toContain('[HIGH]')
})
it('includes [HIGH] tag for high priority', () => {
const banner = formatMessageBanner(makeMessage({ priority: 'high' }))
expect(banner).toContain('[HIGH]')
})
it('includes [URGENT] tag for urgent priority', () => {
const banner = formatMessageBanner(makeMessage({ priority: 'urgent' }))
expect(banner).toContain('[URGENT]')
})
it('includes body after subject', () => {
const banner = formatMessageBanner(makeMessage({ body: 'Some details here' }))
const lines = banner.split('\n')
const subjectIdx = lines.findIndex((l) => l.startsWith('Subject:'))
const bodyIdx = lines.indexOf('Some details here')
expect(subjectIdx).toBeGreaterThanOrEqual(0)
expect(bodyIdx).toBeGreaterThan(subjectIdx)
})
it('omits body line when body is empty', () => {
const banner = formatMessageBanner(makeMessage({ body: '' }))
const lines = banner.split('\n')
// Subject line should be immediately followed by Reply hint (no empty body line)
const subjectIdx = lines.findIndex((l) => l.startsWith('Subject:'))
expect(lines[subjectIdx + 1]).toMatch(/^\[Reply:/)
})
it('includes payload when present', () => {
const payload = '{"taskId":"task_1","exitCode":0}'
const banner = formatMessageBanner(makeMessage({ payload }))
expect(banner).toContain(`[Payload: ${payload}]`)
})
it('omits payload line when payload is null', () => {
const banner = formatMessageBanner(makeMessage({ payload: null }))
expect(banner).not.toContain('[Payload:')
})
it('includes reply hint with message ID', () => {
const banner = formatMessageBanner(makeMessage({ id: 'msg_xyz789' }))
expect(banner).toContain(
'[Reply: orca orchestration reply --id msg_xyz789 --from term_coord --body "..."]'
)
})
it('lets the CLI resolve the live sender for Run and Dispatch addresses', () => {
for (const to_handle of ['run:run_test', 'dispatch:dispatch_test']) {
const banner = formatMessageBanner(makeMessage({ to_handle }))
expect(banner).toContain('[Reply: orca orchestration reply --id msg_test1 --body "..."]')
expect(banner).not.toContain(`--from ${to_handle}`)
}
})
it('marks legacy messages read-only without reply or acknowledgment affordances', () => {
const banner = formatMessageBanner(
makeMessage({ id: 'msg_legacy', run_id: 'run_legacy_local' })
)
expect(banner).toContain('[LEGACY READ-ONLY]')
expect(banner).toContain('[Inspection only: reply and acknowledgment are unavailable.]')
expect(banner).not.toContain('[Reply:')
expect(banner).not.toContain('orchestration reply')
})
it('renders only attested actions for legacy compatibility authority', () => {
const banner = formatMessageBanner(makeMessage({ id: 'msg_legacy' }), {
authority: 'legacy_compatibility',
supportedActionHints: [
'orca orchestration reply --id msg_legacy --from term_coord --body "..."'
]
})
expect(banner).toContain('[LEGACY COMPATIBILITY]')
expect(banner).toContain(
'[Supported action: orca orchestration reply --id msg_legacy --from term_coord --body "..."]'
)
expect(banner).not.toContain('[Reply:')
expect(banner).not.toContain('acknowledgment')
})
it('warns that a bounded legacy recovery replay may already have been seen', () => {
const banner = formatMessageBanner(makeMessage(), {
authority: 'legacy_recovery_replay',
supportedActionHints: ['orca orchestration check --ack delivery_legacy']
})
expect(banner).toContain('[LEGACY RECOVERY REPLAY — MAY HAVE BEEN SEEN]')
expect(banner).toContain('bounded recovery replay may already have been seen')
expect(banner).toContain('[Supported action: orca orchestration check --ack delivery_legacy]')
expect(banner).not.toContain('[Reply:')
})
it('does not infer live compatibility from legacy database provenance', () => {
const banner = formatMessageBanner(makeMessage({ run_id: 'run_legacy_local' }), {
supportedActionHints: ['orca orchestration check --ack delivery_legacy']
})
expect(banner).toContain('[LEGACY READ-ONLY]')
expect(banner).not.toContain('Supported action:')
expect(banner).not.toContain('check --ack')
})
it('keeps adopted legacy and audit messages read-only without runtime authority', () => {
for (const deliveryContract of ['legacy_direct', 'audit_only'] as const) {
const banner = formatMessageBanner(
makeMessage({ run_id: 'run_adopted', delivery_contract: deliveryContract })
)
expect(banner).toContain('[LEGACY READ-ONLY]')
expect(banner).not.toContain('[Reply:')
}
})
it('keeps current formatting unchanged when authority is explicit', () => {
const message = makeMessage({ id: 'msg_current' })
expect(formatMessageBanner(message, { authority: 'current' })).toBe(
formatMessageBanner(message)
)
})
it('ends with a separator line', () => {
const banner = formatMessageBanner(makeMessage())
const lines = banner.split('\n')
expect(lines.at(-1)).toMatch(/^─+$/)
})
})
describe('formatMessagesForInjection', () => {
it('returns empty string for empty array', () => {
expect(formatMessagesForInjection([])).toBe('')
})
it('wraps multiple banners with orchestration messages header', () => {
const messages = [makeMessage({ id: 'msg_1' }), makeMessage({ id: 'msg_2' })]
const result = formatMessagesForInjection(messages)
expect(result).toContain('--- Orchestration Messages (2) ---')
expect(result).toContain('msg_1')
expect(result).toContain('msg_2')
expect(result).toMatch(/\n---\n$/)
})
it('separates multiple banners with blank lines', () => {
const messages = [makeMessage({ id: 'msg_a' }), makeMessage({ id: 'msg_b' })]
const result = formatMessagesForInjection(messages)
// Two banners should be separated by \n\n
const bannerA = formatMessageBanner(messages[0])
const bannerB = formatMessageBanner(messages[1])
expect(result).toContain(`${bannerA}\n\n${bannerB}`)
})
})
describe('formatMessagePointer', () => {
it('formats a singular pointer without message content', () => {
expect(formatMessagePointer(1)).toBe(
'\nYou have 1 orchestration message. Run `orca orchestration check`.\n'
)
})
it('pluralizes a batched pointer', () => {
expect(formatMessagePointer(3)).toContain('3 orchestration messages')
})
})