|
| 1 | +import { describe, expect, test } from 'bun:test' |
| 2 | +import type { Task } from '../../../utils/tasks.js' |
| 3 | +import { |
| 4 | + buildTaskAnchorReminder, |
| 5 | + buildUnfinishedTaskNotice, |
| 6 | + isAnchorlessNudge, |
| 7 | +} from '../taskAnchorReminder.js' |
| 8 | + |
| 9 | +function task(partial: Partial<Task> & Pick<Task, 'id' | 'status'>): Task { |
| 10 | + return { |
| 11 | + subject: `Task ${partial.id}`, |
| 12 | + description: '', |
| 13 | + blocks: [], |
| 14 | + blockedBy: [], |
| 15 | + ...partial, |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +describe('isAnchorlessNudge', () => { |
| 20 | + test('matches short Chinese keep-going nudges', () => { |
| 21 | + expect(isAnchorlessNudge('继续执行后续的工作')).toBe(true) |
| 22 | + expect(isAnchorlessNudge('继续完成当前任务')).toBe(true) |
| 23 | + expect(isAnchorlessNudge('自动执行后续的所有流程')).toBe(true) |
| 24 | + expect(isAnchorlessNudge('接着做')).toBe(true) |
| 25 | + }) |
| 26 | + |
| 27 | + test('matches short English keep-going nudges', () => { |
| 28 | + expect(isAnchorlessNudge('continue')).toBe(true) |
| 29 | + expect(isAnchorlessNudge('keep going')).toBe(true) |
| 30 | + expect(isAnchorlessNudge('go on please')).toBe(true) |
| 31 | + }) |
| 32 | + |
| 33 | + test('does not match empty or whitespace', () => { |
| 34 | + expect(isAnchorlessNudge('')).toBe(false) |
| 35 | + expect(isAnchorlessNudge(' ')).toBe(false) |
| 36 | + }) |
| 37 | + |
| 38 | + test('does not match long messages that merely contain a nudge word', () => { |
| 39 | + expect( |
| 40 | + isAnchorlessNudge( |
| 41 | + '继续用状态机方案重构 REPL 的输入处理模块,并补齐对应的单元测试', |
| 42 | + ), |
| 43 | + ).toBe(false) |
| 44 | + }) |
| 45 | + |
| 46 | + test('does not match substantive requests without nudge words', () => { |
| 47 | + expect(isAnchorlessNudge('给我看下 query.ts 的结构')).toBe(false) |
| 48 | + expect(isAnchorlessNudge('fix the login bug')).toBe(false) |
| 49 | + }) |
| 50 | +}) |
| 51 | + |
| 52 | +describe('buildTaskAnchorReminder', () => { |
| 53 | + test('returns null when there are no unfinished tasks', () => { |
| 54 | + expect(buildTaskAnchorReminder([])).toBeNull() |
| 55 | + expect( |
| 56 | + buildTaskAnchorReminder([ |
| 57 | + task({ id: '1', status: 'completed' }), |
| 58 | + task({ id: '2', status: 'completed' }), |
| 59 | + ]), |
| 60 | + ).toBeNull() |
| 61 | + }) |
| 62 | + |
| 63 | + test('lists only unfinished tasks, in_progress before pending', () => { |
| 64 | + const reminder = buildTaskAnchorReminder([ |
| 65 | + task({ id: '1', status: 'completed', subject: 'done' }), |
| 66 | + task({ id: '2', status: 'pending', subject: 'second' }), |
| 67 | + task({ id: '3', status: 'in_progress', subject: 'third' }), |
| 68 | + ]) |
| 69 | + expect(reminder).not.toBeNull() |
| 70 | + const body = reminder! |
| 71 | + expect(body).toContain('2 unfinished task(s)') |
| 72 | + // in_progress (#3) must appear before pending (#2) |
| 73 | + expect(body.indexOf('#3 [in_progress]')).toBeLessThan( |
| 74 | + body.indexOf('#2 [pending]'), |
| 75 | + ) |
| 76 | + expect(body).not.toContain('#1') |
| 77 | + expect(body).toContain('<system-reminder>') |
| 78 | + expect(body).toContain('</system-reminder>') |
| 79 | + }) |
| 80 | + |
| 81 | + test('orders same-status tasks by numeric id', () => { |
| 82 | + const reminder = buildTaskAnchorReminder([ |
| 83 | + task({ id: '10', status: 'pending', subject: 'ten' }), |
| 84 | + task({ id: '2', status: 'pending', subject: 'two' }), |
| 85 | + ])! |
| 86 | + expect(reminder.indexOf('#2 ')).toBeLessThan(reminder.indexOf('#10 ')) |
| 87 | + }) |
| 88 | + |
| 89 | + test('caps the list and reports overflow', () => { |
| 90 | + const many: Task[] = Array.from({ length: 25 }, (_, i) => |
| 91 | + task({ id: String(i + 1), status: 'pending' }), |
| 92 | + ) |
| 93 | + const reminder = buildTaskAnchorReminder(many)! |
| 94 | + expect(reminder).toContain('25 unfinished task(s)') |
| 95 | + expect(reminder).toContain('…and 5 more') |
| 96 | + }) |
| 97 | +}) |
| 98 | + |
| 99 | +describe('buildUnfinishedTaskNotice', () => { |
| 100 | + test('returns null when no task is in_progress', () => { |
| 101 | + expect(buildUnfinishedTaskNotice([])).toBeNull() |
| 102 | + expect( |
| 103 | + buildUnfinishedTaskNotice([ |
| 104 | + task({ id: '1', status: 'pending' }), |
| 105 | + task({ id: '2', status: 'completed' }), |
| 106 | + ]), |
| 107 | + ).toBeNull() |
| 108 | + }) |
| 109 | + |
| 110 | + test('surfaces in_progress task names and pending count', () => { |
| 111 | + const notice = buildUnfinishedTaskNotice([ |
| 112 | + task({ id: '1', status: 'in_progress', subject: '写模块' }), |
| 113 | + task({ id: '2', status: 'pending' }), |
| 114 | + task({ id: '3', status: 'pending' }), |
| 115 | + task({ id: '4', status: 'completed' }), |
| 116 | + ])! |
| 117 | + expect(notice).toContain('#1 写模块') |
| 118 | + expect(notice).toContain('2 个待办') |
| 119 | + }) |
| 120 | + |
| 121 | + test('collapses overflow when more than three in_progress tasks', () => { |
| 122 | + const notice = buildUnfinishedTaskNotice( |
| 123 | + Array.from({ length: 5 }, (_, i) => |
| 124 | + task({ id: String(i + 1), status: 'in_progress' }), |
| 125 | + ), |
| 126 | + )! |
| 127 | + expect(notice).toContain('等 5 个') |
| 128 | + }) |
| 129 | +}) |
0 commit comments