@boyingliu01/xp-gate 0.15.3 → 0.15.4

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 (75) hide show
  1. package/adapter-common.sh +0 -0
  2. package/adapters/cpp.sh +0 -0
  3. package/adapters/dart.sh +0 -0
  4. package/adapters/flutter.sh +0 -0
  5. package/adapters/go.sh +0 -0
  6. package/adapters/iac.sh +0 -0
  7. package/adapters/java.sh +0 -0
  8. package/adapters/kotlin.sh +0 -0
  9. package/adapters/objectivec.sh +0 -0
  10. package/adapters/plugins/book299-20132-python/scripts/install-gradle.sh +0 -0
  11. package/adapters/plugins/book299-20132-python/scripts/install-maven.sh +0 -0
  12. package/adapters/plugins/book299-4081-c/scripts/install-gradle.sh +0 -0
  13. package/adapters/plugins/book299-4081-c/scripts/install-maven.sh +0 -0
  14. package/adapters/plugins/book299-4083-javascriptes5/scripts/install-gradle.sh +0 -0
  15. package/adapters/plugins/book299-4083-javascriptes5/scripts/install-maven.sh +0 -0
  16. package/adapters/powershell.sh +0 -0
  17. package/adapters/python.sh +0 -0
  18. package/adapters/shell.sh +0 -0
  19. package/adapters/swift.sh +0 -0
  20. package/adapters/typescript.sh +0 -0
  21. package/bin/xp-gate.js +1 -1
  22. package/gate-3.sh +0 -0
  23. package/gate-4.sh +0 -0
  24. package/gate-7.sh +0 -0
  25. package/gate-8.sh +0 -0
  26. package/gate-9.sh +0 -0
  27. package/hooks/adapter-common.sh +0 -0
  28. package/hooks/gate-3.sh +0 -0
  29. package/hooks/gate-4.sh +0 -0
  30. package/hooks/gate-7.sh +0 -0
  31. package/hooks/gate-8.sh +0 -0
  32. package/hooks/gate-9.sh +0 -0
  33. package/hooks/pre-commit +9 -0
  34. package/hooks/pre-push +0 -0
  35. package/lib/__tests__/install-cmd.test.js +0 -35
  36. package/lib/__tests__/phase-transition.test.js +272 -272
  37. package/lib/__tests__/sprint-audit.test.js +207 -207
  38. package/lib/doctor-tui.js +198 -0
  39. package/lib/doctor.js +95 -179
  40. package/lib/init.js +6 -4
  41. package/lib/install-cmd.js +31 -42
  42. package/lib/language-tools.js +833 -833
  43. package/lib/phase-transition.js +249 -249
  44. package/lib/sprint-audit.js +235 -235
  45. package/lib/uninstall.js +22 -0
  46. package/mock-policy/AGENTS.md +2 -2
  47. package/mutation/AGENTS.md +2 -2
  48. package/package.json +1 -1
  49. package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
  50. package/plugins/claude-code/bin/delphi-review-guard.sh +0 -0
  51. package/plugins/claude-code/bin/xp-gate-check +0 -0
  52. package/plugins/claude-code/skills/delphi-review/AGENTS.md +2 -2
  53. package/plugins/claude-code/skills/sprint-flow/AGENTS.md +2 -2
  54. package/plugins/claude-code/skills/sprint-flow/references/phase-4-verify.md +10 -0
  55. package/plugins/claude-code/skills/test-specification-alignment/AGENTS.md +2 -2
  56. package/plugins/opencode/package.json +1 -1
  57. package/plugins/opencode/skills/delphi-review/AGENTS.md +2 -2
  58. package/plugins/opencode/skills/sprint-flow/AGENTS.md +2 -2
  59. package/plugins/opencode/skills/sprint-flow/references/phase-4-verify.md +10 -0
  60. package/plugins/opencode/skills/test-specification-alignment/AGENTS.md +2 -2
  61. package/plugins/qoder/agents/delphi-architecture.md +48 -48
  62. package/plugins/qoder/agents/delphi-feasibility.md +48 -48
  63. package/plugins/qoder/agents/delphi-technical.md +48 -48
  64. package/plugins/qoder/bin/sprint-flow-guard.sh +0 -0
  65. package/plugins/qoder/bin/xp-gate-check +0 -0
  66. package/plugins/qoder/plugin.json +1 -1
  67. package/plugins/qoder/skills/delphi-review/AGENTS.md +2 -2
  68. package/plugins/qoder/skills/sprint-flow/AGENTS.md +2 -2
  69. package/plugins/qoder/skills/test-specification-alignment/AGENTS.md +2 -2
  70. package/principles/AGENTS.md +2 -2
  71. package/scripts/delphi-external-review.cjs +432 -432
  72. package/skills/delphi-review/AGENTS.md +2 -2
  73. package/skills/sprint-flow/AGENTS.md +2 -2
  74. package/skills/sprint-flow/references/phase-4-verify.md +10 -0
  75. package/skills/test-specification-alignment/AGENTS.md +2 -2
