kodingo-cli 1.4.0 → 1.4.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.
@@ -0,0 +1,295 @@
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
+ /** Validation fixtures only; no production or customer identifiers. */
7
+ const strict_1 = __importDefault(require("node:assert/strict"));
8
+ const node_test_1 = require("node:test");
9
+ const deep_scan_1 = require("../commands/deep-scan");
10
+ const repository = "validation-org/validation-repo";
11
+ (0, node_test_1.describe)("decideBranchSettlement", () => {
12
+ (0, node_test_1.it)("proceeds silently when the tracked branch equals the checkout's branch", () => {
13
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "match", trackedBranch: "main", branch: "main", repository, yes: false });
14
+ strict_1.default.deepEqual(decision, { action: "proceed" });
15
+ });
16
+ (0, node_test_1.it)("never overwrites a different tracked branch, even if the caller asked to record", () => {
17
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "mismatch", trackedBranch: "main", branch: "feature/x", repository, yes: false, promptAnswerYes: true });
18
+ strict_1.default.equal(decision.action, "proceed-held");
19
+ if (decision.action === "proceed-held") {
20
+ strict_1.default.match(decision.message, /tracked branch for .* is "main"/i);
21
+ strict_1.default.match(decision.message, /held/i);
22
+ strict_1.default.match(decision.message, /feature\/x/);
23
+ }
24
+ });
25
+ (0, node_test_1.it)("records the branch when none is tracked and the user accepts the prompt", () => {
26
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "none", trackedBranch: null, branch: "main", repository, yes: false, promptAnswerYes: true });
27
+ strict_1.default.deepEqual(decision, { action: "record" });
28
+ });
29
+ (0, node_test_1.it)("holds with a plain message when none is tracked and the user declines", () => {
30
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "none", trackedBranch: null, branch: "main", repository, yes: false, promptAnswerYes: false });
31
+ strict_1.default.equal(decision.action, "proceed-held");
32
+ if (decision.action === "proceed-held") {
33
+ strict_1.default.match(decision.message, new RegExp(`No tracked branch is recorded for ${repository.replace(/[/]/g, "\\/")}`));
34
+ strict_1.default.match(decision.message, /held/i);
35
+ }
36
+ });
37
+ (0, node_test_1.it)("--yes records when none is tracked and the checkout matches the remote's default branch", () => {
38
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "none", trackedBranch: null, branch: "main", repository, yes: true, defaultBranch: "main" });
39
+ strict_1.default.deepEqual(decision, { action: "record" });
40
+ });
41
+ (0, node_test_1.it)("--yes refuses when none is tracked and the checkout does not match the remote's default branch", () => {
42
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "none", trackedBranch: null, branch: "feature/x", repository, yes: true, defaultBranch: "main" });
43
+ strict_1.default.equal(decision.action, "refuse");
44
+ if (decision.action === "refuse")
45
+ strict_1.default.match(decision.message, /does not match the remote's default branch/);
46
+ });
47
+ (0, node_test_1.it)("--yes refuses when none is tracked and the default branch could not be determined", () => {
48
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "none", trackedBranch: null, branch: "main", repository, yes: true, defaultBranch: null });
49
+ strict_1.default.equal(decision.action, "refuse");
50
+ if (decision.action === "refuse")
51
+ strict_1.default.match(decision.message, /could not be determined/);
52
+ });
53
+ });
54
+ (0, node_test_1.describe)("decideBranchSettlement — scanPending:false (the 'coverage already complete, no scan pending' path)", () => {
55
+ (0, node_test_1.it)("a mismatch names both branches and says commits will not update Kortex — never 'this scan will be held' (there is no scan)", () => {
56
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "mismatch", trackedBranch: "main", branch: "feature/x", repository, yes: false, scanPending: false });
57
+ strict_1.default.equal(decision.action, "proceed-held");
58
+ if (decision.action === "proceed-held") {
59
+ strict_1.default.match(decision.message, /This project tracks "main"/);
60
+ strict_1.default.match(decision.message, /commits on "feature\/x" will not update Kortex/);
61
+ strict_1.default.match(decision.message, /Switch branches or ask an org admin/);
62
+ strict_1.default.equal(/this scan will be held/i.test(decision.message), false);
63
+ }
64
+ });
65
+ (0, node_test_1.it)("--yes on a feature branch (not the default) still refuses without recording", () => {
66
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "none", trackedBranch: null, branch: "feature/x", repository, yes: true, defaultBranch: "main", scanPending: false });
67
+ strict_1.default.equal(decision.action, "refuse");
68
+ });
69
+ (0, node_test_1.it)("--yes on the default branch still records", () => {
70
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "none", trackedBranch: null, branch: "main", repository, yes: true, defaultBranch: "main", scanPending: false });
71
+ strict_1.default.deepEqual(decision, { action: "record" });
72
+ });
73
+ (0, node_test_1.it)("interactive accept still records, unaffected by scanPending", () => {
74
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "none", trackedBranch: null, branch: "main", repository, yes: false, promptAnswerYes: true, scanPending: false });
75
+ strict_1.default.deepEqual(decision, { action: "record" });
76
+ });
77
+ (0, node_test_1.it)("interactive decline says commits will not update Kortex, never 'this scan will be held'", () => {
78
+ const decision = (0, deep_scan_1.decideBranchSettlement)({ outcome: "none", trackedBranch: null, branch: "main", repository, yes: false, promptAnswerYes: false, scanPending: false });
79
+ strict_1.default.equal(decision.action, "proceed-held");
80
+ if (decision.action === "proceed-held") {
81
+ strict_1.default.match(decision.message, /Commits will not update Kortex until one is tracked/);
82
+ strict_1.default.equal(/this scan will be held/i.test(decision.message), false);
83
+ }
84
+ });
85
+ (0, node_test_1.it)("omitting scanPending (every existing caller) is byte-identical to the original first-scan wording", () => {
86
+ const withDefault = (0, deep_scan_1.decideBranchSettlement)({ outcome: "mismatch", trackedBranch: "main", branch: "feature/x", repository, yes: false });
87
+ const explicitTrue = (0, deep_scan_1.decideBranchSettlement)({ outcome: "mismatch", trackedBranch: "main", branch: "feature/x", repository, yes: false, scanPending: true });
88
+ strict_1.default.deepEqual(withDefault, explicitTrue);
89
+ });
90
+ });
91
+ (0, node_test_1.describe)("describeCoverageCompleteOutcome", () => {
92
+ (0, node_test_1.it)("is only the confident 'commits will keep it up to date' message when nothing is held", () => {
93
+ const message = (0, deep_scan_1.describeCoverageCompleteOutcome)(false);
94
+ strict_1.default.match(message, /Kortex already has a complete read of this project/);
95
+ strict_1.default.match(message, /Your commits will keep it up to date/);
96
+ });
97
+ (0, node_test_1.it)("drops the confident claim when the branch settlement is held (mismatch, or declined/refused)", () => {
98
+ const message = (0, deep_scan_1.describeCoverageCompleteOutcome)(true);
99
+ strict_1.default.match(message, /Kortex already has a complete read of this project/);
100
+ strict_1.default.equal(/Your commits will keep it up to date/.test(message), false);
101
+ });
102
+ });
103
+ (0, node_test_1.describe)("isFixedByTrackedBranchRecording", () => {
104
+ (0, node_test_1.it)("is fixed by recording: branch_unknown", () => {
105
+ strict_1.default.equal((0, deep_scan_1.isFixedByTrackedBranchRecording)("branch_unknown"), true);
106
+ });
107
+ (0, node_test_1.it)("is fixed by recording: a mismatch against an unregistered tracked branch", () => {
108
+ strict_1.default.equal((0, deep_scan_1.isFixedByTrackedBranchRecording)("branch_mismatch:feature/x:unregistered"), true);
109
+ });
110
+ (0, node_test_1.it)("is NOT fixed by recording: a mismatch against a real, different tracked branch", () => {
111
+ strict_1.default.equal((0, deep_scan_1.isFixedByTrackedBranchRecording)("branch_mismatch:feature/x:main"), false);
112
+ });
113
+ (0, node_test_1.it)("is NOT fixed by recording: dirty_checkout or detached_head", () => {
114
+ strict_1.default.equal((0, deep_scan_1.isFixedByTrackedBranchRecording)("dirty_checkout"), false);
115
+ strict_1.default.equal((0, deep_scan_1.isFixedByTrackedBranchRecording)("detached_head"), false);
116
+ });
117
+ (0, node_test_1.it)("is NOT fixed by recording: null/undefined (already applied, nothing held)", () => {
118
+ strict_1.default.equal((0, deep_scan_1.isFixedByTrackedBranchRecording)(null), false);
119
+ strict_1.default.equal((0, deep_scan_1.isFixedByTrackedBranchRecording)(undefined), false);
120
+ });
121
+ });
122
+ (0, node_test_1.describe)("isHeldScanFixable", () => {
123
+ (0, node_test_1.it)("prefers the server's own fixableByTrackedBranch when present — even when it disagrees with the reason string", () => {
124
+ strict_1.default.equal((0, deep_scan_1.isHeldScanFixable)({ reason: "dirty_checkout", fixableByTrackedBranch: true }), true);
125
+ strict_1.default.equal((0, deep_scan_1.isHeldScanFixable)({ reason: "branch_mismatch:feature/x:unregistered", fixableByTrackedBranch: false }), false);
126
+ });
127
+ (0, node_test_1.it)("falls back to the static reason-string check when the server (an older version) doesn't send fixableByTrackedBranch", () => {
128
+ strict_1.default.equal((0, deep_scan_1.isHeldScanFixable)({ reason: "branch_mismatch:feature/x:unregistered" }), true);
129
+ strict_1.default.equal((0, deep_scan_1.isHeldScanFixable)({ reason: "dirty_checkout" }), false);
130
+ });
131
+ (0, node_test_1.it)("re-checks a mismatch whose scanned branch now equals the tracked branch — via the server's authoritative signal, not just the 'unregistered' literal", () => {
132
+ // reason alone ("branch_mismatch:main:develop") looks unfixable to the
133
+ // static check (tracked was "develop" at upload time, not "unregistered")
134
+ // — but the server re-evaluated it against the CURRENT tracked branch
135
+ // ("main", now equal to the scan's own branch) and says it fixes it.
136
+ strict_1.default.equal((0, deep_scan_1.isHeldScanFixable)({ reason: "branch_mismatch:main:develop", fixableByTrackedBranch: true }), true);
137
+ });
138
+ });
139
+ (0, node_test_1.describe)("describeHeldScanDebugInfo", () => {
140
+ (0, node_test_1.it)("includes the tracked branch, the held scan's id/commit/reason, and the fixability decision", () => {
141
+ const line = (0, deep_scan_1.describeHeldScanDebugInfo)({
142
+ trackedBranch: "main",
143
+ lastCliScan: { id: "scan-1", commitSha: "deadbeef".repeat(5), reason: "branch_mismatch:main:unregistered", fixableByTrackedBranch: true },
144
+ fixable: true,
145
+ });
146
+ strict_1.default.match(line, /^\[debug\]/);
147
+ strict_1.default.match(line, /tracked branch=main/);
148
+ strict_1.default.match(line, /id=scan-1/);
149
+ strict_1.default.match(line, /commit=deadbeef/);
150
+ strict_1.default.match(line, /reason=branch_mismatch:main:unregistered/);
151
+ strict_1.default.match(line, /fixable=true/);
152
+ strict_1.default.match(line, /\(server\)/);
153
+ });
154
+ (0, node_test_1.it)("reports the reason-string fallback source when the server didn't send fixableByTrackedBranch", () => {
155
+ const line = (0, deep_scan_1.describeHeldScanDebugInfo)({
156
+ trackedBranch: "main",
157
+ lastCliScan: { id: "scan-1", commitSha: "a".repeat(40), reason: "branch_mismatch:main:unregistered" },
158
+ fixable: true,
159
+ });
160
+ strict_1.default.match(line, /\(reason-string fallback\)/);
161
+ });
162
+ (0, node_test_1.it)("reports no held scan and no tracked branch plainly", () => {
163
+ const line = (0, deep_scan_1.describeHeldScanDebugInfo)({ trackedBranch: null, lastCliScan: null, fixable: null });
164
+ strict_1.default.match(line, /tracked branch=none/);
165
+ strict_1.default.match(line, /no held CLI scan/);
166
+ strict_1.default.match(line, /fixable=n\/a/);
167
+ });
168
+ });
169
+ (0, node_test_1.describe)("describeLastUploadStatus — the hook's held-reason file feeding deep-scan-status", () => {
170
+ (0, node_test_1.it)("reports a held upload with its reason", () => {
171
+ const line = (0, deep_scan_1.describeLastUploadStatus)({ applied: false, reason: "branch_mismatch:feature/x:unregistered" });
172
+ strict_1.default.match(line, /^Your last commit upload was held: /);
173
+ strict_1.default.match(line, /no tracked branch is recorded/i);
174
+ });
175
+ (0, node_test_1.it)("reports nothing when the last recorded upload applied", () => {
176
+ strict_1.default.equal((0, deep_scan_1.describeLastUploadStatus)({ applied: true, reason: "clean_tracked_branch" }), null);
177
+ });
178
+ (0, node_test_1.it)("reports nothing when there's no local state at all", () => {
179
+ strict_1.default.equal((0, deep_scan_1.describeLastUploadStatus)(null), null);
180
+ strict_1.default.equal((0, deep_scan_1.describeLastUploadStatus)(undefined), null);
181
+ });
182
+ (0, node_test_1.it)("reports nothing when applied is missing (mid-upload, or the run never resolved)", () => {
183
+ strict_1.default.equal((0, deep_scan_1.describeLastUploadStatus)({ reason: "dirty_checkout" }), null);
184
+ });
185
+ });
186
+ (0, node_test_1.describe)("describeTrackedBranchNotRecorded", () => {
187
+ (0, node_test_1.it)("says so on its own line and gives the one command to fix it, when none is recorded", () => {
188
+ const line = (0, deep_scan_1.describeTrackedBranchNotRecorded)("none", "validation-org/validation-repo");
189
+ strict_1.default.match(line, /^No tracked branch is recorded for validation-org\/validation-repo\./);
190
+ strict_1.default.match(line, /`kodingo init`/);
191
+ });
192
+ (0, node_test_1.it)("adds nothing when a tracked branch already exists (match or mismatch)", () => {
193
+ strict_1.default.equal((0, deep_scan_1.describeTrackedBranchNotRecorded)("match", "org/repo"), null);
194
+ strict_1.default.equal((0, deep_scan_1.describeTrackedBranchNotRecorded)("mismatch", "org/repo"), null);
195
+ });
196
+ });
197
+ (0, node_test_1.describe)("describeApplyReason", () => {
198
+ (0, node_test_1.it)("maps stale_full_tree (the server's reapply refusal) to an actionable sentence", () => {
199
+ const { sentence, nextStep } = (0, deep_scan_1.describeApplyReason)("stale_full_tree");
200
+ strict_1.default.match(sentence, /full-repository scan/i);
201
+ strict_1.default.match(sentence, /no longer the tracked branch's current head/i);
202
+ strict_1.default.match(nextStep, /kodingo deep-scan/);
203
+ strict_1.default.match(nextStep, /init --force/);
204
+ });
205
+ (0, node_test_1.it)("maps dirty_checkout to an actionable sentence", () => {
206
+ const { sentence, nextStep } = (0, deep_scan_1.describeApplyReason)("dirty_checkout");
207
+ strict_1.default.match(sentence, /uncommitted changes/i);
208
+ strict_1.default.match(nextStep, /commit or stash/i);
209
+ });
210
+ (0, node_test_1.it)("maps detached_head to an actionable sentence", () => {
211
+ const { sentence, nextStep } = (0, deep_scan_1.describeApplyReason)("detached_head");
212
+ strict_1.default.match(sentence, /detached head/i);
213
+ strict_1.default.match(nextStep, /check out a branch/i);
214
+ });
215
+ (0, node_test_1.it)("maps an unregistered branch mismatch", () => {
216
+ const { sentence, nextStep } = (0, deep_scan_1.describeApplyReason)("branch_mismatch:main:unregistered");
217
+ strict_1.default.match(sentence, /no tracked branch is recorded/i);
218
+ strict_1.default.match(nextStep, /kodingo init/);
219
+ });
220
+ (0, node_test_1.it)("maps a real branch mismatch naming both branches and the switch/admin next step", () => {
221
+ const { sentence, nextStep } = (0, deep_scan_1.describeApplyReason)("branch_mismatch:feature/x:main");
222
+ strict_1.default.match(sentence, /tracked branch is "main"/);
223
+ strict_1.default.match(sentence, /ran on "feature\/x"/);
224
+ strict_1.default.match(nextStep, /switch to "main"/i);
225
+ strict_1.default.match(nextStep, /org admin/i);
226
+ });
227
+ (0, node_test_1.it)("maps clean_tracked_branch to an empty next step", () => {
228
+ const { sentence, nextStep } = (0, deep_scan_1.describeApplyReason)("clean_tracked_branch");
229
+ strict_1.default.equal(sentence, "Applied.");
230
+ strict_1.default.equal(nextStep, "");
231
+ });
232
+ });
233
+ (0, node_test_1.describe)("buildUploadSummary", () => {
234
+ (0, node_test_1.it)("reports read/upload/already-known counts accurately, matching the production regression", () => {
235
+ const summary = (0, deep_scan_1.buildUploadSummary)(165, 2, 163, "4 short evidence excerpts, each capped at 200 characters");
236
+ strict_1.default.match(summary, /165 files read locally/);
237
+ strict_1.default.match(summary, /2 will upload/);
238
+ strict_1.default.match(summary, /163 already known to Kortex/);
239
+ });
240
+ });
241
+ (0, node_test_1.describe)("renderScanStatus — never calls a GitHub backup record a CLI scan", () => {
242
+ (0, node_test_1.it)("a project with a real CLI scan: shows the local-scan line, omits the GitHub line", () => {
243
+ const lines = (0, deep_scan_1.renderScanStatus)({
244
+ coverage: 1, source: "cli",
245
+ lastCliScan: { commitSha: "b".repeat(40), applied: true, reason: "clean_tracked_branch", entityCount: 12, edgeCount: 5, fileCount: 2, completedAt: "2026-11-03T01:00:00.000Z" },
246
+ lastGithubScan: null,
247
+ });
248
+ strict_1.default.equal(lines[0], "Coverage: 100% (local scan)");
249
+ strict_1.default.match(lines[1], /^Last local scan: commit b{12} at 2026-11-03T01:00:00\.000Z — applied \(12 entities, 5 edges, 2 files\)\.$/);
250
+ strict_1.default.equal(lines.some((l) => l.startsWith("Last GitHub read:")), false);
251
+ strict_1.default.equal(lines.some((l) => l.includes("Last CLI scan")), false, "must never use the old, source-blind wording");
252
+ });
253
+ (0, node_test_1.it)("a project read only through GitHub: shows the GitHub-read line, never prints it as a CLI scan", () => {
254
+ const lines = (0, deep_scan_1.renderScanStatus)({
255
+ coverage: 1, source: "github",
256
+ lastCliScan: null,
257
+ lastGithubScan: { commitSha: "a".repeat(40), completedAt: "2026-11-03T05:00:00.000Z" },
258
+ });
259
+ strict_1.default.equal(lines[0], "Coverage: 100% (read through GitHub)");
260
+ strict_1.default.equal(lines[1], "No local scan yet.");
261
+ strict_1.default.match(lines[2], /^Last GitHub read: commit a{12} at 2026-11-03T05:00:00\.000Z\.$/);
262
+ });
263
+ (0, node_test_1.it)("neither a CLI nor a GitHub scan has run: no scan yet, no local scan yet, no GitHub line", () => {
264
+ const lines = (0, deep_scan_1.renderScanStatus)({ coverage: 0, source: null, lastCliScan: null, lastGithubScan: null });
265
+ strict_1.default.equal(lines[0], "Coverage: 0% (no scan yet)");
266
+ strict_1.default.equal(lines[1], "No local scan yet.");
267
+ strict_1.default.equal(lines.length, 2);
268
+ });
269
+ (0, node_test_1.it)("a held CLI scan is reported as held, with its reason — not as applied", () => {
270
+ const lines = (0, deep_scan_1.renderScanStatus)({
271
+ coverage: 0.5, source: "cli",
272
+ lastCliScan: { commitSha: "c".repeat(40), applied: false, reason: "dirty_checkout", entityCount: 0, edgeCount: 0, fileCount: 0, completedAt: "2026-11-03T02:00:00.000Z" },
273
+ lastGithubScan: null,
274
+ });
275
+ strict_1.default.match(lines[1], /^Last local scan: commit c{12} at 2026-11-03T02:00:00\.000Z — held\. Your working tree has uncommitted changes\./);
276
+ });
277
+ });
278
+ (0, node_test_1.describe)("shouldSkipIncrementalScan", () => {
279
+ (0, node_test_1.it)("skips only on a confirmed mismatch", () => {
280
+ strict_1.default.equal((0, deep_scan_1.shouldSkipIncrementalScan)("mismatch"), true);
281
+ });
282
+ (0, node_test_1.it)("never skips on a match or when nothing is tracked yet", () => {
283
+ strict_1.default.equal((0, deep_scan_1.shouldSkipIncrementalScan)("match"), false);
284
+ strict_1.default.equal((0, deep_scan_1.shouldSkipIncrementalScan)("none"), false);
285
+ });
286
+ });
287
+ (0, node_test_1.describe)("buildLocalReadSummary", () => {
288
+ (0, node_test_1.it)("reports only what's known locally — no upload/already-known split, since no session (and so no server data) exists yet", () => {
289
+ const summary = (0, deep_scan_1.buildLocalReadSummary)(165, "4 short evidence excerpts, each capped at 200 characters");
290
+ strict_1.default.match(summary, /165 files read locally/);
291
+ strict_1.default.match(summary, /checked against what Kortex already holds/);
292
+ strict_1.default.equal(/will upload/.test(summary), false);
293
+ strict_1.default.equal(/already known to Kortex/.test(summary), false);
294
+ });
295
+ });
@@ -0,0 +1,113 @@
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
+ /** Validation fixtures only; no production or customer identifiers.
7
+ * Proves nothing that needs the server (the upload session — which sends
8
+ * every file path, size, and blob SHA — and everything after it) happens
9
+ * before consent is granted. Uses a fake fetch client (same hand-rolled
10
+ * stub pattern as deepScanNetworkFlows.test.ts) that is only ever given
11
+ * exactly as many canned responses as the correctly-ordered flow should
12
+ * need — any extra, premature request throws "no mock response left",
13
+ * which fails the test.
14
+ */
15
+ const strict_1 = __importDefault(require("node:assert/strict"));
16
+ const node_fs_1 = __importDefault(require("node:fs"));
17
+ const node_os_1 = __importDefault(require("node:os"));
18
+ const node_path_1 = __importDefault(require("node:path"));
19
+ const node_child_process_1 = require("node:child_process");
20
+ const node_test_1 = require("node:test");
21
+ const deep_scan_1 = require("../commands/deep-scan");
22
+ function fakeFetch(responses) {
23
+ const calls = [];
24
+ const fn = async (url, init) => {
25
+ calls.push({ url, init });
26
+ const next = responses.shift();
27
+ if (!next)
28
+ throw new Error(`deepScanConsentOrdering test: no mock response left for ${url} — a request was made before consent (or beyond the expected flow)`);
29
+ return { ok: (next.status ?? 200) < 300, status: next.status ?? 200, json: async () => next.body };
30
+ };
31
+ return { fn, calls };
32
+ }
33
+ function withFetch(responses, run) {
34
+ const original = global.fetch;
35
+ const { fn, calls } = fakeFetch(responses);
36
+ global.fetch = fn;
37
+ return run(calls).finally(() => { global.fetch = original; });
38
+ }
39
+ function makeRepo() {
40
+ const root = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-consent-order-"));
41
+ (0, node_child_process_1.execFileSync)("git", ["init", "-q", "-b", "main", root]);
42
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.email", "test@example.invalid"]);
43
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.name", "Validation Test"]);
44
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "remote", "add", "origin", "git@github.com:validation/repository.git"]);
45
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "page.ts"), "export function Page() { return 1; }");
46
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "add", "."]);
47
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "commit", "-qm", "fixture"]);
48
+ return root;
49
+ }
50
+ function withConsoleLog(run) {
51
+ const logs = [];
52
+ const original = console.log;
53
+ console.log = (...args) => { logs.push(args.join(" ")); };
54
+ return run(logs).finally(() => { console.log = original; });
55
+ }
56
+ (0, node_test_1.describe)("deepScanCommand — nothing leaves the machine before consent", () => {
57
+ (0, node_test_1.it)("interactive mode (non-TTY declines automatically): only the policy check happens, nothing manifest-bearing is ever sent", async () => {
58
+ const root = makeRepo();
59
+ const oldToken = process.env.KODINGO_TOKEN;
60
+ process.env.KODINGO_TOKEN = "validation-token";
61
+ try {
62
+ await withConsoleLog((logs) => withFetch(
63
+ // Exactly one response: the org excerpt-policy check, which carries
64
+ // no repo/file data and is needed to decide what extraction
65
+ // captures. If the (buggy) old order were still in place, the
66
+ // session-creation call would consume a second, non-existent
67
+ // mock response and throw.
68
+ [{ body: { excerptsEnabled: true } }], async (calls) => {
69
+ await (0, deep_scan_1.deepScanCommand)({ repo: root, yes: false });
70
+ strict_1.default.equal(calls.length, 1, `expected only the policy call, got: ${JSON.stringify(calls.map((c) => c.url))}`);
71
+ strict_1.default.ok(logs.some((l) => l.includes("Upload cancelled.")), `expected a cancellation log, got: ${JSON.stringify(logs)}`);
72
+ }));
73
+ }
74
+ finally {
75
+ if (oldToken === undefined)
76
+ delete process.env.KODINGO_TOKEN;
77
+ else
78
+ process.env.KODINGO_TOKEN = oldToken;
79
+ }
80
+ });
81
+ (0, node_test_1.it)("--yes: consent is local-only and granted before any server call, then the full upload still completes in order", async () => {
82
+ const root = makeRepo();
83
+ const oldToken = process.env.KODINGO_TOKEN;
84
+ process.env.KODINGO_TOKEN = "validation-token";
85
+ try {
86
+ await withConsoleLog((logs) => withFetch([
87
+ { body: { excerptsEnabled: true } }, // GET /deep-scan/policy
88
+ { body: { sessionId: "sess-1", heldBlobShas: [], limits: { filesPerBatch: 200 } } }, // POST /deep-scan/sessions
89
+ { body: { accepted: 1 } }, // POST /deep-scan/sessions/:id/files
90
+ { body: { runId: "run-1" } }, // POST /deep-scan/sessions/:id/finalize
91
+ { body: { state: "completed", applied: true, entity_count: 1, edge_count: 0, file_count: 1 } }, // GET /deep-scan/runs/:id
92
+ ], async (calls) => {
93
+ await (0, deep_scan_1.deepScanCommand)({ repo: root, yes: true });
94
+ strict_1.default.equal(calls.length, 5, `expected exactly 5 calls, got: ${JSON.stringify(calls.map((c) => c.url))}`);
95
+ strict_1.default.match(calls[0].url, /\/deep-scan\/policy$/);
96
+ strict_1.default.match(calls[1].url, /\/deep-scan\/sessions$/);
97
+ const manifest = JSON.parse(calls[1].init.body);
98
+ strict_1.default.equal(manifest.files.length, 1);
99
+ strict_1.default.equal(manifest.files[0].path, "page.ts");
100
+ strict_1.default.match(calls[2].url, /\/deep-scan\/sessions\/sess-1\/files$/);
101
+ strict_1.default.match(calls[3].url, /\/deep-scan\/sessions\/sess-1\/finalize$/);
102
+ strict_1.default.match(calls[4].url, /\/deep-scan\/runs\/run-1$/);
103
+ strict_1.default.ok(logs.some((l) => /Deep scan applied: 1 entities, 0 edges, 1 files/.test(l)), `expected an applied summary line, got: ${JSON.stringify(logs)}`);
104
+ }));
105
+ }
106
+ finally {
107
+ if (oldToken === undefined)
108
+ delete process.env.KODINGO_TOKEN;
109
+ else
110
+ process.env.KODINGO_TOKEN = oldToken;
111
+ }
112
+ });
113
+ });
@@ -0,0 +1,136 @@
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
+ /** Validation fixtures only; no production or customer identifiers.
7
+ * The post-commit hook invokes `deep-scan --changed --yes` unconditionally,
8
+ * on every commit, regardless of branch. This covers the gate that keeps it
9
+ * from uploading a commit it can positively confirm isn't on the tracked
10
+ * branch — and proves a failure in that check never fails the commit (it
11
+ * fails open and the scan proceeds exactly as before this gate existed).
12
+ */
13
+ const strict_1 = __importDefault(require("node:assert/strict"));
14
+ const node_fs_1 = __importDefault(require("node:fs"));
15
+ const node_os_1 = __importDefault(require("node:os"));
16
+ const node_path_1 = __importDefault(require("node:path"));
17
+ const node_child_process_1 = require("node:child_process");
18
+ const node_test_1 = require("node:test");
19
+ const deep_scan_1 = require("../commands/deep-scan");
20
+ function fakeFetch(responses) {
21
+ const calls = [];
22
+ const fn = async (url, init) => {
23
+ calls.push({ url, init });
24
+ const next = responses.shift();
25
+ if (!next)
26
+ throw new Error(`deepScanHookBranchGate test: no mock response left for ${url}`);
27
+ return { ok: (next.status ?? 200) < 300, status: next.status ?? 200, json: async () => next.body };
28
+ };
29
+ return { fn, calls };
30
+ }
31
+ function withFetch(responses, run) {
32
+ const original = global.fetch;
33
+ const { fn, calls } = fakeFetch(responses);
34
+ global.fetch = fn;
35
+ return run(calls).finally(() => { global.fetch = original; });
36
+ }
37
+ function makeRepo() {
38
+ const root = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-hook-gate-"));
39
+ (0, node_child_process_1.execFileSync)("git", ["init", "-q", "-b", "feature/x", root]);
40
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.email", "test@example.invalid"]);
41
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.name", "Validation Test"]);
42
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "remote", "add", "origin", "git@github.com:validation/repository.git"]);
43
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "page.ts"), "export function Page() { return 1; }");
44
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "add", "."]);
45
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "commit", "-qm", "first commit — a root commit has no diff-tree parent to scope --changed against"]);
46
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "page.ts"), "export function Page() { return 2; }");
47
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "add", "."]);
48
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "commit", "-qm", "the commit the hook actually runs against"]);
49
+ return root;
50
+ }
51
+ function withConsoleLog(run) {
52
+ const logs = [];
53
+ const original = console.log;
54
+ console.log = (...args) => { logs.push(args.join(" ")); };
55
+ return run(logs).finally(() => { console.log = original; });
56
+ }
57
+ (0, node_test_1.describe)("deep-scan --changed (the hook's exact invocation) — upload only on the tracked branch", () => {
58
+ (0, node_test_1.it)("a confirmed mismatch skips the scan entirely: no further request is made, and the commit is never failed", async () => {
59
+ const root = makeRepo();
60
+ const oldToken = process.env.KODINGO_TOKEN;
61
+ process.env.KODINGO_TOKEN = "validation-token";
62
+ try {
63
+ await withConsoleLog((logs) => withFetch([{ body: { outcome: "mismatch", trackedBranch: "main" } }], async (calls) => {
64
+ await strict_1.default.doesNotReject(() => (0, deep_scan_1.deepScanCommand)({ repo: root, changed: true, yes: true }));
65
+ strict_1.default.equal(calls.length, 1, `expected only the branch-settle check, got: ${JSON.stringify(calls.map((c) => c.url))}`);
66
+ strict_1.default.match(calls[0].url, /\/deep-scan\/branch\/settle$/);
67
+ strict_1.default.ok(logs.some((l) => /Skipping incremental scan/.test(l)), `expected a skip log, got: ${JSON.stringify(logs)}`);
68
+ }));
69
+ }
70
+ finally {
71
+ if (oldToken === undefined)
72
+ delete process.env.KODINGO_TOKEN;
73
+ else
74
+ process.env.KODINGO_TOKEN = oldToken;
75
+ }
76
+ });
77
+ (0, node_test_1.it)("a match proceeds with the normal incremental scan and upload", async () => {
78
+ const root = makeRepo();
79
+ const oldToken = process.env.KODINGO_TOKEN;
80
+ process.env.KODINGO_TOKEN = "validation-token";
81
+ try {
82
+ await withFetch([
83
+ { body: { outcome: "match", trackedBranch: "feature/x" } }, // POST /deep-scan/branch/settle
84
+ { body: { excerptsEnabled: true } }, // GET /deep-scan/policy
85
+ { body: { sessionId: "sess-1", heldBlobShas: [], limits: { filesPerBatch: 200 } } }, // POST /deep-scan/sessions
86
+ { body: { accepted: 1 } }, // POST /deep-scan/sessions/:id/files
87
+ { body: { runId: "run-1" } }, // POST /deep-scan/sessions/:id/finalize
88
+ { body: { state: "completed", applied: true, entity_count: 1, edge_count: 0, file_count: 1 } }, // GET /deep-scan/runs/:id
89
+ ], async (calls) => {
90
+ await strict_1.default.doesNotReject(() => (0, deep_scan_1.deepScanCommand)({ repo: root, changed: true, yes: true }));
91
+ strict_1.default.equal(calls.length, 6, `expected all 6 calls to happen, got: ${JSON.stringify(calls.map((c) => c.url))}`);
92
+ });
93
+ }
94
+ finally {
95
+ if (oldToken === undefined)
96
+ delete process.env.KODINGO_TOKEN;
97
+ else
98
+ process.env.KODINGO_TOKEN = oldToken;
99
+ }
100
+ });
101
+ (0, node_test_1.it)("never fails the commit when the branch-settle check itself errors — fails open and scans anyway", async () => {
102
+ const root = makeRepo();
103
+ const oldToken = process.env.KODINGO_TOKEN;
104
+ process.env.KODINGO_TOKEN = "validation-token";
105
+ const calls = [];
106
+ const original = global.fetch;
107
+ global.fetch = async (url, init) => {
108
+ calls.push(url);
109
+ if (url.endsWith("/deep-scan/branch/settle"))
110
+ throw new Error("network down");
111
+ if (url.endsWith("/deep-scan/policy"))
112
+ return { ok: true, status: 200, json: async () => ({ excerptsEnabled: true }) };
113
+ if (url.endsWith("/deep-scan/sessions"))
114
+ return { ok: true, status: 201, json: async () => ({ sessionId: "sess-1", heldBlobShas: [], limits: { filesPerBatch: 200 } }) };
115
+ if (url.includes("/deep-scan/sessions/sess-1/files"))
116
+ return { ok: true, status: 200, json: async () => ({ accepted: 1 }) };
117
+ if (url.endsWith("/finalize"))
118
+ return { ok: true, status: 202, json: async () => ({ runId: "run-1" }) };
119
+ if (url.endsWith("/deep-scan/runs/run-1"))
120
+ return { ok: true, status: 200, json: async () => ({ state: "completed", applied: true, entity_count: 1, edge_count: 0, file_count: 1 }) };
121
+ throw new Error(`unexpected url: ${url}`);
122
+ };
123
+ try {
124
+ await strict_1.default.doesNotReject(() => (0, deep_scan_1.deepScanCommand)({ repo: root, changed: true, yes: true }));
125
+ strict_1.default.ok(calls.some((u) => u.endsWith("/deep-scan/branch/settle")));
126
+ strict_1.default.ok(calls.some((u) => u.endsWith("/deep-scan/sessions")), "the scan must still proceed after the gate check fails");
127
+ }
128
+ finally {
129
+ global.fetch = original;
130
+ if (oldToken === undefined)
131
+ delete process.env.KODINGO_TOKEN;
132
+ else
133
+ process.env.KODINGO_TOKEN = oldToken;
134
+ }
135
+ });
136
+ });
@@ -20,6 +20,60 @@ const deep_scan_1 = require("../commands/deep-scan");
20
20
  strict_1.default.equal((0, local_deep_scan_extractor_1.privacySafeExcerpt)(` first();\nsecond();`).trim(), "first()");
