|
| 1 | +import { describe, expect, it, mock } from 'bun:test' |
| 2 | +import type { OpencodeClient } from '@opencode-ai/sdk' |
| 3 | +import moment from 'moment' |
| 4 | +import { RingBuffer } from '../src/plugin/pty/buffer.ts' |
| 5 | +import { NotificationManager } from '../src/plugin/pty/notification-manager.ts' |
| 6 | +import type { PTYSession } from '../src/plugin/pty/types.ts' |
| 7 | + |
| 8 | +type PromptPayload = { |
| 9 | + path: { id: string } |
| 10 | + body: { |
| 11 | + parts: Array<{ type: string; text: string }> |
| 12 | + agent?: string |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +function createSession(overrides: Partial<PTYSession> = {}): PTYSession { |
| 17 | + const buffer = new RingBuffer() |
| 18 | + buffer.append('line 1\nline 2\n') |
| 19 | + |
| 20 | + return { |
| 21 | + id: 'pty_test', |
| 22 | + title: 'Test Session', |
| 23 | + description: 'Test session description', |
| 24 | + command: 'echo', |
| 25 | + args: ['hello'], |
| 26 | + workdir: '/tmp', |
| 27 | + status: 'running', |
| 28 | + pid: 12345, |
| 29 | + createdAt: moment(), |
| 30 | + parentSessionId: 'parent-session-id', |
| 31 | + parentAgent: 'agent-two', |
| 32 | + notifyOnExit: true, |
| 33 | + buffer, |
| 34 | + process: null, |
| 35 | + ...overrides, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +describe('NotificationManager', () => { |
| 40 | + it('includes body.agent when originating agent is present', async () => { |
| 41 | + const promptAsync = mock(async (_payload: PromptPayload) => {}) |
| 42 | + const manager = new NotificationManager() |
| 43 | + |
| 44 | + manager.init({ session: { promptAsync } } as unknown as OpencodeClient) |
| 45 | + |
| 46 | + await manager.sendExitNotification(createSession({ parentAgent: 'agent-two' }), 0) |
| 47 | + |
| 48 | + expect(promptAsync).toHaveBeenCalledTimes(1) |
| 49 | + const payload = promptAsync.mock.calls[0]![0] |
| 50 | + |
| 51 | + expect(payload.path).toEqual({ id: 'parent-session-id' }) |
| 52 | + expect(payload.body.agent).toBe('agent-two') |
| 53 | + expect(payload.body.parts).toHaveLength(1) |
| 54 | + expect(payload.body.parts[0]?.text).toContain('<pty_exited>') |
| 55 | + expect(payload.body.parts[0]?.text).toContain('Use pty_read to check the full output.') |
| 56 | + }) |
| 57 | + |
| 58 | + it('omits body.agent when originating agent is missing', async () => { |
| 59 | + const promptAsync = mock(async (_payload: PromptPayload) => {}) |
| 60 | + const manager = new NotificationManager() |
| 61 | + |
| 62 | + manager.init({ session: { promptAsync } } as unknown as OpencodeClient) |
| 63 | + |
| 64 | + await manager.sendExitNotification(createSession({ parentAgent: undefined }), 1) |
| 65 | + |
| 66 | + expect(promptAsync).toHaveBeenCalledTimes(1) |
| 67 | + const payload = promptAsync.mock.calls[0]![0] |
| 68 | + |
| 69 | + expect(payload.path).toEqual({ id: 'parent-session-id' }) |
| 70 | + expect(Object.hasOwn(payload.body, 'agent')).toBe(false) |
| 71 | + expect(payload.body.parts).toHaveLength(1) |
| 72 | + expect(payload.body.parts[0]?.text).toContain('<pty_exited>') |
| 73 | + expect(payload.body.parts[0]?.text).toContain( |
| 74 | + 'Process failed. Use pty_read with the pattern parameter to search for errors in the output.' |
| 75 | + ) |
| 76 | + }) |
| 77 | +}) |
0 commit comments