|
| 1 | +import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test' |
| 2 | +import * as fs from 'node:fs/promises' |
| 3 | +import * as os from 'node:os' |
| 4 | +import * as path from 'node:path' |
| 5 | +import { |
| 6 | + __persistCliTaskNotificationForTests, |
| 7 | + __resetWebSocketHandlerStateForTests, |
| 8 | +} from '../ws/handler.js' |
| 9 | +import { SessionService, sessionService } from '../services/sessionService.js' |
| 10 | + |
| 11 | +describe('background task notification persistence', () => { |
| 12 | + let configDir = '' |
| 13 | + let previousConfigDir: string | undefined |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + previousConfigDir = process.env.CLAUDE_CONFIG_DIR |
| 17 | + configDir = await fs.mkdtemp(path.join(os.tmpdir(), 'cc-haha-task-notification-')) |
| 18 | + process.env.CLAUDE_CONFIG_DIR = configDir |
| 19 | + await fs.mkdir(path.join(configDir, 'projects'), { recursive: true }) |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(async () => { |
| 23 | + __resetWebSocketHandlerStateForTests() |
| 24 | + mock.restore() |
| 25 | + if (previousConfigDir === undefined) delete process.env.CLAUDE_CONFIG_DIR |
| 26 | + else process.env.CLAUDE_CONFIG_DIR = previousConfigDir |
| 27 | + await fs.rm(configDir, { recursive: true, force: true }) |
| 28 | + }) |
| 29 | + |
| 30 | + it('restores a terminal Agent notification that was not forwarded into transcript history', async () => { |
| 31 | + const sessionId = crypto.randomUUID() |
| 32 | + const projectDir = path.join(configDir, 'projects', '-tmp-background-agent') |
| 33 | + const transcriptPath = path.join(projectDir, `${sessionId}.jsonl`) |
| 34 | + await fs.mkdir(projectDir, { recursive: true }) |
| 35 | + await fs.writeFile(transcriptPath, `${JSON.stringify({ |
| 36 | + type: 'assistant', |
| 37 | + uuid: crypto.randomUUID(), |
| 38 | + timestamp: '2026-07-18T00:00:00.000Z', |
| 39 | + message: { |
| 40 | + role: 'assistant', |
| 41 | + content: [{ |
| 42 | + type: 'tool_use', |
| 43 | + id: 'agent-tool-1', |
| 44 | + name: 'Agent', |
| 45 | + input: { description: 'Verify background restore', run_in_background: true }, |
| 46 | + }], |
| 47 | + }, |
| 48 | + })}\n`, 'utf8') |
| 49 | + |
| 50 | + const service = new SessionService() |
| 51 | + await service.appendSessionTaskNotification(sessionId, { |
| 52 | + taskId: 'agent-task-1', |
| 53 | + toolUseId: 'agent-tool-1', |
| 54 | + status: 'completed', |
| 55 | + summary: 'Agent completed after the foreground Skill output', |
| 56 | + result: 'Background verification passed', |
| 57 | + timestamp: '2026-07-18T00:01:00.000Z', |
| 58 | + }) |
| 59 | + |
| 60 | + expect(await service.getSessionTaskNotifications(sessionId)).toEqual([{ |
| 61 | + taskId: 'agent-task-1', |
| 62 | + toolUseId: 'agent-tool-1', |
| 63 | + status: 'completed', |
| 64 | + summary: 'Agent completed after the foreground Skill output', |
| 65 | + result: 'Background verification passed', |
| 66 | + timestamp: '2026-07-18T00:01:00.000Z', |
| 67 | + }]) |
| 68 | + expect(await fs.readFile(transcriptPath, 'utf8')).toContain('"type":"cc-haha-task-notification"') |
| 69 | + }) |
| 70 | + |
| 71 | + it('keeps restoring legacy task-notification transcript turns', async () => { |
| 72 | + const sessionId = crypto.randomUUID() |
| 73 | + const projectDir = path.join(configDir, 'projects', '-tmp-legacy-notification') |
| 74 | + const transcriptPath = path.join(projectDir, `${sessionId}.jsonl`) |
| 75 | + await fs.mkdir(projectDir, { recursive: true }) |
| 76 | + await fs.writeFile(transcriptPath, `${JSON.stringify({ |
| 77 | + type: 'user', |
| 78 | + uuid: crypto.randomUUID(), |
| 79 | + timestamp: '2026-07-17T00:00:00.000Z', |
| 80 | + message: { |
| 81 | + role: 'user', |
| 82 | + content: '<task-notification>\n<task-id>legacy-task</task-id>\n<tool-use-id>legacy-tool</tool-use-id>\n<status>completed</status>\n<summary>Legacy task completed</summary>\n</task-notification>', |
| 83 | + }, |
| 84 | + })}\n`, 'utf8') |
| 85 | + |
| 86 | + const service = new SessionService() |
| 87 | + expect(await service.getSessionTaskNotifications(sessionId)).toEqual([{ |
| 88 | + taskId: 'legacy-task', |
| 89 | + toolUseId: 'legacy-tool', |
| 90 | + status: 'completed', |
| 91 | + summary: 'Legacy task completed', |
| 92 | + timestamp: '2026-07-17T00:00:00.000Z', |
| 93 | + }]) |
| 94 | + }) |
| 95 | + |
| 96 | + it('normalizes and persists one terminal SDK event for multiple observers', async () => { |
| 97 | + const append = spyOn(sessionService, 'appendSessionTaskNotification').mockResolvedValue() |
| 98 | + const sdkEvent = { |
| 99 | + type: 'system', |
| 100 | + subtype: 'task_notification', |
| 101 | + uuid: 'terminal-event-1', |
| 102 | + task_id: 'agent-task-1', |
| 103 | + tool_use_id: 'agent-tool-1', |
| 104 | + status: 'completed', |
| 105 | + summary: 'Agent completed', |
| 106 | + result: 'All checks passed', |
| 107 | + output_file: '/tmp/agent-task-1.output', |
| 108 | + timestamp: '2026-07-18T00:01:00.000Z', |
| 109 | + } |
| 110 | + |
| 111 | + const first = __persistCliTaskNotificationForTests('session-1', sdkEvent) |
| 112 | + const second = __persistCliTaskNotificationForTests('session-1', sdkEvent) |
| 113 | + expect(first).not.toBeNull() |
| 114 | + expect(second).toBe(first) |
| 115 | + await Promise.all([first, second]) |
| 116 | + |
| 117 | + expect(append).toHaveBeenCalledTimes(1) |
| 118 | + expect(append).toHaveBeenCalledWith('session-1', { |
| 119 | + taskId: 'agent-task-1', |
| 120 | + toolUseId: 'agent-tool-1', |
| 121 | + status: 'completed', |
| 122 | + summary: 'Agent completed', |
| 123 | + result: 'All checks passed', |
| 124 | + outputFile: '/tmp/agent-task-1.output', |
| 125 | + timestamp: '2026-07-18T00:01:00.000Z', |
| 126 | + }) |
| 127 | + }) |
| 128 | +}) |
0 commit comments