@dataparade/cli 0.0.26 → 0.0.29
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 +56 -13
- package/dist/package.json +7 -7
- package/dist/src/ai-enrichment/providers/platform-proxy-provider.js +0 -1
- package/dist/src/cli.d.ts +3 -0
- package/dist/src/cli.js +102 -68
- package/dist/src/discoveries/contract/ocsf-discovery-record.d.ts +1 -1
- package/dist/src/discoveries/load-ocsf-discoveries.d.ts +0 -8
- package/dist/src/discoveries/load-ocsf-discoveries.js +0 -31
- package/dist/src/discoveries/write-ocsf-discoveries.d.ts +0 -8
- package/dist/src/discoveries/write-ocsf-discoveries.js +0 -38
- package/dist/src/evals/run-model-matrix.js +0 -1
- package/dist/src/export/export-ocsf.d.ts +18 -0
- package/dist/src/export/export-ocsf.js +104 -0
- package/dist/src/export/register-export-commands.d.ts +2 -0
- package/dist/src/export/register-export-commands.js +30 -0
- package/dist/src/facts/apply-answers.d.ts +29 -0
- package/dist/src/facts/apply-answers.js +202 -0
- package/dist/src/facts/build-facts.d.ts +24 -0
- package/dist/src/facts/build-facts.js +198 -0
- package/dist/src/facts/io.d.ts +17 -0
- package/dist/src/facts/io.js +67 -0
- package/dist/src/facts/layout.d.ts +29 -0
- package/dist/src/facts/layout.js +38 -0
- package/dist/src/facts/schema.d.ts +265 -0
- package/dist/src/facts/schema.js +122 -0
- package/dist/src/facts/slug.d.ts +7 -0
- package/dist/src/facts/slug.js +47 -0
- package/dist/src/kb/brief.d.ts +1 -2
- package/dist/src/kb/brief.js +2 -4
- package/dist/src/kb/interview-record.d.ts +18 -0
- package/dist/src/kb/interview-record.js +70 -0
- package/dist/src/kb/kb-store.d.ts +24 -18
- package/dist/src/kb/kb-store.js +89 -28
- package/dist/src/kb/land.d.ts +32 -12
- package/dist/src/kb/land.js +97 -90
- package/dist/src/kb/migrate.d.ts +23 -0
- package/dist/src/kb/migrate.js +91 -0
- package/dist/src/kb/project-diagram.js +4 -4
- package/dist/src/kb/register-kb-commands.js +71 -20
- package/dist/src/observability/scan-sentry.js +0 -1
- package/dist/src/output/scan-output-excludes.d.ts +1 -1
- package/dist/src/output/scan-output-excludes.js +14 -5
- package/dist/src/output/write-scan-knowledge.d.ts +33 -0
- package/dist/src/output/write-scan-knowledge.js +115 -0
- package/dist/src/platform-api/estimate-scan-footprint.d.ts +31 -0
- package/dist/src/platform-api/estimate-scan-footprint.js +75 -2
- package/dist/src/platform-api/fetch-failure.d.ts +5 -0
- package/dist/src/platform-api/fetch-failure.js +13 -0
- package/dist/src/platform-api/upload-client.d.ts +5 -0
- package/dist/src/platform-api/upload-client.js +11 -1
- package/dist/src/ui/scan-progress-renderer.d.ts +16 -0
- package/dist/src/ui/scan-progress-renderer.js +29 -3
- package/dist/src/ui/teaching-lines.js +18 -6
- package/dist/src/upload/collect-scan-sidecars.d.ts +2 -27
- package/dist/src/upload/collect-scan-sidecars.js +25 -121
- package/dist/src/upload/run-upload.js +0 -3
- package/dist/tests/cli/cli.spec.js +30 -13
- package/dist/tests/helpers/facts-fixture.d.ts +9 -0
- package/dist/tests/helpers/facts-fixture.js +72 -0
- package/dist/tests/unit/cli/scan-ai-session-fallback.spec.js +156 -0
- package/dist/tests/unit/export/export-ocsf.spec.js +72 -0
- package/dist/tests/unit/facts/apply-answers.spec.d.ts +1 -0
- package/dist/tests/unit/facts/apply-answers.spec.js +128 -0
- package/dist/tests/unit/facts/build-facts.spec.d.ts +1 -0
- package/dist/tests/unit/facts/build-facts.spec.js +161 -0
- package/dist/tests/unit/facts/slug.spec.d.ts +1 -0
- package/dist/tests/unit/facts/slug.spec.js +30 -0
- package/dist/tests/unit/kb/kb-interview-loop.spec.js +83 -35
- package/dist/tests/unit/kb/migrate.spec.d.ts +1 -0
- package/dist/tests/unit/kb/migrate.spec.js +108 -0
- package/dist/tests/unit/output/graphify-cache-dir.spec.d.ts +1 -0
- package/dist/tests/unit/output/graphify-cache-dir.spec.js +20 -0
- package/dist/tests/unit/output/scan-output-excludes.spec.js +4 -2
- package/dist/tests/unit/platform-api/estimate-scan-footprint.spec.js +77 -0
- package/dist/tests/unit/platform-api/upload-client.spec.js +2 -2
- package/dist/tests/unit/ui/scan-progress-renderer.spec.js +21 -0
- package/dist/tests/unit/ui/teaching-lines.spec.js +16 -0
- package/dist/tests/unit/upload/collect-scan-sidecars.spec.js +46 -46
- package/dist/tests/unit/upload/pack-scan-upload.spec.js +6 -6
- package/dist/vendor/scanner/dist/src/classifier/component-factory.js +6 -14
- package/dist/vendor/scanner/dist/src/classifier/external-url-third-party.d.ts +1 -0
- package/dist/vendor/scanner/dist/src/classifier/external-url-third-party.js +32 -1
- package/dist/vendor/scanner/dist/src/classifier/naming.d.ts +5 -0
- package/dist/vendor/scanner/dist/src/classifier/naming.js +8 -4
- package/dist/vendor/scanner/dist/src/core/pipeline/build-scan-personal-data-layers.js +26 -14
- package/dist/vendor/scanner/dist/src/core/pipeline/enrich-personal-data-layers.js +3 -1
- package/dist/vendor/scanner/dist/src/data-flow/intra-component-lineage.d.ts +18 -0
- package/dist/vendor/scanner/dist/src/data-flow/intra-component-lineage.js +125 -66
- package/dist/vendor/scanner/dist/src/index.d.ts +2 -0
- package/dist/vendor/scanner/dist/src/index.js +7 -2
- package/dist/vendor/scanner/dist/src/ingest/file-system.d.ts +5 -0
- package/dist/vendor/scanner/dist/src/ingest/file-system.js +5 -0
- package/dist/vendor/scanner/dist/src/ingest/gitignore.d.ts +17 -0
- package/dist/vendor/scanner/dist/src/ingest/gitignore.js +47 -9
- package/dist/vendor/scanner/dist/src/ingest/schema-files.d.ts +10 -2
- package/dist/vendor/scanner/dist/src/ingest/schema-files.js +24 -7
- package/dist/vendor/scanner/dist/src/patterns/frontend-frameworks.d.ts +3 -0
- package/dist/vendor/scanner/dist/src/patterns/frontend-frameworks.js +45 -0
- package/dist/vendor/scanner/dist/src/patterns/scan-exclusions.js +7 -0
- package/dist/vendor/scanner/dist/src/pii-signals/match-pii-signals.d.ts +24 -0
- package/dist/vendor/scanner/dist/src/pii-signals/match-pii-signals.js +19 -6
- package/dist/vendor/scanner/dist/src/pii-signals/occurrence-group.js +5 -2
- package/dist/vendor/scanner/dist/src/pii-signals/pii-signal-aliases.js +5 -2
- package/dist/vendor/scanner/patterns/classifier/third-party.classifier.yaml +8 -0
- package/dist/vendor/scanner/patterns/third-party.patterns.yaml +9 -0
- package/package.json +7 -7
- package/dist/src/discoveries/project-collected-diagram.d.ts +0 -7
- package/dist/src/discoveries/project-collected-diagram.js +0 -182
- package/dist/src/discoveries/project-ocsf-to-discoveries-document.d.ts +0 -16
- package/dist/src/discoveries/project-ocsf-to-discoveries-document.js +0 -269
- package/dist/src/discoveries/scan-result-to-discovery-input.d.ts +0 -13
- package/dist/src/discoveries/scan-result-to-discovery-input.js +0 -60
- package/dist/src/output/write-knowledge-graph.d.ts +0 -20
- package/dist/src/output/write-knowledge-graph.js +0 -53
- package/dist/src/output/write-scan-discoveries.d.ts +0 -17
- package/dist/src/output/write-scan-discoveries.js +0 -61
- package/dist/tests/unit/discoveries/project-collected-diagram.spec.js +0 -97
- package/dist/tests/unit/discoveries/rescan-replaces-scan-records.spec.js +0 -89
- /package/dist/tests/unit/{discoveries/project-collected-diagram.spec.d.ts → cli/scan-ai-session-fallback.spec.d.ts} +0 -0
- /package/dist/tests/unit/{discoveries/rescan-replaces-scan-records.spec.d.ts → export/export-ocsf.spec.d.ts} +0 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const os_1 = __importDefault(require("os"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const cli_1 = require("../../../src/cli");
|
|
10
|
+
function jsonResponse(body, status = 200) {
|
|
11
|
+
return {
|
|
12
|
+
ok: status >= 200 && status < 300,
|
|
13
|
+
status,
|
|
14
|
+
statusText: status === 200 ? 'OK' : 'Error',
|
|
15
|
+
json: async () => body,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const IP_CAP_MESSAGE = 'Too many anonymous CLI scans from this IP (max 3). Create an account or use a workspace API key.';
|
|
19
|
+
describe('scan keeps going when AI enrichment or upload is unavailable', () => {
|
|
20
|
+
let tempRoot;
|
|
21
|
+
let projectDir;
|
|
22
|
+
let outputPath;
|
|
23
|
+
let fetchMock;
|
|
24
|
+
let logSpy;
|
|
25
|
+
let warnSpy;
|
|
26
|
+
let errorSpy;
|
|
27
|
+
const printed = () => [logSpy, warnSpy, errorSpy]
|
|
28
|
+
.flatMap((spy) => spy.mock.calls.map((call) => call.join(' ')))
|
|
29
|
+
.join('\n');
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
tempRoot = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'dp-scan-fallback-'));
|
|
32
|
+
projectDir = path_1.default.join(tempRoot, 'project');
|
|
33
|
+
fs_1.default.mkdirSync(projectDir);
|
|
34
|
+
fs_1.default.writeFileSync(path_1.default.join(projectDir, 'index.ts'), "export const handler = () => fetch('https://api.stripe.com/v1/charges');\n");
|
|
35
|
+
outputPath = path_1.default.join(tempRoot, 'out', 'dataflow.json');
|
|
36
|
+
fetchMock = jest.fn();
|
|
37
|
+
global.fetch = fetchMock;
|
|
38
|
+
logSpy = jest.spyOn(console, 'log').mockImplementation(() => { });
|
|
39
|
+
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => { });
|
|
40
|
+
errorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
|
|
41
|
+
delete process.env.DATAPARADE_WORKSPACE_API_KEY;
|
|
42
|
+
delete process.env.SCAN_HOSTED_INFER_PROXY_URL;
|
|
43
|
+
process.exitCode = 0;
|
|
44
|
+
});
|
|
45
|
+
afterEach(() => {
|
|
46
|
+
logSpy.mockRestore();
|
|
47
|
+
warnSpy.mockRestore();
|
|
48
|
+
errorSpy.mockRestore();
|
|
49
|
+
fs_1.default.rmSync(tempRoot, { recursive: true, force: true });
|
|
50
|
+
delete process.env.DATAPARADE_SKIP_AUTO_UPLOAD;
|
|
51
|
+
process.exitCode = 0;
|
|
52
|
+
});
|
|
53
|
+
it('runs a structural scan when the anonymous AI session cannot reach the API', async () => {
|
|
54
|
+
process.env.DATAPARADE_SKIP_AUTO_UPLOAD = 'true';
|
|
55
|
+
fetchMock.mockRejectedValue(Object.assign(new TypeError('fetch failed'), {
|
|
56
|
+
cause: { code: 'ECONNREFUSED', message: 'connect ECONNREFUSED' },
|
|
57
|
+
}));
|
|
58
|
+
await (0, cli_1.run)([
|
|
59
|
+
'node',
|
|
60
|
+
'cli',
|
|
61
|
+
'scan',
|
|
62
|
+
projectDir,
|
|
63
|
+
'--ai-inference',
|
|
64
|
+
'--output',
|
|
65
|
+
outputPath,
|
|
66
|
+
]);
|
|
67
|
+
expect(String(fetchMock.mock.calls[0][0])).toContain('/api/scans/cli/ai/anonymous-session');
|
|
68
|
+
expect(process.exitCode ?? 0).toBe(0);
|
|
69
|
+
expect(fs_1.default.existsSync(outputPath)).toBe(true);
|
|
70
|
+
expect(fs_1.default.existsSync(path_1.default.join(tempRoot, 'out', 'facts.json'))).toBe(true);
|
|
71
|
+
const output = printed();
|
|
72
|
+
expect(output).toContain('AI enrichment unavailable (could not reach the DataParade API: ECONNREFUSED: connect ECONNREFUSED); continuing with a structural scan. Use --no-ai-inference to skip this step.');
|
|
73
|
+
expect(output).not.toContain('Scan failed');
|
|
74
|
+
});
|
|
75
|
+
it('runs a structural scan when the anonymous per-IP cap is reached', async () => {
|
|
76
|
+
process.env.DATAPARADE_SKIP_AUTO_UPLOAD = 'true';
|
|
77
|
+
fetchMock.mockResolvedValue(jsonResponse({ statusCode: 429, message: IP_CAP_MESSAGE }, 429));
|
|
78
|
+
await (0, cli_1.run)([
|
|
79
|
+
'node',
|
|
80
|
+
'cli',
|
|
81
|
+
'scan',
|
|
82
|
+
projectDir,
|
|
83
|
+
'--ai-inference',
|
|
84
|
+
'--output',
|
|
85
|
+
outputPath,
|
|
86
|
+
]);
|
|
87
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
88
|
+
expect(process.exitCode ?? 0).toBe(0);
|
|
89
|
+
expect(fs_1.default.existsSync(outputPath)).toBe(true);
|
|
90
|
+
expect(printed()).toContain(`AI enrichment unavailable (${IP_CAP_MESSAGE.replace(/\.$/, '')}); continuing with a structural scan.`);
|
|
91
|
+
});
|
|
92
|
+
it('runs a structural scan when the workspace preflight cannot reach the API', async () => {
|
|
93
|
+
process.env.DATAPARADE_WORKSPACE_API_KEY = 'dp_live_test';
|
|
94
|
+
process.env.DATAPARADE_SKIP_AUTO_UPLOAD = 'true';
|
|
95
|
+
fetchMock.mockRejectedValue(new TypeError('fetch failed'));
|
|
96
|
+
await (0, cli_1.run)([
|
|
97
|
+
'node',
|
|
98
|
+
'cli',
|
|
99
|
+
'scan',
|
|
100
|
+
projectDir,
|
|
101
|
+
'--ai-inference',
|
|
102
|
+
'--output',
|
|
103
|
+
outputPath,
|
|
104
|
+
]);
|
|
105
|
+
expect(String(fetchMock.mock.calls[0][0])).toContain('/api/scans/cli/preflight');
|
|
106
|
+
expect(process.exitCode ?? 0).toBe(0);
|
|
107
|
+
expect(fs_1.default.existsSync(outputPath)).toBe(true);
|
|
108
|
+
expect(printed()).toContain('AI enrichment unavailable (');
|
|
109
|
+
});
|
|
110
|
+
it('says the upload did not complete and how to retry when it is refused', async () => {
|
|
111
|
+
fetchMock.mockImplementation(async (url) => {
|
|
112
|
+
if (String(url).includes('/api/scans/cli/preview')) {
|
|
113
|
+
return jsonResponse({ statusCode: 429, message: IP_CAP_MESSAGE }, 429);
|
|
114
|
+
}
|
|
115
|
+
throw new Error(`unexpected fetch ${String(url)}`);
|
|
116
|
+
});
|
|
117
|
+
await (0, cli_1.run)([
|
|
118
|
+
'node',
|
|
119
|
+
'cli',
|
|
120
|
+
'scan',
|
|
121
|
+
projectDir,
|
|
122
|
+
'--no-ai-inference',
|
|
123
|
+
'--output',
|
|
124
|
+
outputPath,
|
|
125
|
+
]);
|
|
126
|
+
expect(process.exitCode ?? 0).toBe(0);
|
|
127
|
+
expect(fs_1.default.existsSync(outputPath)).toBe(true);
|
|
128
|
+
const output = printed();
|
|
129
|
+
expect(output).toContain(`[scan] Auto-upload failed: ${IP_CAP_MESSAGE}`);
|
|
130
|
+
expect(output).toContain('The upload did not complete:');
|
|
131
|
+
const relative = path_1.default.relative(process.cwd(), outputPath);
|
|
132
|
+
const expectedTarget = relative.startsWith('..') ? outputPath : relative;
|
|
133
|
+
expect(output).toContain(`dataparade upload ${expectedTarget}`);
|
|
134
|
+
expect(output).toContain('DATAPARADE_WORKSPACE_API_KEY');
|
|
135
|
+
expect(output).toContain('Scan finished. The upload did not complete.');
|
|
136
|
+
expect(output).not.toContain('All done — nice work.');
|
|
137
|
+
});
|
|
138
|
+
it('gives the retry command without the API key hint on a network failure', async () => {
|
|
139
|
+
fetchMock.mockRejectedValue(new TypeError('fetch failed'));
|
|
140
|
+
await (0, cli_1.run)([
|
|
141
|
+
'node',
|
|
142
|
+
'cli',
|
|
143
|
+
'scan',
|
|
144
|
+
projectDir,
|
|
145
|
+
'--no-ai-inference',
|
|
146
|
+
'--output',
|
|
147
|
+
outputPath,
|
|
148
|
+
]);
|
|
149
|
+
expect(process.exitCode ?? 0).toBe(0);
|
|
150
|
+
const output = printed();
|
|
151
|
+
expect(output).toContain('The upload did not complete: could not reach the DataParade API');
|
|
152
|
+
expect(output).toContain('dataparade upload ');
|
|
153
|
+
expect(output).not.toContain('DATAPARADE_WORKSPACE_API_KEY');
|
|
154
|
+
expect(output).not.toContain('All done — nice work.');
|
|
155
|
+
});
|
|
156
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const os_1 = __importDefault(require("os"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const load_ocsf_discoveries_1 = require("../../../src/discoveries/load-ocsf-discoveries");
|
|
10
|
+
const export_ocsf_1 = require("../../../src/export/export-ocsf");
|
|
11
|
+
const brief_1 = require("../../../src/kb/brief");
|
|
12
|
+
const land_1 = require("../../../src/kb/land");
|
|
13
|
+
const facts_fixture_1 = require("../../helpers/facts-fixture");
|
|
14
|
+
describe("export ocsf", () => {
|
|
15
|
+
let paths;
|
|
16
|
+
let out;
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
paths = (0, facts_fixture_1.makeKbDir)();
|
|
19
|
+
out = path_1.default.join(fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "dp-export-")), "ocsf");
|
|
20
|
+
});
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
fs_1.default.rmSync(paths.dir, { recursive: true, force: true });
|
|
23
|
+
fs_1.default.rmSync(path_1.default.dirname(out), { recursive: true, force: true });
|
|
24
|
+
});
|
|
25
|
+
const slot = (records, name) => records.filter((r) => r.dataparade.asserted_slot === name).map((r) => r.dataparade.asserted_value);
|
|
26
|
+
it("writes valid OCSF records for the Facts, with no absolute paths", () => {
|
|
27
|
+
const result = (0, export_ocsf_1.exportOcsf)(paths, out);
|
|
28
|
+
expect(result.interviewRecords).toBe(0);
|
|
29
|
+
expect(result.records).toBe(result.scanRecords);
|
|
30
|
+
const { records } = (0, load_ocsf_discoveries_1.loadOcsfDiscoveriesFromDir)(out);
|
|
31
|
+
expect(records).toHaveLength(result.records);
|
|
32
|
+
expect(slot(records, "name").sort()).toEqual(["Customer", "Sendgrid", "backend"]);
|
|
33
|
+
expect(records.some((r) => r.dataparade.asserts === "dp:scan/entity/occurrence:email:src/a.ts:3")).toBe(true);
|
|
34
|
+
expect(records.some((r) => r.dataparade.asserts === "dp:scan/entity/data_item:email")).toBe(true);
|
|
35
|
+
expect(JSON.stringify(records)).not.toContain("/Users/");
|
|
36
|
+
});
|
|
37
|
+
it("exports the applied state: overrides in, removed Facts out", () => {
|
|
38
|
+
(0, land_1.landReviewAnswer)(paths, { target: "cmp_mail", action: "set", property: "name", value: "SendGrid Inc", reviewer: "dev@example.com" });
|
|
39
|
+
(0, land_1.landReviewAnswer)(paths, { target: "data_item:phone", action: "remove", reviewer: "dev@example.com" });
|
|
40
|
+
(0, export_ocsf_1.exportOcsf)(paths, out);
|
|
41
|
+
const { records } = (0, load_ocsf_discoveries_1.loadOcsfDiscoveriesFromDir)(out);
|
|
42
|
+
expect(slot(records, "name")).toContain("SendGrid Inc");
|
|
43
|
+
expect(slot(records, "name")).not.toContain("Sendgrid");
|
|
44
|
+
const asserted = records.map((r) => r.dataparade.asserts);
|
|
45
|
+
expect(asserted.some((a) => a.includes("phone"))).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
it("exports interview answers as interview records that supersede the earlier answer", () => {
|
|
48
|
+
fs_1.default.mkdirSync(paths.evidenceDir, { recursive: true });
|
|
49
|
+
fs_1.default.writeFileSync(path_1.default.join(paths.dir, "evidence/s.md"), "Q/A");
|
|
50
|
+
const { sha } = (0, brief_1.writeKbBrief)(paths);
|
|
51
|
+
const land = (value, now) => (0, land_1.landInterviewAnswer)(paths, { briefSha: sha, target: "flow_mail", slot: "purpose", value, reviewer: "dev@example.com", evidence: "evidence/s.md", skillSha: "f".repeat(40), now });
|
|
52
|
+
land("marketing", new Date("2026-10-10T10:00:00.000Z"));
|
|
53
|
+
land("service_provision", new Date("2026-10-10T11:00:00.000Z"));
|
|
54
|
+
const result = (0, export_ocsf_1.exportOcsf)(paths, out);
|
|
55
|
+
expect(result.interviewRecords).toBe(2);
|
|
56
|
+
const interview = (0, load_ocsf_discoveries_1.loadOcsfDiscoveriesFromDir)(out).records.filter((r) => r.dataparade.source === "interview");
|
|
57
|
+
expect(interview.map((r) => r.dataparade.asserted_value).sort()).toEqual(["marketing", "service_provision"]);
|
|
58
|
+
const second = interview.find((r) => r.dataparade.asserted_value === "service_provision");
|
|
59
|
+
expect(second?.dataparade).toMatchObject({ asserts: "dp:scan/entity/flow_mail", reviewer: "dev@example.com", brief_sha: sha, raw_evidence_ref: "evidence/s.md" });
|
|
60
|
+
expect(second?.dataparade.supersedes).toBe(interview.find((r) => r.dataparade.asserted_value === "marketing")?.metadata.uid);
|
|
61
|
+
});
|
|
62
|
+
it("replaces an earlier export and refuses a folder that holds other files", () => {
|
|
63
|
+
(0, export_ocsf_1.exportOcsf)(paths, out);
|
|
64
|
+
expect(() => (0, export_ocsf_1.exportOcsf)(paths, out)).not.toThrow();
|
|
65
|
+
fs_1.default.writeFileSync(path_1.default.join(out, "notes.txt"), "mine");
|
|
66
|
+
expect(() => (0, export_ocsf_1.exportOcsf)(paths, out)).toThrow(/not OCSF records/);
|
|
67
|
+
expect(fs_1.default.existsSync(path_1.default.join(out, "notes.txt"))).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
it("refuses to export into the knowledge base folder", () => {
|
|
70
|
+
expect(() => (0, export_ocsf_1.exportOcsf)(paths, paths.dir)).toThrow(/separate folder/);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const apply_answers_1 = require("../../../src/facts/apply-answers");
|
|
4
|
+
const facts_fixture_1 = require("../../helpers/facts-fixture");
|
|
5
|
+
let counter = 0;
|
|
6
|
+
function answer(partial) {
|
|
7
|
+
counter += 1;
|
|
8
|
+
return {
|
|
9
|
+
id: `ans_${counter}`,
|
|
10
|
+
by: "dev@example.com",
|
|
11
|
+
at: `2026-10-10T00:00:${String(counter).padStart(2, "0")}.000Z`,
|
|
12
|
+
source: "review",
|
|
13
|
+
...partial,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
describe("applyAnswers", () => {
|
|
17
|
+
const { facts, files } = (0, facts_fixture_1.sampleFacts)();
|
|
18
|
+
it("returns the scan unchanged when there are no answers, and does not change its inputs", () => {
|
|
19
|
+
const before = JSON.stringify({ facts, files });
|
|
20
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, []);
|
|
21
|
+
expect(state.facts).toEqual(facts);
|
|
22
|
+
expect(state.dataItemFiles).toEqual(files);
|
|
23
|
+
expect(JSON.stringify({ facts, files })).toBe(before);
|
|
24
|
+
expect(state.facts).not.toBe(facts);
|
|
25
|
+
});
|
|
26
|
+
describe("set", () => {
|
|
27
|
+
it("overrides one property of a component and leaves the rest", () => {
|
|
28
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [answer({ factId: "cmp_mail", action: "set", property: "name", value: "SendGrid Inc" })]);
|
|
29
|
+
const mail = state.facts.components.find((c) => c.id === "cmp_mail");
|
|
30
|
+
expect(mail).toMatchObject({ name: "SendGrid Inc", type: "third_party", subType: "email" });
|
|
31
|
+
expect(state.applied).toHaveLength(1);
|
|
32
|
+
});
|
|
33
|
+
it("lets the latest answer for a property win, by time and not by position in the file", () => {
|
|
34
|
+
const late = answer({ factId: "flow_mail", action: "set", property: "purpose", value: "marketing", at: "2026-11-01T00:00:00.000Z" });
|
|
35
|
+
const early = answer({ factId: "flow_mail", action: "set", property: "purpose", value: "security", at: "2026-10-01T00:00:00.000Z" });
|
|
36
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [late, early]);
|
|
37
|
+
expect(state.facts.dataFlows.find((f) => f.id === "flow_mail").purpose).toBe("marketing");
|
|
38
|
+
});
|
|
39
|
+
it("adds a property the scan did not have", () => {
|
|
40
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [answer({ factId: "cmp_user", action: "set", property: "actor_kind", value: "persona" })]);
|
|
41
|
+
expect(state.facts.components.find((c) => c.id === "cmp_user").actor_kind).toBe("persona");
|
|
42
|
+
});
|
|
43
|
+
it("sets a data item in facts.json and in its file, and a group in both", () => {
|
|
44
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [
|
|
45
|
+
answer({ factId: "data_item:email", action: "set", property: "labels", value: ["contact", "pii"] }),
|
|
46
|
+
answer({ factId: "group:email:staff", action: "set", property: "label", value: "employees" }),
|
|
47
|
+
]);
|
|
48
|
+
expect(state.facts.dataItems[0].labels).toEqual(["contact", "pii"]);
|
|
49
|
+
expect(state.dataItemFiles[0].labels).toEqual(["contact", "pii"]);
|
|
50
|
+
expect(state.facts.dataItems[0].groups.find((g) => g.id === "group:email:staff")?.label).toBe("employees");
|
|
51
|
+
expect(state.dataItemFiles[0].groups.find((g) => g.id === "group:email:staff")?.label).toBe("employees");
|
|
52
|
+
});
|
|
53
|
+
it("sets a property of an occurrence", () => {
|
|
54
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [answer({ factId: "occurrence:email:src/a.ts:3", action: "set", property: "labels", value: ["verified"] })]);
|
|
55
|
+
expect(state.dataItemFiles[0].occurrences[0].labels).toEqual(["verified"]);
|
|
56
|
+
});
|
|
57
|
+
it("keeps answers about the system apart from Facts", () => {
|
|
58
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [answer({ factId: "dp:a0/system", action: "set", property: "in_scope", value: "The web app" })]);
|
|
59
|
+
expect(state.system).toEqual({ in_scope: "The web app" });
|
|
60
|
+
expect(state.facts).toEqual(facts);
|
|
61
|
+
});
|
|
62
|
+
it.each(["id", "file", "codeRefs", "groups", "occurrences"])("refuses to change %s", (property) => {
|
|
63
|
+
const a = answer({ factId: "cmp_api", action: "set", property, value: "x" });
|
|
64
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [a]);
|
|
65
|
+
expect(state.ignored).toEqual([a]);
|
|
66
|
+
expect(state.facts).toEqual(facts);
|
|
67
|
+
});
|
|
68
|
+
it("ignores a set without a property", () => {
|
|
69
|
+
const a = answer({ factId: "cmp_api", action: "set", value: "x" });
|
|
70
|
+
expect((0, apply_answers_1.applyAnswers)(facts, files, [a]).ignored).toEqual([a]);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
describe("remove", () => {
|
|
74
|
+
it("drops a flow and the relations that mention it", () => {
|
|
75
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [answer({ factId: "flow_mail", action: "remove" })]);
|
|
76
|
+
expect(state.facts.dataFlows.map((f) => f.id)).toEqual(["flow_login"]);
|
|
77
|
+
expect(state.facts.relations.map((r) => r.type)).toEqual(["handled_by"]);
|
|
78
|
+
});
|
|
79
|
+
it("drops a component with its flows and every handledBy reference to it", () => {
|
|
80
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [answer({ factId: "cmp_mail", action: "remove" })]);
|
|
81
|
+
expect(state.facts.components.map((c) => c.id)).toEqual(["cmp_api", "cmp_user"]);
|
|
82
|
+
expect(state.facts.dataFlows.map((f) => f.id)).toEqual(["flow_login"]);
|
|
83
|
+
const staff = state.facts.dataItems[0].groups.find((g) => g.id === "group:email:staff");
|
|
84
|
+
expect(staff?.handledBy).toEqual(["cmp_api"]);
|
|
85
|
+
expect(state.dataItemFiles[0].groups.find((g) => g.id === "group:email:staff")?.handledBy).toEqual(["cmp_api"]);
|
|
86
|
+
expect(state.facts.relations).toEqual([]);
|
|
87
|
+
});
|
|
88
|
+
it("drops a data item with its occurrences, its file and its entry in facts.json", () => {
|
|
89
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [answer({ factId: "data_item:email", action: "remove" })]);
|
|
90
|
+
expect(state.facts.dataItems.map((i) => i.id)).toEqual(["data_item:phone"]);
|
|
91
|
+
expect(state.dataItemFiles.map((f) => f.id)).toEqual(["data_item:phone"]);
|
|
92
|
+
expect(state.facts.relations.some((r) => r.source.startsWith("group:email"))).toBe(false);
|
|
93
|
+
});
|
|
94
|
+
it("drops a group with its occurrences and fixes the counts", () => {
|
|
95
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [answer({ factId: "group:email:customer", action: "remove" })]);
|
|
96
|
+
expect(state.dataItemFiles[0].occurrences.map((o) => o.id)).toEqual(["occurrence:email:src/c.ts:1"]);
|
|
97
|
+
expect(state.facts.dataItems[0].occurrenceCount).toBe(1);
|
|
98
|
+
expect(state.facts.dataItems[0].groups.map((g) => g.id)).toEqual(["group:email:staff"]);
|
|
99
|
+
});
|
|
100
|
+
it("drops one occurrence and fixes the counts", () => {
|
|
101
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [answer({ factId: "occurrence:email:src/a.ts:3", action: "remove" })]);
|
|
102
|
+
expect(state.dataItemFiles[0].occurrences).toHaveLength(2);
|
|
103
|
+
expect(state.facts.dataItems[0].occurrenceCount).toBe(2);
|
|
104
|
+
expect(state.facts.dataItems[0].groups.find((g) => g.id === "group:email:customer")?.occurrenceCount).toBe(1);
|
|
105
|
+
expect(state.dataItemFiles[0].groups.find((g) => g.id === "group:email:customer")?.occurrenceCount).toBe(1);
|
|
106
|
+
});
|
|
107
|
+
it("is final: a later set about a removed Fact is ignored, also through its parent", () => {
|
|
108
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [
|
|
109
|
+
answer({ factId: "data_item:phone", action: "remove" }),
|
|
110
|
+
answer({ factId: "data_item:phone", action: "set", property: "labels", value: ["x"] }),
|
|
111
|
+
answer({ factId: "occurrence:phone:src/a.ts:7", action: "set", property: "labels", value: ["x"] }),
|
|
112
|
+
]);
|
|
113
|
+
expect(state.ignored).toHaveLength(2);
|
|
114
|
+
expect(state.applied).toHaveLength(1);
|
|
115
|
+
});
|
|
116
|
+
it("cannot remove the system", () => {
|
|
117
|
+
const a = answer({ factId: "dp:a0/system", action: "remove" });
|
|
118
|
+
expect((0, apply_answers_1.applyAnswers)(facts, files, [a]).ignored).toEqual([a]);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
it("returns an answer about a Fact the scan does not have as an orphan, and applies the rest", () => {
|
|
122
|
+
const gone = answer({ factId: "flow_old", action: "set", property: "purpose", value: "security" });
|
|
123
|
+
const kept = answer({ factId: "cmp_api", action: "set", property: "name", value: "API" });
|
|
124
|
+
const state = (0, apply_answers_1.applyAnswers)(facts, files, [gone, kept]);
|
|
125
|
+
expect(state.orphans).toEqual([gone]);
|
|
126
|
+
expect(state.applied).toEqual([kept]);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const os_1 = __importDefault(require("os"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const build_facts_1 = require("../../../src/facts/build-facts");
|
|
10
|
+
const io_1 = require("../../../src/facts/io");
|
|
11
|
+
const layout_1 = require("../../../src/facts/layout");
|
|
12
|
+
const schema_1 = require("../../../src/facts/schema");
|
|
13
|
+
const ROOT = "/Users/dev/projects/shop";
|
|
14
|
+
function scanResult() {
|
|
15
|
+
return {
|
|
16
|
+
scanResult: {
|
|
17
|
+
components: [
|
|
18
|
+
{ id: "cmp_api", name: "api", type: "asset", subType: "service", confidence: 0.9, detectedFrom: [], sourceLocations: [{ filePath: `${ROOT}/src/api.ts`, startLine: 1, endLine: 2 }], properties: {} },
|
|
19
|
+
{ id: "cmp_mail", name: "Sendgrid", type: "third_party", confidence: 0.8, detectedFrom: [], sourceLocations: [], properties: {} },
|
|
20
|
+
],
|
|
21
|
+
dataFlows: [
|
|
22
|
+
{ id: "flow_mail", sourceComponentId: "cmp_api", targetComponentId: "cmp_mail", type: "api_call", confidence: 0.9, sourceLocations: [{ filePath: "src/api.ts", startLine: 5, endLine: 5 }] },
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
files: [],
|
|
26
|
+
findings: [],
|
|
27
|
+
occurrences: [
|
|
28
|
+
{ id: "occurrence:email:src/api.ts:4", filePath: "src/api.ts", startLine: 4, endLine: 4, code: "const email", labels: ["contact"], group: "email:customer", groupBasis: "column", declaration: { line: 3, kind: "const" } },
|
|
29
|
+
{ id: "occurrence:email:src/api.ts:3", filePath: "src/api.ts", startLine: 3, endLine: 3, labels: [], group: "email:customer", groupBasis: "column" },
|
|
30
|
+
{ id: `occurrence:email:${ROOT}/src/abs.ts:9`, filePath: `${ROOT}/src/abs.ts`, startLine: 9, endLine: 9, labels: [] },
|
|
31
|
+
{ id: "occurrence:Credit Card:src/pay.ts:2", filePath: "src/pay.ts", startLine: 2, endLine: 2, labels: [], location: "comment" },
|
|
32
|
+
],
|
|
33
|
+
dataItems: [
|
|
34
|
+
{ id: "data_item:email", labels: ["contact"], occurrenceIds: ["occurrence:email:src/api.ts:4", "occurrence:email:src/api.ts:3", `occurrence:email:${ROOT}/src/abs.ts:9`], groups: [{ id: "email:customer", occurrenceIds: ["occurrence:email:src/api.ts:4", "occurrence:email:src/api.ts:3"] }] },
|
|
35
|
+
{ id: "data_item:Credit Card", labels: [], occurrenceIds: ["occurrence:Credit Card:src/pay.ts:2"] },
|
|
36
|
+
],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function graph() {
|
|
40
|
+
const base = { confidence: "EXTRACTED", confidence_score: 1, source_file: null, source_location: null };
|
|
41
|
+
return {
|
|
42
|
+
directed: true,
|
|
43
|
+
multigraph: false,
|
|
44
|
+
graph: { schema: "dataparade-graph/1" },
|
|
45
|
+
nodes: [],
|
|
46
|
+
links: [
|
|
47
|
+
{ ...base, source: "dp::occurrence:email:src/api.ts:4", target: "src_api_ts_handler", relation: "occurs_in", target_kind: "function", target_file: "src/api.ts", target_line: 3 },
|
|
48
|
+
{ ...base, source: "dp::cmp_api", target: `${ROOT}/src/api.ts`, relation: "defined_in", target_kind: "file", target_file: `${ROOT}/src/api.ts`, target_line: 1 },
|
|
49
|
+
{ ...base, source: "dp::flow_mail", target: "src_api_ts_send", relation: "observed_in", target_kind: "function", target_file: "src/api.ts", target_line: 5 },
|
|
50
|
+
{ ...base, source: "dp::occurrence:email:src/api.ts:4", target: "dp::occurrence:email:src/api.ts:3", relation: "declared_by", declaration_kind: "const" },
|
|
51
|
+
{ ...base, source: "dp::occurrence:email:src/api.ts:4", target: "dp::group:email:customer", relation: "occurrence_of" },
|
|
52
|
+
{ ...base, source: "dp::group:email:customer", target: "dp::data_item:email", relation: "part_of" },
|
|
53
|
+
{ ...base, source: "dp::group:email:customer", target: "dp::cmp_api", relation: "handled_by", confidence: "INFERRED", confidence_score: 0.65, basis: "same_file" },
|
|
54
|
+
{ ...base, source: "dp::group:email:customer", target: "dp::cmp_api", relation: "handled_by", confidence: "INFERRED", confidence_score: 0.65, basis: "same_file" },
|
|
55
|
+
{ ...base, source: "dp::cmp_api", target: "dp::flow_mail", relation: "sends" },
|
|
56
|
+
{ ...base, source: "dp::flow_mail", target: "dp::cmp_mail", relation: "delivers_to" },
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const meta = { scanRoot: ROOT, projectName: "shop", cliVersion: "0.0.27", scannerVersion: "abc", scannedAt: "2026-10-09T00:00:00.000Z" };
|
|
61
|
+
describe("buildFacts", () => {
|
|
62
|
+
const { facts, dataItemFiles } = (0, build_facts_1.buildFacts)({ result: scanResult(), graph: graph(), ...meta });
|
|
63
|
+
it("matches the schemas", () => {
|
|
64
|
+
expect(() => schema_1.factsFileSchema.parse(facts)).not.toThrow();
|
|
65
|
+
for (const file of dataItemFiles.values())
|
|
66
|
+
expect(() => schema_1.dataItemFileSchema.parse(file)).not.toThrow();
|
|
67
|
+
});
|
|
68
|
+
it("writes components and flows with code refs, relative to the scan root", () => {
|
|
69
|
+
expect(facts.scan).toEqual({ projectName: "shop", scannedAt: "2026-10-09T00:00:00.000Z", cliVersion: "0.0.27", scannerVersion: "abc" });
|
|
70
|
+
const api = facts.components.find((c) => c.id === "cmp_api");
|
|
71
|
+
expect(api?.sourceLocations).toEqual([{ filePath: "src/api.ts", startLine: 1, endLine: 2 }]);
|
|
72
|
+
expect(api?.codeRefs).toEqual([{ nodeId: "src/api.ts", kind: "file", filePath: "src/api.ts", line: 1 }]);
|
|
73
|
+
expect(facts.components.find((c) => c.id === "cmp_mail")).toMatchObject({ subType: "", codeRefs: [] });
|
|
74
|
+
const flow = facts.dataFlows[0];
|
|
75
|
+
expect(schema_1.dataFlowFactSchema.parse(flow).codeRefs).toEqual([{ nodeId: "src_api_ts_send", kind: "function", filePath: "src/api.ts", line: 5 }]);
|
|
76
|
+
});
|
|
77
|
+
it("never contains the scan root, in any id or path", () => {
|
|
78
|
+
const everything = JSON.stringify([facts, [...dataItemFiles.values()]]);
|
|
79
|
+
expect(everything).not.toContain(ROOT);
|
|
80
|
+
expect(everything).not.toContain("/Users/");
|
|
81
|
+
});
|
|
82
|
+
it("summarises data items with groups, and points to one file per item", () => {
|
|
83
|
+
expect(facts.dataItems.map((i) => [i.id, i.file, i.occurrenceCount])).toEqual([
|
|
84
|
+
["data_item:Credit Card", "data-items/credit-card.json", 1],
|
|
85
|
+
["data_item:email", "data-items/email.json", 3],
|
|
86
|
+
]);
|
|
87
|
+
const email = facts.dataItems.find((i) => i.name === "email");
|
|
88
|
+
expect(email?.groups).toEqual([
|
|
89
|
+
{ id: "group:email:customer", label: "customer", basis: "column", occurrenceCount: 2, handledBy: ["cmp_api"] },
|
|
90
|
+
]);
|
|
91
|
+
expect([...dataItemFiles.keys()].sort()).toEqual(["data-items/credit-card.json", "data-items/email.json"]);
|
|
92
|
+
});
|
|
93
|
+
it("puts every occurrence once, in its data item file, with group, code refs and declaration", () => {
|
|
94
|
+
const email = dataItemFiles.get("data-items/email.json");
|
|
95
|
+
expect(email?.groups).toEqual(facts.dataItems.find((i) => i.name === "email")?.groups);
|
|
96
|
+
expect(email?.occurrences.map((o) => o.id)).toEqual([
|
|
97
|
+
"occurrence:email:src/abs.ts:9",
|
|
98
|
+
"occurrence:email:src/api.ts:3",
|
|
99
|
+
"occurrence:email:src/api.ts:4",
|
|
100
|
+
]);
|
|
101
|
+
const first = email?.occurrences.find((o) => o.id === "occurrence:email:src/api.ts:4");
|
|
102
|
+
expect(first).toMatchObject({
|
|
103
|
+
groupId: "group:email:customer",
|
|
104
|
+
filePath: "src/api.ts",
|
|
105
|
+
code: "const email",
|
|
106
|
+
codeRefs: [{ nodeId: "src_api_ts_handler", kind: "function", filePath: "src/api.ts", line: 3 }],
|
|
107
|
+
declaredBy: { id: "occurrence:email:src/api.ts:3", kind: "const" },
|
|
108
|
+
});
|
|
109
|
+
expect(email?.occurrences.find((o) => o.id.endsWith("abs.ts:9"))?.groupId).toBeNull();
|
|
110
|
+
expect(dataItemFiles.get("data-items/credit-card.json")?.occurrences[0].location).toBe("comment");
|
|
111
|
+
expect(JSON.stringify(facts)).not.toContain("occurrence:email:src/api.ts:4");
|
|
112
|
+
});
|
|
113
|
+
it("keeps the non-occurrence links as relations, once, without code or structure links", () => {
|
|
114
|
+
expect(facts.relations.map((r) => `${r.type} ${r.source} ${r.target}`)).toEqual([
|
|
115
|
+
"delivers_to flow_mail cmp_mail",
|
|
116
|
+
"handled_by group:email:customer cmp_api",
|
|
117
|
+
"sends cmp_api flow_mail",
|
|
118
|
+
]);
|
|
119
|
+
});
|
|
120
|
+
it("works without a knowledge graph", () => {
|
|
121
|
+
const bare = (0, build_facts_1.buildFacts)({ result: scanResult(), ...meta });
|
|
122
|
+
expect(bare.facts.relations).toEqual([]);
|
|
123
|
+
expect(bare.facts.components[0].codeRefs).toEqual([]);
|
|
124
|
+
expect(bare.facts.dataItems.find((i) => i.name === "email")?.groups[0].handledBy).toEqual([]);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe("writing and reading Facts", () => {
|
|
128
|
+
let dir;
|
|
129
|
+
beforeEach(() => {
|
|
130
|
+
dir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "dp-facts-"));
|
|
131
|
+
});
|
|
132
|
+
afterEach(() => fs_1.default.rmSync(dir, { recursive: true, force: true }));
|
|
133
|
+
it("writes facts.json and one file per data item, and reads them back", () => {
|
|
134
|
+
const layout = (0, layout_1.layoutForDir)(dir);
|
|
135
|
+
const built = (0, build_facts_1.buildFacts)({ result: scanResult(), graph: graph(), ...meta });
|
|
136
|
+
(0, io_1.writeFactsOutput)(layout, built.facts, built.dataItemFiles);
|
|
137
|
+
expect(fs_1.default.readdirSync(dir).sort()).toEqual(["data-items", "facts.json"]);
|
|
138
|
+
expect(fs_1.default.readdirSync(layout.dataItemsDir).sort()).toEqual(["credit-card.json", "email.json"]);
|
|
139
|
+
const facts = (0, io_1.readFacts)(layout);
|
|
140
|
+
expect(facts).toEqual(built.facts);
|
|
141
|
+
expect((0, io_1.readDataItemFiles)(layout, facts).map((f) => f.name)).toEqual(["Credit Card", "email"]);
|
|
142
|
+
});
|
|
143
|
+
it("replaces facts.json and removes stale data item files on a rescan, but keeps answers.json", () => {
|
|
144
|
+
const layout = (0, layout_1.layoutForDir)(dir);
|
|
145
|
+
const first = (0, build_facts_1.buildFacts)({ result: scanResult(), ...meta });
|
|
146
|
+
(0, io_1.writeFactsOutput)(layout, first.facts, first.dataItemFiles);
|
|
147
|
+
(0, io_1.appendAnswers)(layout, [{ id: "ans_1", factId: "cmp_api", action: "set", property: "name", value: "API", by: "dev@example.com", at: "2026-10-10T00:00:00.000Z", source: "review" }]);
|
|
148
|
+
const answersBefore = fs_1.default.readFileSync(layout.answersPath, "utf8");
|
|
149
|
+
const next = scanResult();
|
|
150
|
+
next.dataItems = next.dataItems.slice(0, 1);
|
|
151
|
+
const second = (0, build_facts_1.buildFacts)({ result: next, ...meta, scannedAt: "2026-10-11T00:00:00.000Z" });
|
|
152
|
+
(0, io_1.writeFactsOutput)(layout, second.facts, second.dataItemFiles);
|
|
153
|
+
expect(fs_1.default.readdirSync(layout.dataItemsDir)).toEqual(["email.json"]);
|
|
154
|
+
expect((0, io_1.readFacts)(layout).scan.scannedAt).toBe("2026-10-11T00:00:00.000Z");
|
|
155
|
+
expect(fs_1.default.readFileSync(layout.answersPath, "utf8")).toBe(answersBefore);
|
|
156
|
+
expect((0, io_1.readAnswers)(layout).answers).toHaveLength(1);
|
|
157
|
+
});
|
|
158
|
+
it("reads an empty answers list when answers.json does not exist", () => {
|
|
159
|
+
expect((0, io_1.readAnswers)((0, layout_1.layoutForDir)(dir))).toEqual({ schemaVersion: 1, answers: [] });
|
|
160
|
+
});
|
|
161
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const slug_1 = require("../../../src/facts/slug");
|
|
4
|
+
describe("dataItemSlugs", () => {
|
|
5
|
+
it("makes plain, file-safe slugs", () => {
|
|
6
|
+
const slugs = (0, slug_1.dataItemSlugs)(["email", "Credit Card", "a/b\\c:d", "ünïcode", " ", "../etc/passwd"]);
|
|
7
|
+
expect(slugs.get("email")).toBe("email");
|
|
8
|
+
expect(slugs.get("Credit Card")).toBe("credit-card");
|
|
9
|
+
expect(slugs.get("a/b\\c:d")).toBe("a-b-c-d");
|
|
10
|
+
expect(slugs.get("ünïcode")).toBe("unicode");
|
|
11
|
+
expect(slugs.get(" ")).toBe("item");
|
|
12
|
+
expect(slugs.get("../etc/passwd")).toBe("etc-passwd");
|
|
13
|
+
for (const slug of slugs.values())
|
|
14
|
+
expect(slug).toMatch(/^[a-z0-9][a-z0-9._-]*$/);
|
|
15
|
+
});
|
|
16
|
+
it("gives clashing names a hash of their own name, whatever the order", () => {
|
|
17
|
+
const one = (0, slug_1.dataItemSlugs)(["Email", "email"]);
|
|
18
|
+
const two = (0, slug_1.dataItemSlugs)(["email", "Email"]);
|
|
19
|
+
expect(one.get("Email")).toBe(two.get("Email"));
|
|
20
|
+
expect(one.get("email")).toBe(two.get("email"));
|
|
21
|
+
expect(one.get("Email")).not.toBe(one.get("email"));
|
|
22
|
+
expect(one.get("email")).toMatch(/^email-[0-9a-f]{6}$/);
|
|
23
|
+
});
|
|
24
|
+
it("keeps long names short", () => {
|
|
25
|
+
expect(((0, slug_1.dataItemSlugs)(["x".repeat(500)]).get("x".repeat(500)) ?? "").length).toBeLessThanOrEqual(80);
|
|
26
|
+
});
|
|
27
|
+
it("builds the path relative to the output folder", () => {
|
|
28
|
+
expect((0, slug_1.dataItemFilePath)("email")).toBe("data-items/email.json");
|
|
29
|
+
});
|
|
30
|
+
});
|