|
1 | 1 | import { describe, expect, it, vi } from 'vitest'; |
2 | 2 |
|
3 | | -import { ProgressTracker } from '../../src/utils/progress.js'; |
| 3 | +import { RELATED_TASK_META_KEY } from '../../src/const.js'; |
| 4 | +import { createProgressTracker, ProgressTracker } from '../../src/utils/progress.js'; |
4 | 5 |
|
5 | 6 | describe('ProgressTracker', () => { |
6 | 7 | it('should send progress notifications correctly', async () => { |
@@ -83,4 +84,70 @@ describe('ProgressTracker', () => { |
83 | 84 | await expect(tracker.updateProgress('Test')).resolves.toBeUndefined(); |
84 | 85 | expect(mockOnStatusMessage).toHaveBeenCalledWith('Test'); |
85 | 86 | }); |
| 87 | + |
| 88 | + it('should include related-task metadata with taskId in progress notifications', async () => { |
| 89 | + const mockSendNotification = vi.fn(); |
| 90 | + const tracker = new ProgressTracker({ |
| 91 | + progressToken: 'tok', |
| 92 | + sendNotification: mockSendNotification, |
| 93 | + taskId: 'task-abc', |
| 94 | + }); |
| 95 | + |
| 96 | + await tracker.updateProgress('running'); |
| 97 | + |
| 98 | + expect(mockSendNotification).toHaveBeenCalledWith({ |
| 99 | + method: 'notifications/progress', |
| 100 | + params: { |
| 101 | + progressToken: 'tok', |
| 102 | + progress: 1, |
| 103 | + message: 'running', |
| 104 | + }, |
| 105 | + _meta: { |
| 106 | + [RELATED_TASK_META_KEY]: { |
| 107 | + taskId: 'task-abc', |
| 108 | + }, |
| 109 | + }, |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should not include _meta when taskId is not provided', async () => { |
| 114 | + const mockSendNotification = vi.fn(); |
| 115 | + const tracker = new ProgressTracker({ |
| 116 | + progressToken: 'tok', |
| 117 | + sendNotification: mockSendNotification, |
| 118 | + }); |
| 119 | + |
| 120 | + await tracker.updateProgress('running'); |
| 121 | + |
| 122 | + const notification = mockSendNotification.mock.calls[0][0]; |
| 123 | + expect(notification).not.toHaveProperty('_meta'); |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +describe('createProgressTracker', () => { |
| 128 | + it('should return null when no progressToken, no sendNotification, and no onStatusMessage', () => { |
| 129 | + expect(createProgressTracker(undefined, undefined)).toBeNull(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should return ProgressTracker when only onStatusMessage is provided', () => { |
| 133 | + const tracker = createProgressTracker(undefined, undefined, undefined, vi.fn()); |
| 134 | + expect(tracker).toBeInstanceOf(ProgressTracker); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should return ProgressTracker and send notifications for progressToken = 0', async () => { |
| 138 | + const mockSendNotification = vi.fn(); |
| 139 | + const tracker = createProgressTracker(0, mockSendNotification); |
| 140 | + |
| 141 | + expect(tracker).toBeInstanceOf(ProgressTracker); |
| 142 | + await tracker?.updateProgress('Started'); |
| 143 | + |
| 144 | + expect(mockSendNotification).toHaveBeenCalledWith({ |
| 145 | + method: 'notifications/progress', |
| 146 | + params: { |
| 147 | + progressToken: 0, |
| 148 | + progress: 1, |
| 149 | + message: 'Started', |
| 150 | + }, |
| 151 | + }); |
| 152 | + }); |
86 | 153 | }); |
0 commit comments