|
| 1 | +// Tests for the background-task slash commands: /tasks and /background. |
| 2 | +// Both drive a session-scoped TaskManager (ctx.tasks). Here it's a real |
| 3 | +// TaskManager wired to a stub runner — no sub-agent actually runs, so the tests |
| 4 | +// stay fast and deterministic while exercising create / list / get / output. |
| 5 | + |
| 6 | +import { describe, expect, it } from 'vitest'; |
| 7 | +import { SessionManager, TaskManager, type TaskRunHandle } from '@deepcode/core'; |
| 8 | +import { CommandRegistry, type SessionContext } from './commands.js'; |
| 9 | + |
| 10 | +const reg = new CommandRegistry(); |
| 11 | + |
| 12 | +/** A TaskManager whose runner immediately resolves with a fixed result string. */ |
| 13 | +function stubManager(result = 'done'): TaskManager { |
| 14 | + return new TaskManager( |
| 15 | + () => ({ done: Promise.resolve(result), abort: () => {} }) as TaskRunHandle, |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +function ctx(overrides: Partial<SessionContext> = {}): SessionContext { |
| 20 | + return { |
| 21 | + cwd: '/tmp/x', |
| 22 | + model: 'deepseek-chat', |
| 23 | + mode: 'default', |
| 24 | + effort: 'medium', |
| 25 | + settings: {}, |
| 26 | + creds: { apiKey: 'sk-test' }, |
| 27 | + sessionId: 's1', |
| 28 | + sessions: new SessionManager({ root: '/tmp/x' }), |
| 29 | + usage: { inputTokens: 0, outputTokens: 0, reasoningTokens: 0, cacheReadTokens: 0 }, |
| 30 | + ...overrides, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +describe('/background', () => { |
| 35 | + it('creates a task and reports its id', async () => { |
| 36 | + const tasks = stubManager(); |
| 37 | + const out = ( |
| 38 | + await reg.match('/background')!.cmd.run(['fix', 'the', 'flaky', 'test'], ctx({ tasks })) |
| 39 | + ).join('\n'); |
| 40 | + expect(out).toMatch(/Started background task task-/); |
| 41 | + const list = tasks.list(); |
| 42 | + expect(list).toHaveLength(1); |
| 43 | + expect(list[0]!.description).toBe('fix the flaky test'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('the `/bg` alias works', async () => { |
| 47 | + const tasks = stubManager(); |
| 48 | + await reg.match('/bg')!.cmd.run(['do', 'thing'], ctx({ tasks })); |
| 49 | + expect(tasks.list()).toHaveLength(1); |
| 50 | + }); |
| 51 | + |
| 52 | + it('shows usage when given no prompt', async () => { |
| 53 | + const out = (await reg.match('/background')!.cmd.run([], ctx({ tasks: stubManager() }))).join( |
| 54 | + '\n', |
| 55 | + ); |
| 56 | + expect(out).toMatch(/Usage: \/background/); |
| 57 | + }); |
| 58 | + |
| 59 | + it('is unavailable without a task manager', async () => { |
| 60 | + const out = (await reg.match('/background')!.cmd.run(['x'], ctx())).join('\n'); |
| 61 | + expect(out).toMatch(/unavailable/i); |
| 62 | + }); |
| 63 | + |
| 64 | + it('reports a runner failure instead of throwing', async () => { |
| 65 | + const tasks = new TaskManager(() => { |
| 66 | + throw new Error('no runner attached'); |
| 67 | + }); |
| 68 | + const out = (await reg.match('/background')!.cmd.run(['x'], ctx({ tasks }))).join('\n'); |
| 69 | + expect(out).toMatch(/Could not start background task: no runner attached/); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe('/tasks', () => { |
| 74 | + it('reports an empty list', async () => { |
| 75 | + const out = (await reg.match('/tasks')!.cmd.run([], ctx({ tasks: stubManager() }))).join('\n'); |
| 76 | + expect(out).toMatch(/No background tasks yet/); |
| 77 | + }); |
| 78 | + |
| 79 | + it('lists started tasks with id, status, and description', async () => { |
| 80 | + const tasks = stubManager(); |
| 81 | + tasks.create({ description: 'task one', prompt: 'p1' }); |
| 82 | + tasks.create({ description: 'task two', prompt: 'p2' }); |
| 83 | + const out = (await reg.match('/tasks')!.cmd.run([], ctx({ tasks }))).join('\n'); |
| 84 | + expect(out).toMatch(/Background tasks \(2\)/); |
| 85 | + expect(out).toContain('task one'); |
| 86 | + expect(out).toContain('task two'); |
| 87 | + expect(out).toMatch(/\[(running|completed)\]/); |
| 88 | + }); |
| 89 | + |
| 90 | + it('`/tasks <id>` shows a single task’s status and output', async () => { |
| 91 | + const tasks = stubManager('the background result'); |
| 92 | + const t = tasks.create({ description: 'investigate', prompt: 'look into x' }); |
| 93 | + await tasks.wait(t.id); // let the stub runner settle → completed + output |
| 94 | + const out = (await reg.match('/tasks')!.cmd.run([t.id], ctx({ tasks }))).join('\n'); |
| 95 | + expect(out).toContain(t.id); |
| 96 | + expect(out).toMatch(/\[completed\]/); |
| 97 | + expect(out).toContain('the background result'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('`/tasks <unknown>` reports no such task', async () => { |
| 101 | + const out = ( |
| 102 | + await reg.match('/tasks')!.cmd.run(['task-nope'], ctx({ tasks: stubManager() })) |
| 103 | + ).join('\n'); |
| 104 | + expect(out).toMatch(/No task "task-nope"/); |
| 105 | + }); |
| 106 | + |
| 107 | + it('is unavailable without a task manager', async () => { |
| 108 | + const out = (await reg.match('/tasks')!.cmd.run([], ctx())).join('\n'); |
| 109 | + expect(out).toMatch(/unavailable/i); |
| 110 | + }); |
| 111 | +}); |
0 commit comments