@angli/unit-test-tool 0.1.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.
Files changed (151) hide show
  1. package/.claude/settings.local.json +14 -0
  2. package/README.md +232 -0
  3. package/dist/src/cli/commands/analyze.js +20 -0
  4. package/dist/src/cli/commands/guard.js +26 -0
  5. package/dist/src/cli/commands/init.js +39 -0
  6. package/dist/src/cli/commands/run.js +72 -0
  7. package/dist/src/cli/commands/schedule.js +29 -0
  8. package/dist/src/cli/commands/start.js +102 -0
  9. package/dist/src/cli/commands/status.js +27 -0
  10. package/dist/src/cli/commands/verify.js +15 -0
  11. package/dist/src/cli/context/create-context.js +101 -0
  12. package/dist/src/cli/index.js +23 -0
  13. package/dist/src/cli/utils/scan-dir.js +6 -0
  14. package/dist/src/core/analyzers/coverage-analyzer.js +8 -0
  15. package/dist/src/core/analyzers/dependency-complexity-analyzer.js +19 -0
  16. package/dist/src/core/analyzers/existing-test-analyzer.js +66 -0
  17. package/dist/src/core/analyzers/failure-history-analyzer.js +9 -0
  18. package/dist/src/core/analyzers/file-classifier-analyzer.js +24 -0
  19. package/dist/src/core/analyzers/index.js +37 -0
  20. package/dist/src/core/analyzers/llm-semantic-analyzer.js +3 -0
  21. package/dist/src/core/analyzers/path-priority-analyzer.js +34 -0
  22. package/dist/src/core/coverage/read-coverage-summary.js +183 -0
  23. package/dist/src/core/executor/claude-cli-executor.js +91 -0
  24. package/dist/src/core/middleware/loop-detection.js +6 -0
  25. package/dist/src/core/middleware/pre-completion-checklist.js +27 -0
  26. package/dist/src/core/middleware/silent-success-post-check.js +13 -0
  27. package/dist/src/core/planner/rank-candidates.js +3 -0
  28. package/dist/src/core/planner/rule-planner.js +41 -0
  29. package/dist/src/core/planner/score-candidate.js +49 -0
  30. package/dist/src/core/prompts/case-library.js +35 -0
  31. package/dist/src/core/prompts/edit-boundary-prompt.js +24 -0
  32. package/dist/src/core/prompts/retry-prompt.js +18 -0
  33. package/dist/src/core/prompts/system-prompt.js +27 -0
  34. package/dist/src/core/prompts/task-prompt.js +22 -0
  35. package/dist/src/core/reporter/index.js +48 -0
  36. package/dist/src/core/state-machine/index.js +12 -0
  37. package/dist/src/core/storage/defaults.js +16 -0
  38. package/dist/src/core/storage/event-store.js +16 -0
  39. package/dist/src/core/storage/lifecycle-store.js +18 -0
  40. package/dist/src/core/storage/report-store.js +16 -0
  41. package/dist/src/core/storage/state-store.js +11 -0
  42. package/dist/src/core/strategies/classify-failure.js +14 -0
  43. package/dist/src/core/strategies/switch-mock-strategy.js +9 -0
  44. package/dist/src/core/tools/analyze-baseline.js +54 -0
  45. package/dist/src/core/tools/guard.js +68 -0
  46. package/dist/src/core/tools/run-loop.js +108 -0
  47. package/dist/src/core/tools/run-with-claude-cli.js +645 -0
  48. package/dist/src/core/tools/verify-all.js +75 -0
  49. package/dist/src/core/worktrees/is-git-repo.js +10 -0
  50. package/dist/src/types/index.js +1 -0
  51. package/dist/src/types/logger.js +1 -0
  52. package/dist/src/utils/clock.js +10 -0
  53. package/dist/src/utils/command-runner.js +18 -0
  54. package/dist/src/utils/commands.js +28 -0
  55. package/dist/src/utils/duration.js +22 -0
  56. package/dist/src/utils/fs.js +53 -0
  57. package/dist/src/utils/logger.js +10 -0
  58. package/dist/src/utils/paths.js +21 -0
  59. package/dist/src/utils/process-lifecycle.js +74 -0
  60. package/dist/src/utils/prompts.js +20 -0
  61. package/dist/tests/core/create-context.test.js +41 -0
  62. package/dist/tests/core/default-state.test.js +10 -0
  63. package/dist/tests/core/failure-classification.test.js +7 -0
  64. package/dist/tests/core/loop-detection.test.js +7 -0
  65. package/dist/tests/core/paths.test.js +11 -0
  66. package/dist/tests/core/prompt-builders.test.js +33 -0
  67. package/dist/tests/core/score-candidate.test.js +28 -0
  68. package/dist/tests/core/state-machine.test.js +12 -0
  69. package/dist/tests/integration/status-report.test.js +21 -0
  70. package/docs/architecture.md +20 -0
  71. package/docs/demo.sh +266 -0
  72. package/docs/skill-integration.md +15 -0
  73. package/docs/state-machine.md +15 -0
  74. package/package.json +31 -0
  75. package/src/cli/commands/analyze.ts +22 -0
  76. package/src/cli/commands/guard.ts +28 -0
  77. package/src/cli/commands/init.ts +41 -0
  78. package/src/cli/commands/run.ts +79 -0
  79. package/src/cli/commands/schedule.ts +32 -0
  80. package/src/cli/commands/start.ts +111 -0
  81. package/src/cli/commands/status.ts +30 -0
  82. package/src/cli/commands/verify.ts +17 -0
  83. package/src/cli/context/create-context.ts +142 -0
  84. package/src/cli/index.ts +27 -0
  85. package/src/cli/utils/scan-dir.ts +5 -0
  86. package/src/core/analyzers/coverage-analyzer.ts +10 -0
  87. package/src/core/analyzers/dependency-complexity-analyzer.ts +25 -0
  88. package/src/core/analyzers/existing-test-analyzer.ts +76 -0
  89. package/src/core/analyzers/failure-history-analyzer.ts +12 -0
  90. package/src/core/analyzers/file-classifier-analyzer.ts +25 -0
  91. package/src/core/analyzers/index.ts +51 -0
  92. package/src/core/analyzers/llm-semantic-analyzer.ts +6 -0
  93. package/src/core/analyzers/path-priority-analyzer.ts +41 -0
  94. package/src/core/coverage/read-coverage-summary.ts +224 -0
  95. package/src/core/executor/claude-cli-executor.ts +94 -0
  96. package/src/core/middleware/loop-detection.ts +8 -0
  97. package/src/core/middleware/pre-completion-checklist.ts +32 -0
  98. package/src/core/middleware/silent-success-post-check.ts +16 -0
  99. package/src/core/planner/rank-candidates.ts +5 -0
  100. package/src/core/planner/rule-planner.ts +65 -0
  101. package/src/core/planner/score-candidate.ts +60 -0
  102. package/src/core/prompts/case-library.ts +36 -0
  103. package/src/core/prompts/edit-boundary-prompt.ts +26 -0
  104. package/src/core/prompts/retry-prompt.ts +22 -0
  105. package/src/core/prompts/system-prompt.ts +32 -0
  106. package/src/core/prompts/task-prompt.ts +26 -0
  107. package/src/core/reporter/index.ts +56 -0
  108. package/src/core/state-machine/index.ts +14 -0
  109. package/src/core/storage/defaults.ts +18 -0
  110. package/src/core/storage/event-store.ts +18 -0
  111. package/src/core/storage/lifecycle-store.ts +20 -0
  112. package/src/core/storage/report-store.ts +19 -0
  113. package/src/core/storage/state-store.ts +18 -0
  114. package/src/core/strategies/classify-failure.ts +9 -0
  115. package/src/core/strategies/switch-mock-strategy.ts +12 -0
  116. package/src/core/tools/analyze-baseline.ts +61 -0
  117. package/src/core/tools/guard.ts +89 -0
  118. package/src/core/tools/run-loop.ts +142 -0
  119. package/src/core/tools/run-with-claude-cli.ts +926 -0
  120. package/src/core/tools/verify-all.ts +83 -0
  121. package/src/core/worktrees/is-git-repo.ts +10 -0
  122. package/src/types/index.ts +291 -0
  123. package/src/types/logger.ts +6 -0
  124. package/src/utils/clock.ts +10 -0
  125. package/src/utils/command-runner.ts +24 -0
  126. package/src/utils/commands.ts +42 -0
  127. package/src/utils/duration.ts +20 -0
  128. package/src/utils/fs.ts +50 -0
  129. package/src/utils/logger.ts +12 -0
  130. package/src/utils/paths.ts +24 -0
  131. package/src/utils/process-lifecycle.ts +92 -0
  132. package/src/utils/prompts.ts +22 -0
  133. package/tests/core/create-context.test.ts +45 -0
  134. package/tests/core/default-state.test.ts +11 -0
  135. package/tests/core/failure-classification.test.ts +8 -0
  136. package/tests/core/loop-detection.test.ts +8 -0
  137. package/tests/core/paths.test.ts +13 -0
  138. package/tests/core/prompt-builders.test.ts +38 -0
  139. package/tests/core/score-candidate.test.ts +30 -0
  140. package/tests/core/state-machine.test.ts +14 -0
  141. package/tests/fixtures/simple-project/.openclaw-testbot/logs/events.jsonl +10 -0
  142. package/tests/fixtures/simple-project/.openclaw-testbot/plan.json +75 -0
  143. package/tests/fixtures/simple-project/.openclaw-testbot/reports/coverage-summary.json +9 -0
  144. package/tests/fixtures/simple-project/.openclaw-testbot/reports/final-report.json +14 -0
  145. package/tests/fixtures/simple-project/.openclaw-testbot/state.json +18 -0
  146. package/tests/fixtures/simple-project/coverage-summary.json +1 -0
  147. package/tests/fixtures/simple-project/package.json +8 -0
  148. package/tests/fixtures/simple-project/src/add.js +3 -0
  149. package/tests/fixtures/simple-project/test-runner.js +18 -0
  150. package/tests/integration/status-report.test.ts +24 -0
  151. package/tsconfig.json +18 -0