@@ -1,207 +1,207 @@
1
- /**
2
- * Tests for sprint-audit CLI command (Layer 2)
3
- * @test REQ-338 Sprint audit — final completeness check
4
- * @intent Verify sprint-audit correctly checks phase coverage, artifacts, consistency
5
- * @covers AC-AUDIT-01 (SKIP when no sprint-state.json)
6
- * @covers AC-AUDIT-02 (PASS when 6/6 completed)
7
- * @covers AC-AUDIT-03 (PASS_WITH_WARNINGS when 4-5/6 completed)
8
- * @covers AC-AUDIT-04 (FAIL when <4/6 completed)
9
- * @covers AC-AUDIT-05 (FAIL on state inconsistency)
10
- * @covers AC-AUDIT-06 (--json flag outputs JSON)
11
- * @covers AC-AUDIT-07 (report persisted to audit-report.json)
12
- */
13
-
14
- import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
15
- import fs from 'node:fs';
16
- import path from 'node:path';
17
- import os from 'node:os';
18
- import { handleSprintAudit } from '../sprint-audit.js';
19
-
20
- describe('sprint-audit', () => {
21
- let tmpDir;
22
-
23
- beforeEach(() => {
24
- tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sprint-audit-test-'));
25
- });
26
-
27
- afterEach(() => {
28
- fs.rmSync(tmpDir, { recursive: true, force: true });
29
- });
30
-
31
- function writeState(state) {
32
- const stateDir = path.join(tmpDir, '.sprint-state');
33
- fs.mkdirSync(stateDir, { recursive: true });
34
- fs.writeFileSync(path.join(stateDir, 'sprint-state.json'), JSON.stringify(state));
35
- }
36
-
37
- function makeState(overrides = {}) {
38
- return {
39
- _schema_version: 1,
40
- id: 'sprint-test-audit',
41
- task_description: 'Test audit feature',
42
- phase: 6,
43
- status: 'completed',
44
- started_at: '2026-07-22T10:00:00Z',
45
- phase_history: [],
46
- isolation: { branch: 'sprint/test-audit', worktree_path: null },
47
- outputs: {},
48
- ...overrides,
49
- };
50
- }
51
-
52
- function makePhaseHistory(completedPhases) {
53
- const phaseNames = ['PREP', 'DESIGN', 'BUILD', 'VERIFY', 'SHIP', 'CLOSE'];
54
- return completedPhases.map((p, i) => ({
55
- phase: p,
56
- phase_name: phaseNames[p - 1],
57
- status: 'completed',
58
- started_at: `2026-07-22T10:${String(i).padStart(2, '0')}:00Z`,
59
- completed_at: `2026-07-22T10:${String(i).padStart(2, '0')}:30Z`,
60
- duration_seconds: 30,
61
- }));
62
- }
63
-
64
- describe('handleSprintAudit()', () => {
65
- it('returns 0 and shows help with --help', async () => {
66
- const code = await handleSprintAudit(['--help']);
67
- expect(code).toBe(0);
68
- });
69
-
70
- it('returns SKIP (exit 0) when no sprint-state.json exists', async () => {
71
- const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
72
- const code = await handleSprintAudit(['--dir', tmpDir]);
73
- expect(code).toBe(0);
74
- expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('No sprint-state.json'));
75
- logSpy.mockRestore();
76
- });
77
-
78
- it('returns PASS when 6/6 phases completed', async () => {
79
- writeState(makeState({ phase_history: makePhaseHistory([1, 2, 3, 4, 5, 6]) }));
80
-
81
- const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
82
- const code = await handleSprintAudit(['--dir', tmpDir]);
83
- expect(code).toBe(0);
84
- // Check output contains PASS
85
- const output = logSpy.mock.calls.map(c => c[0]).join('\n');
86
- expect(output).toContain('PASS');
87
- expect(output).toContain('6/6');
88
- logSpy.mockRestore();
89
- });
90
-
91
- it('returns PASS_WITH_WARNINGS when 5/6 completed', async () => {
92
- writeState(makeState({ phase: 5, phase_history: makePhaseHistory([1, 2, 3, 4, 5]) }));
93
-
94
- const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
95
- const code = await handleSprintAudit(['--dir', tmpDir]);
96
- expect(code).toBe(0);
97
- const output = logSpy.mock.calls.map(c => c[0]).join('\n');
98
- expect(output).toContain('PASS_WITH_WARNINGS');
99
- logSpy.mockRestore();
100
- });
101
-
102
- it('returns PASS_WITH_WARNINGS when 4/6 completed', async () => {
103
- writeState(makeState({ phase: 4, phase_history: makePhaseHistory([1, 2, 3, 4]) }));
104
-
105
- const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
106
- const code = await handleSprintAudit(['--dir', tmpDir]);
107
- expect(code).toBe(0);
108
- const output = logSpy.mock.calls.map(c => c[0]).join('\n');
109
- expect(output).toContain('PASS_WITH_WARNINGS');
110
- logSpy.mockRestore();
111
- });
112
-
113
- it('returns FAIL when <4/6 completed', async () => {
114
- writeState(makeState({ phase: 3, phase_history: makePhaseHistory([1, 2, 3]) }));
115
-
116
- const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
117
- const code = await handleSprintAudit(['--dir', tmpDir]);
118
- expect(code).toBe(1);
119
- const output = logSpy.mock.calls.map(c => c[0]).join('\n');
120
- expect(output).toContain('FAIL');
121
- logSpy.mockRestore();
122
- });
123
-
124
- it('returns FAIL on state inconsistency (phase vs phase_history)', async () => {
125
- const history = makePhaseHistory([1, 2, 3, 4, 5, 6]);
126
- writeState(makeState({ phase: 3, phase_history: history })); // phase=3 but last entry is 6
127
-
128
- const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
129
- const code = await handleSprintAudit(['--dir', tmpDir]);
130
- expect(code).toBe(1);
131
- const output = logSpy.mock.calls.map(c => c[0]).join('\n');
132
- expect(output).toContain('State inconsistency');
133
- logSpy.mockRestore();
134
- });
135
-
136
- it('outputs JSON when --json flag is set', async () => {
137
- writeState(makeState({ phase_history: makePhaseHistory([1, 2, 3, 4, 5, 6]) }));
138
-
139
- const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
140
- const code = await handleSprintAudit(['--dir', tmpDir, '--json']);
141
- expect(code).toBe(0);
142
- const output = logSpy.mock.calls.map(c => c[0]).join('\n');
143
- const result = JSON.parse(output);
144
- expect(result.verdict).toBe('PASS');
145
- expect(result.coverage.completed).toBe(6);
146
- logSpy.mockRestore();
147
- });
148
-
149
- it('counts skipped phases as completed in coverage', async () => {
150
- const history = makePhaseHistory([1, 2, 3, 4, 5]);
151
- history.push({
152
- phase: 6, phase_name: 'CLOSE', status: 'skipped',
153
- started_at: '2026-07-22T10:05:00Z', completed_at: '2026-07-22T10:05:00Z', duration_seconds: 0,
154
- });
155
- writeState(makeState({ phase: 6, phase_history: history }));
156
-
157
- const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
158
- const code = await handleSprintAudit(['--dir', tmpDir, '--json']);
159
- expect(code).toBe(0);
160
- const output = logSpy.mock.calls.map(c => c[0]).join('\n');
161
- const result = JSON.parse(output);
162
- expect(result.coverage.completed).toBe(6);
163
- expect(result.verdict).toBe('PASS');
164
- logSpy.mockRestore();
165
- });
166
-
167
- it('warns about missing summary files for completed phases', async () => {
168
- writeState(makeState({ phase_history: makePhaseHistory([1, 2, 3, 4, 5, 6]) }));
169
- // No phase-outputs directory → all summaries missing
170
-
171
- const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
172
- const code = await handleSprintAudit(['--dir', tmpDir, '--json']);
173
- expect(code).toBe(0);
174
- const output = logSpy.mock.calls.map(c => c[0]).join('\n');
175
- const result = JSON.parse(output);
176
- expect(result.warnings.length).toBeGreaterThan(0);
177
- expect(result.warnings.some(w => w.includes('summary missing'))).toBe(true);
178
- logSpy.mockRestore();
179
- });
180
-
181
- it('persists audit report to audit-report.json', async () => {
182
- writeState(makeState({ phase_history: makePhaseHistory([1, 2, 3, 4, 5, 6]) }));
183
-
184
- const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
185
- await handleSprintAudit(['--dir', tmpDir]);
186
- logSpy.mockRestore();
187
-
188
- const reportPath = path.join(tmpDir, '.sprint-state', 'audit-report.json');
189
- expect(fs.existsSync(reportPath)).toBe(true);
190
- const report = JSON.parse(fs.readFileSync(reportPath, 'utf8'));
191
- expect(report.verdict).toBe('PASS');
192
- expect(report.sprint_id).toBe('sprint-test-audit');
193
- });
194
-
195
- it('handles corrupted sprint-state.json gracefully', async () => {
196
- const stateDir = path.join(tmpDir, '.sprint-state');
197
- fs.mkdirSync(stateDir, { recursive: true });
198
- fs.writeFileSync(path.join(stateDir, 'sprint-state.json'), 'not-valid-json');
199
-
200
- const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
201
- const code = await handleSprintAudit(['--dir', tmpDir]);
202
- expect(code).toBe(1);
203
- expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('Failed to parse'));
204
- errSpy.mockRestore();
205
- });
206
- });
207
- });
1
+ /**
2
+ * Tests for sprint-audit CLI command (Layer 2)
3
+ * @test REQ-338 Sprint audit — final completeness check
4
+ * @intent Verify sprint-audit correctly checks phase coverage, artifacts, consistency
5
+ * @covers AC-AUDIT-01 (SKIP when no sprint-state.json)
6
+ * @covers AC-AUDIT-02 (PASS when 6/6 completed)
7
+ * @covers AC-AUDIT-03 (PASS_WITH_WARNINGS when 4-5/6 completed)
8
+ * @covers AC-AUDIT-04 (FAIL when <4/6 completed)
9
+ * @covers AC-AUDIT-05 (FAIL on state inconsistency)
10
+ * @covers AC-AUDIT-06 (--json flag outputs JSON)
11
+ * @covers AC-AUDIT-07 (report persisted to audit-report.json)
12
+ */
13
+
14
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
15
+ import fs from 'node:fs';
16
+ import path from 'node:path';
17
+ import os from 'node:os';
18
+ import { handleSprintAudit } from '../sprint-audit.js';
19
+
20
+ describe('sprint-audit', () => {
21
+ let tmpDir;
22
+
23
+ beforeEach(() => {
24
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sprint-audit-test-'));
25
+ });
26
+
27
+ afterEach(() => {
28
+ fs.rmSync(tmpDir, { recursive: true, force: true });
29
+ });
30
+
31
+ function writeState(state) {
32
+ const stateDir = path.join(tmpDir, '.sprint-state');
33
+ fs.mkdirSync(stateDir, { recursive: true });
34
+ fs.writeFileSync(path.join(stateDir, 'sprint-state.json'), JSON.stringify(state));
35
+ }
36
+
37
+ function makeState(overrides = {}) {
38
+ return {
39
+ _schema_version: 1,
40
+ id: 'sprint-test-audit',
41
+ task_description: 'Test audit feature',
42
+ phase: 6,
43
+ status: 'completed',
44
+ started_at: '2026-07-22T10:00:00Z',
45
+ phase_history: [],
46
+ isolation: { branch: 'sprint/test-audit', worktree_path: null },
47
+ outputs: {},
48
+ ...overrides,
49
+ };
50
+ }
51
+
52
+ function makePhaseHistory(completedPhases) {
53
+ const phaseNames = ['PREP', 'DESIGN', 'BUILD', 'VERIFY', 'SHIP', 'CLOSE'];
54
+ return completedPhases.map((p, i) => ({
55
+ phase: p,
56
+ phase_name: phaseNames[p - 1],
57
+ status: 'completed',
58
+ started_at: `2026-07-22T10:${String(i).padStart(2, '0')}:00Z`,
59
+ completed_at: `2026-07-22T10:${String(i).padStart(2, '0')}:30Z`,
60
+ duration_seconds: 30,
61
+ }));
62
+ }
63
+
64
+ describe('handleSprintAudit()', () => {
65
+ it('returns 0 and shows help with --help', async () => {
66
+ const code = await handleSprintAudit(['--help']);
67
+ expect(code).toBe(0);
68
+ });
69
+
70
+ it('returns SKIP (exit 0) when no sprint-state.json exists', async () => {
71
+ const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
72
+ const code = await handleSprintAudit(['--dir', tmpDir]);
73
+ expect(code).toBe(0);
74
+ expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('No sprint-state.json'));
75
+ logSpy.mockRestore();
76
+ });
77
+
78
+ it('returns PASS when 6/6 phases completed', async () => {
79
+ writeState(makeState({ phase_history: makePhaseHistory([1, 2, 3, 4, 5, 6]) }));
80
+
81
+ const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
82
+ const code = await handleSprintAudit(['--dir', tmpDir]);
83
+ expect(code).toBe(0);
84
+ // Check output contains PASS
85
+ const output = logSpy.mock.calls.map(c => c[0]).join('\n');
86
+ expect(output).toContain('PASS');
87
+ expect(output).toContain('6/6');
88
+ logSpy.mockRestore();
89
+ });
90
+
91
+ it('returns PASS_WITH_WARNINGS when 5/6 completed', async () => {
92
+ writeState(makeState({ phase: 5, phase_history: makePhaseHistory([1, 2, 3, 4, 5]) }));
93
+
94
+ const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
95
+ const code = await handleSprintAudit(['--dir', tmpDir]);
96
+ expect(code).toBe(0);
97
+ const output = logSpy.mock.calls.map(c => c[0]).join('\n');
98
+ expect(output).toContain('PASS_WITH_WARNINGS');
99
+ logSpy.mockRestore();
100
+ });
101
+
102
+ it('returns PASS_WITH_WARNINGS when 4/6 completed', async () => {
103
+ writeState(makeState({ phase: 4, phase_history: makePhaseHistory([1, 2, 3, 4]) }));
104
+
105
+ const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
106
+ const code = await handleSprintAudit(['--dir', tmpDir]);
107
+ expect(code).toBe(0);
108
+ const output = logSpy.mock.calls.map(c => c[0]).join('\n');
109
+ expect(output).toContain('PASS_WITH_WARNINGS');
110
+ logSpy.mockRestore();
111
+ });
112
+
113
+ it('returns FAIL when <4/6 completed', async () => {
114
+ writeState(makeState({ phase: 3, phase_history: makePhaseHistory([1, 2, 3]) }));
115
+
116
+ const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
117
+ const code = await handleSprintAudit(['--dir', tmpDir]);
118
+ expect(code).toBe(1);
119
+ const output = logSpy.mock.calls.map(c => c[0]).join('\n');
120
+ expect(output).toContain('FAIL');
121
+ logSpy.mockRestore();
122
+ });
123
+
124
+ it('returns FAIL on state inconsistency (phase vs phase_history)', async () => {
125
+ const history = makePhaseHistory([1, 2, 3, 4, 5, 6]);
126
+ writeState(makeState({ phase: 3, phase_history: history })); // phase=3 but last entry is 6
127
+
128
+ const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
129
+ const code = await handleSprintAudit(['--dir', tmpDir]);
130
+ expect(code).toBe(1);
131
+ const output = logSpy.mock.calls.map(c => c[0]).join('\n');
132
+ expect(output).toContain('State inconsistency');
133
+ logSpy.mockRestore();
134
+ });
135
+
136
+ it('outputs JSON when --json flag is set', async () => {
137
+ writeState(makeState({ phase_history: makePhaseHistory([1, 2, 3, 4, 5, 6]) }));
138
+
139
+ const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
140
+ const code = await handleSprintAudit(['--dir', tmpDir, '--json']);
141
+ expect(code).toBe(0);
142
+ const output = logSpy.mock.calls.map(c => c[0]).join('\n');
143
+ const result = JSON.parse(output);
144
+ expect(result.verdict).toBe('PASS');
145
+ expect(result.coverage.completed).toBe(6);
146
+ logSpy.mockRestore();
147
+ });
148
+
149
+ it('counts skipped phases as completed in coverage', async () => {
150
+ const history = makePhaseHistory([1, 2, 3, 4, 5]);
151
+ history.push({
152
+ phase: 6, phase_name: 'CLOSE', status: 'skipped',
153
+ started_at: '2026-07-22T10:05:00Z', completed_at: '2026-07-22T10:05:00Z', duration_seconds: 0,
154
+ });
155
+ writeState(makeState({ phase: 6, phase_history: history }));
156
+
157
+ const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
158
+ const code = await handleSprintAudit(['--dir', tmpDir, '--json']);
159
+ expect(code).toBe(0);
160
+ const output = logSpy.mock.calls.map(c => c[0]).join('\n');
161
+ const result = JSON.parse(output);
162
+ expect(result.coverage.completed).toBe(6);
163
+ expect(result.verdict).toBe('PASS');
164
+ logSpy.mockRestore();
165
+ });
166
+
167
+ it('warns about missing summary files for completed phases', async () => {
168
+ writeState(makeState({ phase_history: makePhaseHistory([1, 2, 3, 4, 5, 6]) }));
169
+ // No phase-outputs directory → all summaries missing
170
+
171
+ const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
172
+ const code = await handleSprintAudit(['--dir', tmpDir, '--json']);
173
+ expect(code).toBe(0);
174
+ const output = logSpy.mock.calls.map(c => c[0]).join('\n');
175
+ const result = JSON.parse(output);
176
+ expect(result.warnings.length).toBeGreaterThan(0);
177
+ expect(result.warnings.some(w => w.includes('summary missing'))).toBe(true);
178
+ logSpy.mockRestore();
179
+ });
180
+
181
+ it('persists audit report to audit-report.json', async () => {
182
+ writeState(makeState({ phase_history: makePhaseHistory([1, 2, 3, 4, 5, 6]) }));
183
+
184
+ const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
185
+ await handleSprintAudit(['--dir', tmpDir]);
186
+ logSpy.mockRestore();
187
+
188
+ const reportPath = path.join(tmpDir, '.sprint-state', 'audit-report.json');
189
+ expect(fs.existsSync(reportPath)).toBe(true);
190
+ const report = JSON.parse(fs.readFileSync(reportPath, 'utf8'));
191
+ expect(report.verdict).toBe('PASS');
192
+ expect(report.sprint_id).toBe('sprint-test-audit');
193
+ });
194
+
195
+ it('handles corrupted sprint-state.json gracefully', async () => {
196
+ const stateDir = path.join(tmpDir, '.sprint-state');
197
+ fs.mkdirSync(stateDir, { recursive: true });
198
+ fs.writeFileSync(path.join(stateDir, 'sprint-state.json'), 'not-valid-json');
199
+
200
+ const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
201
+ const code = await handleSprintAudit(['--dir', tmpDir]);
202
+ expect(code).toBe(1);
203
+ expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('Failed to parse'));
204
+ errSpy.mockRestore();
205
+ });
206
+ });
207
+ });
@@ -0,0 +1,198 @@
1
+ /**
2
+ * TUI registration and installed-skills diagnostics for doctor.
3
+ * Extracted from doctor.js to keep file size under the large-file threshold.
4
+ */
5
+ const fs = require('fs');
6
+ const path = require('path');
7
+ const { HOME_DIR, detectPlatform } = require('./shared-paths.js');
8
+
9
+ // npm package source dir (template hooks/adapters)
10
+ const PKG_DIR = path.dirname(__dirname);
11
+
12
+ /**
13
+ * TUI registration path and expected plugin entry.
14
+ */
15
+ const TUI_JSON_PATH = path.join(HOME_DIR, '.config', 'opencode', 'tui.json');
16
+ const TUI_PLUGIN_ENTRY = '@boyingliu01/opencode-plugin/tui';
17
+
18
+ /**
19
+ * Read and parse tui.json, returning { data, error }.
20
+ * data = parsed object on success, null on file missing, undefined on corrupt.
21
+ */
22
+ function readTuiJson() {
23
+ if (!fs.existsSync(TUI_JSON_PATH)) return { data: null, error: null };
24
+ try {
25
+ const raw = fs.readFileSync(TUI_JSON_PATH, 'utf8');
26
+ const data = JSON.parse(raw);
27
+ return { data, error: null };
28
+ } catch (e) {
29
+ return { data: undefined, error: `Corrupt JSON: ${e.message}` };
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Check 9: TUI auto-registration in ~/.config/opencode/tui.json.
35
+ * @returns {number} issue count
36
+ */
37
+ function diagnoseTuiRegistration(checks) {
38
+ const { data, error } = readTuiJson();
39
+
40
+ if (error) {
41
+ checks.push({ name: 'TUI registration', status: 'FAIL', detail: error });
42
+ return 1;
43
+ }
44
+
45
+ if (data === null) {
46
+ checks.push({ name: 'TUI registration', status: 'FAIL', detail: 'Not registered' });
47
+ return 1;
48
+ }
49
+
50
+ const plugins = Array.isArray(data.plugin) ? data.plugin : [];
51
+ if (plugins.includes(TUI_PLUGIN_ENTRY)) {
52
+ checks.push({ name: 'TUI registration', status: 'PASS', detail: `${TUI_PLUGIN_ENTRY} registered` });
53
+ return 0;
54
+ }
55
+
56
+ checks.push({ name: 'TUI registration', status: 'FAIL', detail: 'Not registered' });
57
+ return 1;
58
+ }
59
+
60
+ /**
61
+ * Check 9: Installed skills vs package-bundled skills (#332).
62
+ * Reports WARN for each installed skill whose SKILL.md differs from the bundled version.
63
+ * @param {object} config
64
+ * @param {Array} checks
65
+ * @returns {number} issue count
66
+ */
67
+ function diagnoseInstalledSkills(config, checks) {
68
+ const installedSkills = config.installedSkills || {};
69
+ const skillNames = Object.keys(installedSkills);
70
+ if (skillNames.length === 0) {
71
+ checks.push({ name: 'Installed skills', status: 'SKIP', detail: 'No skills installed' });
72
+ return 0;
73
+ }
74
+
75
+ const bundledSkillsDir = path.join(PKG_DIR, 'skills');
76
+ if (!fs.existsSync(bundledSkillsDir)) {
77
+ checks.push({ name: 'Installed skills', status: 'SKIP', detail: 'Package skills dir not found' });
78
+ return 0;
79
+ }
80
+
81
+ const platform = detectPlatform();
82
+ let userSkillsDir;
83
+ if (platform === 'qoder') {
84
+ userSkillsDir = path.join(HOME_DIR, '.qoder', 'skills');
85
+ } else if (platform === 'claude-code') {
86
+ userSkillsDir = path.join(HOME_DIR, '.claude', 'skills');
87
+ } else {
88
+ userSkillsDir = path.join(HOME_DIR, '.config', 'opencode', 'skills');
89
+ }
90
+
91
+ let issues = 0;
92
+ for (const name of skillNames) {
93
+ const bundledSkillMd = path.join(bundledSkillsDir, name, 'SKILL.md');
94
+ const userSkillMd = path.join(userSkillsDir, name, 'SKILL.md');
95
+
96
+ if (!fs.existsSync(bundledSkillMd)) continue;
97
+ if (!fs.existsSync(userSkillMd)) {
98
+ checks.push({ name: `Skill: ${name}`, status: 'FAIL', detail: 'Installed SKILL.md missing' });
99
+ issues++;
100
+ continue;
101
+ }
102
+
103
+ const bundledContent = fs.readFileSync(bundledSkillMd, 'utf8');
104
+ const userContent = fs.readFileSync(userSkillMd, 'utf8');
105
+ if (bundledContent !== userContent) {
106
+ checks.push({
107
+ name: `Skill: ${name}`,
108
+ status: 'WARN',
109
+ detail: 'Outdated — run xp-gate update-skill --all',
110
+ });
111
+ issues++;
112
+ } else {
113
+ checks.push({ name: `Skill: ${name}`, status: 'PASS', detail: 'Up to date' });
114
+ }
115
+ }
116
+
117
+ return issues;
118
+ }
119
+
120
+ /**
121
+ * Fix TUI registration: ensure @boyingliu01/opencode-plugin/tui is in tui.json.
122
+ * Uses atomic write (tmp + renameSync) for JSON safety.
123
+ * On corrupt JSON: backup to .corrupt-{timestamp}.bak then rebuild.
124
+ * Idempotent: skips if already registered.
125
+ * @returns {boolean} Whether a fix was applied
126
+ */
127
+ function fixTuiRegistration() {
128
+ const { data, error } = readTuiJson();
129
+
130
+ // Corrupt JSON: backup old file, rebuild from scratch
131
+ if (error && data === undefined) {
132
+ const ts = Date.now();
133
+ const backupPath = `${TUI_JSON_PATH}.corrupt-${ts}.bak`;
134
+ try {
135
+ fs.copyFileSync(TUI_JSON_PATH, backupPath);
136
+ console.log(` ⚠ TUI config corrupted — backed up to ${backupPath}`);
137
+ } catch { /* non-critical */ }
138
+ // Rebuild fresh
139
+ const dir = path.dirname(TUI_JSON_PATH);
140
+ fs.mkdirSync(dir, { recursive: true });
141
+ const newConfig = { plugin: [TUI_PLUGIN_ENTRY] };
142
+ const tmpPath = `${TUI_JSON_PATH}.tmp`;
143
+ fs.writeFileSync(tmpPath, JSON.stringify(newConfig, null, 2));
144
+ fs.renameSync(tmpPath, TUI_JSON_PATH);
145
+ console.log(` ✓ Registered ${TUI_PLUGIN_ENTRY} in TUI (rebuilt after corrupt backup)`);
146
+ return true;
147
+ }
148
+
149
+ // File doesn't exist: create it
150
+ if (data === null) {
151
+ const dir = path.dirname(TUI_JSON_PATH);
152
+ fs.mkdirSync(dir, { recursive: true });
153
+ const newConfig = { plugin: [TUI_PLUGIN_ENTRY] };
154
+ const tmpPath = `${TUI_JSON_PATH}.tmp`;
155
+ fs.writeFileSync(tmpPath, JSON.stringify(newConfig, null, 2));
156
+ fs.renameSync(tmpPath, TUI_JSON_PATH);
157
+ console.log(` ✓ Created TUI config with ${TUI_PLUGIN_ENTRY}`);
158
+ return true;
159
+ }
160
+
161
+ // File exists, check if plugin already registered (idempotent)
162
+ const plugins = Array.isArray(data.plugin) ? data.plugin : [];
163
+ if (plugins.includes(TUI_PLUGIN_ENTRY)) return false;
164
+
165
+ // Append plugin entry
166
+ data.plugin = plugins.concat([TUI_PLUGIN_ENTRY]);
167
+ const tmpPath = `${TUI_JSON_PATH}.tmp`;
168
+ fs.writeFileSync(tmpPath, JSON.stringify(data, null, 2));
169
+ fs.renameSync(tmpPath, TUI_JSON_PATH);
170
+ console.log(` ✓ Added ${TUI_PLUGIN_ENTRY} to TUI config`);
171
+ return true;
172
+ }
173
+
174
+ /**
175
+ * Wrapper for init.js: ensure TUI is registered without console output.
176
+ * Returns true if the plugin is already registered or was just added.
177
+ */
178
+ function ensureTuiRegistration() {
179
+ const { data } = readTuiJson();
180
+ if (data === null || data === undefined) {
181
+ fixTuiRegistration();
182
+ return;
183
+ }
184
+ const plugins = Array.isArray(data.plugin) ? data.plugin : [];
185
+ if (!plugins.includes(TUI_PLUGIN_ENTRY)) {
186
+ fixTuiRegistration();
187
+ }
188
+ }
189
+
190
+ module.exports = {
191
+ TUI_JSON_PATH,
192
+ TUI_PLUGIN_ENTRY,
193
+ readTuiJson,
194
+ diagnoseTuiRegistration,
195
+ diagnoseInstalledSkills,
196
+ fixTuiRegistration,
197
+ ensureTuiRegistration,
198
+ };