@boyingliu01/xp-gate 0.38.76 → 0.38.77

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 (104) hide show
  1. package/lib/__tests__/mirror-sync.test.js +387 -5
  2. package/lib/mattpocock-license-verdict.json +1 -1
  3. package/lib/mirror-sync.js +202 -15
  4. package/package.json +1 -1
  5. package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
  6. package/plugins/claude-code/skills/delphi-review/AGENTS.md +1 -1
  7. package/plugins/claude-code/skills/sprint-flow/AGENTS.md +1 -1
  8. package/plugins/claude-code/skills/test-specification-alignment/AGENTS.md +1 -1
  9. package/plugins/dsh/package.json +1 -1
  10. package/plugins/dsh/skills/delphi-review/AGENTS.md +1 -1
  11. package/plugins/dsh/skills/sprint-flow/AGENTS.md +1 -1
  12. package/plugins/dsh/skills/test-specification-alignment/AGENTS.md +1 -1
  13. package/plugins/opencode/package.json +1 -1
  14. package/plugins/opencode/skills/delphi-review/AGENTS.md +1 -1
  15. package/plugins/opencode/skills/sprint-flow/AGENTS.md +1 -1
  16. package/plugins/opencode/skills/test-specification-alignment/AGENTS.md +1 -1
  17. package/plugins/qoder/plugin.json +1 -1
  18. package/plugins/qoder/skills/delphi-review/AGENTS.md +1 -1
  19. package/plugins/qoder/skills/sprint-flow/AGENTS.md +1 -1
  20. package/plugins/qoder/skills/test-specification-alignment/AGENTS.md +1 -1
  21. package/scripts/generate-mirrors.cjs +10 -0
  22. package/skills/delphi-review/AGENTS.md +1 -1
  23. package/skills/sprint-flow/AGENTS.md +1 -1
  24. package/skills/test-specification-alignment/AGENTS.md +1 -1
  25. package/mock-policy/__tests__/config.test.ts +0 -292
  26. package/mock-policy/__tests__/gate-m3.test.ts +0 -379
  27. package/mock-policy/__tests__/integration.test.ts +0 -600
  28. package/mock-policy/__tests__/mock-decision-engine.test.ts +0 -231
  29. package/mock-policy/__tests__/schema.test.ts +0 -239
  30. package/mock-policy/__tests__/scope-scanner.test.ts +0 -541
  31. package/mutation/__tests__/detect-ai-test.test.ts +0 -152
  32. package/mutation/__tests__/gate-m-survivors.test.ts +0 -167
  33. package/mutation/__tests__/gate-m.test.ts +0 -246
  34. package/mutation/__tests__/go-mutant-runner.test.ts +0 -288
  35. package/mutation/__tests__/gremlins-runner.test.ts +0 -167
  36. package/mutation/__tests__/init-baseline.test.ts +0 -347
  37. package/mutation/__tests__/mutation-mirror.test.ts +0 -49
  38. package/mutation/__tests__/mutmut-runner.test.ts +0 -1049
  39. package/mutation/__tests__/pitest-gradle-availability.test.ts +0 -74
  40. package/mutation/__tests__/pitest-runner.test.ts +0 -574
  41. package/mutation/__tests__/runners-index.test.ts +0 -162
  42. package/mutation/__tests__/stryker-runner.test.ts +0 -455
  43. package/mutation/__tests__/test-filter-parity.test.ts +0 -40
  44. package/mutation/__tests__/update-baseline.test.ts +0 -395
  45. package/principles/__tests__/adapter-metrics.test.ts +0 -292
  46. package/principles/__tests__/analyze-read-failure.test.ts +0 -70
  47. package/principles/__tests__/analyzer.test.ts +0 -367
  48. package/principles/__tests__/baseline-entry-total-warnings.test.ts +0 -76
  49. package/principles/__tests__/baseline-prune-timeout.test.ts +0 -71
  50. package/principles/__tests__/baseline-prune.test.ts +0 -399
  51. package/principles/__tests__/baseline-storage.test.ts +0 -188
  52. package/principles/__tests__/baseline.test.ts +0 -330
  53. package/principles/__tests__/boy-scout-init-baseline.test.ts +0 -167
  54. package/principles/__tests__/boy-scout-integration.test.ts +0 -355
  55. package/principles/__tests__/boy-scout.test.ts +0 -893
  56. package/principles/__tests__/cli-files-separators.test.ts +0 -69
  57. package/principles/__tests__/config-arch-group.test.ts +0 -106
  58. package/principles/__tests__/config-enforcement.test.ts +0 -470
  59. package/principles/__tests__/config-git-timeout.test.ts +0 -66
  60. package/principles/__tests__/config-merge-warnings.test.ts +0 -148
  61. package/principles/__tests__/config.test.ts +0 -78
  62. package/principles/__tests__/direct-execution.test.ts +0 -187
  63. package/principles/__tests__/index.test.ts +0 -94
  64. package/principles/__tests__/issue-587-java-member-count.test.ts +0 -279
  65. package/principles/__tests__/issue-588-lsp-java-false-positives.test.ts +0 -213
  66. package/principles/__tests__/lint-baseline.test.ts +0 -314
  67. package/principles/__tests__/many-exports-reexport.test.ts +0 -40
  68. package/principles/__tests__/reporter.test.ts +0 -660
  69. package/principles/__tests__/types.test.ts +0 -103
  70. package/principles/__tests__/warnings-baseline-budgets.test.ts +0 -74
  71. package/principles/adapters/__tests__/base.test.ts +0 -95
  72. package/principles/adapters/__tests__/cpp-code-block.test.ts +0 -123
  73. package/principles/adapters/__tests__/cpp-scanner-equivalence.test.ts +0 -67
  74. package/principles/adapters/__tests__/cpp.test.ts +0 -290
  75. package/principles/adapters/__tests__/dart.test.ts +0 -84
  76. package/principles/adapters/__tests__/go.test.ts +0 -68
  77. package/principles/adapters/__tests__/java.test.ts +0 -385
  78. package/principles/adapters/__tests__/kotlin.test.ts +0 -76
  79. package/principles/adapters/__tests__/objectivec.test.ts +0 -453
  80. package/principles/adapters/__tests__/python.test.ts +0 -84
  81. package/principles/adapters/__tests__/swift.test.ts +0 -68
  82. package/principles/adapters/__tests__/typescript-block-comment-lines.test.ts +0 -53
  83. package/principles/adapters/__tests__/typescript-masking.test.ts +0 -96
  84. package/principles/adapters/__tests__/typescript.test.ts +0 -116
  85. package/principles/rules/__tests__/clean-code/code-duplication.test.ts +0 -85
  86. package/principles/rules/__tests__/clean-code/deep-nesting.test.ts +0 -73
  87. package/principles/rules/__tests__/clean-code/god-class.test.ts +0 -109
  88. package/principles/rules/__tests__/clean-code/large-file.test.ts +0 -73
  89. package/principles/rules/__tests__/clean-code/long-function.test.ts +0 -81
  90. package/principles/rules/__tests__/clean-code/magic-numbers.test.ts +0 -68
  91. package/principles/rules/__tests__/clean-code/many-exports.test.ts +0 -123
  92. package/principles/rules/__tests__/clean-code/missing-error-handling.test.ts +0 -171
  93. package/principles/rules/__tests__/clean-code/too-many-params.test.ts +0 -58
  94. package/principles/rules/__tests__/clean-code/unused-imports.test.ts +0 -107
  95. package/principles/rules/__tests__/index.test.ts +0 -49
  96. package/principles/rules/__tests__/solid/dip.test.ts +0 -119
  97. package/principles/rules/__tests__/solid/isp.test.ts +0 -58
  98. package/principles/rules/__tests__/solid/issue-588-lsp-base-contract.test.ts +0 -252
  99. package/principles/rules/__tests__/solid/lsp.test.ts +0 -186
  100. package/principles/rules/__tests__/solid/ocp.test.ts +0 -67
  101. package/principles/rules/__tests__/solid/srp.test.ts +0 -87
  102. package/principles/rules/arch/__tests__/dependency.test.ts +0 -180
  103. package/principles/rules/arch/__tests__/naming.test.ts +0 -133
  104. package/principles/rules/arch/__tests__/shared.test.ts +0 -113