21
21
  strict_1.default.ok((0, local_deep_scan_extractor_1.privacySafeExcerpt)("x".repeat(500)).length <= 200);
22
22
  });
23
+ (0, node_test_1.it)("the commit hook's --changed scope is bounded by the commit diff, not by whether the local entity cache is populated — never a full-project extraction", () => {
24
+ const root = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-deep-scan-"));
25
+ (0, node_child_process_1.execFileSync)("git", ["init", "-q", root]);
26
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.email", "test@example.invalid"]);
27
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.name", "Validation Test"]);
28
+ node_fs_1.default.mkdirSync(node_path_1.default.join(root, "src"));
29
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "src", "a.ts"), "export function a() { return 1; }");
30
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "src", "b.ts"), "export function b() { return 2; }");
31
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "src", "c.ts"), "export function c() { return 3; }");
32
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "add", "."]);
33
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "commit", "-qm", "first commit — a new engineer's empty-cache clone starts here"]);
34
+ // A new engineer's clone has no .kortex/entity-cache at all yet — confirm
35
+ // that's genuinely true for this fixture before trusting the assertion.
36
+ strict_1.default.equal(node_fs_1.default.existsSync(node_path_1.default.join(root, ".kortex", "entity-cache")), false);
37
+ // Only one file changes in the NEXT commit — this is what the post-commit
38
+ // hook's `deep-scan --changed` runs against.
39
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "src", "b.ts"), "export function b() { return 22; }");
40
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "add", "."]);
41
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "commit", "-qm", "second commit — only b.ts changes"]);
42
+ const full = (0, deep_scan_1.listTrackedFiles)(root, false);
43
+ strict_1.default.deepEqual(full.map((f) => f.path).sort(), ["src/a.ts", "src/b.ts", "src/c.ts"]);
44
+ const changedOnly = (0, deep_scan_1.listTrackedFiles)(root, true);
45
+ strict_1.default.deepEqual(changedOnly.map((f) => f.path), ["src/b.ts"], "the hook's file set must be exactly the commit diff, never the whole tracked tree, regardless of cache state");
46
+ });
47
+ (0, node_test_1.it)("tracks declarative files (package manifests, lockfiles, deployment/compose files, plain YAML) — the production incident's root cause: these were silently excluded by a stale, hand-maintained extension list, so a fullTree apply deleted every entity ever produced for them, project-wide", () => {
48
+ const root = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-deep-scan-"));
49
+ (0, node_child_process_1.execFileSync)("git", ["init", "-q", root]);
50
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.email", "test@example.invalid"]);
51
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.name", "Validation Test"]);
52
+ node_fs_1.default.mkdirSync(node_path_1.default.join(root, "k8s"));
53
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "package.json"), JSON.stringify({ name: "my-app", dependencies: { react: "^18.2.0" } }));
54
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "package-lock.json"), JSON.stringify({ name: "my-app", lockfileVersion: 1 }));
55
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "docker-compose.yml"), "services:\n web:\n image: my-app:latest\n");
56
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "Dockerfile"), "FROM node:20\n");
57
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, "k8s", "deployment.yaml"), "apiVersion: apps/v1\nkind: Deployment\n");
58
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "add", "."]);
59
+ const files = (0, deep_scan_1.listTrackedFiles)(root).map((file) => file.path).sort();
60
+ strict_1.default.deepEqual(files, [
61
+ "Dockerfile",
62
+ "docker-compose.yml",
63
+ "k8s/deployment.yaml",
64
+ "package-lock.json",
65
+ "package.json",
66
+ ].sort());
67
+ });
68
+ (0, node_test_1.it)("tracks a committed .env.example template but never a real .env file", () => {
69
+ const root = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-deep-scan-"));
70
+ (0, node_child_process_1.execFileSync)("git", ["init", "-q", root]);
71
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, ".env.example"), "DATABASE_URL=postgres://localhost/app\n");
72
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, ".env"), "SECRET=value\n");
73
+ node_fs_1.default.writeFileSync(node_path_1.default.join(root, ".env.production"), "SECRET=value\n");
74
+ (0, node_child_process_1.execFileSync)("git", ["-C", root, "add", "-f", "."]);
75
+ strict_1.default.deepEqual((0, deep_scan_1.listTrackedFiles)(root).map((file) => file.path), [".env.example"]);
76
+ });
23
77
  (0, node_test_1.it)("honors git tracking, .kodingoignore, and env exclusion", () => {
24
78
  const root = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-deep-scan-"));
25
79
  (0, node_child_process_1.execFileSync)("git", ["init", "-q", root]);
@@ -51,6 +105,15 @@ const deep_scan_1 = require("../commands/deep-scan");
51
105
  strict_1.default.equal((0, deep_scan_1.normalizeRemote)("git@github.com:owner/repo.git"), "owner/repo");
52
106
  strict_1.default.equal((0, deep_scan_1.normalizeRemote)("https://github.com/owner/repo.git"), "owner/repo");
53
107
  });