@@ -0,0 +1,61 @@
1
+ import path from 'node:path'
2
+ import type { AppContext } from '../../cli/context/create-context.js'
3
+ import type { AnalyzeBaselineResult } from '../../types/index.js'
4
+ import { deriveStatusFromPhase } from '../state-machine/index.js'
5
+ import { runAndReadCoverage } from '../coverage/read-coverage-summary.js'
6
+
7
+ export async function analyzeBaseline(ctx: AppContext): Promise<AnalyzeBaselineResult> {
8
+ await ctx.fileSystem.ensureDir(ctx.paths.runtimeDir)
9
+ await ctx.fileSystem.ensureDir(ctx.paths.logsPath)
10
+ await ctx.fileSystem.ensureDir(ctx.paths.promptsDir)
11
+ await ctx.fileSystem.ensureDir(ctx.paths.promptRunsDir)
12
+ await ctx.fileSystem.ensureDir(ctx.paths.reportsDir)
13
+
14
+ const packageJson = await ctx.fileSystem.readJson<{ scripts?: Record<string, string> }>(path.join(ctx.projectPath, 'package.json'))
15
+ const framework = detectFramework(packageJson?.scripts ?? {})
16
+ const coverageSnapshot = await runAndReadCoverage(ctx)
17
+
18
+ await ctx.reportStore.saveCoverageSummary({
19
+ framework,
20
+ coverageSnapshot
21
+ })
22
+
23
+ const state = await ctx.stateStore.load()
24
+ const nextState = {
25
+ ...state,
26
+ phase: 'ANALYZE_BASELINE' as const,
27
+ status: deriveStatusFromPhase('ANALYZE_BASELINE'),
28
+ coverageSnapshot,
29
+ lastAction: 'analyze-baseline',
30
+ nextSuggestedAction: 'run',
31
+ updatedAt: ctx.clock.nowIso()
32
+ }
33
+ await ctx.stateStore.save(nextState)
34
+
35
+ await ctx.eventStore.append({
36
+ eventType: 'baseline_analyzed',
37
+ phase: 'ANALYZE_BASELINE',
38
+ message: `baseline coverage lines=${coverageSnapshot.lines ?? 'unknown'}`,
39
+ data: { framework, coverageSnapshot },
40
+ timestamp: ctx.clock.nowIso()
41
+ })
42
+
43
+ return {
44
+ ok: true,
45
+ phase: 'ANALYZE_BASELINE',
46
+ summary: `完成真实覆盖率基线分析,lines=${coverageSnapshot.lines ?? 'unknown'}%。`,
47
+ nextAction: 'run',
48
+ artifacts: {
49
+ coverageSummaryPath: ctx.paths.coverageSummaryPath
50
+ },
51
+ coverageSnapshot,
52
+ testFramework: framework
53
+ }
54
+ }
55
+
56
+ function detectFramework(scripts: Record<string, string>): 'jest' | 'vitest' | 'unknown' {
57
+ const scriptValues = Object.values(scripts).join(' ')
58
+ if (scriptValues.includes('vitest')) return 'vitest'
59
+ if (scriptValues.includes('jest')) return 'jest'
60
+ return 'unknown'
61
+ }
@@ -0,0 +1,89 @@
1
+ import { execa } from 'execa'
2
+ import type { AppContext } from '../../cli/context/create-context.js'
3
+
4
+ export interface GuardOptions {
5
+ staleTimeoutMs?: number
6
+ maxRestart?: number
7
+ cooldownMs?: number
8
+ }
9
+
10
+ const DEFAULT_STALE_TIMEOUT_MS = 2 * 60_000
11
+ const DEFAULT_MAX_RESTART = 3
12
+ const DEFAULT_COOLDOWN_MS = 60_000
13
+
14
+ export async function runGuard(ctx: AppContext, options: GuardOptions = {}): Promise<{ recovered: boolean; summary: string }> {
15
+ const lifecycle = await ctx.lifecycleStore.load()
16
+ if (!lifecycle) {
17
+ return { recovered: false, summary: '未找到 lifecycle 记录。' }
18
+ }
19
+
20
+ const staleTimeoutMs = options.staleTimeoutMs ?? DEFAULT_STALE_TIMEOUT_MS
21
+ const maxRestart = options.maxRestart ?? DEFAULT_MAX_RESTART
22
+ const cooldownMs = options.cooldownMs ?? DEFAULT_COOLDOWN_MS
23
+ const now = Date.now()
24
+ const lastHeartbeat = Date.parse(lifecycle.lastHeartbeat)
25
+ const isStale = Number.isFinite(lastHeartbeat) && now - lastHeartbeat > staleTimeoutMs
26
+ const isAbnormal = (lifecycle.status === 'running' && isStale) || lifecycle.status === 'crashed'
27
+
28
+ if (!isAbnormal) {
29
+ return { recovered: false, summary: `无需恢复:status=${lifecycle.status}` }
30
+ }
31
+
32
+ if (lifecycle.command !== 'start') {
33
+ return { recovered: false, summary: `检测到 ${lifecycle.command} 异常,但按设计不自动恢复。` }
34
+ }
35
+
36
+ if (lifecycle.restartCount >= maxRestart) {
37
+ return { recovered: false, summary: `已达到最大恢复次数 ${maxRestart}。` }
38
+ }
39
+
40
+ const updatedAt = Date.parse(lifecycle.updatedAt)
41
+ if (Number.isFinite(updatedAt) && now - updatedAt < cooldownMs) {
42
+ return { recovered: false, summary: `仍处于冷却期,稍后再试。` }
43
+ }
44
+
45
+ const nextRestartCount = lifecycle.restartCount + 1
46
+ const recoveringAt = ctx.clock.nowIso()
47
+ await ctx.lifecycleStore.patch({
48
+ status: 'recovering',
49
+ restartCount: nextRestartCount,
50
+ updatedAt: recoveringAt,
51
+ lastError: lifecycle.lastError ?? (isStale ? 'heartbeat stale' : 'crashed')
52
+ })
53
+ await ctx.eventStore.append({
54
+ eventType: 'lifecycle_recovering',
55
+ phase: 'BLOCKED',
56
+ message: `recover start process #${nextRestartCount}`,
57
+ data: { argv: lifecycle.argv },
58
+ timestamp: recoveringAt
59
+ })
60
+
61
+ const child = execa(process.execPath, lifecycle.argv, {
62
+ cwd: ctx.projectPath,
63
+ detached: true,
64
+ stdio: 'ignore'
65
+ })
66
+ child.unref()
67
+
68
+ return { recovered: true, summary: `已触发恢复,第 ${nextRestartCount} 次。` }
69
+ }
70
+
71
+ export async function runSchedule(
72
+ ctx: AppContext,
73
+ intervalMs: number,
74
+ guardOptions: GuardOptions = {}
75
+ ): Promise<void> {
76
+ await runGuard(ctx, guardOptions)
77
+ setInterval(async () => {
78
+ try {
79
+ await runGuard(ctx, guardOptions)
80
+ } catch (error) {
81
+ await ctx.eventStore.append({
82
+ eventType: 'schedule_guard_failed',
83
+ phase: 'BLOCKED',
84
+ message: error instanceof Error ? error.message : String(error),
85
+ timestamp: ctx.clock.nowIso()
86
+ })
87
+ }
88
+ }, intervalMs)
89
+ }
@@ -0,0 +1,142 @@
1
+ import type { AppContext } from '../../cli/context/create-context.js'
2
+ import type { BaseToolResult } from '../../types/index.js'
3
+ import { analyzeBaseline } from './analyze-baseline.js'
4
+ import { runWithClaudeCli } from './run-with-claude-cli.js'
5
+ import { buildFinalReport } from '../reporter/index.js'
6
+
7
+ export interface RunLoopInput {
8
+ targetFile?: string
9
+ topN?: number
10
+ allowedTools?: string[]
11
+ permissionMode?: string
12
+ timeoutMs?: number
13
+ }
14
+
15
+ export async function runLoop(
16
+ ctx: AppContext,
17
+ input: RunLoopInput = {}
18
+ ): Promise<BaseToolResult & { targetFile?: string }> {
19
+ await analyzeBaseline(ctx)
20
+
21
+ const attemptedTargets = new Set<string>()
22
+ let iterationCount = 0
23
+ let completedTargets = 0
24
+ let blockedTargets = 0
25
+ let noOpTargets = 0
26
+ let lastCompletedTarget: string | undefined
27
+ let lastResult: (BaseToolResult & { targetFile?: string }) | undefined
28
+
29
+ const maxIterations = input.targetFile ? 1 : Math.max(1, ctx.limits.maxIterationsPerRun)
30
+
31
+ let lastCoverage: number | undefined
32
+
33
+ while (iterationCount < maxIterations) {
34
+ iterationCount += 1
35
+ const result = await runWithClaudeCli(ctx, {
36
+ ...input,
37
+ excludeTargets: input.targetFile ? undefined : [...attemptedTargets]
38
+ })
39
+ lastResult = result
40
+
41
+ if (result.targetFile) attemptedTargets.add(result.targetFile)
42
+
43
+
44
+ const coverageSummary = await ctx.reportStore.loadCoverageSummary<{ coverageSnapshot?: { lines?: number } }>()
45
+ const currentCoverage = coverageSummary?.coverageSnapshot?.lines
46
+ lastCoverage = currentCoverage
47
+ if (currentCoverage !== undefined && currentCoverage >= ctx.coverageTarget) {
48
+ break
49
+ }
50
+
51
+ if (result.ok) {
52
+ completedTargets += 1
53
+ lastCompletedTarget = result.targetFile
54
+ await syncLoopSummary(ctx, {
55
+ iterationCount,
56
+ completedTargets,
57
+ blockedTargets,
58
+ noOpTargets,
59
+ lastCompletedTarget
60
+ })
61
+ continue
62
+ }
63
+
64
+ if (result.summary.includes('no-op')) {
65
+ noOpTargets += 1
66
+ } else {
67
+ blockedTargets += 1
68
+ }
69
+
70
+ await syncLoopSummary(ctx, {
71
+ iterationCount,
72
+ completedTargets,
73
+ blockedTargets,
74
+ noOpTargets,
75
+ lastCompletedTarget
76
+ })
77
+
78
+ if (input.targetFile) break
79
+ if (blockedTargets >= Math.max(1, ctx.limits.maxRetryPerTask)) break
80
+ if (noOpTargets >= Math.max(1, ctx.limits.loopThreshold)) break
81
+ if (!result.targetFile) break
82
+ }
83
+
84
+ const finalLoopSummary = {
85
+ iterationCount,
86
+ completedTargets,
87
+ blockedTargets,
88
+ noOpTargets,
89
+ lastCompletedTarget
90
+ }
91
+ await syncLoopSummary(ctx, finalLoopSummary)
92
+
93
+ const report = await buildFinalReport(ctx)
94
+ await ctx.reportStore.saveFinalReport(report)
95
+
96
+ if (!lastResult) {
97
+ return {
98
+ ok: false,
99
+ phase: 'BLOCKED',
100
+ summary: '自动执行未产生任何任务结果。',
101
+ nextAction: 'manual-intervention'
102
+ }
103
+ }
104
+
105
+ const summaryParts = [
106
+ `completed=${completedTargets}`,
107
+ `blocked=${blockedTargets}`,
108
+ `noop=${noOpTargets}`,
109
+ `iterations=${iterationCount}`
110
+ ]
111
+ if (lastCoverage !== undefined) {
112
+ summaryParts.push(`coverage=${lastCoverage}/${ctx.coverageTarget}`)
113
+ }
114
+
115
+ return {
116
+ ...lastResult,
117
+ artifacts: {
118
+ ...lastResult.artifacts,
119
+ finalReportPath: ctx.paths.finalReportPath,
120
+ loopSummary: JSON.stringify(finalLoopSummary)
121
+ },
122
+ summary: `自动执行结束:${summaryParts.join(', ')}`
123
+ }
124
+ }
125
+
126
+ async function syncLoopSummary(
127
+ ctx: AppContext,
128
+ loopSummary: {
129
+ iterationCount: number
130
+ completedTargets: number
131
+ blockedTargets: number
132
+ noOpTargets: number
133
+ lastCompletedTarget?: string
134
+ }
135
+ ) {
136
+ const state = await ctx.stateStore.load()
137
+ await ctx.stateStore.save({
138
+ ...state,
139
+ loopSummary,
140
+ updatedAt: ctx.clock.nowIso()
141
+ })
142
+ }