@boyingliu01/xp-gate 0.38.126 → 0.39.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -0
- package/adapters/gate-3.sh +13 -2
- package/adapters/python.sh +36 -38
- package/bin/xp-gate.js +11 -0
- package/build-integrity/gate-10-lines.ts +98 -0
- package/build-integrity/gate-10.ts +98 -109
- package/build-integrity/quote-masker.ts +17 -3
- package/gate-3.sh +13 -2
- package/gates/common-process.ts +116 -34
- package/gates/common.ts +51 -15
- package/gates/gate-3.ts +166 -61
- package/gates/gate-8.ts +76 -24
- package/gates/gate-9.ts +276 -82
- package/gates/pbt-detect.ts +70 -39
- package/gates/python-health.ts +236 -98
- package/gates/test-layers.ts +103 -30
- package/hooks/gate-3.sh +13 -2
- package/hooks/gate-entries/gate-1-entry.sh +10 -1
- package/hooks/gate-entries/gate-5-entry.sh +5 -1
- package/hooks/lib/validate-code-walkthrough.cjs +324 -99
- package/hooks/pre-commit +52 -0
- package/hooks/pre-push +54 -3
- package/lib/agent-runtime-journal.js +104 -37
- package/lib/arch.js +6 -0
- package/lib/baseline.js +329 -201
- package/lib/check.js +38 -2
- package/lib/doctor-constants.js +43 -0
- package/lib/doctor.js +2008 -1001
- package/lib/flaky.js +178 -79
- package/lib/gate-runner.js +110 -63
- package/lib/init.js +6 -0
- package/lib/language-capability-surfaces.js +5 -2
- package/lib/language-registry.js +608 -0
- package/lib/language-tools.js +487 -1018
- package/lib/phase-transition.js +646 -446
- package/lib/prepush-evidence.js +174 -0
- package/lib/sprint-audit.js +52 -22
- package/lib/sprint-discovery.js +66 -35
- package/lib/sprint-format.js +114 -0
- package/lib/sprint-init.js +372 -118
- package/lib/sprint-status.js +134 -90
- package/lib/star-notice.js +346 -0
- package/lib/tool-display.js +52 -0
- package/lib/tool-install.js +179 -0
- package/lib/uninstall.js +331 -188
- package/mock-policy/gate-m3.ts +74 -23
- package/mock-policy/mock-decision-engine.ts +21 -7
- package/mock-policy/scope-scanner.ts +128 -69
- package/mutation/detect-ai-test.ts +20 -12
- package/mutation/gate-m.ts +6 -2
- package/mutation/init-baseline.ts +49 -13
- package/mutation/runners/go-mutant-runner.ts +7 -49
- package/mutation/runners/gremlins-runner.ts +53 -18
- package/mutation/runners/mutmut-runner.ts +455 -163
- package/mutation/runners/pitest-runner.ts +85 -131
- package/mutation/runners/spawn-with-capture.ts +304 -0
- package/mutation/runners/stryker-runner.ts +14 -55
- package/mutation/update-baseline.ts +25 -18
- package/package.json +20 -8
- package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
- package/plugins/claude-code/bin/delphi-review-guard.sh +12 -61
- package/plugins/claude-code/skills/delphi-review/.delphi-config.json.example +6 -11
- 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/package.json +1 -1
- package/plugins/dsh/scripts/prepack.cjs +43 -18
- package/plugins/dsh/scripts/verify-skill-provider.mjs +39 -14
- package/plugins/dsh/skills/delphi-review/.delphi-config.json.example +6 -11
- 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/opencode/index.ts +337 -123
- package/plugins/opencode/package.json +1 -1
- package/plugins/opencode/scripts/prepack.cjs +41 -16
- package/plugins/opencode/skills/delphi-review/.delphi-config.json.example +6 -11
- 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/bin/sprint-flow-guard.sh +12 -60
- package/plugins/qoder/plugin.json +1 -1
- package/plugins/qoder/skills/delphi-review/.delphi-config.json.example +6 -11
- 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/plugins/shared/README.md +3 -0
- package/plugins/shared/bin/delphi-review-guard.sh +68 -0
- package/principles/adapters/control-nesting.ts +481 -168
- package/principles/adapters/cpp.ts +61 -22
- package/principles/adapters/csharp.ts +183 -66
- package/principles/adapters/dart.ts +11 -1
- package/principles/adapters/duplication.ts +49 -16
- package/principles/adapters/import-clause.ts +62 -15
- package/principles/adapters/java.ts +199 -70
- package/principles/adapters/line-comment.ts +18 -0
- package/principles/adapters/member-scan.ts +50 -8
- package/principles/adapters/php.ts +232 -103
- package/principles/adapters/python.ts +25 -18
- package/principles/adapters/quote-state.ts +21 -8
- package/principles/adapters/typescript-mask.ts +276 -0
- package/principles/adapters/typescript.ts +206 -200
- package/principles/analyzer.ts +16 -6
- package/principles/baseline-prune.ts +18 -8
- package/principles/baseline.ts +28 -11
- package/principles/boy-scout-usage.ts +26 -0
- package/principles/boy-scout.ts +261 -180
- package/principles/config.ts +183 -57
- package/principles/direct-execution.ts +24 -12
- package/principles/lint-baseline.ts +235 -122
- package/principles/rules/arch/dependency.ts +34 -25
- package/principles/rules/arch/shared.ts +24 -20
- package/principles/rules/clean-code/deep-nesting.ts +23 -13
- package/principles/rules/clean-code/god-class.ts +67 -41
- package/principles/rules/clean-code/large-file.ts +30 -23
- package/principles/rules/clean-code/long-function.ts +51 -37
- package/principles/rules/clean-code/magic-numbers.ts +51 -36
- package/principles/rules/clean-code/many-exports.ts +46 -29
- package/principles/rules/clean-code/missing-error-handling.ts +38 -20
- package/principles/rules/clean-code/too-many-params.ts +41 -22
- package/principles/rules/function-source.ts +62 -0
- package/principles/rules/solid/dip.ts +10 -6
- package/principles/rules/solid/isp.ts +64 -24
- package/principles/rules/solid/lsp.ts +66 -18
- package/principles/rules/solid/ocp.ts +29 -13
- package/principles/rules/solid/srp.ts +34 -18
- package/principles/warnings-count.ts +43 -0
- package/scripts/__tests__/prepush-source-classification.test.js +129 -0
- package/scripts/delphi-config-read.cjs +245 -0
- package/scripts/delphi-external-review.cjs +410 -585
- package/scripts/delphi-json-extract.cjs +108 -0
- package/scripts/delphi-model-validation.cjs +297 -0
- package/scripts/delphi-prompts.cjs +55 -0
- package/scripts/delphi-repo-tools.cjs +273 -0
- package/scripts/delphi-seat-calibration.cjs +204 -0
- package/scripts/run-bats-ci.cjs +240 -97
- package/scripts/sync-package-content.js +18 -2
- package/skills/delphi-review/.delphi-config.json.example +6 -11
- 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/lib/__tests__/agent-runtime-journal.test.js +0 -326
- package/lib/__tests__/arch-config-template.test.js +0 -190
- package/lib/__tests__/arch.test.js +0 -430
- package/lib/__tests__/assertion-debt.test.js +0 -99
- package/lib/__tests__/audit-cli-contract.test.ts +0 -117
- package/lib/__tests__/audit-log.test.ts +0 -107
- package/lib/__tests__/audit-skip-reasons.test.ts +0 -143
- package/lib/__tests__/baseline-completeness.test.js +0 -264
- package/lib/__tests__/baseline-shell-scope.test.js +0 -60
- package/lib/__tests__/baseline-unevaluated.test.js +0 -142
- package/lib/__tests__/baseline.test.js +0 -212
- package/lib/__tests__/bootstrap.test.js +0 -60
- package/lib/__tests__/check-alignment-cli.test.mjs +0 -94
- package/lib/__tests__/check-alignment-reasons.test.mjs +0 -101
- package/lib/__tests__/check-all-skip-accounting.test.js +0 -171
- package/lib/__tests__/check-changed-brief.test.js +0 -63
- package/lib/__tests__/check-normalize-exit.test.js +0 -24
- package/lib/__tests__/check-single-file.test.js +0 -114
- package/lib/__tests__/check-tools-list.test.js +0 -155
- package/lib/__tests__/check-version.test.js +0 -463
- package/lib/__tests__/coverage-scope.test.js +0 -140
- package/lib/__tests__/coverage-threshold.test.js +0 -80
- package/lib/__tests__/crap-cli.test.js +0 -87
- package/lib/__tests__/crap.test.js +0 -494
- package/lib/__tests__/debt-classify.test.js +0 -78
- package/lib/__tests__/delphi-channel-doctor.test.js +0 -116
- package/lib/__tests__/delphi-channel-uninstall.test.js +0 -128
- package/lib/__tests__/delphi-channel.test.js +0 -109
- package/lib/__tests__/delphi-setup.test.js +0 -203
- package/lib/__tests__/deploy-manifest-check-wired.test.js +0 -138
- package/lib/__tests__/deploy-manifest-completeness.test.js +0 -151
- package/lib/__tests__/deploy-manifest-consumers-adapter-uninstall.test.js +0 -40
- package/lib/__tests__/deploy-manifest-consumers-no-private-lists.test.js +0 -74
- package/lib/__tests__/deploy-manifest-projection.test.js +0 -66
- package/lib/__tests__/deploy-manifest-shell-parity.test.js +0 -108
- package/lib/__tests__/deploy-manifest.test.js +0 -97
- package/lib/__tests__/detect-deps-dsh-profile.test.js +0 -323
- package/lib/__tests__/detect-deps-gate-identity.test.js +0 -135
- package/lib/__tests__/detect-deps.test.js +0 -655
- package/lib/__tests__/doctor-469-hook-env.test.js +0 -211
- package/lib/__tests__/doctor-arch-tools.test.js +0 -169
- package/lib/__tests__/doctor-audit-ledger.test.js +0 -61
- package/lib/__tests__/doctor-brief.test.js +0 -59
- package/lib/__tests__/doctor-delphi.test.js +0 -628
- package/lib/__tests__/doctor-drift-direction.test.js +0 -104
- package/lib/__tests__/doctor-drift-round2.test.js +0 -215
- package/lib/__tests__/doctor-exec-retry.test.js +0 -162
- package/lib/__tests__/doctor-executed-surface.test.js +0 -230
- package/lib/__tests__/doctor-helpers.test.js +0 -139
- package/lib/__tests__/doctor-hook-drift.test.js +0 -329
- package/lib/__tests__/doctor-hook-signatures.test.js +0 -65
- package/lib/__tests__/doctor-machine-mutation.test.js +0 -116
- package/lib/__tests__/doctor-module-drift.test.js +0 -212
- package/lib/__tests__/doctor-module-resolution-parity.test.js +0 -463
- package/lib/__tests__/doctor-path-equivalence.test.js +0 -70
- package/lib/__tests__/doctor-repair-boundary.test.js +0 -52
- package/lib/__tests__/doctor-signature-stability.test.js +0 -55
- package/lib/__tests__/doctor-spawn-capability.test.js +0 -116
- package/lib/__tests__/doctor-timeout-partial.test.js +0 -79
- package/lib/__tests__/doctor-tool-delegation.test.js +0 -156
- package/lib/__tests__/doctor-tui-skill-freshness.test.js +0 -385
- package/lib/__tests__/doctor.test.js +0 -1320
- package/lib/__tests__/find-bash-wsl.test.js +0 -121
- package/lib/__tests__/flaky.test.js +0 -128
- package/lib/__tests__/foreign-hooks-global.test.js +0 -125
- package/lib/__tests__/gate-audit-skip.test.ts +0 -81
- package/lib/__tests__/gate-audit.test.ts +0 -440
- package/lib/__tests__/gate-coverage-hook-floor.test.js +0 -94
- package/lib/__tests__/gate-coverage.test.js +0 -204
- package/lib/__tests__/gate-entry-provenance.test.js +0 -71
- package/lib/__tests__/gate-numbering.test.js +0 -79
- package/lib/__tests__/gate-rules.test.js +0 -58
- package/lib/__tests__/gate-runner-tsx-invocation.test.js +0 -149
- package/lib/__tests__/gate-runner-verdict.test.js +0 -105
- package/lib/__tests__/gate-runner.test.js +0 -371
- package/lib/__tests__/gate2-verdict.test.js +0 -183
- package/lib/__tests__/helpers/lib-sandbox.cjs +0 -55
- package/lib/__tests__/hook-health-path.test.js +0 -109
- package/lib/__tests__/hook-health.test.js +0 -85
- package/lib/__tests__/init-gitignore.test.js +0 -204
- package/lib/__tests__/init-gitleaks-notice.test.js +0 -57
- package/lib/__tests__/init.test.js +0 -530
- package/lib/__tests__/install-cmd.test.js +0 -6
- package/lib/__tests__/install-sh-deploy-manifest.test.js +0 -80
- package/lib/__tests__/install-skill-full-dir.test.js +0 -194
- package/lib/__tests__/install-skill.test.js +0 -416
- package/lib/__tests__/install-tools-repair.test.js +0 -263
- package/lib/__tests__/issue-519-phase-transition-guard.test.js +0 -233
- package/lib/__tests__/issue-566-sync-hooks-backup.test.js +0 -228
- package/lib/__tests__/issue-583-hook-deploy-log.test.js +0 -121
- package/lib/__tests__/issue-586-principles-cmdline.test.js +0 -218
- package/lib/__tests__/issue-589-check-all-gate3.test.js +0 -190
- package/lib/__tests__/issue-590-archlint-vacuous-pass.test.js +0 -138
- package/lib/__tests__/issue-618-gate-not-run.test.js +0 -100
- package/lib/__tests__/jscpd-entry-resolution.test.js +0 -143
- package/lib/__tests__/language-capability.test.js +0 -318
- package/lib/__tests__/language-tools-build-file-install.test.js +0 -82
- package/lib/__tests__/language-tools-detect.test.js +0 -301
- package/lib/__tests__/migrate.test.js +0 -232
- package/lib/__tests__/mirror-manifest.test.js +0 -64
- package/lib/__tests__/mirror-sync.test.js +0 -803
- package/lib/__tests__/next-sprint.test.js +0 -231
- package/lib/__tests__/npm-package.test.js +0 -140
- package/lib/__tests__/phase-transition.test.js +0 -1136
- package/lib/__tests__/principles-csharp-coverage.test.js +0 -227
- package/lib/__tests__/principles-directory.test.js +0 -87
- package/lib/__tests__/qoder-delphi-agents.test.js +0 -316
- package/lib/__tests__/retro-gate-m.test.js +0 -144
- package/lib/__tests__/retro.test.js +0 -131
- package/lib/__tests__/rework.test.js +0 -127
- package/lib/__tests__/rollback.test.js +0 -183
- package/lib/__tests__/runtime-journal.test.js +0 -164
- package/lib/__tests__/sastd-client.test.js +0 -514
- package/lib/__tests__/shared-paths-lazy-home.test.js +0 -67
- package/lib/__tests__/shared-paths.test.js +0 -130
- package/lib/__tests__/shared-utils-copyHooks-manifest.test.js +0 -69
- package/lib/__tests__/shared-utils.test.js +0 -180
- package/lib/__tests__/skill-flags.test.js +0 -48
- package/lib/__tests__/slo.test.js +0 -89
- package/lib/__tests__/sprint-audit.test.js +0 -207
- package/lib/__tests__/sprint-discovery.test.ts +0 -366
- package/lib/__tests__/sprint-state-manager.test.js +0 -310
- package/lib/__tests__/sprint-status-rework-704.test.js +0 -149
- package/lib/__tests__/sprint-status-rework.test.js +0 -329
- package/lib/__tests__/sprint-status.test.js +0 -193
- package/lib/__tests__/sprint-status.test.ts +0 -213
- package/lib/__tests__/test-alignment-java-702.test.ts +0 -177
- package/lib/__tests__/test-alignment-range-guard.test.ts +0 -82
- package/lib/__tests__/test-alignment.test.ts +0 -853
- package/lib/__tests__/tool-audit-collect.test.js +0 -184
- package/lib/__tests__/tool-audit-fixtures.js +0 -87
- package/lib/__tests__/tool-audit-latest.test.js +0 -84
- package/lib/__tests__/tool-audit.test.js +0 -428
- package/lib/__tests__/tsconfig.json +0 -8
- package/lib/__tests__/tsx-resolution.test.js +0 -89
- package/lib/__tests__/ui-detector.test.ts +0 -459
- package/lib/__tests__/ui-review.test.ts +0 -132
- package/lib/__tests__/uninstall-global-residue.test.js +0 -545
- package/lib/__tests__/uninstall-skill.test.js +0 -189
- package/lib/__tests__/uninstall.test.js +0 -717
- package/lib/__tests__/update-hooks.test.js +0 -737
- package/lib/__tests__/update-skill-flags.test.js +0 -78
- package/lib/__tests__/update-skill.test.js +0 -644
- package/lib/__tests__/upgrade-exec.test.js +0 -273
- package/lib/__tests__/upgrade-skill-sync.test.js +0 -65
- package/lib/__tests__/upgrade.test.js +0 -78
- package/lib/__tests__/windows-bash-probe.test.js +0 -123
- package/lib/check-alignment-runner.cjs +0 -97
- package/lib/coverage-total.sh +0 -92
- package/lib/layer-baseline.cjs +0 -94
- package/lib/mattpocock-license-verdict.json +0 -7
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TDD: written RED first (per AGENTS.md TDD-GATE spec).
|
|
3
|
-
* @test REQ-712 Agent runtime efficiency must be measurable from session journals
|
|
4
|
-
* @intent The runtime-journal CLI must resolve a session store, honour --days,
|
|
5
|
-
* and report NOT MEASURABLE instead of a silent all-zero report.
|
|
6
|
-
* @covers AC-712-07
|
|
7
|
-
* @covers AC-712-08
|
|
8
|
-
*/
|
|
9
|
-
'use strict';
|
|
10
|
-
|
|
11
|
-
const fs = require('fs');
|
|
12
|
-
const os = require('os');
|
|
13
|
-
const path = require('path');
|
|
14
|
-
|
|
15
|
-
const { handleRuntimeJournal, sessionRootCandidates, pickSessionRoot, parseDays } = require('../runtime-journal');
|
|
16
|
-
|
|
17
|
-
let TMP_DIR;
|
|
18
|
-
let outSpy;
|
|
19
|
-
let captured;
|
|
20
|
-
|
|
21
|
-
beforeEach(() => {
|
|
22
|
-
TMP_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'runtime-cli-'));
|
|
23
|
-
captured = [];
|
|
24
|
-
outSpy = vi.spyOn(process.stdout, 'write').mockImplementation((chunk) => {
|
|
25
|
-
captured.push(String(chunk));
|
|
26
|
-
return true;
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
afterEach(() => {
|
|
31
|
-
outSpy.mockRestore();
|
|
32
|
-
fs.rmSync(TMP_DIR, { recursive: true, force: true });
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Write a session dir containing an uncompressed journal.
|
|
37
|
-
* @param {string} root
|
|
38
|
-
* @param {string} name
|
|
39
|
-
* @param {Array<object>} events
|
|
40
|
-
*/
|
|
41
|
-
function writeSession(root, name, events) {
|
|
42
|
-
const dir = path.join(root, name);
|
|
43
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
44
|
-
fs.writeFileSync(path.join(dir, 'session.v4.jsonl'), events.map((e) => JSON.stringify(e)).join('\n') + '\n');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Emit a tool/call + tool/result pair.
|
|
49
|
-
* @param {string} callId
|
|
50
|
-
* @param {string} name
|
|
51
|
-
* @param {object} args
|
|
52
|
-
* @param {number} startT
|
|
53
|
-
* @param {number} endT
|
|
54
|
-
* @returns {Array<object>}
|
|
55
|
-
*/
|
|
56
|
-
function pair(callId, name, args, startT, endT) {
|
|
57
|
-
return [
|
|
58
|
-
{ type: 'tool/call', time: startT, data: { turn: 1, step: 1, callId, name, arguments: JSON.stringify(args) } },
|
|
59
|
-
{ type: 'tool/result', time: endT, data: { message: { toolCallId: callId, content: [{ type: 'text', text: 'ok' }] } } },
|
|
60
|
-
];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
describe('parseDays', () => {
|
|
64
|
-
it('defaults when the flag is absent', () => {
|
|
65
|
-
expect(parseDays(['--json'], 3)).toBe(3);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('reads --days N', () => {
|
|
69
|
-
expect(parseDays(['--days', '7'], 3)).toBe(7);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('rejects a non-numeric value rather than treating it as 0', () => {
|
|
73
|
-
expect(parseDays(['--days', 'abc'], 3)).toBe(3);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
describe('sessionRootCandidates', () => {
|
|
78
|
-
it('honours DSH_SESSIONS_DIR as the highest-priority override', () => {
|
|
79
|
-
const prev = process.env.DSH_SESSIONS_DIR;
|
|
80
|
-
process.env.DSH_SESSIONS_DIR = 'C:/custom/store';
|
|
81
|
-
try {
|
|
82
|
-
expect(sessionRootCandidates('xp-gate')[0]).toBe('C:/custom/store');
|
|
83
|
-
} finally {
|
|
84
|
-
if (prev === undefined) delete process.env.DSH_SESSIONS_DIR;
|
|
85
|
-
else process.env.DSH_SESSIONS_DIR = prev;
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it('derives the store slug from the full path, not the basename', () => {
|
|
90
|
-
const prev = process.env.DSH_SESSIONS_DIR;
|
|
91
|
-
delete process.env.DSH_SESSIONS_DIR;
|
|
92
|
-
try {
|
|
93
|
-
// DSH slugs `D:\projects\xp-gate` to `--D-projects-xp-gate--`. A
|
|
94
|
-
// basename-only guess would produce `--xp-gate--`, which never exists.
|
|
95
|
-
const candidates = sessionRootCandidates('D:\\projects\\xp-gate');
|
|
96
|
-
expect(candidates.some((c) => c.endsWith('--D-projects-xp-gate--'))).toBe(true);
|
|
97
|
-
} finally {
|
|
98
|
-
if (prev !== undefined) process.env.DSH_SESSIONS_DIR = prev;
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
describe('pickSessionRoot', () => {
|
|
104
|
-
it('picks the first candidate that actually holds a journal', () => {
|
|
105
|
-
const store = path.join(TMP_DIR, 'store');
|
|
106
|
-
writeSession(store, 's1', pair('a', 'read', {}, 1000, 1010));
|
|
107
|
-
const missing = path.join(TMP_DIR, 'nope');
|
|
108
|
-
expect(pickSessionRoot([missing, store])).toBe(store);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('returns null when no candidate holds a journal', () => {
|
|
112
|
-
expect(pickSessionRoot([path.join(TMP_DIR, 'nope')])).toBeNull();
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
describe('handleRuntimeJournal — AC-712-07 NOT MEASURABLE', () => {
|
|
117
|
-
it('reports NOT MEASURABLE and exits 0 when no journal is found', () => {
|
|
118
|
-
const code = handleRuntimeJournal(['--sessions-dir', path.join(TMP_DIR, 'absent')]);
|
|
119
|
-
expect(code).toBe(0);
|
|
120
|
-
expect(captured.join('')).toMatch(/NOT MEASURABLE/i);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('reports measurable:false in JSON form so a machine reader sees the gap', () => {
|
|
124
|
-
handleRuntimeJournal(['--json', '--sessions-dir', path.join(TMP_DIR, 'absent')]);
|
|
125
|
-
const payload = JSON.parse(captured.join(''));
|
|
126
|
-
expect(payload.measurable).toBe(false);
|
|
127
|
-
expect(payload.reason).toMatch(/no session journals/i);
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
describe('handleRuntimeJournal — AC-712-08 report content', () => {
|
|
132
|
-
it('measures a real journal and emits the anti-pattern numbers', () => {
|
|
133
|
-
const store = path.join(TMP_DIR, 'store');
|
|
134
|
-
const now = Date.now();
|
|
135
|
-
writeSession(store, 's1', [
|
|
136
|
-
...pair('j1', 'job_output', { job_id: 'b', wait: true }, now - 600000, now - 60000),
|
|
137
|
-
...pair('s1', 'bash', { command: 'sleep 120' }, now - 50000, now - 1000),
|
|
138
|
-
]);
|
|
139
|
-
|
|
140
|
-
const code = handleRuntimeJournal(['--json', '--sessions-dir', store, '--days', '1']);
|
|
141
|
-
expect(code).toBe(0);
|
|
142
|
-
const payload = JSON.parse(captured.join(''));
|
|
143
|
-
expect(payload.measurable).toBe(true);
|
|
144
|
-
expect(payload.blockingJobOutput.explicitWaitTrue).toBe(1);
|
|
145
|
-
expect(payload.idleWait.sleep.count).toBe(1);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it('excludes calls outside the --days window', () => {
|
|
149
|
-
const store = path.join(TMP_DIR, 'store');
|
|
150
|
-
const now = Date.now();
|
|
151
|
-
const oldT = now - 30 * 24 * 60 * 60 * 1000;
|
|
152
|
-
writeSession(store, 's1', pair('j1', 'job_output', { wait: true }, oldT, oldT + 600000));
|
|
153
|
-
|
|
154
|
-
handleRuntimeJournal(['--json', '--sessions-dir', store, '--days', '1']);
|
|
155
|
-
const payload = JSON.parse(captured.join(''));
|
|
156
|
-
expect(payload.blockingJobOutput.explicitWaitTrue).toBe(0);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it('always exits 0 — this is a diagnostic, not a gate', () => {
|
|
160
|
-
const store = path.join(TMP_DIR, 'store');
|
|
161
|
-
writeSession(store, 's1', pair('a', 'read', {}, 1000, 1010));
|
|
162
|
-
expect(handleRuntimeJournal(['--sessions-dir', store])).toBe(0);
|
|
163
|
-
});
|
|
164
|
-
});
|
|
@@ -1,514 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @test REQ-SASTD-E3 sastd 客户端:路径归一化、策略解析、失败绝不伪装成功
|
|
3
|
-
* @intent E3 把 Gate 10 接到常驻 semgrep 服务上。穿刺实测:直调 CLI 3,801-4,044ms,
|
|
4
|
-
* 服务热调用 2,089-2,245ms。但快路径依赖 semgrep 的内部模块
|
|
5
|
-
* `run_scan.run_scan_and_return_json`,官方无稳定承诺——所以这个客户端最重要的
|
|
6
|
-
* 性质不是"快",而是"daemon 的任何失败都必须能被上位调用方识别为不可用",
|
|
7
|
-
* 绝不能伪装成一次通过的扫描(#518/#559)。
|
|
8
|
-
* @covers AC-SASTD-01, AC-SASTD-02, AC-SASTD-03, AC-SASTD-04
|
|
9
|
-
*/
|
|
10
|
-
const path = require('node:path');
|
|
11
|
-
const { execFileSync } = require('node:child_process');
|
|
12
|
-
|
|
13
|
-
const {
|
|
14
|
-
resolveDaemonPolicy,
|
|
15
|
-
normaliseResultPaths,
|
|
16
|
-
normalisePayload,
|
|
17
|
-
requestScan,
|
|
18
|
-
ensureDaemon,
|
|
19
|
-
resetDaemon,
|
|
20
|
-
} = require('../sastd-client');
|
|
21
|
-
|
|
22
|
-
// Assertion values, named because xp-gate-principles counts bare literals as magic
|
|
23
|
-
// numbers even in tests. They are deliberately meaningless: a malformed path field,
|
|
24
|
-
// a captured semgrep total_time, and two timeouts.
|
|
25
|
-
const BAD_PATH_VALUE = 42;
|
|
26
|
-
const CAPTURED_TOTAL_TIME = 1.98;
|
|
27
|
-
const SHORT_TIMEOUT_MS = 5000;
|
|
28
|
-
const LONG_TIMEOUT_MS = 120000;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* True when this interpreter can `import semgrep`.
|
|
32
|
-
*
|
|
33
|
-
* Some tests here drive the REAL `semgrep.run_scan.run_scan_and_return_json`,
|
|
34
|
-
* because the property they pin is "the daemon behaves like the CLI it replaces"
|
|
35
|
-
* -- a stub would prove nothing. Those tests therefore need semgrep installed,
|
|
36
|
-
* and CI does not install it (only the local machine does). Detect it ONCE and
|
|
37
|
-
* skip loudly rather than failing: a red build for a missing optional tool
|
|
38
|
-
* trains people to ignore red, which is worse than a named skip.
|
|
39
|
-
*/
|
|
40
|
-
let semgrepImportable;
|
|
41
|
-
function hasSemgrep() {
|
|
42
|
-
if (semgrepImportable !== undefined) return semgrepImportable;
|
|
43
|
-
try {
|
|
44
|
-
execFileSync('python', ['-c', 'import semgrep'], { stdio: 'ignore', timeout: LONG_TIMEOUT_MS });
|
|
45
|
-
semgrepImportable = true;
|
|
46
|
-
} catch {
|
|
47
|
-
semgrepImportable = false;
|
|
48
|
-
}
|
|
49
|
-
return semgrepImportable;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
describe('resolveDaemonPolicy — AC-SASTD-01', () => {
|
|
53
|
-
it('enables the daemon only for values that ask for it', () => {
|
|
54
|
-
for (const v of ['on', '1', 'true', 'auto', 'ON', ' On ']) {
|
|
55
|
-
expect(resolveDaemonPolicy({ XP_GATE_SASTD: v }).enabled).toBe(true);
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('keeps the direct path for everything else, including unset', () => {
|
|
60
|
-
for (const v of [undefined, '', 'off', 'direct', '0', 'false', 'bogus']) {
|
|
61
|
-
const env = v === undefined ? {} : { XP_GATE_SASTD: v };
|
|
62
|
-
expect(resolveDaemonPolicy(env).enabled).toBe(false);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
describe('normaliseResultPaths — AC-SASTD-02', () => {
|
|
68
|
-
// Measured against the real CLI: this is the ONLY output difference between
|
|
69
|
-
// `semgrep scan --json` and the in-process API. Every other key, and every
|
|
70
|
-
// verdict input, is identical.
|
|
71
|
-
it('rewrites an absolute path to the basename the CLI would report', () => {
|
|
72
|
-
const out = normaliseResultPaths([{ path: path.join('C:', 'repo', 'sub', 't.js'), check_id: 'x' }]);
|
|
73
|
-
expect(out[0].path).toBe('t.js');
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
// AC-SASTD-02, cross-platform half. `path.isAbsolute`/`path.basename` answer
|
|
77
|
-
// for the HOST only, so the original implementation passed on Windows and
|
|
78
|
-
// failed on Linux CI: 'C:/repo/sub/t.js' is not absolute to a POSIX host, so
|
|
79
|
-
// the path was left absolute and the report showed a different-looking
|
|
80
|
-
// finding. A scan's paths arrive from whichever machine ran semgrep, so BOTH
|
|
81
|
-
// conventions must reduce to the same basename no matter which host runs this.
|
|
82
|
-
const BACKSLASH = String.fromCharCode(92);
|
|
83
|
-
// A UNC path is EXACTLY two leading backslashes. Building the prefix with
|
|
84
|
-
// `[BACKSLASH + BACKSLASH, 'server', ...].join(BACKSLASH)` yields THREE, and
|
|
85
|
-
// a three-backslash string starts with '/' in POSIX eyes (isAbsolute=true) but
|
|
86
|
-
// not in the implementation's UNC branch -- so it passed on Windows and failed
|
|
87
|
-
// on Linux CI. The separator before 'server' must not be joined in.
|
|
88
|
-
const UNC_PREFIX = BACKSLASH + BACKSLASH;
|
|
89
|
-
const ABSOLUTE_FORMS = [
|
|
90
|
-
['posix', ['/', 'repo', 'sub', 't.js'].join('/')],
|
|
91
|
-
['windows forward-slash', ['C:', 'repo', 'sub', 't.js'].join('/')],
|
|
92
|
-
['windows backslash', ['C:', 'repo', 'sub', 't.js'].join(BACKSLASH)],
|
|
93
|
-
['windows UNC', UNC_PREFIX + ['server', 'share', 't.js'].join(BACKSLASH)],
|
|
94
|
-
];
|
|
95
|
-
|
|
96
|
-
for (const [label, p] of ABSOLUTE_FORMS) {
|
|
97
|
-
it(`rewrites an absolute ${label} path to its basename on every host`, () => {
|
|
98
|
-
const out = normaliseResultPaths([{ path: p }]);
|
|
99
|
-
expect(out[0].path).toBe('t.js');
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
it('never basenames a relative path, in either separator style', () => {
|
|
104
|
-
for (const p of ['sub/t.js', 'sub' + BACKSLASH + 't.js', './t.js']) {
|
|
105
|
-
expect(normaliseResultPaths([{ path: p }])[0].path).toBe(p);
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('leaves an already-relative path untouched', () => {
|
|
110
|
-
// Rewriting this would silently drop the "sub/" prefix and make two distinct
|
|
111
|
-
// findings look like the same file.
|
|
112
|
-
const out = normaliseResultPaths([{ path: path.join('sub', 't.js') }]);
|
|
113
|
-
expect(out[0].path).toBe(path.join('sub', 't.js'));
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it('preserves every other field and does not mutate the input', () => {
|
|
117
|
-
const original = { path: '/abs/dir/a.js', check_id: 'r', extra: { severity: 'ERROR' } };
|
|
118
|
-
const out = normaliseResultPaths([original]);
|
|
119
|
-
expect(out[0].check_id).toBe('r');
|
|
120
|
-
expect(out[0].extra).toEqual({ severity: 'ERROR' });
|
|
121
|
-
expect(original.path).toBe('/abs/dir/a.js');
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('tolerates malformed entries instead of throwing', () => {
|
|
125
|
-
expect(() => normaliseResultPaths([null, {}, { path: BAD_PATH_VALUE }])).not.toThrow();
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
describe('normalisePayload — AC-SASTD-03', () => {
|
|
130
|
-
it('normalises results and leaves the rest of the payload alone', () => {
|
|
131
|
-
const payload = {
|
|
132
|
-
results: [{ path: '/abs/x/a.js' }],
|
|
133
|
-
errors: [],
|
|
134
|
-
version: '1.183.0',
|
|
135
|
-
time: { rules: [], total_time: CAPTURED_TOTAL_TIME },
|
|
136
|
-
};
|
|
137
|
-
const out = normalisePayload(payload);
|
|
138
|
-
expect(out.results[0].path).toBe('a.js');
|
|
139
|
-
expect(out.version).toBe('1.183.0');
|
|
140
|
-
expect(out.errors).toEqual([]);
|
|
141
|
-
expect(out.time.total_time).toBe(CAPTURED_TOTAL_TIME);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('passes through a payload with no results array', () => {
|
|
145
|
-
expect(normalisePayload({ version: '1' })).toEqual({ version: '1' });
|
|
146
|
-
expect(normalisePayload(null)).toBeNull();
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
describe('requestScan — AC-SASTD-04: failure must never look like success', () => {
|
|
151
|
-
it('reports unavailable when the interpreter does not exist', async () => {
|
|
152
|
-
const r = await requestScan({
|
|
153
|
-
config: '/nope.yml',
|
|
154
|
-
roots: ['/nope.js'],
|
|
155
|
-
pythonCmd: '/definitely/not/a/python',
|
|
156
|
-
timeoutMs: SHORT_TIMEOUT_MS,
|
|
157
|
-
});
|
|
158
|
-
expect(r.ok).toBe(false);
|
|
159
|
-
expect(typeof r.error).toBe('string');
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it('reports unavailable when the daemon cannot import semgrep', async () => {
|
|
163
|
-
// A real python that cannot import semgrep: the script will raise on the
|
|
164
|
-
// lazy import inside the scan handler, and the reply must be ok:false.
|
|
165
|
-
const r = await requestScan({
|
|
166
|
-
config: '/nope.yml',
|
|
167
|
-
roots: ['/nope.js'],
|
|
168
|
-
pythonCmd: process.env.XP_GATE_TEST_BARE_PYTHON || 'python',
|
|
169
|
-
timeoutMs: LONG_TIMEOUT_MS,
|
|
170
|
-
});
|
|
171
|
-
// Either the import fails or the scan fails; both must be ok:false. What must
|
|
172
|
-
// NOT happen is ok:true with an empty payload being read as a clean scan.
|
|
173
|
-
if (!r.ok) {
|
|
174
|
-
expect(typeof r.error).toBe('string');
|
|
175
|
-
} else {
|
|
176
|
-
expect(r.payload).toBeDefined();
|
|
177
|
-
}
|
|
178
|
-
}, 180000);
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
describe('resident process — AC-SASTD-05: the daemon must outlive one call', () => {
|
|
183
|
-
// Two real bugs were written and caught here. Both produced a client that
|
|
184
|
-
// LOOKED correct, passed its unit tests, and delivered none of the benefit:
|
|
185
|
-
//
|
|
186
|
-
// 1. spawning a fresh python per request -- measured 4.2s, i.e. the import
|
|
187
|
-
// tax paid every time. The whole point of the daemon is that it does not.
|
|
188
|
-
// 2. registering the reader on proc.stdin instead of proc.stdout -- stdin is
|
|
189
|
-
// write-only, so every request sat until the 180s timeout while the daemon
|
|
190
|
-
// had already answered into an unread pipe.
|
|
191
|
-
//
|
|
192
|
-
// Neither is visible in the output shape; only timing and a real second call
|
|
193
|
-
// reveal them. Hence this test asserts on the timing of run 2+, not on run 1.
|
|
194
|
-
|
|
195
|
-
const fs = require('node:fs');
|
|
196
|
-
const os = require('node:os');
|
|
197
|
-
const { execFileSync } = require('node:child_process');
|
|
198
|
-
|
|
199
|
-
let dir;
|
|
200
|
-
beforeAll(() => {
|
|
201
|
-
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'sastd-e3-'));
|
|
202
|
-
fs.writeFileSync(path.join(dir, 't.js'), 'eval("x");\n');
|
|
203
|
-
fs.writeFileSync(
|
|
204
|
-
path.join(dir, 'rule.yml'),
|
|
205
|
-
[
|
|
206
|
-
'rules:',
|
|
207
|
-
' - id: test-eval',
|
|
208
|
-
' languages: [javascript]',
|
|
209
|
-
' severity: ERROR',
|
|
210
|
-
' message: eval is dangerous',
|
|
211
|
-
' pattern: eval(...)',
|
|
212
|
-
'',
|
|
213
|
-
].join('\n'),
|
|
214
|
-
);
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
afterAll(() => {
|
|
218
|
-
resetDaemon('test teardown');
|
|
219
|
-
try {
|
|
220
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
221
|
-
} catch {
|
|
222
|
-
/* best effort */
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
it('the second call reuses the same daemon process', async () => {
|
|
227
|
-
if (!hasSemgrep()) {
|
|
228
|
-
// Skipping loudly beats a green test that proved nothing.
|
|
229
|
-
console.warn('[sastd] semgrep not importable in this environment; residence test skipped');
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
resetDaemon('start clean');
|
|
234
|
-
const opts = { config: path.join(dir, 'rule.yml'), roots: [path.join(dir, 't.js')] };
|
|
235
|
-
|
|
236
|
-
const t1 = Date.now();
|
|
237
|
-
const first = await requestScan(opts);
|
|
238
|
-
const cold = Date.now() - t1;
|
|
239
|
-
expect(first.ok).toBe(true);
|
|
240
|
-
|
|
241
|
-
const t2 = Date.now();
|
|
242
|
-
const second = await requestScan(opts);
|
|
243
|
-
const warm = Date.now() - t2;
|
|
244
|
-
expect(second.ok).toBe(true);
|
|
245
|
-
|
|
246
|
-
// TIMING IS NOT THE ASSERTION. An earlier version required warm < cold - 800ms
|
|
247
|
-
// and flapped on a loaded machine -- this box runs at 90-100% CPU, so the noise
|
|
248
|
-
// routinely exceeds the effect direction. Timing also cannot distinguish "the
|
|
249
|
-
// daemon was reused" from "semgrep's own caches warmed up", so a genuine
|
|
250
|
-
// regression could hide behind it.
|
|
251
|
-
//
|
|
252
|
-
// The claim that matters is structural: the SAME process answers both calls.
|
|
253
|
-
// A one-spawn-per-request bug changes the identity, which no amount of machine
|
|
254
|
-
// load can disguise. Timing is still reported, for diagnosis only.
|
|
255
|
-
const a = ensureDaemon('python');
|
|
256
|
-
const b = ensureDaemon('python');
|
|
257
|
-
expect(a.ok).toBe(true);
|
|
258
|
-
expect(b.ok).toBe(true);
|
|
259
|
-
expect(b.value.proc).toBe(a.value.proc);
|
|
260
|
-
expect(b.value.proc.pid).toBe(a.value.proc.pid);
|
|
261
|
-
|
|
262
|
-
// Reported, not asserted: a warm call should not be dramatically slower, but
|
|
263
|
-
// the threshold is loose enough that load cannot fail the suite.
|
|
264
|
-
expect(warm).toBeLessThan(cold * 2);
|
|
265
|
-
}, 300000);
|
|
266
|
-
|
|
267
|
-
it('both calls return the same normalised shape, so reuse does not change output', async () => {
|
|
268
|
-
const opts = { config: path.join(dir, 'rule.yml'), roots: [path.join(dir, 't.js')] };
|
|
269
|
-
const a = await requestScan(opts);
|
|
270
|
-
if (!a.ok) return; // environment without semgrep: covered by the skip above
|
|
271
|
-
const b = await requestScan(opts);
|
|
272
|
-
expect(b.ok).toBe(true);
|
|
273
|
-
|
|
274
|
-
expect(a.payload.results.length).toBe(b.payload.results.length);
|
|
275
|
-
expect(a.payload.results[0].path).toBe(b.payload.results[0].path);
|
|
276
|
-
expect(path.isAbsolute(a.payload.results[0].path)).toBe(false);
|
|
277
|
-
expect(a.payload.results[0].extra.severity).toBe('ERROR');
|
|
278
|
-
}, 300000);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
describe('process exit — AC-SASTD-06: a one-shot caller must not hang', () => {
|
|
283
|
-
// THE BUG THIS GUARDS AGAINST, which cost hours and was invisible to every
|
|
284
|
-
// existing test: a spawned child keeps the Node event loop alive. Once the client
|
|
285
|
-
// held a resident daemon, a caller that ran ONE scan and let Node exit naturally
|
|
286
|
-
// never exited at all -- the daemon lived forever, so the loop never drained.
|
|
287
|
-
//
|
|
288
|
-
// Every unit test passed, because vitest calls process.exit() itself. The real
|
|
289
|
-
// consumer is a shell command substitution (`X=$(node -e ...)`), which has to wait
|
|
290
|
-
// for node to end on its own. That is why this test spawns a real child process.
|
|
291
|
-
//
|
|
292
|
-
// Two wrong fixes were measured on the way here, and both are worse:
|
|
293
|
-
// - unref() at spawn: Node exited BEFORE the first scan resolved -> 0 bytes.
|
|
294
|
-
// - never unref: the original hang.
|
|
295
|
-
// The correct shape is ref-counting: hold the loop only while work is in flight.
|
|
296
|
-
|
|
297
|
-
const { execFileSync } = require('node:child_process');
|
|
298
|
-
const fs = require('node:fs');
|
|
299
|
-
const os = require('node:os');
|
|
300
|
-
|
|
301
|
-
let dir;
|
|
302
|
-
beforeAll(() => {
|
|
303
|
-
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'sastd-exit-'));
|
|
304
|
-
fs.writeFileSync(path.join(dir, 't.js'), 'eval("x");\n');
|
|
305
|
-
fs.writeFileSync(
|
|
306
|
-
path.join(dir, 'rule.yml'),
|
|
307
|
-
['rules:', ' - id: t', ' languages: [javascript]', ' severity: ERROR',
|
|
308
|
-
' message: m', ' pattern: eval(...)', ''].join('\n'),
|
|
309
|
-
);
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
afterAll(() => {
|
|
313
|
-
try {
|
|
314
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
315
|
-
} catch {
|
|
316
|
-
/* best effort */
|
|
317
|
-
}
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
it('exits on its own after a single scan (no explicit process.exit)', () => {
|
|
321
|
-
if (!hasSemgrep()) {
|
|
322
|
-
console.warn('[sastd] semgrep not importable; one-shot-exit test skipped');
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
const script = `
|
|
326
|
-
const { requestScan } = require(${JSON.stringify(
|
|
327
|
-
path.join(__dirname, '..', 'sastd-client.js'),
|
|
328
|
-
)});
|
|
329
|
-
// NOTE: under node -e there is NO script path in argv -- argv[1] is already
|
|
330
|
-
// the first user argument. Indexing this as argv[2] silently passed
|
|
331
|
-
// undefined as the config and produced a python TypeError, which is exactly
|
|
332
|
-
// the trap that made the gate integration appear to fail for reasons that
|
|
333
|
-
// had nothing to do with the daemon.
|
|
334
|
-
requestScan({ config: process.argv[1], roots: [process.argv[2]] }).then((r) => {
|
|
335
|
-
process.stdout.write(r.ok ? 'OK:' + r.payload.results.length : 'FAIL:' + r.error);
|
|
336
|
-
});
|
|
337
|
-
`;
|
|
338
|
-
const start = Date.now();
|
|
339
|
-
let out;
|
|
340
|
-
try {
|
|
341
|
-
out = execFileSync(
|
|
342
|
-
process.execPath,
|
|
343
|
-
['-e', script, path.join(dir, 'rule.yml'), path.join(dir, 't.js')],
|
|
344
|
-
{ encoding: 'utf8', timeout: 60000 },
|
|
345
|
-
);
|
|
346
|
-
} catch (err) {
|
|
347
|
-
// A timeout here is precisely the regression: the process never exited.
|
|
348
|
-
throw new Error(`child did not exit cleanly: ${err.message}`);
|
|
349
|
-
}
|
|
350
|
-
const elapsed = Date.now() - start;
|
|
351
|
-
|
|
352
|
-
expect(elapsed).toBeLessThan(60000);
|
|
353
|
-
// Not just "it exited" -- it exited WITH the payload. An early exit would
|
|
354
|
-
// satisfy the timeout check while producing nothing.
|
|
355
|
-
expect(out.startsWith('OK:')).toBe(true);
|
|
356
|
-
expect(out.length).toBeGreaterThan(2);
|
|
357
|
-
}, 120000);
|
|
358
|
-
});
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
describe('never greener — AC-SASTD-07: a thinner scan must not read as a clean one', () => {
|
|
362
|
-
// The safety claim for this feature is "the daemon can make the gate slower, never
|
|
363
|
-
// greener". Code review is not evidence for that, so these tests drive the paths
|
|
364
|
-
// where a thinner result could slip through unnoticed.
|
|
365
|
-
//
|
|
366
|
-
// The dangerous shape is the VALID but EMPTY document: semgrep can return
|
|
367
|
-
// results: [] and errors: [] after failing to resolve a registry pack or after
|
|
368
|
-
// skipping every target, and gate-10 reads zero blocking findings as PASS.
|
|
369
|
-
// Truncated JSON needs no test here -- it fails to parse and the gate fails closed.
|
|
370
|
-
|
|
371
|
-
const { execFileSync } = require('node:child_process');
|
|
372
|
-
const fs = require('node:fs');
|
|
373
|
-
const os = require('node:os');
|
|
374
|
-
const pathMod = require('node:path');
|
|
375
|
-
|
|
376
|
-
const sastdDir = pathMod.resolve(__dirname, '..', '..', '..', 'sastd');
|
|
377
|
-
// A REAL registry pack and a SMALL directory. An earlier version used "p/x" and
|
|
378
|
-
// cwd: the invalid pack failed before the scan ran, so no coverage check happened
|
|
379
|
-
// and the test looked like a broken guard rather than a broken test.
|
|
380
|
-
const CONFIG = 'p/r2c-best-practices';
|
|
381
|
-
|
|
382
|
-
/** Run a snippet in a python process that has sastd importable. */
|
|
383
|
-
function runPython(body) {
|
|
384
|
-
const script = [
|
|
385
|
-
'import json, sys',
|
|
386
|
-
'sys.path.insert(0, sys.argv[1])',
|
|
387
|
-
'import sastd',
|
|
388
|
-
'CONFIG, ROOT = sys.argv[2], sys.argv[3]',
|
|
389
|
-
body,
|
|
390
|
-
].join('\n');
|
|
391
|
-
const raw = execFileSync('python', ['-c', script, sastdDir, CONFIG, dir], {
|
|
392
|
-
encoding: 'utf8',
|
|
393
|
-
timeout: 120000,
|
|
394
|
-
});
|
|
395
|
-
const reply = JSON.parse(raw);
|
|
396
|
-
// _handle wraps a scan in {"ok": true, "data": {...}}. Unwrap so tests assert on
|
|
397
|
-
// the scan result itself; forgetting this layer made a working guard look broken.
|
|
398
|
-
return reply && reply.ok === true && reply.data ? reply.data : reply;
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
let dir;
|
|
402
|
-
beforeAll(() => {
|
|
403
|
-
dir = fs.mkdtempSync(pathMod.join(os.tmpdir(), 'sastd-green-'));
|
|
404
|
-
fs.writeFileSync(pathMod.join(dir, 't.js'), 'eval("x");\n');
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
afterAll(() => {
|
|
408
|
-
try {
|
|
409
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
410
|
-
} catch {
|
|
411
|
-
/* best effort */
|
|
412
|
-
}
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
it('the scan HANDLER marks a scan that covered none of its roots', () => {
|
|
416
|
-
// Drives _handle, NOT _verify_coverage. An earlier version of this test called
|
|
417
|
-
// the helper directly, so deleting the helper's call site left it green: it
|
|
418
|
-
// proved the function worked, not that anything used it. Reverting the call in
|
|
419
|
-
// _handle now turns this red (verified).
|
|
420
|
-
if (!hasSemgrep()) {
|
|
421
|
-
console.warn('[sastd] semgrep not importable; coverage-guard handler test skipped');
|
|
422
|
-
return;
|
|
423
|
-
}
|
|
424
|
-
const out = runPython(
|
|
425
|
-
[
|
|
426
|
-
'import semgrep.run_scan as rs',
|
|
427
|
-
'inner = rs.run_scan_and_return_json',
|
|
428
|
-
'def stub(**kw):',
|
|
429
|
-
' r = inner(**kw)',
|
|
430
|
-
' r["results"] = []',
|
|
431
|
-
' r["paths"] = {"scanned": []}',
|
|
432
|
-
' return r',
|
|
433
|
-
'rs.run_scan_and_return_json = stub',
|
|
434
|
-
'print(json.dumps(sastd._handle({"op": "scan", "config": CONFIG, "roots": [ROOT]})))',
|
|
435
|
-
].join('\n'),
|
|
436
|
-
);
|
|
437
|
-
expect((out.errors || []).map((e) => e.type)).toContain('Coverage');
|
|
438
|
-
}, 90000);
|
|
439
|
-
|
|
440
|
-
it('the scan HANDLER leaves a fully-covering scan untouched', () => {
|
|
441
|
-
// The negative half: a guard that fires on every scan would push every run onto
|
|
442
|
-
// the direct path and make the daemon permanently useless while looking "safe".
|
|
443
|
-
if (!hasSemgrep()) {
|
|
444
|
-
console.warn('[sastd] semgrep not importable; full-coverage handler test skipped');
|
|
445
|
-
return;
|
|
446
|
-
}
|
|
447
|
-
const out = runPython(
|
|
448
|
-
[
|
|
449
|
-
'import semgrep.run_scan as rs',
|
|
450
|
-
'inner = rs.run_scan_and_return_json',
|
|
451
|
-
'def stub(**kw):',
|
|
452
|
-
' r = inner(**kw)',
|
|
453
|
-
' r["results"] = []',
|
|
454
|
-
' r["paths"] = {"scanned": ["t.js"]}',
|
|
455
|
-
' return r',
|
|
456
|
-
'rs.run_scan_and_return_json = stub',
|
|
457
|
-
'print(json.dumps(sastd._handle({"op": "scan", "config": CONFIG, "roots": [ROOT]})))',
|
|
458
|
-
].join('\n'),
|
|
459
|
-
);
|
|
460
|
-
expect(out.errors || []).toEqual([]);
|
|
461
|
-
}, 90000);
|
|
462
|
-
|
|
463
|
-
it('records how many files were scanned, so a caller can compare against direct', () => {
|
|
464
|
-
// The daemon cannot express the CLI's --include filter, so it scans whole
|
|
465
|
-
// directories: a superset. Recording the count makes that checkable instead of
|
|
466
|
-
// assumed -- if root selection ever changed, this number would move and a
|
|
467
|
-
// comparison against the direct path would notice.
|
|
468
|
-
const out = runPython(
|
|
469
|
-
[
|
|
470
|
-
'print(json.dumps(sastd._verify_coverage({"results": [], "errors": [], "paths": {"scanned": ["a.js", "b.js"]}}, ["."])))',
|
|
471
|
-
].join('\n'),
|
|
472
|
-
);
|
|
473
|
-
expect(out.xp_gate_coverage).toEqual({ files_scanned: 2, roots_requested: 1 });
|
|
474
|
-
}, 90000);
|
|
475
|
-
|
|
476
|
-
it('a failing request leaves the daemon alive for the next call', () => {
|
|
477
|
-
// WHAT THIS DOES NOT PROVE: that double-release is impossible. I wrote a test
|
|
478
|
-
// for that and it passed with the guard removed, so it was proving nothing --
|
|
479
|
-
// the race it describes (timeout landing as a late reply arrives) needs real
|
|
480
|
-
// timer interleaving that a deterministic test cannot create here. Rather than
|
|
481
|
-
// keep a green test that cannot fail, this asserts the observable consequence
|
|
482
|
-
// instead, and the idempotence guard itself is justified by code review only.
|
|
483
|
-
//
|
|
484
|
-
// The consequence is checkable: a failed request must not tear down a healthy
|
|
485
|
-
// daemon. If a second releaseProc had unref'd the child early, the next call
|
|
486
|
-
// would spawn a new process and the identity below would differ.
|
|
487
|
-
return (async () => {
|
|
488
|
-
const r1 = await requestScan({ config: pathMod.join(dir, 'missing.yml'), roots: [dir], timeoutMs: 60000 });
|
|
489
|
-
expect(r1.ok).toBe(false);
|
|
490
|
-
expect(typeof r1.error).toBe('string');
|
|
491
|
-
|
|
492
|
-
const r2 = await requestScan({ config: pathMod.join(dir, 'missing.yml'), roots: [dir], timeoutMs: 60000 });
|
|
493
|
-
expect(r2.ok).toBe(false);
|
|
494
|
-
|
|
495
|
-
const live = ensureDaemon('python');
|
|
496
|
-
expect(live.ok).toBe(true);
|
|
497
|
-
expect(live.value.proc.killed).toBe(false);
|
|
498
|
-
})();
|
|
499
|
-
}, 180000);
|
|
500
|
-
|
|
501
|
-
it('a failed request surfaces as ok:false, never as an empty discovery', async () => {
|
|
502
|
-
// The client contract: there is no shape in which "the daemon did not answer"
|
|
503
|
-
// is expressed as "the daemon found nothing". Were this ever {ok:true,
|
|
504
|
-
// payload:{results:[]}}, the gate would report PASS on a dead daemon.
|
|
505
|
-
const r = await requestScan({
|
|
506
|
-
config: pathMod.join(dir, 'no-such-rules.yml'),
|
|
507
|
-
roots: [pathMod.join(dir, 't.js')],
|
|
508
|
-
timeoutMs: 60000,
|
|
509
|
-
});
|
|
510
|
-
expect(r.ok).toBe(false);
|
|
511
|
-
expect(typeof r.error).toBe('string');
|
|
512
|
-
expect(r.payload).toBeUndefined();
|
|
513
|
-
}, 120000);
|
|
514
|
-
});
|