108
+ (0, node_test_1.it)("derives the bare repo name from the normalized owner/name form", () => {
109
+ strict_1.default.equal((0, deep_scan_1.deriveBareRepoName)("Dragnet-Solutions/checkin-tracker"), "checkin-tracker");
110
+ strict_1.default.equal((0, deep_scan_1.deriveBareRepoName)("checkin-tracker"), "checkin-tracker");
111
+ });
112
+ (0, node_test_1.it)("builds an actionable error message from a server mismatch response, without mutating a plain message", () => {
113
+ strict_1.default.equal((0, deep_scan_1.describeApiError)({ error: "boom" }, 500), "boom");
114
+ strict_1.default.equal((0, deep_scan_1.describeApiError)({ error: "repository is not registered to this project", sentLabel: "Dragnet-Solutions/checkin-tracker", registeredRepos: ["checkin-tracker"] }, 403), 'repository is not registered to this project (sent: "Dragnet-Solutions/checkin-tracker"; registered: "checkin-tracker")');
115
+ strict_1.default.equal((0, deep_scan_1.describeApiError)({ error: "repository label \"foo\" matches more than one registered repository and cannot be resolved automatically", sentLabel: "foo", registeredRepos: ["OrgA/foo", "OrgB/foo"], matchingRegisteredRepos: ["OrgA/foo", "OrgB/foo"] }, 409), 'repository label "foo" matches more than one registered repository and cannot be resolved automatically (sent: "foo"; registered: "OrgA/foo", "OrgB/foo"; matches: "OrgA/foo", "OrgB/foo")');
116
+ });
54
117
  (0, node_test_1.it)("writes an inspectable dry-run payload without contacting the server", async () => {
55
118
  const root = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-dry-run-"));
56
119
  (0, node_child_process_1.execFileSync)("git", ["init", "-q", root]);
@@ -74,6 +137,8 @@ const deep_scan_1 = require("../commands/deep-scan");
74
137
  }
75
138
  const payload = JSON.parse(node_fs_1.default.readFileSync(output, "utf8"));
76
139
  strict_1.default.equal(payload.manifest.repository, "validation/repository");
140
+ strict_1.default.equal(payload.manifest.repositoryName, "repository");
141
+ strict_1.default.equal(payload.manifest.remote, "git@github.com:validation/repository.git");
77
142
  strict_1.default.equal(payload.files[0].path, "page.ts");
78
143
  strict_1.default.ok(Array.isArray(payload.files[0].linkPlan.attributeUpdates));
79
144
  strict_1.default.ok(Array.isArray(payload.files[0].linkPlan.derivedEntities));