|
1 | | -import { describe, expect, test } from 'bun:test'; |
2 | | -import { backlogCommand } from './backlog'; |
| 1 | +import { describe, expect, mock, test } from 'bun:test'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { type BacklogDeps, backlogCommand, createBacklogCommand, runBacklog } from './backlog'; |
| 4 | +import type { ConsoleLike } from './context'; |
3 | 5 |
|
4 | | -describe('backlog command', () => { |
5 | | - test('has correct name and description', () => { |
6 | | - expect(backlogCommand.name()).toBe('backlog'); |
7 | | - expect(backlogCommand.description()).toBe('Add items to the project backlog'); |
| 6 | +interface MockBacklogDeps extends BacklogDeps { |
| 7 | + logs: string[]; |
| 8 | + errors: string[]; |
| 9 | + warnings: string[]; |
| 10 | + exitCode?: number; |
| 11 | + files: Map<string, string>; |
| 12 | +} |
| 13 | + |
| 14 | +function createMockBacklogDeps(initialFiles?: Record<string, string>): MockBacklogDeps { |
| 15 | + const files = new Map<string, string>(Object.entries(initialFiles ?? {})); |
| 16 | + const logs: string[] = []; |
| 17 | + const errors: string[] = []; |
| 18 | + const warnings: string[] = []; |
| 19 | + let exitCode: number | undefined; |
| 20 | + |
| 21 | + const consoleLike: ConsoleLike = { |
| 22 | + log: (...args: unknown[]) => { |
| 23 | + logs.push(args.join(' ')); |
| 24 | + }, |
| 25 | + error: (...args: unknown[]) => { |
| 26 | + errors.push(args.join(' ')); |
| 27 | + }, |
| 28 | + warn: (...args: unknown[]) => { |
| 29 | + warnings.push(args.join(' ')); |
| 30 | + }, |
| 31 | + }; |
| 32 | + |
| 33 | + return { |
| 34 | + console: consoleLike, |
| 35 | + path, |
| 36 | + process: { |
| 37 | + cwd: () => '/project', |
| 38 | + env: {}, |
| 39 | + getExitCode: () => exitCode, |
| 40 | + setExitCode: (code: number) => { |
| 41 | + exitCode = code; |
| 42 | + }, |
| 43 | + }, |
| 44 | + fs: { |
| 45 | + existsSync: (targetPath: string) => files.has(targetPath), |
| 46 | + readFileSync: (targetPath: string) => { |
| 47 | + const value = files.get(targetPath); |
| 48 | + if (value === undefined) { |
| 49 | + throw new Error(`File not found: ${targetPath}`); |
| 50 | + } |
| 51 | + return value; |
| 52 | + }, |
| 53 | + writeFileSync: (targetPath: string, content: string | NodeJS.ArrayBufferView) => { |
| 54 | + files.set(targetPath, content.toString()); |
| 55 | + }, |
| 56 | + appendFileSync: (targetPath: string, content: string | NodeJS.ArrayBufferView) => { |
| 57 | + const previous = files.get(targetPath) ?? ''; |
| 58 | + files.set(targetPath, `${previous}${content.toString()}`); |
| 59 | + }, |
| 60 | + mkdirSync: mock(() => {}), |
| 61 | + readdirSync: mock(() => []), |
| 62 | + unlinkSync: mock(() => {}), |
| 63 | + copyFileSync: mock(() => {}), |
| 64 | + }, |
| 65 | + time: { |
| 66 | + now: () => new Date('2025-01-01T00:00:00Z'), |
| 67 | + todayISO: () => '2025-01-01', |
| 68 | + }, |
| 69 | + logger: () => ({ |
| 70 | + info: mock(() => {}), |
| 71 | + warn: mock(() => {}), |
| 72 | + error: mock(() => {}), |
| 73 | + debug: mock(() => {}), |
| 74 | + exception: mock(() => {}), |
| 75 | + }), |
| 76 | + logs, |
| 77 | + errors, |
| 78 | + warnings, |
| 79 | + exitCode, |
| 80 | + files, |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +describe('runBacklog', () => { |
| 85 | + test('fails when no items are provided', () => { |
| 86 | + const deps = createMockBacklogDeps(); |
| 87 | + |
| 88 | + const result = runBacklog([], {}, deps); |
| 89 | + |
| 90 | + expect(result.success).toBeFalse(); |
| 91 | + if (result.success) return; |
| 92 | + expect(result.error).toContain('No items provided'); |
| 93 | + expect(result.exitCode).toBe(1); |
8 | 94 | }); |
9 | 95 |
|
10 | | - test('has expected options', () => { |
11 | | - const options = backlogCommand.options; |
12 | | - const optionNames = options.map((opt) => opt.long); |
| 96 | + test('creates backlog file and appends items when missing', () => { |
| 97 | + const deps = createMockBacklogDeps(); |
| 98 | + |
| 99 | + const result = runBacklog(['Improve docs', 'Refactor parser'], {}, deps); |
| 100 | + |
| 101 | + expect(result.success).toBeTrue(); |
| 102 | + if (!result.success) return; |
13 | 103 |
|
14 | | - expect(optionNames).toContain('--list'); |
15 | | - expect(optionNames).toContain('--file'); |
| 104 | + const backlogPath = path.join('/project', '.claude', 'backlog.md'); |
| 105 | + const fileContent = deps.files.get(backlogPath); |
| 106 | + expect(fileContent).toBeDefined(); |
| 107 | + expect(fileContent).toContain('# Backlog'); |
| 108 | + expect(fileContent).toContain('[2025-01-01] Improve docs'); |
| 109 | + expect(fileContent).toContain('[2025-01-01] Refactor parser'); |
| 110 | + expect(result.messages).toEqual(['✅ Added 2 item(s) to backlog', ' - Improve docs', ' - Refactor parser']); |
16 | 111 | }); |
17 | 112 |
|
18 | | - test('command structure is correct', () => { |
19 | | - // Basic validation that command has expected structure |
20 | | - expect(backlogCommand.name()).toBe('backlog'); |
21 | | - expect(backlogCommand.options).toBeDefined(); |
22 | | - expect(backlogCommand.options.length).toBeGreaterThan(0); |
| 113 | + test('lists backlog content when file exists', () => { |
| 114 | + const backlogPath = path.join('/project', '.claude', 'backlog.md'); |
| 115 | + const deps = createMockBacklogDeps({ |
| 116 | + [backlogPath]: '# Backlog\n\n## Items\n- Existing item\n', |
| 117 | + }); |
| 118 | + |
| 119 | + const result = runBacklog([], { list: true }, deps); |
| 120 | + |
| 121 | + expect(result.success).toBeTrue(); |
| 122 | + if (!result.success) return; |
| 123 | + |
| 124 | + expect(result.data?.content).toContain('Existing item'); |
| 125 | + expect(result.messages?.[0]).toContain('Existing item'); |
23 | 126 | }); |
24 | 127 |
|
25 | | - test('option descriptions are correct', () => { |
26 | | - const options = backlogCommand.options; |
27 | | - const listOption = options.find((opt) => opt.long === '--list'); |
28 | | - const fileOption = options.find((opt) => opt.long === '--file'); |
| 128 | + test('reports failure when filesystem throws', () => { |
| 129 | + const deps = createMockBacklogDeps(); |
| 130 | + deps.fs.writeFileSync = () => { |
| 131 | + throw new Error('disk full'); |
| 132 | + }; |
| 133 | + |
| 134 | + const result = runBacklog(['Improve docs'], {}, deps); |
| 135 | + |
| 136 | + expect(result.success).toBeFalse(); |
| 137 | + if (result.success) return; |
| 138 | + expect(result.error).toContain('Failed to update backlog'); |
| 139 | + }); |
| 140 | +}); |
29 | 141 |
|
30 | | - expect(listOption?.description).toBe('list current backlog items'); |
31 | | - expect(fileOption?.description).toBe('backlog file path (default: .claude/backlog.md)'); |
| 142 | +describe('createBacklogCommand', () => { |
| 143 | + test('creates command with expected metadata', () => { |
| 144 | + const command = createBacklogCommand(); |
| 145 | + expect(command.name()).toBe('backlog'); |
| 146 | + expect(command.description()).toBe('Add items to the project backlog'); |
| 147 | + |
| 148 | + const optionFlags = command.options.map((option) => option.long); |
| 149 | + expect(optionFlags).toContain('--list'); |
| 150 | + expect(optionFlags).toContain('--file'); |
| 151 | + const handler = (command as unknown as { _actionHandler?: unknown })._actionHandler; |
| 152 | + expect(typeof handler).toBe('function'); |
| 153 | + }); |
| 154 | +}); |
| 155 | + |
| 156 | +describe('exported backlogCommand', () => { |
| 157 | + test('is instantiated command', () => { |
| 158 | + expect(backlogCommand.name()).toBe('backlog'); |
32 | 159 | }); |
33 | 160 | }); |
0 commit comments