clawt 2.14.1 → 2.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -2
- package/dist/index.js +194 -50
- package/dist/postinstall.js +26 -2
- package/docs/spec.md +96 -6
- package/package.json +1 -1
- package/src/commands/run.ts +68 -21
- package/src/commands/validate.ts +38 -0
- package/src/constants/messages/run.ts +16 -0
- package/src/constants/messages/validate.ts +10 -0
- package/src/types/command.ts +4 -0
- package/src/utils/config.ts +20 -0
- package/src/utils/dry-run.ts +89 -0
- package/src/utils/index.ts +4 -3
- package/src/utils/shell.ts +21 -1
- package/src/utils/task-file.ts +18 -0
- package/tests/unit/commands/resume.test.ts +9 -9
- package/tests/unit/commands/run.test.ts +160 -6
- package/tests/unit/commands/validate.test.ts +136 -0
- package/tests/unit/constants/messages.test.ts +5 -1
- package/tests/unit/utils/config.test.ts +23 -1
- package/tests/unit/utils/dry-run.test.ts +124 -0
- package/tests/unit/utils/task-file.test.ts +26 -1
|
@@ -19,7 +19,7 @@ vi.mock('../../../src/utils/index.js', () => ({
|
|
|
19
19
|
validateClaudeCodeInstalled: vi.fn(),
|
|
20
20
|
getProjectWorktrees: vi.fn(),
|
|
21
21
|
launchInteractiveClaude: vi.fn(),
|
|
22
|
-
|
|
22
|
+
resolveTargetWorktrees: vi.fn(),
|
|
23
23
|
}));
|
|
24
24
|
|
|
25
25
|
import { registerResumeCommand } from '../../../src/commands/resume.js';
|
|
@@ -28,21 +28,21 @@ import {
|
|
|
28
28
|
validateClaudeCodeInstalled,
|
|
29
29
|
getProjectWorktrees,
|
|
30
30
|
launchInteractiveClaude,
|
|
31
|
-
|
|
31
|
+
resolveTargetWorktrees,
|
|
32
32
|
} from '../../../src/utils/index.js';
|
|
33
33
|
|
|
34
34
|
const mockedValidateMainWorktree = vi.mocked(validateMainWorktree);
|
|
35
35
|
const mockedValidateClaudeCodeInstalled = vi.mocked(validateClaudeCodeInstalled);
|
|
36
36
|
const mockedGetProjectWorktrees = vi.mocked(getProjectWorktrees);
|
|
37
37
|
const mockedLaunchInteractiveClaude = vi.mocked(launchInteractiveClaude);
|
|
38
|
-
const
|
|
38
|
+
const mockedResolveTargetWorktrees = vi.mocked(resolveTargetWorktrees);
|
|
39
39
|
|
|
40
40
|
beforeEach(() => {
|
|
41
41
|
mockedValidateMainWorktree.mockReset();
|
|
42
42
|
mockedValidateClaudeCodeInstalled.mockReset();
|
|
43
43
|
mockedGetProjectWorktrees.mockReset();
|
|
44
44
|
mockedLaunchInteractiveClaude.mockReset();
|
|
45
|
-
|
|
45
|
+
mockedResolveTargetWorktrees.mockReset();
|
|
46
46
|
});
|
|
47
47
|
|
|
48
48
|
describe('registerResumeCommand', () => {
|
|
@@ -58,7 +58,7 @@ describe('handleResume', () => {
|
|
|
58
58
|
it('成功恢复 Claude Code 会话', async () => {
|
|
59
59
|
const worktree = { path: '/path/feature', branch: 'feature' };
|
|
60
60
|
mockedGetProjectWorktrees.mockReturnValue([worktree]);
|
|
61
|
-
|
|
61
|
+
mockedResolveTargetWorktrees.mockResolvedValue([worktree]);
|
|
62
62
|
|
|
63
63
|
const program = new Command();
|
|
64
64
|
program.exitOverride();
|
|
@@ -67,14 +67,14 @@ describe('handleResume', () => {
|
|
|
67
67
|
|
|
68
68
|
expect(mockedValidateMainWorktree).toHaveBeenCalled();
|
|
69
69
|
expect(mockedValidateClaudeCodeInstalled).toHaveBeenCalled();
|
|
70
|
-
expect(
|
|
70
|
+
expect(mockedResolveTargetWorktrees).toHaveBeenCalled();
|
|
71
71
|
expect(mockedLaunchInteractiveClaude).toHaveBeenCalledWith(worktree, { autoContinue: true });
|
|
72
72
|
});
|
|
73
73
|
|
|
74
|
-
it('不传 -b 时也能调用
|
|
74
|
+
it('不传 -b 时也能调用 resolveTargetWorktrees', async () => {
|
|
75
75
|
const worktree = { path: '/path/feature', branch: 'feature' };
|
|
76
76
|
mockedGetProjectWorktrees.mockReturnValue([worktree]);
|
|
77
|
-
|
|
77
|
+
mockedResolveTargetWorktrees.mockResolvedValue([worktree]);
|
|
78
78
|
|
|
79
79
|
const program = new Command();
|
|
80
80
|
program.exitOverride();
|
|
@@ -82,7 +82,7 @@ describe('handleResume', () => {
|
|
|
82
82
|
await program.parseAsync(['resume'], { from: 'user' });
|
|
83
83
|
|
|
84
84
|
// branchName 参数为 undefined
|
|
85
|
-
expect(
|
|
85
|
+
expect(mockedResolveTargetWorktrees).toHaveBeenCalledWith(
|
|
86
86
|
expect.any(Array),
|
|
87
87
|
expect.any(Object),
|
|
88
88
|
undefined,
|
|
@@ -31,6 +31,14 @@ vi.mock('../../../src/constants/index.js', () => ({
|
|
|
31
31
|
BRANCH_OR_FILE_REQUIRED: '请指定 -b 或 -f',
|
|
32
32
|
TASK_FILE_LOADED: (count: number, path: string) => `✓ 从 ${path} 加载了 ${count} 个任务`,
|
|
33
33
|
TASK_FILE_MISSING_TASK_BY_INDEX: (blockIndex: number) => `第 ${blockIndex} 个任务块缺少任务描述`,
|
|
34
|
+
DRY_RUN_TITLE: 'Dry Run 预览',
|
|
35
|
+
DRY_RUN_TASK_COUNT: (count: number) => `任务数: ${count}`,
|
|
36
|
+
DRY_RUN_CONCURRENCY: (concurrency: number) => `并发数: ${concurrency === 0 ? '不限制' : concurrency}`,
|
|
37
|
+
DRY_RUN_WORKTREE_DIR: (dir: string) => `Worktree 目录: ${dir}`,
|
|
38
|
+
DRY_RUN_BRANCH_EXISTS_WARNING: (name: string) => `分支 ${name} 已存在`,
|
|
39
|
+
DRY_RUN_INTERACTIVE_MODE: '模式: 交互式(无预设任务)',
|
|
40
|
+
DRY_RUN_READY: '预览完成,无冲突。移除 --dry-run 即可正式执行。',
|
|
41
|
+
DRY_RUN_HAS_CONFLICT: '存在分支冲突,实际执行时将会报错。请先处理冲突的分支。',
|
|
34
42
|
},
|
|
35
43
|
}));
|
|
36
44
|
|
|
@@ -46,8 +54,11 @@ vi.mock('../../../src/utils/index.js', async (importOriginal) => {
|
|
|
46
54
|
validateClaudeCodeInstalled: vi.fn(),
|
|
47
55
|
createWorktrees: vi.fn(),
|
|
48
56
|
sanitizeBranchName: vi.fn(),
|
|
57
|
+
generateBranchNames: vi.fn(),
|
|
49
58
|
checkBranchExists: vi.fn(),
|
|
50
59
|
getConfigValue: vi.fn().mockReturnValue(0),
|
|
60
|
+
parseConcurrency: vi.fn().mockReturnValue(0),
|
|
61
|
+
getProjectWorktreeDir: vi.fn().mockReturnValue('/mock/.clawt/worktrees/test-project'),
|
|
51
62
|
printSuccess: vi.fn(),
|
|
52
63
|
printError: vi.fn(),
|
|
53
64
|
printWarning: vi.fn(),
|
|
@@ -57,7 +68,9 @@ vi.mock('../../../src/utils/index.js', async (importOriginal) => {
|
|
|
57
68
|
confirmAction: vi.fn(),
|
|
58
69
|
launchInteractiveClaude: vi.fn(),
|
|
59
70
|
loadTaskFile: vi.fn(),
|
|
71
|
+
parseTasksFromOptions: vi.fn(),
|
|
60
72
|
createWorktreesByBranches: vi.fn(),
|
|
73
|
+
printDryRunPreview: vi.fn(),
|
|
61
74
|
};
|
|
62
75
|
});
|
|
63
76
|
|
|
@@ -80,6 +93,7 @@ vi.mock('../../../src/utils/worktree.js', () => ({
|
|
|
80
93
|
|
|
81
94
|
vi.mock('../../../src/utils/config.js', () => ({
|
|
82
95
|
getConfigValue: vi.fn().mockReturnValue(0),
|
|
96
|
+
parseConcurrency: vi.fn().mockReturnValue(0),
|
|
83
97
|
loadConfig: vi.fn(),
|
|
84
98
|
writeDefaultConfig: vi.fn(),
|
|
85
99
|
ensureClawtDirs: vi.fn(),
|
|
@@ -110,30 +124,45 @@ vi.mock('../../../src/utils/progress.js', () => ({
|
|
|
110
124
|
},
|
|
111
125
|
}));
|
|
112
126
|
|
|
127
|
+
vi.mock('../../../src/utils/dry-run.js', () => ({
|
|
128
|
+
truncateTaskDesc: vi.fn(),
|
|
129
|
+
printDryRunPreview: vi.fn(),
|
|
130
|
+
}));
|
|
131
|
+
|
|
113
132
|
import { registerRunCommand } from '../../../src/commands/run.js';
|
|
114
133
|
import {
|
|
115
134
|
createWorktrees,
|
|
116
135
|
createWorktreesByBranches,
|
|
117
136
|
sanitizeBranchName,
|
|
137
|
+
generateBranchNames,
|
|
118
138
|
checkBranchExists,
|
|
139
|
+
parseConcurrency,
|
|
119
140
|
printSuccess,
|
|
141
|
+
printDryRunPreview,
|
|
120
142
|
launchInteractiveClaude,
|
|
121
143
|
getConfigValue,
|
|
122
144
|
loadTaskFile,
|
|
145
|
+
parseTasksFromOptions,
|
|
146
|
+
validateClaudeCodeInstalled,
|
|
123
147
|
} from '../../../src/utils/index.js';
|
|
124
148
|
import { spawnProcess } from '../../../src/utils/shell.js';
|
|
125
|
-
import { printInfo } from '../../../src/utils/formatter.js';
|
|
149
|
+
import { printInfo as formatterPrintInfo } from '../../../src/utils/formatter.js';
|
|
126
150
|
|
|
127
151
|
const mockedCreateWorktrees = vi.mocked(createWorktrees);
|
|
128
152
|
const mockedCreateWorktreesByBranches = vi.mocked(createWorktreesByBranches);
|
|
129
153
|
const mockedSanitizeBranchName = vi.mocked(sanitizeBranchName);
|
|
154
|
+
const mockedGenerateBranchNames = vi.mocked(generateBranchNames);
|
|
130
155
|
const mockedCheckBranchExists = vi.mocked(checkBranchExists);
|
|
156
|
+
const mockedParseConcurrency = vi.mocked(parseConcurrency);
|
|
131
157
|
const mockedSpawnProcess = vi.mocked(spawnProcess);
|
|
132
158
|
const mockedPrintSuccess = vi.mocked(printSuccess);
|
|
133
|
-
const
|
|
159
|
+
const mockedPrintDryRunPreview = vi.mocked(printDryRunPreview);
|
|
160
|
+
const mockedFormatterPrintInfo = vi.mocked(formatterPrintInfo);
|
|
134
161
|
const mockedLaunchInteractiveClaude = vi.mocked(launchInteractiveClaude);
|
|
135
162
|
const mockedGetConfigValue = vi.mocked(getConfigValue);
|
|
136
163
|
const mockedLoadTaskFile = vi.mocked(loadTaskFile);
|
|
164
|
+
const mockedParseTasksFromOptions = vi.mocked(parseTasksFromOptions);
|
|
165
|
+
const mockedValidateClaudeCodeInstalled = vi.mocked(validateClaudeCodeInstalled);
|
|
137
166
|
|
|
138
167
|
/**
|
|
139
168
|
* 创建模拟子进程
|
|
@@ -166,16 +195,33 @@ beforeEach(() => {
|
|
|
166
195
|
mockedCreateWorktrees.mockReset();
|
|
167
196
|
mockedCreateWorktreesByBranches.mockReset();
|
|
168
197
|
mockedSanitizeBranchName.mockReset();
|
|
198
|
+
mockedGenerateBranchNames.mockReset();
|
|
169
199
|
mockedCheckBranchExists.mockReset();
|
|
200
|
+
mockedParseConcurrency.mockReset();
|
|
201
|
+
mockedParseConcurrency.mockReturnValue(0);
|
|
170
202
|
mockedSpawnProcess.mockReset();
|
|
171
203
|
mockedPrintSuccess.mockReset();
|
|
172
|
-
|
|
204
|
+
mockedPrintDryRunPreview.mockReset();
|
|
205
|
+
mockedFormatterPrintInfo.mockReset();
|
|
173
206
|
mockedLaunchInteractiveClaude.mockReset();
|
|
174
207
|
mockedGetConfigValue.mockReset();
|
|
175
208
|
mockedGetConfigValue.mockReturnValue(0 as any);
|
|
176
209
|
mockedLoadTaskFile.mockReset();
|
|
210
|
+
mockedParseTasksFromOptions.mockReset();
|
|
211
|
+
mockedValidateClaudeCodeInstalled.mockReset();
|
|
177
212
|
// sanitizeBranchName 默认返回输入值
|
|
178
213
|
mockedSanitizeBranchName.mockImplementation((name: string) => name);
|
|
214
|
+
// generateBranchNames 默认使用真实逻辑
|
|
215
|
+
mockedGenerateBranchNames.mockImplementation((name: string, count: number) => {
|
|
216
|
+
if (count === 1) return [name];
|
|
217
|
+
return Array.from({ length: count }, (_, i) => `${name}-${i + 1}`);
|
|
218
|
+
});
|
|
219
|
+
// parseTasksFromOptions 默认使用真实逻辑
|
|
220
|
+
mockedParseTasksFromOptions.mockImplementation((rawTasks: string[]) => {
|
|
221
|
+
const tasks = rawTasks.map((t) => t.trim()).filter(Boolean);
|
|
222
|
+
if (tasks.length === 0) throw new Error('任务列表不能为空');
|
|
223
|
+
return tasks;
|
|
224
|
+
});
|
|
179
225
|
});
|
|
180
226
|
|
|
181
227
|
describe('registerRunCommand', () => {
|
|
@@ -285,6 +331,7 @@ describe('handleRun', () => {
|
|
|
285
331
|
});
|
|
286
332
|
|
|
287
333
|
it('传 --concurrency 限制并发数', async () => {
|
|
334
|
+
mockedParseConcurrency.mockReturnValue(1);
|
|
288
335
|
const worktrees = [
|
|
289
336
|
{ path: '/path/feat-1', branch: 'feat-1' },
|
|
290
337
|
{ path: '/path/feat-2', branch: 'feat-2' },
|
|
@@ -311,7 +358,7 @@ describe('handleRun', () => {
|
|
|
311
358
|
// 所有任务都应执行完毕
|
|
312
359
|
expect(mockedSpawnProcess).toHaveBeenCalledTimes(3);
|
|
313
360
|
// 应输出并发限制提示
|
|
314
|
-
expect(
|
|
361
|
+
expect(mockedFormatterPrintInfo).toHaveBeenCalledWith(expect.stringContaining('并发限制'));
|
|
315
362
|
});
|
|
316
363
|
|
|
317
364
|
it('--concurrency 为 0 时不限制并发', async () => {
|
|
@@ -336,12 +383,13 @@ describe('handleRun', () => {
|
|
|
336
383
|
await program.parseAsync(['run', '-b', 'feat', '--tasks', 'task1', 'task2', '-c', '0'], { from: 'user' });
|
|
337
384
|
|
|
338
385
|
// 不限制并发时不输出并发限制提示
|
|
339
|
-
expect(
|
|
386
|
+
expect(mockedFormatterPrintInfo).not.toHaveBeenCalledWith(expect.stringContaining('并发限制'));
|
|
340
387
|
expect(mockedSpawnProcess).toHaveBeenCalledTimes(2);
|
|
341
388
|
});
|
|
342
389
|
|
|
343
390
|
it('未传 -c 时使用全局配置的 maxConcurrency', async () => {
|
|
344
391
|
mockedGetConfigValue.mockReturnValue(2 as any);
|
|
392
|
+
mockedParseConcurrency.mockReturnValue(2);
|
|
345
393
|
|
|
346
394
|
const worktrees = [
|
|
347
395
|
{ path: '/path/feat-1', branch: 'feat-1' },
|
|
@@ -366,7 +414,7 @@ describe('handleRun', () => {
|
|
|
366
414
|
await program.parseAsync(['run', '-b', 'feat', '--tasks', 'task1', 'task2', 'task3'], { from: 'user' });
|
|
367
415
|
|
|
368
416
|
// 应输出并发限制提示(使用配置值 2)
|
|
369
|
-
expect(
|
|
417
|
+
expect(mockedFormatterPrintInfo).toHaveBeenCalledWith(expect.stringContaining('并发限制'));
|
|
370
418
|
expect(mockedSpawnProcess).toHaveBeenCalledTimes(3);
|
|
371
419
|
});
|
|
372
420
|
|
|
@@ -454,3 +502,109 @@ describe('handleRun', () => {
|
|
|
454
502
|
).rejects.toThrow();
|
|
455
503
|
});
|
|
456
504
|
});
|
|
505
|
+
|
|
506
|
+
describe('handleRun --dry-run', () => {
|
|
507
|
+
it('--dry-run + --tasks 展示任务预览,不创建 worktree 也不启动 Claude Code', async () => {
|
|
508
|
+
const program = new Command();
|
|
509
|
+
program.exitOverride();
|
|
510
|
+
registerRunCommand(program);
|
|
511
|
+
await program.parseAsync(['run', '-b', 'feat', '--tasks', '实现登录功能', '修复首页bug', '--dry-run'], { from: 'user' });
|
|
512
|
+
|
|
513
|
+
// 不创建 worktree
|
|
514
|
+
expect(mockedCreateWorktrees).not.toHaveBeenCalled();
|
|
515
|
+
expect(mockedCreateWorktreesByBranches).not.toHaveBeenCalled();
|
|
516
|
+
// 不启动 Claude Code
|
|
517
|
+
expect(mockedLaunchInteractiveClaude).not.toHaveBeenCalled();
|
|
518
|
+
expect(mockedSpawnProcess).not.toHaveBeenCalled();
|
|
519
|
+
// 应调用 generateBranchNames 生成分支名
|
|
520
|
+
expect(mockedGenerateBranchNames).toHaveBeenCalledWith('feat', 2);
|
|
521
|
+
// 应调用 printDryRunPreview 输出预览
|
|
522
|
+
expect(mockedPrintDryRunPreview).toHaveBeenCalledWith(['feat-1', 'feat-2'], ['实现登录功能', '修复首页bug'], 0);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
it('--dry-run 不调用 validateClaudeCodeInstalled', async () => {
|
|
526
|
+
const program = new Command();
|
|
527
|
+
program.exitOverride();
|
|
528
|
+
registerRunCommand(program);
|
|
529
|
+
await program.parseAsync(['run', '-b', 'feat', '--tasks', 'task1', '--dry-run'], { from: 'user' });
|
|
530
|
+
|
|
531
|
+
expect(mockedValidateClaudeCodeInstalled).not.toHaveBeenCalled();
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
it('--dry-run 交互式模式展示单个 worktree 信息', async () => {
|
|
535
|
+
const program = new Command();
|
|
536
|
+
program.exitOverride();
|
|
537
|
+
registerRunCommand(program);
|
|
538
|
+
await program.parseAsync(['run', '-b', 'feat', '--dry-run'], { from: 'user' });
|
|
539
|
+
|
|
540
|
+
// 不创建 worktree
|
|
541
|
+
expect(mockedCreateWorktrees).not.toHaveBeenCalled();
|
|
542
|
+
// 不启动 Claude Code
|
|
543
|
+
expect(mockedLaunchInteractiveClaude).not.toHaveBeenCalled();
|
|
544
|
+
// 交互式模式:分支名列表为单个,任务为空数组
|
|
545
|
+
expect(mockedPrintDryRunPreview).toHaveBeenCalledWith(['feat'], [], 0);
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
it('--dry-run + -f 从任务文件展示预览', async () => {
|
|
549
|
+
mockedLoadTaskFile.mockReturnValue([
|
|
550
|
+
{ branch: 'feat-login', task: '实现登录功能' },
|
|
551
|
+
{ branch: 'fix-bug', task: '修复问题' },
|
|
552
|
+
]);
|
|
553
|
+
|
|
554
|
+
const program = new Command();
|
|
555
|
+
program.exitOverride();
|
|
556
|
+
registerRunCommand(program);
|
|
557
|
+
await program.parseAsync(['run', '-f', 'tasks.md', '--dry-run'], { from: 'user' });
|
|
558
|
+
|
|
559
|
+
// 应加载任务文件
|
|
560
|
+
expect(mockedLoadTaskFile).toHaveBeenCalledWith('tasks.md', { branchRequired: true });
|
|
561
|
+
// 不创建 worktree
|
|
562
|
+
expect(mockedCreateWorktrees).not.toHaveBeenCalled();
|
|
563
|
+
expect(mockedCreateWorktreesByBranches).not.toHaveBeenCalled();
|
|
564
|
+
// 不启动 Claude Code
|
|
565
|
+
expect(mockedSpawnProcess).not.toHaveBeenCalled();
|
|
566
|
+
// 应调用 printDryRunPreview
|
|
567
|
+
expect(mockedPrintDryRunPreview).toHaveBeenCalledWith(
|
|
568
|
+
['feat-login', 'fix-bug'],
|
|
569
|
+
['实现登录功能', '修复问题'],
|
|
570
|
+
0,
|
|
571
|
+
);
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
it('--dry-run + -f + -b 模式使用 -b 自动编号', async () => {
|
|
575
|
+
mockedLoadTaskFile.mockReturnValue([
|
|
576
|
+
{ task: '任务1' },
|
|
577
|
+
{ task: '任务2' },
|
|
578
|
+
]);
|
|
579
|
+
|
|
580
|
+
const program = new Command();
|
|
581
|
+
program.exitOverride();
|
|
582
|
+
registerRunCommand(program);
|
|
583
|
+
await program.parseAsync(['run', '-b', 'feat', '-f', 'tasks.md', '--dry-run'], { from: 'user' });
|
|
584
|
+
|
|
585
|
+
// 应使用 generateBranchNames 自动编号
|
|
586
|
+
expect(mockedGenerateBranchNames).toHaveBeenCalledWith('feat', 2);
|
|
587
|
+
// 不实际创建 worktree
|
|
588
|
+
expect(mockedCreateWorktrees).not.toHaveBeenCalled();
|
|
589
|
+
// 应调用 printDryRunPreview
|
|
590
|
+
expect(mockedPrintDryRunPreview).toHaveBeenCalled();
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
it('--dry-run 传递并发配置给 printDryRunPreview', async () => {
|
|
594
|
+
mockedParseConcurrency.mockReturnValue(3);
|
|
595
|
+
|
|
596
|
+
const program = new Command();
|
|
597
|
+
program.exitOverride();
|
|
598
|
+
registerRunCommand(program);
|
|
599
|
+
await program.parseAsync(['run', '-b', 'feat', '--tasks', 'task1', 'task2', '-c', '3', '--dry-run'], { from: 'user' });
|
|
600
|
+
|
|
601
|
+
// 不创建 worktree
|
|
602
|
+
expect(mockedCreateWorktrees).not.toHaveBeenCalled();
|
|
603
|
+
// 应将并发数传递给 printDryRunPreview
|
|
604
|
+
expect(mockedPrintDryRunPreview).toHaveBeenCalledWith(
|
|
605
|
+
expect.any(Array),
|
|
606
|
+
expect.any(Array),
|
|
607
|
+
3,
|
|
608
|
+
);
|
|
609
|
+
});
|
|
610
|
+
});
|
|
@@ -28,6 +28,11 @@ vi.mock('../../../src/constants/index.js', () => ({
|
|
|
28
28
|
INCREMENTAL_VALIDATE_SUCCESS: (branch: string) => `✓ 增量验证 ${branch}`,
|
|
29
29
|
INCREMENTAL_VALIDATE_FALLBACK: '降级为全量模式',
|
|
30
30
|
DESTRUCTIVE_OP_CANCELLED: '已取消操作',
|
|
31
|
+
VALIDATE_RUN_START: (cmd: string) => `正在执行命令: ${cmd}`,
|
|
32
|
+
VALIDATE_RUN_SUCCESS: (cmd: string) => `✓ 命令执行完成: ${cmd}`,
|
|
33
|
+
VALIDATE_RUN_FAILED: (cmd: string, code: number) => `✗ 命令失败: ${cmd},退出码: ${code}`,
|
|
34
|
+
VALIDATE_RUN_ERROR: (cmd: string, msg: string) => `✗ 命令出错: ${msg}`,
|
|
35
|
+
SEPARATOR: '────',
|
|
31
36
|
},
|
|
32
37
|
}));
|
|
33
38
|
|
|
@@ -71,6 +76,9 @@ vi.mock('../../../src/utils/index.js', () => ({
|
|
|
71
76
|
printWarning: vi.fn(),
|
|
72
77
|
printInfo: vi.fn(),
|
|
73
78
|
resolveTargetWorktree: vi.fn(),
|
|
79
|
+
runCommandInherited: vi.fn(),
|
|
80
|
+
printError: vi.fn(),
|
|
81
|
+
printSeparator: vi.fn(),
|
|
74
82
|
}));
|
|
75
83
|
|
|
76
84
|
import { registerValidateCommand } from '../../../src/commands/validate.js';
|
|
@@ -105,6 +113,9 @@ import {
|
|
|
105
113
|
gitApplyCachedCheck,
|
|
106
114
|
gitApplyCachedFromStdin,
|
|
107
115
|
printWarning,
|
|
116
|
+
runCommandInherited,
|
|
117
|
+
printError,
|
|
118
|
+
printSeparator,
|
|
108
119
|
} from '../../../src/utils/index.js';
|
|
109
120
|
|
|
110
121
|
const mockedGetProjectName = vi.mocked(getProjectName);
|
|
@@ -137,6 +148,9 @@ const mockedGitDiffTree = vi.mocked(gitDiffTree);
|
|
|
137
148
|
const mockedGitApplyCachedCheck = vi.mocked(gitApplyCachedCheck);
|
|
138
149
|
const mockedGitApplyCachedFromStdin = vi.mocked(gitApplyCachedFromStdin);
|
|
139
150
|
const mockedPrintWarning = vi.mocked(printWarning);
|
|
151
|
+
const mockedRunCommandInherited = vi.mocked(runCommandInherited);
|
|
152
|
+
const mockedPrintError = vi.mocked(printError);
|
|
153
|
+
const mockedPrintSeparator = vi.mocked(printSeparator);
|
|
140
154
|
|
|
141
155
|
const worktree = { path: '/path/feature', branch: 'feature' };
|
|
142
156
|
|
|
@@ -171,6 +185,9 @@ beforeEach(() => {
|
|
|
171
185
|
mockedGitApplyCachedCheck.mockReset();
|
|
172
186
|
mockedGitApplyCachedFromStdin.mockReset();
|
|
173
187
|
mockedPrintWarning.mockReset();
|
|
188
|
+
mockedRunCommandInherited.mockReset();
|
|
189
|
+
mockedPrintError.mockReset();
|
|
190
|
+
mockedPrintSeparator.mockReset();
|
|
174
191
|
});
|
|
175
192
|
|
|
176
193
|
describe('registerValidateCommand', () => {
|
|
@@ -380,3 +397,122 @@ describe('增量 validate', () => {
|
|
|
380
397
|
expect(mockedPrintSuccess).toHaveBeenCalled();
|
|
381
398
|
});
|
|
382
399
|
});
|
|
400
|
+
|
|
401
|
+
describe('--run 选项', () => {
|
|
402
|
+
/** 设置首次 validate 成功的公共 mock */
|
|
403
|
+
function setupSuccessfulFirstValidate(): void {
|
|
404
|
+
mockedIsWorkingDirClean.mockReturnValue(true);
|
|
405
|
+
mockedHasLocalCommits.mockReturnValue(true);
|
|
406
|
+
mockedHasSnapshot.mockReturnValue(false);
|
|
407
|
+
mockedGitDiffBinaryAgainstBranch.mockReturnValue(Buffer.from('diff'));
|
|
408
|
+
mockedGitWriteTree.mockReturnValue('treehash');
|
|
409
|
+
mockedGetHeadCommitHash.mockReturnValue('headhash');
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** 构造 spawnSync 返回值的辅助函数 */
|
|
413
|
+
function createSpawnResult(overrides: { status?: number | null; error?: Error }) {
|
|
414
|
+
return {
|
|
415
|
+
pid: 0,
|
|
416
|
+
output: [],
|
|
417
|
+
stdout: Buffer.alloc(0),
|
|
418
|
+
stderr: Buffer.alloc(0),
|
|
419
|
+
status: overrides.status ?? null,
|
|
420
|
+
signal: null,
|
|
421
|
+
error: overrides.error,
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
it('validate 成功后执行 --run 指定的命令', async () => {
|
|
426
|
+
setupSuccessfulFirstValidate();
|
|
427
|
+
mockedRunCommandInherited.mockReturnValue(createSpawnResult({ status: 0 }));
|
|
428
|
+
|
|
429
|
+
const program = new Command();
|
|
430
|
+
program.exitOverride();
|
|
431
|
+
registerValidateCommand(program);
|
|
432
|
+
await program.parseAsync(['validate', '-b', 'feature', '-r', 'npm test'], { from: 'user' });
|
|
433
|
+
|
|
434
|
+
expect(mockedRunCommandInherited).toHaveBeenCalledWith('npm test', { cwd: '/repo' });
|
|
435
|
+
expect(mockedPrintSuccess).toHaveBeenCalledTimes(2);
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it('--run 命令失败时输出错误信息但不抛异常', async () => {
|
|
439
|
+
setupSuccessfulFirstValidate();
|
|
440
|
+
mockedRunCommandInherited.mockReturnValue(createSpawnResult({ status: 1 }));
|
|
441
|
+
|
|
442
|
+
const program = new Command();
|
|
443
|
+
program.exitOverride();
|
|
444
|
+
registerValidateCommand(program);
|
|
445
|
+
await program.parseAsync(['validate', '-b', 'feature', '--run', 'npm test'], { from: 'user' });
|
|
446
|
+
|
|
447
|
+
expect(mockedRunCommandInherited).toHaveBeenCalledWith('npm test', { cwd: '/repo' });
|
|
448
|
+
expect(mockedPrintError).toHaveBeenCalled();
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it('--run 命令进程启动失败时输出错误信息', async () => {
|
|
452
|
+
setupSuccessfulFirstValidate();
|
|
453
|
+
mockedRunCommandInherited.mockReturnValue(createSpawnResult({ error: new Error('spawn ENOENT') }));
|
|
454
|
+
|
|
455
|
+
const program = new Command();
|
|
456
|
+
program.exitOverride();
|
|
457
|
+
registerValidateCommand(program);
|
|
458
|
+
await program.parseAsync(['validate', '-b', 'feature', '-r', 'nonexistent'], { from: 'user' });
|
|
459
|
+
|
|
460
|
+
expect(mockedPrintError).toHaveBeenCalled();
|
|
461
|
+
// validate 成功 + run 出错,printSuccess 只被调用 1 次(validate 成功)
|
|
462
|
+
expect(mockedPrintSuccess).toHaveBeenCalledTimes(1);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
it('未传 --run 时不执行任何命令', async () => {
|
|
466
|
+
setupSuccessfulFirstValidate();
|
|
467
|
+
|
|
468
|
+
const program = new Command();
|
|
469
|
+
program.exitOverride();
|
|
470
|
+
registerValidateCommand(program);
|
|
471
|
+
await program.parseAsync(['validate', '-b', 'feature'], { from: 'user' });
|
|
472
|
+
|
|
473
|
+
expect(mockedRunCommandInherited).not.toHaveBeenCalled();
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
it('--clean 与 --run 同时传入时只执行 clean 不执行 run', async () => {
|
|
477
|
+
mockedGetConfigValue.mockReturnValue(false);
|
|
478
|
+
mockedIsWorkingDirClean.mockReturnValue(true);
|
|
479
|
+
|
|
480
|
+
const program = new Command();
|
|
481
|
+
program.exitOverride();
|
|
482
|
+
registerValidateCommand(program);
|
|
483
|
+
await program.parseAsync(['validate', '--clean', '-b', 'feature', '-r', 'npm test'], { from: 'user' });
|
|
484
|
+
|
|
485
|
+
expect(mockedRemoveSnapshot).toHaveBeenCalled();
|
|
486
|
+
expect(mockedRunCommandInherited).not.toHaveBeenCalled();
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it('增量 validate 成功后也执行 --run 命令', async () => {
|
|
490
|
+
mockedIsWorkingDirClean.mockReturnValue(true);
|
|
491
|
+
mockedHasLocalCommits.mockReturnValue(true);
|
|
492
|
+
mockedHasSnapshot.mockReturnValue(true);
|
|
493
|
+
mockedReadSnapshot.mockReturnValue({ treeHash: 'oldtree', headCommitHash: 'headhash' });
|
|
494
|
+
mockedGetHeadCommitHash.mockReturnValue('headhash');
|
|
495
|
+
mockedGitDiffBinaryAgainstBranch.mockReturnValue(Buffer.from('diff'));
|
|
496
|
+
mockedGitWriteTree.mockReturnValue('newtree');
|
|
497
|
+
mockedRunCommandInherited.mockReturnValue(createSpawnResult({ status: 0 }));
|
|
498
|
+
|
|
499
|
+
const program = new Command();
|
|
500
|
+
program.exitOverride();
|
|
501
|
+
registerValidateCommand(program);
|
|
502
|
+
await program.parseAsync(['validate', '-b', 'feature', '-r', 'npm test'], { from: 'user' });
|
|
503
|
+
|
|
504
|
+
expect(mockedRunCommandInherited).toHaveBeenCalledWith('npm test', { cwd: '/repo' });
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
it('目标分支无变更时不执行 --run', async () => {
|
|
508
|
+
mockedIsWorkingDirClean.mockReturnValue(true);
|
|
509
|
+
mockedHasLocalCommits.mockReturnValue(false);
|
|
510
|
+
|
|
511
|
+
const program = new Command();
|
|
512
|
+
program.exitOverride();
|
|
513
|
+
registerValidateCommand(program);
|
|
514
|
+
await program.parseAsync(['validate', '-b', 'feature', '-r', 'npm test'], { from: 'user' });
|
|
515
|
+
|
|
516
|
+
expect(mockedRunCommandInherited).not.toHaveBeenCalled();
|
|
517
|
+
});
|
|
518
|
+
});
|
|
@@ -12,7 +12,6 @@ describe('MESSAGES', () => {
|
|
|
12
12
|
'TARGET_WORKTREE_CLEAN',
|
|
13
13
|
'MERGE_CONFLICT',
|
|
14
14
|
'COMMIT_MESSAGE_REQUIRED',
|
|
15
|
-
'TARGET_WORKTREE_DIRTY_NO_MESSAGE',
|
|
16
15
|
'TARGET_WORKTREE_NO_CHANGES',
|
|
17
16
|
'INTERRUPTED',
|
|
18
17
|
'INTERRUPT_CONFIRM_CLEANUP',
|
|
@@ -49,6 +48,11 @@ describe('MESSAGES', () => {
|
|
|
49
48
|
});
|
|
50
49
|
|
|
51
50
|
describe('模板函数消息', () => {
|
|
51
|
+
it('TARGET_WORKTREE_DIRTY_NO_MESSAGE 包含 worktree 路径', () => {
|
|
52
|
+
const result = MESSAGES.TARGET_WORKTREE_DIRTY_NO_MESSAGE('/path/to/wt');
|
|
53
|
+
expect(result).toContain('/path/to/wt');
|
|
54
|
+
});
|
|
55
|
+
|
|
52
56
|
it('BRANCH_EXISTS 包含分支名', () => {
|
|
53
57
|
const result = MESSAGES.BRANCH_EXISTS('feature-a');
|
|
54
58
|
expect(result).toContain('feature-a');
|
|
@@ -18,7 +18,7 @@ vi.mock('../../../src/utils/fs.js', () => ({
|
|
|
18
18
|
}));
|
|
19
19
|
|
|
20
20
|
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
21
|
-
import { loadConfig, getConfigValue, writeDefaultConfig, writeConfig, saveConfig, ensureClawtDirs } from '../../../src/utils/config.js';
|
|
21
|
+
import { loadConfig, getConfigValue, writeDefaultConfig, writeConfig, saveConfig, ensureClawtDirs, parseConcurrency } from '../../../src/utils/config.js';
|
|
22
22
|
import { DEFAULT_CONFIG } from '../../../src/constants/index.js';
|
|
23
23
|
import { ensureDir } from '../../../src/utils/fs.js';
|
|
24
24
|
|
|
@@ -96,6 +96,28 @@ describe('ensureClawtDirs', () => {
|
|
|
96
96
|
});
|
|
97
97
|
});
|
|
98
98
|
|
|
99
|
+
describe('parseConcurrency', () => {
|
|
100
|
+
it('命令行参数 undefined 时返回配置值', () => {
|
|
101
|
+
expect(parseConcurrency(undefined, 5)).toBe(5);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('命令行参数为有效正整数时返回解析值', () => {
|
|
105
|
+
expect(parseConcurrency('3', 0)).toBe(3);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('命令行参数为 0 时返回 0(不限制)', () => {
|
|
109
|
+
expect(parseConcurrency('0', 5)).toBe(0);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('命令行参数为负数时抛出错误', () => {
|
|
113
|
+
expect(() => parseConcurrency('-1', 0)).toThrow();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('命令行参数为非数字时抛出错误', () => {
|
|
117
|
+
expect(() => parseConcurrency('abc', 0)).toThrow();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
99
121
|
describe('saveConfig', () => {
|
|
100
122
|
it('将配置对象写入配置文件', () => {
|
|
101
123
|
const customConfig = { ...DEFAULT_CONFIG, autoDeleteBranch: true };
|