@boyingliu01/xp-gate 0.38.75 → 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.
- package/lib/__tests__/mirror-sync.test.js +387 -5
- package/lib/mattpocock-license-verdict.json +1 -1
- package/lib/mirror-sync.js +202 -15
- package/package.json +1 -1
- package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
- package/plugins/claude-code/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/claude-code/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/claude-code/skills/test-specification-alignment/AGENTS.md +1 -1
- package/plugins/dsh/lib/hooks.d.ts +10 -1
- package/plugins/dsh/lib/hooks.d.ts.map +1 -1
- package/plugins/dsh/lib/hooks.js +56 -5
- package/plugins/dsh/lib/index.js +6 -1
- package/plugins/dsh/package.json +1 -1
- package/plugins/dsh/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/dsh/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/dsh/skills/test-specification-alignment/AGENTS.md +1 -1
- package/plugins/dsh/src/hooks.test.ts +70 -0
- package/plugins/dsh/src/hooks.ts +66 -5
- package/plugins/dsh/src/index.ts +6 -1
- package/plugins/opencode/package.json +1 -1
- package/plugins/opencode/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/opencode/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/opencode/skills/test-specification-alignment/AGENTS.md +1 -1
- package/plugins/qoder/plugin.json +1 -1
- package/plugins/qoder/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/qoder/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/qoder/skills/test-specification-alignment/AGENTS.md +1 -1
- package/scripts/generate-mirrors.cjs +10 -0
- package/skills/delphi-review/AGENTS.md +1 -1
- package/skills/sprint-flow/AGENTS.md +1 -1
- package/skills/test-specification-alignment/AGENTS.md +1 -1
- package/mock-policy/__tests__/config.test.ts +0 -292
- package/mock-policy/__tests__/gate-m3.test.ts +0 -379
- package/mock-policy/__tests__/integration.test.ts +0 -600
- package/mock-policy/__tests__/mock-decision-engine.test.ts +0 -231
- package/mock-policy/__tests__/schema.test.ts +0 -239
- package/mock-policy/__tests__/scope-scanner.test.ts +0 -541
- package/mutation/__tests__/detect-ai-test.test.ts +0 -152
- package/mutation/__tests__/gate-m-survivors.test.ts +0 -167
- package/mutation/__tests__/gate-m.test.ts +0 -246
- package/mutation/__tests__/go-mutant-runner.test.ts +0 -288
- package/mutation/__tests__/gremlins-runner.test.ts +0 -167
- package/mutation/__tests__/init-baseline.test.ts +0 -347
- package/mutation/__tests__/mutation-mirror.test.ts +0 -49
- package/mutation/__tests__/mutmut-runner.test.ts +0 -1049
- package/mutation/__tests__/pitest-gradle-availability.test.ts +0 -74
- package/mutation/__tests__/pitest-runner.test.ts +0 -574
- package/mutation/__tests__/runners-index.test.ts +0 -162
- package/mutation/__tests__/stryker-runner.test.ts +0 -455
- package/mutation/__tests__/test-filter-parity.test.ts +0 -40
- package/mutation/__tests__/update-baseline.test.ts +0 -395
- package/principles/__tests__/adapter-metrics.test.ts +0 -292
- package/principles/__tests__/analyze-read-failure.test.ts +0 -70
- package/principles/__tests__/analyzer.test.ts +0 -367
- package/principles/__tests__/baseline-entry-total-warnings.test.ts +0 -76
- package/principles/__tests__/baseline-prune-timeout.test.ts +0 -71
- package/principles/__tests__/baseline-prune.test.ts +0 -399
- package/principles/__tests__/baseline-storage.test.ts +0 -188
- package/principles/__tests__/baseline.test.ts +0 -330
- package/principles/__tests__/boy-scout-init-baseline.test.ts +0 -167
- package/principles/__tests__/boy-scout-integration.test.ts +0 -355
- package/principles/__tests__/boy-scout.test.ts +0 -893
- package/principles/__tests__/cli-files-separators.test.ts +0 -69
- package/principles/__tests__/config-arch-group.test.ts +0 -106
- package/principles/__tests__/config-enforcement.test.ts +0 -470
- package/principles/__tests__/config-git-timeout.test.ts +0 -66
- package/principles/__tests__/config-merge-warnings.test.ts +0 -148
- package/principles/__tests__/config.test.ts +0 -78
- package/principles/__tests__/direct-execution.test.ts +0 -187
- package/principles/__tests__/index.test.ts +0 -94
- package/principles/__tests__/issue-587-java-member-count.test.ts +0 -279
- package/principles/__tests__/issue-588-lsp-java-false-positives.test.ts +0 -213
- package/principles/__tests__/lint-baseline.test.ts +0 -314
- package/principles/__tests__/many-exports-reexport.test.ts +0 -40
- package/principles/__tests__/reporter.test.ts +0 -660
- package/principles/__tests__/types.test.ts +0 -103
- package/principles/__tests__/warnings-baseline-budgets.test.ts +0 -74
- package/principles/adapters/__tests__/base.test.ts +0 -95
- package/principles/adapters/__tests__/cpp-code-block.test.ts +0 -123
- package/principles/adapters/__tests__/cpp-scanner-equivalence.test.ts +0 -67
- package/principles/adapters/__tests__/cpp.test.ts +0 -290
- package/principles/adapters/__tests__/dart.test.ts +0 -84
- package/principles/adapters/__tests__/go.test.ts +0 -68
- package/principles/adapters/__tests__/java.test.ts +0 -385
- package/principles/adapters/__tests__/kotlin.test.ts +0 -76
- package/principles/adapters/__tests__/objectivec.test.ts +0 -453
- package/principles/adapters/__tests__/python.test.ts +0 -84
- package/principles/adapters/__tests__/swift.test.ts +0 -68
- package/principles/adapters/__tests__/typescript-block-comment-lines.test.ts +0 -53
- package/principles/adapters/__tests__/typescript-masking.test.ts +0 -96
- package/principles/adapters/__tests__/typescript.test.ts +0 -116
- package/principles/rules/__tests__/clean-code/code-duplication.test.ts +0 -85
- package/principles/rules/__tests__/clean-code/deep-nesting.test.ts +0 -73
- package/principles/rules/__tests__/clean-code/god-class.test.ts +0 -109
- package/principles/rules/__tests__/clean-code/large-file.test.ts +0 -73
- package/principles/rules/__tests__/clean-code/long-function.test.ts +0 -81
- package/principles/rules/__tests__/clean-code/magic-numbers.test.ts +0 -68
- package/principles/rules/__tests__/clean-code/many-exports.test.ts +0 -123
- package/principles/rules/__tests__/clean-code/missing-error-handling.test.ts +0 -171
- package/principles/rules/__tests__/clean-code/too-many-params.test.ts +0 -58
- package/principles/rules/__tests__/clean-code/unused-imports.test.ts +0 -107
- package/principles/rules/__tests__/index.test.ts +0 -49
- package/principles/rules/__tests__/solid/dip.test.ts +0 -119
- package/principles/rules/__tests__/solid/isp.test.ts +0 -58
- package/principles/rules/__tests__/solid/issue-588-lsp-base-contract.test.ts +0 -252
- package/principles/rules/__tests__/solid/lsp.test.ts +0 -186
- package/principles/rules/__tests__/solid/ocp.test.ts +0 -67
- package/principles/rules/__tests__/solid/srp.test.ts +0 -87
- package/principles/rules/arch/__tests__/dependency.test.ts +0 -180
- package/principles/rules/arch/__tests__/naming.test.ts +0 -133
- package/principles/rules/arch/__tests__/shared.test.ts +0 -113
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @test types.ts - Core type definitions
|
|
3
|
-
* @intent Verify Rule and Violation interfaces match design spec
|
|
4
|
-
* @covers clean-code-solid-checker-design Section 6-7
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { describe, it, expect } from 'vitest';
|
|
8
|
-
|
|
9
|
-
// These imports will fail until implementation exists - that's the RED phase!
|
|
10
|
-
import type { Rule, Violation, Severity, Adapter } from '../types';
|
|
11
|
-
|
|
12
|
-
describe('types.ts - Core Interfaces', () => {
|
|
13
|
-
describe('Rule interface', () => {
|
|
14
|
-
it('should have required fields: id, name, threshold, severity, check', () => {
|
|
15
|
-
// This test will fail until types.ts exists
|
|
16
|
-
const rule: Rule = {
|
|
17
|
-
id: 'clean-code.long-function',
|
|
18
|
-
name: 'Long Function',
|
|
19
|
-
threshold: 50,
|
|
20
|
-
severity: 'warning',
|
|
21
|
-
check: () => [],
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
expect(rule.id).toBeDefined();
|
|
25
|
-
expect(rule.name).toBeDefined();
|
|
26
|
-
expect(rule.threshold).toBeDefined();
|
|
27
|
-
expect(rule.severity).toBeDefined();
|
|
28
|
-
expect(rule.check).toBeDefined();
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('should support all severity levels', () => {
|
|
32
|
-
const severities: Severity[] = ['error', 'warning', 'info'];
|
|
33
|
-
|
|
34
|
-
expect(severities).toContain('error');
|
|
35
|
-
expect(severities).toContain('warning');
|
|
36
|
-
expect(severities).toContain('info');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('should have correct rule ID format', () => {
|
|
40
|
-
// Rule IDs should follow pattern: category.rule-name
|
|
41
|
-
const validIds = [
|
|
42
|
-
'clean-code.long-function',
|
|
43
|
-
'clean-code.magic-numbers',
|
|
44
|
-
'solid.srp',
|
|
45
|
-
'solid.dip',
|
|
46
|
-
];
|
|
47
|
-
|
|
48
|
-
validIds.forEach(id => {
|
|
49
|
-
expect(id).toMatch(/^[a-z-]+\.[a-z-]+$/);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe('Violation interface', () => {
|
|
55
|
-
it('should have required fields: file, line, ruleId, message, severity', () => {
|
|
56
|
-
const violation: Violation = {
|
|
57
|
-
file: 'src/test.ts',
|
|
58
|
-
line: 42,
|
|
59
|
-
ruleId: 'clean-code.long-function',
|
|
60
|
-
message: 'Function exceeds 50 lines',
|
|
61
|
-
severity: 'warning',
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
expect(violation.file).toBeDefined();
|
|
65
|
-
expect(violation.line).toBeDefined();
|
|
66
|
-
expect(violation.ruleId).toBeDefined();
|
|
67
|
-
expect(violation.message).toBeDefined();
|
|
68
|
-
expect(violation.severity).toBeDefined();
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('should support optional column field', () => {
|
|
72
|
-
const violationWithColumn: Violation = {
|
|
73
|
-
file: 'src/test.ts',
|
|
74
|
-
line: 42,
|
|
75
|
-
column: 10,
|
|
76
|
-
ruleId: 'clean-code.magic-numbers',
|
|
77
|
-
message: 'Magic number 47 detected',
|
|
78
|
-
severity: 'info',
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
expect(violationWithColumn.column).toBe(10);
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
describe('Adapter interface', () => {
|
|
86
|
-
it('should have required methods: detectLanguage, parseAST, extractFunctions', () => {
|
|
87
|
-
// This will fail until Adapter interface is defined
|
|
88
|
-
const adapter: Adapter = {
|
|
89
|
-
detectLanguage: () => 'typescript',
|
|
90
|
-
parseAST: () => null,
|
|
91
|
-
extractFunctions: () => [],
|
|
92
|
-
extractClasses: () => [],
|
|
93
|
-
extractExports: () => [],
|
|
94
|
-
countLines: () => 0,
|
|
95
|
-
getDuplication: () => ({ percentage: 0, blocks: [] }), declaresRuleCoverage: true,
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
expect(adapter.detectLanguage).toBeDefined();
|
|
99
|
-
expect(adapter.parseAST).toBeDefined();
|
|
100
|
-
expect(adapter.extractFunctions).toBeDefined();
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
});
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @test REQ-452
|
|
3
|
-
* @intent 验证 .warnings-baseline.json 里每一条基线都仍对应"该文件当前确有告警",
|
|
4
|
-
* 否则它就是一份幽灵额度(Gate 6 允许告警数上升而无人报警)
|
|
5
|
-
* @covers AC-452-15
|
|
6
|
-
*
|
|
7
|
-
* Why this is a property and not a snapshot: an entry is the Boy Scout Rule's
|
|
8
|
-
* budget for one file. `pruneBaselineEntries` (#452) drops entries whose FILE has
|
|
9
|
-
* disappeared, which is the wrong key -- a file that stayed and had its warnings
|
|
10
|
-
* fixed keeps a budget of N forever, and the next edit that adds N warnings is
|
|
11
|
-
* "within baseline". That is not a hypothetical: the #457 branch recorded
|
|
12
|
-
* `config.ts: 1` and `config-enforcement.test.ts: 2` when those files carried
|
|
13
|
-
* warnings, then removed the warnings and left the entries behind (walkthrough
|
|
14
|
-
* FC-13).
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { readFileSync } from 'node:fs';
|
|
18
|
-
import { join } from 'node:path';
|
|
19
|
-
import { describe, expect, it } from 'vitest';
|
|
20
|
-
import { analyzeWarningsForFiles } from '../boy-scout';
|
|
21
|
-
|
|
22
|
-
const REPO_ROOT = process.cwd();
|
|
23
|
-
const BASELINE = join(REPO_ROOT, '.warnings-baseline.json');
|
|
24
|
-
|
|
25
|
-
interface BaselineEntry {
|
|
26
|
-
totalWarnings: number;
|
|
27
|
-
lastAnalyzed: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function readBaseline(): Record<string, BaselineEntry> {
|
|
31
|
-
let raw: unknown;
|
|
32
|
-
try {
|
|
33
|
-
raw = JSON.parse(readFileSync(BASELINE, 'utf8'));
|
|
34
|
-
} catch (err) {
|
|
35
|
-
// A missing or corrupt baseline is a fixture failure, not a property failure:
|
|
36
|
-
// without this the suite reports "cannot read properties of undefined".
|
|
37
|
-
throw new Error(`Cannot read the warnings baseline at ${BASELINE}: ${String(err)}`);
|
|
38
|
-
}
|
|
39
|
-
const entries = raw as Record<string, unknown>;
|
|
40
|
-
// The on-disk shape has no wrapper object (boy-scout reads the top level as the
|
|
41
|
-
// file map); assert that here so a format change fails this test rather than
|
|
42
|
-
// scanning zero entries and passing.
|
|
43
|
-
expect(Object.keys(entries).length, 'the baseline must not be empty').toBeGreaterThan(0);
|
|
44
|
-
return entries as Record<string, BaselineEntry>;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
describe('the warnings baseline budgets only warnings that exist', () => {
|
|
48
|
-
it('AC-452-15: no entry grants a budget to a file that currently has zero warnings', async () => {
|
|
49
|
-
const baseline = readBaseline();
|
|
50
|
-
const files = Object.keys(baseline);
|
|
51
|
-
const current = await analyzeWarningsForFiles(files);
|
|
52
|
-
const phantom = files.filter((file) => (current[file] ?? 0) === 0);
|
|
53
|
-
|
|
54
|
-
expect(
|
|
55
|
-
phantom,
|
|
56
|
-
`These files carry a baseline budget but have no warnings today: ${phantom.join(', ')}. ` +
|
|
57
|
-
'Delete their entries -- an unused budget is permission to add warnings later.',
|
|
58
|
-
).toEqual([]);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('AC-452-15: no entry overstates the count it tracks', async () => {
|
|
62
|
-
const baseline = readBaseline();
|
|
63
|
-
const files = Object.keys(baseline);
|
|
64
|
-
const current = await analyzeWarningsForFiles(files);
|
|
65
|
-
// A count above today's total is the same defect in a milder form: the extra
|
|
66
|
-
// margin is a budget nobody granted.
|
|
67
|
-
for (const file of files) {
|
|
68
|
-
expect(
|
|
69
|
-
baseline[file].totalWarnings,
|
|
70
|
-
`${file} baseline is above its current warning count`,
|
|
71
|
-
).toBeLessThanOrEqual(current[file] ?? 0);
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
});
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, type Mock } from 'vitest';;
|
|
2
|
-
import type { Adapter } from '../../types';
|
|
3
|
-
import { BaseAdapter } from '../base';
|
|
4
|
-
|
|
5
|
-
class MockAdapter extends BaseAdapter {
|
|
6
|
-
parseAST(): unknown {
|
|
7
|
-
return { content: this.fileContent, astType: 'mock' };
|
|
8
|
-
}
|
|
9
|
-
extractFunctions(): unknown[] {
|
|
10
|
-
return [];
|
|
11
|
-
}
|
|
12
|
-
extractClasses(): unknown[] {
|
|
13
|
-
return [];
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
vi.mock('fs', () => ({
|
|
18
|
-
readFileSync: vi.fn(),
|
|
19
|
-
existsSync: vi.fn(() => true),
|
|
20
|
-
}));
|
|
21
|
-
|
|
22
|
-
import { readFileSync } from 'fs';
|
|
23
|
-
|
|
24
|
-
describe('BaseAdapter - Default Implementation', () => {
|
|
25
|
-
|
|
26
|
-
it('should implement the Adapter interface', () => {
|
|
27
|
-
(readFileSync as Mock).mockReturnValue('mock file content\ntest line 2');
|
|
28
|
-
const adapter = new MockAdapter('./test.ts');
|
|
29
|
-
const implemented: Adapter = adapter as unknown as Adapter;
|
|
30
|
-
|
|
31
|
-
expect(implemented).toBeDefined();
|
|
32
|
-
expect(implemented.detectLanguage).toBeDefined();
|
|
33
|
-
expect(implemented.parseAST).toBeDefined();
|
|
34
|
-
expect(implemented.extractFunctions).toBeDefined();
|
|
35
|
-
expect(implemented.extractClasses).toBeDefined();
|
|
36
|
-
expect(implemented.countLines).toBeDefined();
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('should detect language based on file extension', () => {
|
|
40
|
-
(readFileSync as Mock).mockReturnValue('file content');
|
|
41
|
-
|
|
42
|
-
const adapterTs = new MockAdapter('./test.ts');
|
|
43
|
-
expect(adapterTs.detectLanguage()).toBe('typescript');
|
|
44
|
-
|
|
45
|
-
const adapterTsx = new MockAdapter('./test.tsx');
|
|
46
|
-
expect(adapterTsx.detectLanguage()).toBe('typescript');
|
|
47
|
-
|
|
48
|
-
const adapterJs = new MockAdapter('./test.js');
|
|
49
|
-
expect(adapterJs.detectLanguage()).toBe('javascript');
|
|
50
|
-
|
|
51
|
-
const adapterPy = new MockAdapter('./test.py');
|
|
52
|
-
expect(adapterPy.detectLanguage()).toBe('python');
|
|
53
|
-
|
|
54
|
-
const adapterUnknown = new MockAdapter('./test.unknown');
|
|
55
|
-
expect(adapterUnknown.detectLanguage()).toBe('unknown');
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('should parse file AST correctly', () => {
|
|
59
|
-
(readFileSync as Mock).mockReturnValue('mock file content\ntest line 2');
|
|
60
|
-
const adapter = new MockAdapter('./test.ts');
|
|
61
|
-
const ast = adapter.parseAST();
|
|
62
|
-
expect(ast).toEqual({ content: 'mock file content\ntest line 2', astType: 'mock' });
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('should extract functions from AST', () => {
|
|
66
|
-
(readFileSync as Mock).mockReturnValue('');
|
|
67
|
-
const adapter = new MockAdapter('./test.ts');
|
|
68
|
-
const functions = adapter.extractFunctions();
|
|
69
|
-
expect(Array.isArray(functions)).toBe(true);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should extract classes from AST', () => {
|
|
73
|
-
(readFileSync as Mock).mockReturnValue('');
|
|
74
|
-
const adapter = new MockAdapter('./test.ts');
|
|
75
|
-
const classes = adapter.extractClasses();
|
|
76
|
-
expect(Array.isArray(classes)).toBe(true);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('should count physical lines correctly', () => {
|
|
80
|
-
(readFileSync as Mock).mockReturnValue('line 1\nline 2\nline 3');
|
|
81
|
-
const adapter = new MockAdapter('./test.ts');
|
|
82
|
-
const lineCount = adapter.countLines();
|
|
83
|
-
expect(lineCount).toBe(3);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('should throw error for unsupported file operations', () => {
|
|
87
|
-
(readFileSync as Mock).mockImplementation(() => {
|
|
88
|
-
throw new Error('Could not read file');
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
expect(() => {
|
|
92
|
-
new MockAdapter('./nonexistent-file.txt');
|
|
93
|
-
}).toThrow('Could not read file:');
|
|
94
|
-
});
|
|
95
|
-
});
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @test REQ-620
|
|
3
|
-
* @intent Pin the C/C++ code-block scanner's treatment of comments and string
|
|
4
|
-
* literals before its CCN-28 body is split.
|
|
5
|
-
*
|
|
6
|
-
* Every case is built to DEFEAT the fallback path. `extractCodeBlock`
|
|
7
|
-
* falls back to `substring(startPos)` capped at 200 characters, so a
|
|
8
|
-
* short fixture returns "the whole thing" even when the scanner never
|
|
9
|
-
* found a closing brace -- the assertions then pass on a broken scanner.
|
|
10
|
-
* Each source below therefore pads past 200 characters and asserts the
|
|
11
|
-
* exact end position, so a truncated scan cannot look correct.
|
|
12
|
-
*
|
|
13
|
-
* Verified by injecting three real defects (brace tracker seeing into
|
|
14
|
-
* strings, block comments untracked, backslash parity ignored): each one
|
|
15
|
-
* reddens at least one case here.
|
|
16
|
-
* @covers AC-620-03
|
|
17
|
-
*/
|
|
18
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
19
|
-
import { CppAdapter } from '../cpp';
|
|
20
|
-
|
|
21
|
-
vi.mock('fs', () => ({
|
|
22
|
-
readFileSync: vi.fn(),
|
|
23
|
-
existsSync: vi.fn(() => true),
|
|
24
|
-
}));
|
|
25
|
-
|
|
26
|
-
import { readFileSync } from 'fs';
|
|
27
|
-
|
|
28
|
-
const MARKER = 'END_OF_FUNCTION';
|
|
29
|
-
/** Padding that keeps the source well past the 200-char fallback cap. */
|
|
30
|
-
const PAD = ' '.repeat(240);
|
|
31
|
-
|
|
32
|
-
function block(src: string, start = 0): string {
|
|
33
|
-
(readFileSync as unknown as { mockReturnValue: (v: string) => void }).mockReturnValue(src);
|
|
34
|
-
const adapter = new CppAdapter('probe.cpp');
|
|
35
|
-
return adapter.extractCodeBlock(start);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Build `void f() { <body> }` followed by `<MARKER>`, padded past the fallback
|
|
40
|
-
* cap. The padding sits INSIDE the function body so the fallback (which runs to
|
|
41
|
-
* the 200-char cap) would include the marker, while a correct scan stops at the
|
|
42
|
-
* real closing brace and does not.
|
|
43
|
-
*/
|
|
44
|
-
function wrap(body: string): { src: string; open: number } {
|
|
45
|
-
const src = `void f() {\n${body}\n${PAD}\n}${MARKER}`;
|
|
46
|
-
return { src, open: src.indexOf('{') };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
describe('AC-620-03: the C/C++ block scanner ignores braces in comments and strings', () => {
|
|
50
|
-
it('returns exactly the balanced body of a plain function', () => {
|
|
51
|
-
const { src, open } = wrap(' int a = 1;');
|
|
52
|
-
const out = block(src, open);
|
|
53
|
-
expect(out, `tail=${JSON.stringify(out.slice(-60))} len=${out.length}`).toMatch(/}$/);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('does not close on a brace inside a line comment', () => {
|
|
57
|
-
const { src, open } = wrap(' // } not the end\n int a = 1;');
|
|
58
|
-
const out = block(src, open);
|
|
59
|
-
expect(out, `extracted: ${JSON.stringify(out.slice(0, 80))}`).toContain('int a = 1;');
|
|
60
|
-
expect(out).not.toContain(MARKER);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('does not close on a brace inside a block comment', () => {
|
|
64
|
-
const { src, open } = wrap(' /* } not the end */\n int a = 1;');
|
|
65
|
-
const out = block(src, open);
|
|
66
|
-
expect(out).toContain('int a = 1;');
|
|
67
|
-
expect(out).not.toContain(MARKER);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('does not close on a brace inside a string literal', () => {
|
|
71
|
-
const { src, open } = wrap(' const char* s = "}";\n int a = 1;');
|
|
72
|
-
const out = block(src, open);
|
|
73
|
-
expect(out).toContain('int a = 1;');
|
|
74
|
-
expect(out).not.toContain(MARKER);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('keeps scanning past a string that CONTAINS the only early close brace', () => {
|
|
78
|
-
// The string's `}` is the first `}` after the opener. A scanner that counts
|
|
79
|
-
// it ends the block immediately and never reaches the statements below.
|
|
80
|
-
const { src, open } = wrap(' const char* s = "}";\n int markerStatement = 1;\n int tail = 2;');
|
|
81
|
-
const out = block(src, open);
|
|
82
|
-
expect(out, `extracted: ${JSON.stringify(out.slice(0, 120))}`)
|
|
83
|
-
.toContain('int tail = 2;');
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('keeps scanning past a char literal that contains a close brace', () => {
|
|
87
|
-
const { src, open } = wrap(" char c = '}';\n int tail = 2;");
|
|
88
|
-
const out = block(src, open);
|
|
89
|
-
expect(out).toContain('int tail = 2;');
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('does not close on a brace inside a char literal', () => {
|
|
93
|
-
const { src, open } = wrap(" char c = '}';\n int a = 1;");
|
|
94
|
-
const out = block(src, open);
|
|
95
|
-
expect(out).toContain('int a = 1;');
|
|
96
|
-
expect(out).not.toContain(MARKER);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('treats an escaped quote inside a string as data, not a terminator', () => {
|
|
100
|
-
// `\"` does not close the string, so the `}` after it is still string data.
|
|
101
|
-
const { src, open } = wrap(' const char* s = "a\\"}b";\n int a = 1;');
|
|
102
|
-
const out = block(src, open);
|
|
103
|
-
expect(out).toContain('int a = 1;');
|
|
104
|
-
expect(out).not.toContain(MARKER);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('treats an even run of backslashes as NOT escaping the quote', () => {
|
|
108
|
-
// `\\` is a literal backslash, so the following `"` DOES close the string,
|
|
109
|
-
// and the `}` that ends the line is real structure.
|
|
110
|
-
const { src, open } = wrap(' const char* s = "a\\\\";\n int a = 1;');
|
|
111
|
-
const out = block(src, open);
|
|
112
|
-
expect(out.trimEnd().endsWith('}')).toBe(true);
|
|
113
|
-
expect(out).toContain('int a = 1;');
|
|
114
|
-
expect(out).not.toContain(MARKER);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('falls back when no balanced block is ever found', () => {
|
|
118
|
-
const src = 'void f() {\n int a = 1;' + PAD;
|
|
119
|
-
const out = block(src, 9);
|
|
120
|
-
expect(out.length).toBe(200);
|
|
121
|
-
expect(out.length).toBeLessThan(src.length);
|
|
122
|
-
});
|
|
123
|
-
});
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @test REQ-620
|
|
3
|
-
* @intent Prove the C/C++ block scanner split is behaviour-preserving on real
|
|
4
|
-
* source, not just on the handful of fixtures above. The scanner feeds
|
|
5
|
-
* every C/C++ metric (length, nesting, io operations), so an off-by-one
|
|
6
|
-
* in the refactored loop would silently shift every reported span.
|
|
7
|
-
* @covers AC-620-03
|
|
8
|
-
*/
|
|
9
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
10
|
-
import { CppAdapter } from '../cpp';
|
|
11
|
-
|
|
12
|
-
vi.mock('fs', () => ({
|
|
13
|
-
readFileSync: vi.fn(),
|
|
14
|
-
existsSync: vi.fn(() => true),
|
|
15
|
-
}));
|
|
16
|
-
|
|
17
|
-
import { readFileSync } from 'fs';
|
|
18
|
-
|
|
19
|
-
const SOURCE = [
|
|
20
|
-
'#include <stdio.h>',
|
|
21
|
-
'/* a header comment with a } brace */',
|
|
22
|
-
'int helper(int x) {',
|
|
23
|
-
' // a line comment with a } brace',
|
|
24
|
-
' return x * 2;',
|
|
25
|
-
'}',
|
|
26
|
-
'',
|
|
27
|
-
'int main(void) {',
|
|
28
|
-
' const char* msg = "a } inside a string";',
|
|
29
|
-
" char c = '}';",
|
|
30
|
-
' /* block } comment */',
|
|
31
|
-
' if (helper(1) > 0) {',
|
|
32
|
-
' printf("%s", msg);',
|
|
33
|
-
' }',
|
|
34
|
-
' return 0;',
|
|
35
|
-
'}',
|
|
36
|
-
].join('\n');
|
|
37
|
-
|
|
38
|
-
describe('AC-620-03: the split scanner finds the same spans as the original', () => {
|
|
39
|
-
it('extracts main() up to its real closing brace', () => {
|
|
40
|
-
(readFileSync as unknown as { mockReturnValue: (v: string) => void }).mockReturnValue(SOURCE);
|
|
41
|
-
const adapter = new CppAdapter('probe.c');
|
|
42
|
-
const start = SOURCE.indexOf('int main(void)');
|
|
43
|
-
const open = SOURCE.indexOf('{', start);
|
|
44
|
-
const out = adapter.extractCodeBlock(open);
|
|
45
|
-
|
|
46
|
-
// The body must run to the LAST brace of main, not to the `}` in a comment
|
|
47
|
-
// or a string. Line count is the cheap proxy for "did not truncate".
|
|
48
|
-
const bodyLines = out.split('\n').length;
|
|
49
|
-
expect(bodyLines, `extracted: ${JSON.stringify(out)}`).toBe(
|
|
50
|
-
SOURCE.slice(open).split('\n').length,
|
|
51
|
-
);
|
|
52
|
-
expect(out).toContain('return 0;');
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('every declaration in the file yields a span that ends on a balanced brace', () => {
|
|
56
|
-
(readFileSync as unknown as { mockReturnValue: (v: string) => void }).mockReturnValue(SOURCE);
|
|
57
|
-
const adapter = new CppAdapter('probe.c');
|
|
58
|
-
const fns = adapter.extractFunctions() as Array<{ name: string; code?: string }>;
|
|
59
|
-
expect(fns.length).toBeGreaterThanOrEqual(2);
|
|
60
|
-
for (const fn of fns) {
|
|
61
|
-
if (!fn.code) continue;
|
|
62
|
-
// Balanced braces modulo the literals/comments the scanner masks.
|
|
63
|
-
expect(fn.code.length, `${fn.name} produced an empty or unbounded span`)
|
|
64
|
-
.toBeLessThanOrEqual(SOURCE.length);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
});
|