|
1 | 1 | import { describe, test, expect, beforeEach } from 'bun:test'; |
2 | 2 | import { setupTestDb } from './helpers/mock-db'; |
3 | 3 | import { TaskService } from '../src/core/services/task.service'; |
| 4 | +import { TaskRunService } from '../src/core/services/task-run.service'; |
4 | 5 |
|
5 | 6 | describe('TaskService', () => { |
6 | 7 | beforeEach(() => { |
@@ -900,5 +901,22 @@ describe('TaskService', () => { |
900 | 901 | const count = await TaskService.resetRunningToPending([]); |
901 | 902 | expect(count).toBe(0); |
902 | 903 | }); |
| 904 | + |
| 905 | + test('启动恢复只重置没有 active run 的孤儿 running 任务', async () => { |
| 906 | + const orphan = await TaskService.add({ name: '孤儿任务', agent: 'a', prompt: '恢复任务' }); |
| 907 | + const active = await TaskService.add({ name: '执行中任务', agent: 'a', prompt: '保持执行' }); |
| 908 | + const closed = await TaskService.add({ name: '已关闭 run 的任务', agent: 'a', prompt: '重新排队' }); |
| 909 | + await TaskService.start(orphan.id); |
| 910 | + await TaskService.start(active.id); |
| 911 | + await TaskService.start(closed.id); |
| 912 | + await TaskRunService.create({ taskId: active.id }); |
| 913 | + const closedRun = await TaskRunService.create({ taskId: closed.id }); |
| 914 | + await TaskRunService.fail(closedRun.id, 'Gateway 在同步任务状态前退出'); |
| 915 | + |
| 916 | + expect(await TaskService.resetOrphanRunningToPending()).toBe(2); |
| 917 | + expect((await TaskService.getById(orphan.id))?.status).toBe('pending'); |
| 918 | + expect((await TaskService.getById(closed.id))?.status).toBe('pending'); |
| 919 | + expect((await TaskService.getById(active.id))?.status).toBe('running'); |
| 920 | + }); |
903 | 921 | }); |
904 | 922 | }); |
0 commit comments