@@ -1,292 +0,0 @@
1
- /**
2
- * @test config.ts - Mock Policy config loader
3
- * @intent Verify config loads and validates correctly from .mockpolicyrc and .xp-gate/mock-policy.yaml
4
- * @covers mock-policy-config-loader
5
- */
6
-
7
- import { describe, it, expect, vi, beforeEach } from 'vitest';
8
-
9
- // Shared mock state — each test sets these before re-importing the module
10
- let mockExistsSync: (path: string) => boolean;
11
- let mockReadFile: (path: string) => Promise<string>;
12
-
13
- vi.mock('fs', () => ({
14
- existsSync: vi.fn((path: string) => mockExistsSync(path)),
15
- }));
16
-
17
- vi.mock('fs/promises', () => ({
18
- readFile: vi.fn((path: string) => mockReadFile(path)),
19
- }));
20
-
21
- const MOCK_DEFAULT_CONFIG = {
22
- version: 1 as const,
23
- layers: {
24
- unit: {
25
- mockPolicy: 'lenient' as const,
26
- requireRealForImplemented: false,
27
- allowExternalMock: true,
28
- requirePendingRemoval: false,
29
- maxMockDensity: 100,
30
- },
31
- integration: {
32
- mockPolicy: 'strict' as const,
33
- requireRealForImplemented: true,
34
- allowExternalMock: true,
35
- requirePendingRemoval: true,
36
- maxMockDensity: 30,
37
- },
38
- e2e: {
39
- mockPolicy: 'strict' as const,
40
- requireRealForImplemented: true,
41
- allowExternalMock: false,
42
- requirePendingRemoval: false,
43
- maxMockDensity: 0,
44
- },
45
- },
46
- projectBoundary: ['src/**'],
47
- severity: 'warning' as const,
48
- };
49
-
50
- describe('loadMockPolicyConfig', () => {
51
- let loadMockPolicyConfig: typeof import('../config').loadMockPolicyConfig;
52
- let DEFAULT_CONFIG: typeof import('../config').DEFAULT_CONFIG;
53
-
54
- beforeEach(async () => {
55
- // Default: no config files exist
56
- mockExistsSync = () => false;
57
- mockReadFile = () => Promise.reject(new Error('ENOENT'));
58
-
59
- // Re-import after mock state is set
60
- vi.resetModules();
61
- const mod = await import('../config');
62
- loadMockPolicyConfig = mod.loadMockPolicyConfig;
63
- DEFAULT_CONFIG = mod.DEFAULT_CONFIG;
64
- });
65
-
66
- describe('DEFAULT_CONFIG', () => {
67
- it('should have correct default values', () => {
68
- expect(DEFAULT_CONFIG).toEqual(MOCK_DEFAULT_CONFIG);
69
- });
70
-
71
- it('should have lenient unit policy', () => {
72
- expect(DEFAULT_CONFIG.layers.unit.mockPolicy).toBe('lenient');
73
- expect(DEFAULT_CONFIG.layers.unit.allowExternalMock).toBe(true);
74
- });
75
-
76
- it('should have strict integration policy', () => {
77
- expect(DEFAULT_CONFIG.layers.integration.mockPolicy).toBe('strict');
78
- expect(DEFAULT_CONFIG.layers.integration.requirePendingRemoval).toBe(true);
79
- expect(DEFAULT_CONFIG.layers.integration.maxMockDensity).toBe(30);
80
- });
81
-
82
- it('should have strict e2e policy forbidding external mocks', () => {
83
- expect(DEFAULT_CONFIG.layers.e2e.allowExternalMock).toBe(false);
84
- expect(DEFAULT_CONFIG.layers.e2e.maxMockDensity).toBe(0);
85
- });
86
-
87
- it('should have projectBoundary and warning severity', () => {
88
- expect(DEFAULT_CONFIG.projectBoundary).toEqual(['src/**']);
89
- expect(DEFAULT_CONFIG.severity).toBe('warning');
90
- });
91
- });
92
-
93
- describe('no config files exist', () => {
94
- it('should return default config when no files found', async () => {
95
- const config = await loadMockPolicyConfig('/fake/project');
96
- expect(config).toEqual(DEFAULT_CONFIG);
97
- });
98
- });
99
-
100
- describe('JSON config (.mockpolicyrc)', () => {
101
- it('should merge partial JSON config with defaults', async () => {
102
- mockExistsSync = (path: string) => path.endsWith('.mockpolicyrc');
103
- mockReadFile = (path: string) => {
104
- if (path.endsWith('.mockpolicyrc')) {
105
- return Promise.resolve(JSON.stringify({
106
- severity: 'error',
107
- layers: {
108
- unit: { mockPolicy: 'strict', maxMockDensity: 50 },
109
- },
110
- }));
111
- }
112
- return Promise.reject(new Error('file not found'));
113
- };
114
-
115
- vi.resetModules();
116
- const mod = await import('../config');
117
- loadMockPolicyConfig = mod.loadMockPolicyConfig;
118
-
119
- const config = await loadMockPolicyConfig('/fake/project');
120
-
121
- expect(config.severity).toBe('error');
122
- expect(config.layers.unit.mockPolicy).toBe('strict');
123
- expect(config.layers.unit.maxMockDensity).toBe(50);
124
- // Defaults preserved for non-overridden fields
125
- expect(config.layers.unit.requireRealForImplemented).toBe(false);
126
- expect(config.layers.unit.allowExternalMock).toBe(true);
127
- expect(config.layers.integration).toEqual(MOCK_DEFAULT_CONFIG.layers.integration);
128
- expect(config.layers.e2e).toEqual(MOCK_DEFAULT_CONFIG.layers.e2e);
129
- expect(config.projectBoundary).toEqual(MOCK_DEFAULT_CONFIG.projectBoundary);
130
- });
131
-
132
- it('should load full JSON config correctly', async () => {
133
- const fullConfig = {
134
- version: 1,
135
- layers: {
136
- unit: {
137
- mockPolicy: 'strict' as const,
138
- requireRealForImplemented: true,
139
- allowExternalMock: false,
140
- requirePendingRemoval: true,
141
- maxMockDensity: 80,
142
- },
143
- integration: {
144
- mockPolicy: 'strict' as const,
145
- requireRealForImplemented: true,
146
- allowExternalMock: false,
147
- requirePendingRemoval: true,
148
- maxMockDensity: 20,
149
- },
150
- e2e: {
151
- mockPolicy: 'strict' as const,
152
- requireRealForImplemented: true,
153
- allowExternalMock: false,
154
- requirePendingRemoval: true,
155
- maxMockDensity: 5,
156
- },
157
- },
158
- projectBoundary: ['src/**', '!src/external/**'],
159
- severity: 'error' as const,
160
- };
161
-
162
- mockExistsSync = (path: string) => path.endsWith('.mockpolicyrc');
163
- mockReadFile = (path: string) => {
164
- if (path.endsWith('.mockpolicyrc')) {
165
- return Promise.resolve(JSON.stringify(fullConfig));
166
- }
167
- return Promise.reject(new Error('file not found'));
168
- };
169
-
170
- vi.resetModules();
171
- const mod = await import('../config');
172
- loadMockPolicyConfig = mod.loadMockPolicyConfig;
173
-
174
- const config = await loadMockPolicyConfig('/fake/project');
175
-
176
- expect(config.layers.unit.maxMockDensity).toBe(80);
177
- expect(config.layers.integration.mockPolicy).toBe('strict');
178
- expect(config.layers.e2e.maxMockDensity).toBe(5);
179
- expect(config.projectBoundary).toEqual(['src/**', '!src/external/**']);
180
- expect(config.severity).toBe('error');
181
- });
182
- });
183
-
184
- describe('YAML config (.xp-gate/mock-policy.yaml)', () => {
185
- it('should load YAML config as fallback', async () => {
186
- mockExistsSync = (path: string) => path.endsWith('mock-policy.yaml');
187
- mockReadFile = (path: string) => {
188
- if (path.endsWith('mock-policy.yaml')) {
189
- return Promise.resolve(`
190
- version: 1
191
- severity: error
192
- layers:
193
- unit:
194
- mockPolicy: strict
195
- maxMockDensity: 60
196
- projectBoundary:
197
- - src/**
198
- `);
199
- }
200
- return Promise.reject(new Error('file not found'));
201
- };
202
-
203
- vi.resetModules();
204
- const mod = await import('../config');
205
- loadMockPolicyConfig = mod.loadMockPolicyConfig;
206
-
207
- const config = await loadMockPolicyConfig('/fake/project');
208
-
209
- expect(config.severity).toBe('error');
210
- expect(config.layers.unit.mockPolicy).toBe('strict');
211
- expect(config.layers.unit.maxMockDensity).toBe(60);
212
- expect(config.projectBoundary).toEqual(['src/**']);
213
- });
214
-
215
- it('should prefer JSON over YAML when both exist', async () => {
216
- mockExistsSync = (path: string) =>
217
- path.endsWith('.mockpolicyrc') || path.endsWith('mock-policy.yaml');
218
- mockReadFile = (path: string) => {
219
- if (path.endsWith('.mockpolicyrc')) {
220
- return Promise.resolve(JSON.stringify({ severity: 'error' }));
221
- }
222
- if (path.endsWith('mock-policy.yaml')) {
223
- return Promise.resolve('severity: warning');
224
- }
225
- return Promise.reject(new Error('file not found'));
226
- };
227
-
228
- vi.resetModules();
229
- const mod = await import('../config');
230
- loadMockPolicyConfig = mod.loadMockPolicyConfig;
231
-
232
- const config = await loadMockPolicyConfig('/fake/project');
233
-
234
- // JSON has higher priority
235
- expect(config.severity).toBe('error');
236
- });
237
- });
238
-
239
- describe('validation', () => {
240
- it('should throw on invalid version', async () => {
241
- mockExistsSync = (path: string) => path.endsWith('.mockpolicyrc');
242
- mockReadFile = () =>
243
- Promise.resolve(JSON.stringify({ version: 999 }));
244
-
245
- vi.resetModules();
246
- const mod = await import('../config');
247
- loadMockPolicyConfig = mod.loadMockPolicyConfig;
248
-
249
- await expect(loadMockPolicyConfig('/fake/project')).rejects.toThrow();
250
- });
251
-
252
- it('should throw on invalid mockPolicy value', async () => {
253
- mockExistsSync = (path: string) => path.endsWith('.mockpolicyrc');
254
- mockReadFile = () =>
255
- Promise.resolve(
256
- JSON.stringify({
257
- layers: { unit: { mockPolicy: 'invalid' } },
258
- }),
259
- );
260
-
261
- vi.resetModules();
262
- const mod = await import('../config');
263
- loadMockPolicyConfig = mod.loadMockPolicyConfig;
264
-
265
- await expect(loadMockPolicyConfig('/fake/project')).rejects.toThrow();
266
- });
267
-
268
- it('should throw on empty projectBoundary', async () => {
269
- mockExistsSync = (path: string) => path.endsWith('.mockpolicyrc');
270
- mockReadFile = () =>
271
- Promise.resolve(JSON.stringify({ projectBoundary: [] }));
272
-
273
- vi.resetModules();
274
- const mod = await import('../config');
275
- loadMockPolicyConfig = mod.loadMockPolicyConfig;
276
-
277
- await expect(loadMockPolicyConfig('/fake/project')).rejects.toThrow();
278
- });
279
-
280
- it('should throw on invalid severity', async () => {
281
- mockExistsSync = (path: string) => path.endsWith('.mockpolicyrc');
282
- mockReadFile = () =>
283
- Promise.resolve(JSON.stringify({ severity: 'critical' }));
284
-
285
- vi.resetModules();
286
- const mod = await import('../config');
287
- loadMockPolicyConfig = mod.loadMockPolicyConfig;
288
-
289
- await expect(loadMockPolicyConfig('/fake/project')).rejects.toThrow();
290
- });
291
- });
292
- });
@@ -1,379 +0,0 @@
1
- /**
2
- * @test REQ-327 mock-policy test coverage
3
- * @intent Verify gate-m3 orchestrator (runGateM3) and CLI entry (main)
4
- * @covers AC-327-01
5
- */
6
-
7
- import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
8
- import { mkdirSync, writeFileSync, rmSync } from 'fs';
9
- import { join } from 'path';
10
- import { tmpdir } from 'os';
11
- import { runGateM3, main } from '../gate-m3';
12
-
13
- // ---------------------------------------------------------------------------
14
- // Helpers
15
- // ---------------------------------------------------------------------------
16
-
17
- function createTempProject(name: string): string {
18
- const tmpDir = join(tmpdir(), `gate-m3-test-${name}-${Date.now()}`);
19
- mkdirSync(tmpDir, { recursive: true });
20
-
21
- writeFileSync(
22
- join(tmpDir, 'package.json'),
23
- JSON.stringify({
24
- dependencies: { stripe: '^12.0.0', axios: '^1.0.0' },
25
- devDependencies: { vitest: '^1.0.0' },
26
- }),
27
- );
28
-
29
- mkdirSync(join(tmpDir, 'src', '__tests__'), { recursive: true });
30
- mkdirSync(join(tmpDir, 'src', 'services'), { recursive: true });
31
-
32
- return tmpDir;
33
- }
34
-
35
- // ---------------------------------------------------------------------------
36
- // runGateM3 — orchestrator
37
- // ---------------------------------------------------------------------------
38
- describe('runGateM3', () => {
39
- let tmpDir: string;
40
-
41
- beforeEach(() => {
42
- tmpDir = createTempProject('orchestrator');
43
- });
44
-
45
- afterEach(() => {
46
- rmSync(tmpDir, { recursive: true, force: true });
47
- });
48
-
49
- it('returns skip status for empty file list', async () => {
50
- const result = await runGateM3([], tmpDir);
51
-
52
- expect(result.status).toBe('skip');
53
- expect(result.exitCode).toBe(0);
54
- expect(result.violations).toEqual([]);
55
- expect(result.scores.totalTests).toBe(0);
56
- expect(result.scores.integrationTests).toBe(0);
57
- expect(result.scores.mockDensity).toBe(0);
58
- expect(result.scores.pendingMocks).toBe(0);
59
- });
60
-
61
- it('returns skip when only non-test files are provided', async () => {
62
- const result = await runGateM3(
63
- ['src/services/user.ts', 'src/services/order.ts'],
64
- tmpDir,
65
- );
66
-
67
- expect(result.status).toBe('skip');
68
- expect(result.exitCode).toBe(0);
69
- expect(result.scores.totalTests).toBe(0);
70
- });
71
-
72
- it('filters out non-test files and processes only test files', async () => {
73
- const testFile = join(tmpDir, 'src/__tests__/user.test.ts');
74
- writeFileSync(testFile, [
75
- "import { describe, it, expect } from 'vitest';",
76
- '',
77
- "describe('user', () => {",
78
- " it('works', () => {",
79
- ' expect(true).toBe(true);',
80
- ' });',
81
- '});',
82
- ].join('\n'));
83
-
84
- const result = await runGateM3(
85
- ['src/services/user.ts', testFile],
86
- tmpDir,
87
- );
88
-
89
- expect(result.status).toBe('pass');
90
- expect(result.scores.totalTests).toBe(1);
91
- });
92
-
93
- it('counts integration tests correctly in scores', async () => {
94
- const intTest = join(tmpDir, 'src/__tests__/payment.integration.test.ts');
95
- writeFileSync(intTest, [
96
- "import Stripe from 'stripe';",
97
- '',
98
- "describe('payment', () => {",
99
- " it('creates charge', async () => {",
100
- " const s = new Stripe('sk_test');",
101
- ' });',
102
- '});',
103
- ].join('\n'));
104
-
105
- const unitTest = join(tmpDir, 'src/__tests__/helper.test.ts');
106
- writeFileSync(unitTest, [
107
- "import { describe, it, expect } from 'vitest';",
108
- '',
109
- "describe('helper', () => {",
110
- " it('works', () => {",
111
- ' expect(1 + 1).toBe(2);',
112
- ' });',
113
- '});',
114
- ].join('\n'));
115
-
116
- const result = await runGateM3([intTest, unitTest], tmpDir);
117
-
118
- expect(result.scores.totalTests).toBe(2);
119
- expect(result.scores.integrationTests).toBe(1);
120
- });
121
-
122
- it('returns pass with warning severity even when violations exist', async () => {
123
- const testFile = join(tmpDir, 'src/__tests__/payment.integration.test.ts');
124
- writeFileSync(testFile, [
125
- "import Stripe from 'stripe';",
126
- '',
127
- "describe('payment', () => {",
128
- " it('creates charge', async () => {",
129
- " const s = new Stripe('sk_test');",
130
- ' });',
131
- '});',
132
- ].join('\n'));
133
-
134
- // No .mockpolicyrc → default severity is 'warning'
135
- const result = await runGateM3([testFile], tmpDir);
136
-
137
- expect(result.status).toBe('pass');
138
- expect(result.exitCode).toBe(0);
139
- // Violations may exist but severity=warning means no block
140
- if (result.violations.length > 0) {
141
- expect(result.violations.every(v => v.severity === 'warning')).toBe(true);
142
- }
143
- });
144
-
145
- it('returns block with error severity when violations exist', async () => {
146
- writeFileSync(
147
- join(tmpDir, '.mockpolicyrc'),
148
- JSON.stringify({ severity: 'error' }),
149
- );
150
-
151
- const testFile = join(tmpDir, 'src/__tests__/payment.integration.test.ts');
152
- writeFileSync(testFile, [
153
- "import Stripe from 'stripe';",
154
- '',
155
- "describe('payment', () => {",
156
- " it('creates charge', async () => {",
157
- " const s = new Stripe('sk_test');",
158
- ' });',
159
- '});',
160
- ].join('\n'));
161
-
162
- const result = await runGateM3([testFile], tmpDir);
163
-
164
- expect(result.status).toBe('block');
165
- expect(result.exitCode).toBe(1);
166
- expect(result.violations.length).toBeGreaterThan(0);
167
- });
168
-
169
- it('returns pass with error severity when no violations exist', async () => {
170
- writeFileSync(
171
- join(tmpDir, '.mockpolicyrc'),
172
- JSON.stringify({ severity: 'error' }),
173
- );
174
-
175
- // Test file with no imports → no violations possible
176
- const testFile = join(tmpDir, 'src/__tests__/clean.test.ts');
177
- writeFileSync(testFile, [
178
- 'describe("clean", () => {',
179
- ' it("works", () => {',
180
- ' // no imports, no violations',
181
- ' });',
182
- '});',
183
- ].join('\n'));
184
-
185
- const result = await runGateM3([testFile], tmpDir);
186
-
187
- expect(result.status).toBe('pass');
188
- expect(result.exitCode).toBe(0);
189
- expect(result.violations).toHaveLength(0);
190
- });
191
-
192
- it('throws when a test file is unreadable during validation', async () => {
193
- const nonExistentTest = join(tmpDir, 'src/__tests__/missing.test.ts');
194
-
195
- await expect(runGateM3([nonExistentTest], tmpDir)).rejects.toThrow('ENOENT');
196
- });
197
-
198
- it('processes .spec.ts files as test files', async () => {
199
- const specFile = join(tmpDir, 'src/__tests__/helper.spec.ts');
200
- writeFileSync(specFile, [
201
- "import { describe, it, expect } from 'vitest';",
202
- '',
203
- "describe('helper', () => {",
204
- " it('works', () => {",
205
- ' expect(1).toBe(1);',
206
- ' });',
207
- '});',
208
- ].join('\n'));
209
-
210
- const result = await runGateM3([specFile], tmpDir);
211
-
212
- expect(result.scores.totalTests).toBe(1);
213
- expect(result.status).toBe('pass');
214
- });
215
-
216
- it('uses process.cwd() as default projectRoot when not specified', async () => {
217
- // This test verifies the default parameter behavior
218
- // We can't easily test the actual cwd behavior without side effects,
219
- // but we verify the function accepts being called without projectRoot
220
- const result = await runGateM3([]);
221
-
222
- expect(result.status).toBe('skip');
223
- expect(result.scores.totalTests).toBe(0);
224
- });
225
-
226
- it('produces correct result shape with all required fields', async () => {
227
- const result = await runGateM3([], tmpDir);
228
-
229
- expect(result).toHaveProperty('exitCode');
230
- expect(result).toHaveProperty('status');
231
- expect(result).toHaveProperty('violations');
232
- expect(result).toHaveProperty('scores');
233
- expect(result.scores).toHaveProperty('totalTests');
234
- expect(result.scores).toHaveProperty('integrationTests');
235
- expect(result.scores).toHaveProperty('mockDensity');
236
- expect(result.scores).toHaveProperty('pendingMocks');
237
- expect(typeof result.exitCode).toBe('number');
238
- expect(['pass', 'block', 'skip']).toContain(result.status);
239
- expect(Array.isArray(result.violations)).toBe(true);
240
- });
241
-
242
- it('counts pendingMocks in scores when pending violations exist', async () => {
243
- // Create an integration test that imports a non-existent module
244
- // which may be classified as pending by the scope scanner
245
- const testFile = join(tmpDir, 'src/__tests__/feature.integration.test.ts');
246
- writeFileSync(testFile, [
247
- '',
248
- "vi.mock('../services/not-implemented', () => {",
249
- ' return { doSomething: vi.fn() };',
250
- '});',
251
- '',
252
- "import { doSomething } from '../services/not-implemented';",
253
- '',
254
- "describe('feature', () => {",
255
- " it('does something', () => {",
256
- ' doSomething();',
257
- ' });',
258
- '});',
259
- ].join('\n'));
260
-
261
- const result = await runGateM3([testFile], tmpDir);
262
-
263
- // pendingMocks counts violations whose reason includes 'pending'
264
- expect(typeof result.scores.pendingMocks).toBe('number');
265
- expect(result.scores.pendingMocks).toBeGreaterThanOrEqual(0);
266
- });
267
- });
268
-
269
- // ---------------------------------------------------------------------------
270
- // main — CLI entry point
271
- // ---------------------------------------------------------------------------
272
- describe('main', () => {
273
- let tmpDir: string;
274
- let consoleErrorSpy: any;
275
- let consoleLogSpy: any;
276
-
277
- beforeEach(() => {
278
- tmpDir = createTempProject('main-cli');
279
- consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
280
- consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => { /* noop */ });
281
- });
282
-
283
- afterEach(() => {
284
- rmSync(tmpDir, { recursive: true, force: true });
285
- consoleErrorSpy.mockRestore();
286
- consoleLogSpy.mockRestore();
287
- });
288
-
289
- it('returns 1 and prints usage when no args provided', async () => {
290
- const exitCode = await main([]);
291
-
292
- expect(exitCode).toBe(1);
293
- expect(consoleErrorSpy).toHaveBeenCalledWith(
294
- expect.stringContaining('Usage:'),
295
- );
296
- });
297
-
298
- it('returns 0 for valid test files with no violations', async () => {
299
- const testFile = join(tmpDir, 'src/__tests__/clean.test.ts');
300
- writeFileSync(testFile, [
301
- "import { describe, it, expect } from 'vitest';",
302
- '',
303
- "describe('clean', () => {",
304
- " it('works', () => {",
305
- ' expect(true).toBe(true);',
306
- ' });',
307
- '});',
308
- ].join('\n'));
309
-
310
- const exitCode = await main([testFile]);
311
-
312
- expect(exitCode).toBe(0);
313
- expect(consoleLogSpy).toHaveBeenCalledWith(
314
- expect.stringContaining('Gate M3'),
315
- );
316
- });
317
-
318
- it('prints violation details when violations exist', async () => {
319
- // main() uses process.cwd(), so we cannot control config via tmpDir.
320
- // Instead, we verify that when violations are found, the "Violations:"
321
- // header is printed. With default config (severity=warning), exitCode=0.
322
- const testFile = join(tmpDir, 'src/__tests__/payment.integration.test.ts');
323
- writeFileSync(testFile, [
324
- "import Stripe from 'stripe';",
325
- '',
326
- "describe('payment', () => {",
327
- " it('creates charge', async () => {",
328
- " const s = new Stripe('sk_test');",
329
- ' });',
330
- '});',
331
- ].join('\n'));
332
-
333
- const exitCode = await main([testFile]);
334
-
335
- const logCalls: string[] = consoleLogSpy.mock.calls.map(
336
- (c: any[]) => String(c[0]),
337
- );
338
- expect(logCalls.some((msg) => msg.includes('Gate M3'))).toBe(true);
339
- expect(exitCode).toBe(0);
340
- });
341
-
342
- it('prints PASS label when no blocking violations', async () => {
343
- const testFile = join(tmpDir, 'src/__tests__/clean.test.ts');
344
- writeFileSync(testFile, [
345
- "import { describe, it, expect } from 'vitest';",
346
- '',
347
- "describe('clean', () => {",
348
- " it('works', () => {",
349
- ' expect(true).toBe(true);',
350
- ' });',
351
- '});',
352
- ].join('\n'));
353
-
354
- await main([testFile]);
355
-
356
- const logCalls: string[] = consoleLogSpy.mock.calls.map(
357
- (c: any[]) => String(c[0]),
358
- );
359
- const hasPassLabel = logCalls.some(
360
- (msg) => msg.includes('PASS'),
361
- );
362
- expect(hasPassLabel).toBe(true);
363
- });
364
-
365
- it('prints file count in header', async () => {
366
- const testFile = join(tmpDir, 'src/__tests__/clean.test.ts');
367
- writeFileSync(testFile, "import { describe } from 'vitest';");
368
-
369
- await main([testFile]);
370
-
371
- const logCalls: string[] = consoleLogSpy.mock.calls.map(
372
- (c: any[]) => String(c[0]),
373
- );
374
- const hasFileCount = logCalls.some(
375
- (msg) => msg.includes('Changed files: 1'),
376
- );
377
- expect(hasFileCount).toBe(true);
378
- });
379
- });