Repository navigation
Expand file tree
/
Copy pathcheck.ts
More file actions
791 lines (718 loc) · 25.7 KB
/
Copy pathcheck.ts
File metadata and controls
791 lines (718 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/**
* Package checks — one script, run once, in CI and before a release.
*
* Verifies that the packed artifact installs cleanly for a consumer, that the
* host plugin and client bundle initialize, that peer ranges are resolvable by
* plain npm, that release workflows are wired correctly, and that no credentials
* or stale builds ship.
*
* node --experimental-strip-types scripts/check.ts
*/
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import {
existsSync,
mkdtempSync,
readFileSync,
readdirSync,
rmSync,
statSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { en as cardCopy } from "../src/settings-copy.ts";
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const failures: string[] = [];
const notes: string[] = [];
const fail = (message: string): void => {
failures.push(message);
};
const ok = (message: string): void => {
notes.push(message);
};
const pkg = JSON.parse(readFileSync(join(ROOT, "package.json"), "utf8"));
/* ------------------------------------------------------- 1. lib/ is current */
function newestMtime(dir: string): number {
let newest = 0;
for (const name of readdirSync(dir, { recursive: true })) {
const full = join(dir, String(name));
try {
if (statSync(full).isFile())
newest = Math.max(newest, statSync(full).mtimeMs);
} catch {
// Racing a build is not a failure; skip it.
}
}
return newest;
}
const lib = join(ROOT, "lib");
if (!existsSync(lib)) {
fail("lib/ is missing — run `pnpm run build`");
} else if (newestMtime(join(ROOT, "src")) > newestMtime(lib)) {
fail(
"src/ is newer than lib/ — run `pnpm run build`, or DSH will load the old one"
);
} else {
ok("lib/ is newer than src/");
}
/* ------------------------------------------- 2. peers npm must be able to read */
for (const [name, range] of Object.entries(
(pkg.peerDependencies ?? {}) as Record<string, unknown>
)) {
if (typeof range !== "string" || range.trim() === "") {
fail(`peer ${name} has no usable range`);
continue;
}
if (/^(workspace|link|file|catalog):/.test(range)) {
fail(
`peer ${name} uses "${range.split(":")[0]}:" — npm cannot parse it, so install fails`
);
continue;
}
if (!/^[\^~><=|*]/.test(range.trim())) {
fail(
`peer ${name} is pinned to "${range}" — a DSH patch release would orphan the plugin`
);
}
}
if (!failures.length) {
ok(
`${Object.keys(pkg.peerDependencies ?? {}).length} peer ranges npm can resolve`
);
}
/* ------------------------------------------- 3. it is a bundle, and it loads */
const patch = pkg.dsh?.bundle?.patch;
if (!patch) {
fail("no dsh.bundle.patch — DSH would install this and then ignore it");
} else if (existsSync(join(ROOT, patch))) {
ok(`dsh.bundle.patch -> ${patch}`);
} else {
fail(`dsh.bundle.patch points at "${patch}", which does not exist`);
}
for (const field of ["main", "types"]) {
if (!pkg[field]) fail(`no ${field} declared`);
}
/* --------------------------------------- 4. the harness surfaces are still there */
const SURFACES = ["@deepseek-ai/dsh-typert-protocol"];
const REQUIRED: Record<string, [RegExp, string][] | undefined> = {
"@deepseek-ai/dsh-typert-protocol": [
[
/(?:export declare class RemoteError|export \{[^}]*\bRemoteError\b)/,
"RemoteError",
],
[
/export declare (?:abstract )?class TypertRemoteService/,
"TypertRemoteService",
],
[/interface TypertRemoteContribution\b/, "TypertRemoteContribution"],
],
};
for (const name of SURFACES) {
let dir;
try {
dir = dirname(
execFileSync(
process.execPath,
[
"-e",
`process.stdout.write(require.resolve(${JSON.stringify(`${name}/package.json`)}))`,
],
{ cwd: ROOT, encoding: "utf8" }
)
);
} catch {
notes.push(
`skip ${name} is not installed, so its surfaces were not checked`
);
continue;
}
const typesDir = join(dir, "lib/types");
const files = existsSync(typesDir)
? readdirSync(typesDir)
.filter((f) => f.endsWith(".d.ts"))
.map((f) => join(typesDir, f))
: [join(dir, "lib/index.d.ts")].filter(existsSync);
if (files.length === 0) {
fail(`${name} ships no readable type declarations — the contract moved`);
continue;
}
const dts = files.map((f) => readFileSync(f, "utf8")).join("\n");
const required = REQUIRED[name];
assert.ok(required, `no surface contract for ${name}`);
for (const [pattern, what] of required) {
if (!pattern.test(dts)) fail(`${name} no longer exposes ${what}`);
}
}
if (!failures.some((f) => f.includes("no longer exposes"))) {
ok("every harness surface this package depends on is present");
}
/* --------------------------------- 5. the lint and format configs are actually loaded */
try {
const printed = execFileSync(
"pnpm",
["exec", "vp", "lint", "--print-config"],
{
cwd: ROOT,
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
}
);
const effective = JSON.parse(printed.slice(printed.indexOf("{")));
const ruleCount = Object.keys(effective.rules ?? {}).length;
if (ruleCount < 300) {
fail(
`the effective lint config has ${ruleCount} rules — the \`lint\` block in vite.config.ts is not being loaded`
);
} else if (effective.options?.typeAware !== true) {
fail("typeAware is off, so every typescript/* gate is listed but inert");
} else if (effective.rules["typescript/no-floating-promises"] === undefined) {
fail("the effective config is missing this package's own gates");
} else {
ok(`lint config is live (${ruleCount} rules, typeAware on)`);
}
} catch (error) {
const detail =
error instanceof Error ? error.message.split("\n")[0] : String(error);
fail(`could not read the effective lint config: ${detail}`);
}
/* --------------------------------------------------- 6. no credentials in the tree */
const SECRETS: [RegExp, string][] = [
[/\bmonid_live_[A-Za-z0-9]{10,}/, "a Monid platform key"],
[/\bsk-tinyfish-[A-Za-z0-9_-]{10,}/, "a TinyFish key"],
[/\bgh[pousr]_[A-Za-z0-9]{20,}/, "a GitHub token"],
[/sk-ant-[A-Za-z0-9_-]{10,}/, "an Anthropic key"],
];
const parts = { m: "monid_live_", t: "sk-tinyfish-", g: "ghp_", a: "sk-ant-" };
const CANARIES = [
[`${parts.m}AbCdEf1234567890`, true],
[`${parts.t}Pm-QkiaUloPEyNN`, true],
[`${parts.g}abcdefghijklmnopqrstuvwxyz0123456789`, true],
[`${parts.a}api03-AbCdEf1234567890`, true],
[parts.m, false],
[parts.t, false],
["$OPENCODE_GO_API_KEY", false],
["Authorization: Bearer <key>", false],
];
for (const [sample, shouldMatch] of CANARIES as [string, boolean][]) {
if (SECRETS.some(([re]) => re.test(sample)) !== shouldMatch) {
fail(
`the secret scanner is ${shouldMatch ? "missing" : "over-matching"} on a sample`
);
}
}
const SKIP = new Set(["node_modules", ".git", "lib"]);
let scanned = 0;
const walk = (dir: string): void => {
for (const name of readdirSync(dir)) {
if (
SKIP.has(name) ||
name.startsWith(".build-check") ||
name.endsWith(".tgz")
)
continue;
const full = join(dir, name);
if (statSync(full).isDirectory()) {
walk(full);
continue;
}
if (!/\.(mjs|js|ts|json|yml|yaml|md)$/.test(name)) continue;
scanned += 1;
const text = readFileSync(full, "utf8");
for (const [re, what] of SECRETS) {
if (re.test(text))
fail(`${full.replace(`${ROOT}/`, "")} contains ${what}`);
}
}
};
walk(ROOT);
ok(`scanned ${scanned} files for credentials`);
/* ------------------------------ 7. install it the way a consumer will, and load it */
const scratch = mkdtempSync(join(tmpdir(), "dsh-opencode-"));
const CHILD_ENV = Object.fromEntries(
Object.entries(process.env).filter(([name]) => !/^npm_config_/i.test(name))
);
try {
const [packed] = JSON.parse(
execFileSync(
"npm",
["pack", "--json", "--ignore-scripts", "--pack-destination", scratch],
{ cwd: ROOT, encoding: "utf8", env: CHILD_ENV }
)
) as [{ filename: string; entryCount: number }];
const project = join(scratch, "consumer");
execFileSync("mkdir", ["-p", project]);
const peers = Object.entries(
(pkg.peerDependencies ?? {}) as Record<string, unknown>
).map(([n, r]) => `${n}@${String(r)}`);
execFileSync(
"npm",
[
"install",
join(scratch, packed.filename),
...peers,
"--no-audit",
"--no-fund",
"--ignore-scripts",
],
{
cwd: project,
encoding: "utf8",
env: CHILD_ENV,
stdio: ["ignore", "pipe", "pipe"],
}
);
const installed = join(project, "node_modules/dsh-opencode-patch");
const installedManifest = JSON.parse(
readFileSync(join(installed, "package.json"), "utf8")
);
if (installedManifest.dsh?.bundle?.patch === undefined) {
fail(
"the published manifest lost dsh.bundle.patch — DSH would ignore the package"
);
}
// Loaded from the installed copy, verifying the host entrypoint and Config schema
const probe = `
const m = await import(${JSON.stringify(join(installed, pkg.main))});
if (typeof m.apply !== "function") throw new Error("no apply()");
if (typeof m.Config !== "function") throw new Error("no Config");
const calls = [];
const ctx = {
plugin: (p) => calls.push(p?.name || "plugin"),
effect: (fn) => fn(),
on: (ev) => calls.push(ev),
get: () => undefined,
logger: { info: () => {}, warn: () => {} },
};
m.apply(ctx, m.Config({}));
if (!calls.includes("GoUsageService")) throw new Error("GoUsageService was not registered");
if (!calls.includes("llm/stream")) throw new Error("llm/stream was not registered");
process.stdout.write("ok");
`;
execFileSync(process.execPath, ["--input-type=module", "-e", probe], {
cwd: project,
encoding: "utf8",
env: CHILD_ENV,
stdio: ["ignore", "pipe", "pipe"],
});
ok(
`installed the packed tarball with plain npm (${packed.entryCount} entries) and loaded the host + client halves`
);
} catch (error: unknown) {
const err = error as { stderr?: string; message?: string };
const detail = [err.stderr, err.message]
.filter(Boolean)
.join("\n")
.split("\n")
.filter((l) => /Error|Cannot find|ENOENT|EEXIST|npm error|code /.test(l))
.slice(0, 3)
.join(" | ");
fail(`a consumer install or load failed: ${detail}`);
} finally {
rmSync(scratch, { recursive: true, force: true });
}
/* ------------------------------------------------- 8. the release can fire */
const workflows = join(ROOT, ".github/workflows");
const release = readFileSync(join(workflows, "release.yml"), "utf8");
const ci = readFileSync(join(workflows, "ci.yml"), "utf8");
if (/^\s*tags:\s*\[?\s*"?v\*\.\*\.\*"?/m.test(release)) {
ok("release.yml is triggered by a version tag");
} else {
fail("release.yml does not trigger on v*.*.* tags — nothing would publish");
}
if (/^\s*tags-ignore:\s*\[?\s*"?v\*\.\*\.\*"?/m.test(ci)) {
ok("ci.yml ignores version tags, leaving them to release.yml");
} else {
fail(
"ci.yml does not ignore version tags; a tag push matches no branch, so " +
"ci.yml never runs on one and anything gated on it is unreachable"
);
}
if (/^\s*id-token:\s*write/m.test(release)) {
ok("release.yml grants id-token: write for OIDC trusted publishing");
} else {
fail(
"release.yml has no id-token: write, so npm trusted publishing cannot " +
"authenticate and the publish would need a stored token"
);
}
if (/npm view .* version 2>\/dev\/null/.test(release)) {
ok("release.yml skips a version that is already on the registry");
} else {
fail(
"release.yml does not skip an already-published version, so re-running a " +
"tag fails on npm's refusal to republish"
);
}
/* --------------------------------------------------- 9. the toolchain is Vite+ */
const TOOLCHAIN = ["build", "check", "format", "lint", "test"];
const UNBYPASSED = /(?:^|[\s(])(oxlint|oxfmt|tsdown|vitest|tsc)(?:[\s)]|$)/;
if (pkg.devDependencies?.["vite-plus"]) {
ok(`the toolchain is vite-plus (${pkg.devDependencies["vite-plus"]})`);
} else {
fail("the package does not depend on vite-plus; the toolchain is unpinned");
}
for (const name of TOOLCHAIN) {
const script = pkg.scripts?.[name];
if (!script) {
fail(`no ${name} script`);
} else if (!/\bvp\b/.test(script)) {
fail(`the ${name} script does not go through vp: ${script}`);
} else if (UNBYPASSED.test(script.replaceAll(/\bvp\b[^\s]*/g, ""))) {
const bare = script.match(new RegExp(UNBYPASSED, "g")) ?? [];
const real = bare.filter((tool: string) => tool.trim() !== "tsc");
if (real.length > 0) {
fail(
`the ${name} script reaches past vp for ${real.map((t: string) => t.trim()).join(", ")}; ` +
"that bypasses the entry point that reads the config"
);
}
}
}
ok(`${TOOLCHAIN.length} toolchain scripts route through vp`);
/* -------------------------------------------- 10. the scoped alias ships too */
const releaseYml = readFileSync(join(workflows, "release.yml"), "utf8");
if (releaseYml.includes("scripts/publish-scoped.ts")) {
ok("release.yml publishes scoped aliases from the same tree");
} else {
fail(
"release.yml does not publish the scoped alias; @viztor/dsh-opencode-patch " +
"would go stale while dsh-opencode-patch moves on"
);
}
/* ------------------------------------------ 11. the client stays lean */
const CLIENT_BUDGET = 64 * 1024;
const clientPath = join(ROOT, "lib/client.js");
if (existsSync(clientPath)) {
const clientStat = statSync(clientPath);
if (clientStat.size <= CLIENT_BUDGET) {
ok(`lib/client.js is ${clientStat.size} bytes, under budget`);
} else {
fail(
`lib/client.js is ${clientStat.size} bytes, over the ${CLIENT_BUDGET} budget — ` +
"a dependency was likely bundled instead of left external"
);
}
} else {
fail("lib/client.js is missing — run `pnpm run build`");
}
/* ------------------------------------ 12. one name everywhere (identity + i18n) */
const settingsPage = readFileSync(join(ROOT, "src/settings-page.tsx"), "utf8");
const nsMatch = /export const NS = "([^"]+)"/u.exec(settingsPage);
if (nsMatch?.[1] === pkg.name) {
ok(`settings namespace == package name (${pkg.name})`);
} else {
fail(
`settings NS is "${nsMatch?.[1]}" but package.json name is "${pkg.name}" — ` +
"the card would bind a namespace the host never serves"
);
}
const localeKeys = (value: unknown, prefix = ""): string[] =>
value !== null && typeof value === "object" && !Array.isArray(value)
? Object.entries(value as Record<string, unknown>).flatMap(([key, child]) =>
localeKeys(child, prefix === "" ? key : `${prefix}.${key}`)
)
: [prefix];
const [enLocale, zhLocale] = ["en", "zh"].map(
(lang) =>
JSON.parse(
readFileSync(join(ROOT, `locale/${lang}.json`), "utf8")
) as unknown
);
const enKeys = localeKeys(enLocale).toSorted();
const zhKeys = localeKeys(zhLocale).toSorted();
if (enKeys.join("\n") === zhKeys.join("\n")) {
ok(`${enKeys.length} locale keys in sync across en/zh`);
} else {
fail(
`locale/en.json and locale/zh.json disagree on keys (${enKeys.length} en vs ` +
`${zhKeys.length} zh) — a missing translation would render as a raw key`
);
}
const bundleTitle = (enLocale as { meta?: { title?: unknown } }).meta?.title;
if (bundleTitle === cardCopy.title) {
ok(`one display name everywhere ("${bundleTitle}")`);
} else {
fail(
`the plugin shows two names: locale/en.json meta.title "${String(bundleTitle)}" ` +
`!= settings-card title "${cardCopy.title}" — keep one display name`
);
}
/* ------------------------------- 13. the live suite stays wired and opt-in */
const e2eConfigPath = join(ROOT, "vitest.e2e.config.ts");
const e2eScript: unknown = pkg.scripts?.["test:e2e"];
if (!existsSync(e2eConfigPath)) {
fail("vitest.e2e.config.ts is missing — `pnpm run test:e2e` has no config");
} else if (
typeof e2eScript !== "string" ||
!e2eScript.includes("vitest.e2e.config.ts")
) {
fail(
"the test:e2e script does not use vitest.e2e.config.ts, so the E2E files " +
"would either be collected by the unit run or never run at all"
);
} else {
ok("the E2E suite has its own config and script");
}
// The unit run must stay offline. An E2E file matched by the unit `include`
// would start reaching the network on every `pnpm test`.
const unitInclude =
/include:\s*\[([^\]]*)\]/u.exec(
readFileSync(join(ROOT, "vite.config.ts"), "utf8")
)?.[1] ?? "";
if (unitInclude.includes("e2e")) {
fail(
"the unit test include matches E2E files; `pnpm test` would stop being " +
"offline and deterministic"
);
} else {
ok("the unit test include excludes the E2E files");
}
if (/pnpm run test:e2e/u.test(ci)) {
ok("ci.yml runs the live E2E suite");
} else {
fail(
"ci.yml does not run `pnpm run test:e2e`; the live gateway contract would " +
"go unchecked until a user's meter broke"
);
}
/* --------------------------------- 14. the automation itself cannot rot quietly */
/**
* The generated catalog.
*
* `src/catalog-data.ts` is written from models.dev, and the one field that went
* stale before — `provider_npm` — put nine models on an endpoint that cannot
* serve them. That is a WRONG answer rather than a missing one, so no unit test
* can catch it: only regenerating and diffing can. Hence a script, and hence a
* job that runs it.
*/
const shimScript: unknown = pkg.scripts?.["catalog:shim"];
const shimPath = join(ROOT, "scripts/regenerate-catalog-shim.ts");
/**
* Whether a workflow RUNS a command, as opposed to merely mentioning it.
*
* Matching the whole file is not good enough: a step that fails prints the very
* command it ran, so `pnpm run catalog:shim` appears in ci.yml whether or not
* anything invokes it. So each line is reduced past its `run:` scaffolding —
* `- run: cmd`, `run: cmd`, `run: |` plus the block body — comments dropped — and
* the command has to be the first thing on a line. An `echo "… run 'pnpm run X'"`
* line no longer qualifies, which is exactly the false positive this exists to
* remove.
*/
const runs = (source: string, command: string): boolean =>
source
.split("\n")
.map((line) =>
line
.trim()
.replace(/^(?:- )?run:(?:\s*\|\s*)?/, "")
.trim()
)
// A comment-only line is documentation, not an invocation.
.filter((line) => line.length > 0 && !line.startsWith("#"))
.some((line) => line.startsWith(command));
const shimScripted =
typeof shimScript === "string" &&
shimScript.includes("scripts/regenerate-catalog-shim.ts");
const shimRunsInCi = runs(ci, String.raw`pnpm run catalog:shim`);
if (existsSync(shimPath) && shimScripted && shimRunsInCi) {
ok("the bundled catalog is generated, scripted, and checked on a schedule");
} else {
// Plain unnested `if`s, not a chain: the first test has to be the POSITIVE
// one, and a chain of `else if (!…)` also hides that exactly one of several
// independent things broke. Most specific reason first.
if (!existsSync(shimPath)) {
fail(
"scripts/regenerate-catalog-shim.ts is missing; the catalog is unmaintainable"
);
}
if (!shimScripted) {
fail(
"no catalog:shim script; regenerating the bundled catalog means knowing a " +
"path, and nothing runs it on a schedule"
);
}
if (!shimRunsInCi) {
fail(
"no ci.yml step RUNS `pnpm run catalog:shim`; a stale bundled catalog " +
"goes unnoticed until a model is dispatched to the wrong endpoint"
);
}
}
/**
* The coverage ratchet.
*
* A threshold set above today's number would block every PR, and one set below
* it would never fire. The value of a ratchet is that it fails when coverage
* DROPS, so what has to be asserted here is that one exists at all — and that
* it actually runs, or it is decoration.
*/
const viteConfig = readFileSync(join(ROOT, "vite.config.ts"), "utf8");
const coverageProvider: unknown = pkg.devDependencies?.["@vitest/coverage-v8"];
const hasCoverageScript = typeof pkg.scripts?.["test:coverage"] === "string";
// Anchored: an unanchored /thresholds:/ also matches `_thresholds:`, so a
// rename would still read as present.
const hasCoverageThresholds = /^\s*thresholds\s*:/mu.test(viteConfig);
const coverageRunsInCi = runs(ci, String.raw`pnpm run test:coverage`);
if (
hasCoverageScript &&
typeof coverageProvider === "string" &&
hasCoverageThresholds &&
coverageRunsInCi
) {
ok(
`coverage is ratcheted and runs in CI (@vitest/coverage-v8 ${coverageProvider})`
);
} else {
if (!hasCoverageScript) {
fail(
"no test:coverage script; a coverage threshold that never runs is decoration"
);
}
if (typeof coverageProvider !== "string") {
fail(
"@vitest/coverage-v8 is not a devDependency; `vp test --coverage` fails " +
"at startup without it"
);
}
if (!hasCoverageThresholds) {
fail(
"vite.config.ts sets no coverage thresholds; coverage is reported but never " +
"gated, so a regression in the load-bearing modules goes unnoticed"
);
}
if (!coverageRunsInCi) {
fail(
"no ci.yml step RUNS `pnpm run test:coverage`; the ratchet cannot fail a " +
"build from the unit run alone"
);
}
}
/**
* The `jobs:` block of a workflow, split into one string per job.
*
* Deliberately a scan rather than a YAML parse: the gate has no YAML dependency,
* and "a dependency to validate two files" is a worse trade than four lines of
* indentation-aware splitting. A job key is a bare `name:` at exactly two spaces,
* which is what `jobs:` children always look like — `steps:`/`with:`/`run:` are
* deeper, and top-level keys sit at column zero.
*/
const jobsIn = (source: string): string[] => {
const heading = /^jobs:\s*$/mu.exec(source);
if (heading === null) {
return [];
}
const body = source.slice(heading.index + heading[0].length);
// Everything before the next top-level key is the jobs block.
const nextTopLevel = /^\S/mu.exec(body);
const block =
nextTopLevel === null ? body : body.slice(0, nextTopLevel.index);
return block
.split(/^ {2}(?=[\w-]+:[ \t]*$)/mu)
.map((job) => job.trim())
.filter((job) => job.length > 0);
};
/**
* Every job is bounded.
*
* GitHub's default is six hours. A hung test in a fast suite should fail in
* minutes, and a release whose polling loop has wedged should fail rather than
* hold a concurrency group open indefinitely.
*/
let unboundedTotal = 0;
for (const [label, source] of [
["ci.yml", ci],
["release.yml", releaseYml],
] as const) {
const jobs = jobsIn(source);
if (jobs.length === 0) {
fail(`${label} has no readable jobs block; this check cannot see its jobs`);
continue;
}
const unbounded = jobs.filter((job) => !/^\s*timeout-minutes:/mu.test(job));
if (unbounded.length > 0) {
unboundedTotal += unbounded.length;
fail(
`${label} has ${unbounded.length} of ${jobs.length} job(s) without ` +
"timeout-minutes; a hang would burn GitHub's six-hour default first"
);
}
}
if (unboundedTotal === 0) {
ok(
`every job in ci.yml and release.yml is bounded (${jobsIn(ci).length + jobsIn(releaseYml).length} jobs)`
);
}
/**
* Release concurrency must queue, never cancel.
*
* Cancelling a re-run of the same tag is precisely the failure that lost
* v0.7.0: the first run holds a half-published registry and the second — the
* recovery — is killed before it can finish it.
*/
const releaseQueues =
/concurrency:/u.test(releaseYml) &&
!/cancel-in-progress:\s*true/u.test(releaseYml);
if (releaseQueues) {
ok("release runs queue per tag instead of cancelling each other");
} else {
if (!/concurrency:/u.test(releaseYml)) {
fail(
"release.yml sets no concurrency group; two runs of one tag race on the " +
"registry"
);
}
if (/cancel-in-progress:\s*true/u.test(releaseYml)) {
fail(
"release.yml cancels an in-progress run of the same tag; a re-run meant " +
"to recover a transient npm fault would instead be killed"
);
}
}
/** The tarball a release publishes is recorded, not assumed. */
const packsTarball = /npm pack/u.test(releaseYml);
const hashesTarball = /shasum -a 256/u.test(releaseYml);
if (packsTarball && hashesTarball) {
ok("the published tarball is packed, hashed and kept as an artifact");
} else {
if (!packsTarball) {
fail(
"release.yml never packs the tarball it publishes, so the bytes that reach " +
"the registry cannot be compared with the bytes that were built"
);
}
if (!hashesTarball) {
fail(
"release.yml packs without recording a digest; there is nothing to verify"
);
}
}
/** Dependabot, because the release path is actions and cannot be exercised. */
const dependabotPath = join(ROOT, ".github/dependabot.yml");
if (existsSync(dependabotPath)) {
const dependabot = readFileSync(dependabotPath, "utf8");
const missing = ["github-actions", "npm"].filter(
(ecosystem) => !dependabot.includes(`package-ecosystem: ${ecosystem}`)
);
if (missing.length > 0) {
fail(
`dependabot.yml does not cover ${missing.join(", ")}; the toolchain that ` +
"runs the release is the thing most likely to rot"
);
} else {
ok("dependabot covers the release actions and the dependency tree");
}
} else {
fail(
".github/dependabot.yml is missing; a stale release action is how a tag " +
"stops producing a publish while the workflow stays green"
);
}
/* ------------------------------------------------------------------- report */
for (const note of notes) console.log(` ok ${note}`);
for (const message of failures) console.error(` FAIL ${message}`);
if (failures.length) {
console.error(`\ncheck failed — ${failures.length} problem(s)`);
process.exit(1);
}
console.log("\ncheck passed");