@cevek/screentest 0.3.7 → 0.5.0
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 +11 -9
- package/dist/global-setup.js +14953 -70
- package/dist/index.js +626 -116
- package/dist/runner.js +19 -9
- package/dist/web/assets/index-CQ8ij0HR.js +72 -0
- package/dist/web/assets/index-DWf2oQZg.css +1 -0
- package/dist/web/index.html +2 -2
- package/package.json +1 -1
- package/dist/web/assets/index-Bi-jx470.css +0 -1
- package/dist/web/assets/index-BvqwrlU8.js +0 -72
package/dist/index.js
CHANGED
|
@@ -14560,9 +14560,24 @@ var projectInfoSchema = external_exports.object({
|
|
|
14560
14560
|
/** Current git branch, or `null` if not a git repo (or `git` not on PATH). */
|
|
14561
14561
|
branch: external_exports.string().nullable()
|
|
14562
14562
|
});
|
|
14563
|
+
var rendererSchema = external_exports.object({
|
|
14564
|
+
fingerprint: external_exports.string().min(1),
|
|
14565
|
+
label: external_exports.string()
|
|
14566
|
+
});
|
|
14563
14567
|
var docSchema = external_exports.object({
|
|
14564
14568
|
/** Optional for backward compatibility with pre-0.3 doc.json files. */
|
|
14565
14569
|
project: projectInfoSchema.optional(),
|
|
14570
|
+
/**
|
|
14571
|
+
* `current` shot this run's actuals; `baseline` is what snapshot.json
|
|
14572
|
+
* records for its hashes (null when it records none). `mismatch`: the
|
|
14573
|
+
* renderers differ AND the diffs look like it (see generateDoc). Optional
|
|
14574
|
+
* for pre-0.5 doc.json files.
|
|
14575
|
+
*/
|
|
14576
|
+
renderer: external_exports.object({
|
|
14577
|
+
current: rendererSchema,
|
|
14578
|
+
baseline: rendererSchema.nullable(),
|
|
14579
|
+
mismatch: external_exports.boolean()
|
|
14580
|
+
}).optional(),
|
|
14566
14581
|
groups: external_exports.array(groupItemSchema)
|
|
14567
14582
|
});
|
|
14568
14583
|
var snapshotTestSchema = external_exports.object({
|
|
@@ -14576,6 +14591,7 @@ var snapshotGroupSchema = external_exports.lazy(
|
|
|
14576
14591
|
})
|
|
14577
14592
|
);
|
|
14578
14593
|
var snapshotDocSchema = external_exports.object({
|
|
14594
|
+
renderer: rendererSchema.optional(),
|
|
14579
14595
|
groups: external_exports.array(snapshotGroupSchema)
|
|
14580
14596
|
});
|
|
14581
14597
|
var apiDocResponseSchema = external_exports.object({
|
|
@@ -14663,13 +14679,26 @@ import { createServer } from "http";
|
|
|
14663
14679
|
import { Router } from "express";
|
|
14664
14680
|
|
|
14665
14681
|
// src/cloudflare.ts
|
|
14682
|
+
function storeKey(project, hash2) {
|
|
14683
|
+
return `${project}/${hash2}.png`;
|
|
14684
|
+
}
|
|
14685
|
+
function storePrefix(project) {
|
|
14686
|
+
return `${project}/`;
|
|
14687
|
+
}
|
|
14688
|
+
function hashFromKey(project, key) {
|
|
14689
|
+
const prefix = storePrefix(project);
|
|
14690
|
+
if (!key.startsWith(prefix)) return null;
|
|
14691
|
+
const rest = key.slice(prefix.length);
|
|
14692
|
+
const m = /^([a-f0-9]{64})\.png$/.exec(rest);
|
|
14693
|
+
return m ? m[1] : null;
|
|
14694
|
+
}
|
|
14666
14695
|
async function uploadPng(cfg, hash2, body) {
|
|
14667
|
-
const
|
|
14696
|
+
const key = storeKey(cfg.project, hash2);
|
|
14668
14697
|
const res = await fetch(cfg.workerUrl, {
|
|
14669
14698
|
method: "POST",
|
|
14670
14699
|
headers: {
|
|
14671
14700
|
"Content-Type": "image/png",
|
|
14672
|
-
"X-File-Name":
|
|
14701
|
+
"X-File-Name": key,
|
|
14673
14702
|
Authorization: `Bearer ${cfg.token}`
|
|
14674
14703
|
},
|
|
14675
14704
|
body: new Uint8Array(body)
|
|
@@ -14678,13 +14707,48 @@ async function uploadPng(cfg, hash2, body) {
|
|
|
14678
14707
|
const text = await res.text().catch(() => "");
|
|
14679
14708
|
throw new Error(`Cloudflare upload failed: ${res.status} ${res.statusText} ${text}`.trim());
|
|
14680
14709
|
}
|
|
14681
|
-
return
|
|
14710
|
+
return `${cfg.workerUrl}/${key}`;
|
|
14711
|
+
}
|
|
14712
|
+
async function existsInStore(cfg, hash2) {
|
|
14713
|
+
const key = storeKey(cfg.project, hash2);
|
|
14714
|
+
const res = await fetch(`${cfg.workerUrl}/${key}`);
|
|
14715
|
+
if (res.body) await res.arrayBuffer().catch(() => void 0);
|
|
14716
|
+
if (res.ok) return true;
|
|
14717
|
+
if (res.status === 404) return false;
|
|
14718
|
+
throw new Error(`Cloudflare probe failed for ${key}: ${res.status} ${res.statusText}`);
|
|
14719
|
+
}
|
|
14720
|
+
async function listStore(cfg) {
|
|
14721
|
+
const out = [];
|
|
14722
|
+
let cursor = null;
|
|
14723
|
+
do {
|
|
14724
|
+
const url2 = new URL(cfg.workerUrl);
|
|
14725
|
+
url2.searchParams.set("list", "1");
|
|
14726
|
+
url2.searchParams.set("prefix", storePrefix(cfg.project));
|
|
14727
|
+
if (cursor) url2.searchParams.set("cursor", cursor);
|
|
14728
|
+
const res = await fetch(url2, { headers: { Authorization: `Bearer ${cfg.token}` } });
|
|
14729
|
+
if (!res.ok) {
|
|
14730
|
+
throw new Error(`Cloudflare list failed: ${res.status} ${res.statusText}`);
|
|
14731
|
+
}
|
|
14732
|
+
const page = await res.json();
|
|
14733
|
+
out.push(...page.keys);
|
|
14734
|
+
cursor = page.cursor;
|
|
14735
|
+
} while (cursor);
|
|
14736
|
+
return out;
|
|
14737
|
+
}
|
|
14738
|
+
async function deleteKey(cfg, key) {
|
|
14739
|
+
const res = await fetch(`${cfg.workerUrl}/${key}`, {
|
|
14740
|
+
method: "DELETE",
|
|
14741
|
+
headers: { Authorization: `Bearer ${cfg.token}` }
|
|
14742
|
+
});
|
|
14743
|
+
if (!res.ok) {
|
|
14744
|
+
throw new Error(`Cloudflare delete failed: ${res.status} ${res.statusText}`);
|
|
14745
|
+
}
|
|
14682
14746
|
}
|
|
14683
|
-
function expectedUrlFor(
|
|
14684
|
-
return `${workerUrl}/${hash2}
|
|
14747
|
+
function expectedUrlFor(cfg, hash2) {
|
|
14748
|
+
return `${cfg.workerUrl}/${storeKey(cfg.project, hash2)}`;
|
|
14685
14749
|
}
|
|
14686
|
-
function expectedUrlPattern(
|
|
14687
|
-
return `${workerUrl}
|
|
14750
|
+
function expectedUrlPattern(cfg) {
|
|
14751
|
+
return `${cfg.workerUrl}/${storeKey(cfg.project, "{hash}")}`;
|
|
14688
14752
|
}
|
|
14689
14753
|
|
|
14690
14754
|
// src/version.ts
|
|
@@ -14713,8 +14777,8 @@ function docRoute(state) {
|
|
|
14713
14777
|
r.get("/doc", (_req, res) => {
|
|
14714
14778
|
res.json({
|
|
14715
14779
|
doc: state.doc,
|
|
14716
|
-
expectedUrlPattern: expectedUrlPattern(state.
|
|
14717
|
-
workerUrl: state.workerUrl,
|
|
14780
|
+
expectedUrlPattern: expectedUrlPattern(state.store),
|
|
14781
|
+
workerUrl: state.store.workerUrl,
|
|
14718
14782
|
serverVersion: SCREENTEST_VERSION
|
|
14719
14783
|
});
|
|
14720
14784
|
});
|
|
@@ -14811,6 +14875,9 @@ async function withFileLock(absPath, fn) {
|
|
|
14811
14875
|
function isSnapshotGroup(x) {
|
|
14812
14876
|
return Array.isArray(x.items);
|
|
14813
14877
|
}
|
|
14878
|
+
function hasAnyHash(items) {
|
|
14879
|
+
return items.some((n) => isSnapshotGroup(n) ? hasAnyHash(n.items) : n.hash !== null);
|
|
14880
|
+
}
|
|
14814
14881
|
function upsertHash(doc, path, hash2) {
|
|
14815
14882
|
if (path.length === 0) throw new Error("Empty path for upsertHash");
|
|
14816
14883
|
const testName = path[path.length - 1];
|
|
@@ -14902,7 +14969,9 @@ async function patchSnapshot(args) {
|
|
|
14902
14969
|
throw err;
|
|
14903
14970
|
}
|
|
14904
14971
|
}
|
|
14972
|
+
const fresh = !doc.renderer && !hasAnyHash(doc.groups);
|
|
14905
14973
|
upsertHash(doc, args.path, args.hash);
|
|
14974
|
+
if (args.renderer && fresh) doc = { renderer: args.renderer, groups: doc.groups };
|
|
14906
14975
|
await atomicWriteJson(abs, doc);
|
|
14907
14976
|
});
|
|
14908
14977
|
}
|
|
@@ -14937,16 +15006,17 @@ function acceptRoute(state) {
|
|
|
14937
15006
|
}
|
|
14938
15007
|
const buf = await fs2.readFile(entry.actualAbsPath);
|
|
14939
15008
|
const hash2 = sha256Hex(buf);
|
|
14940
|
-
await uploadPng(
|
|
15009
|
+
await uploadPng(state.store, hash2, buf);
|
|
14941
15010
|
await patchSnapshot({
|
|
14942
15011
|
docDir: state.docDir,
|
|
14943
15012
|
patchSnapshotJsonFile: entry.item.patchSnapshotJsonFile,
|
|
14944
15013
|
path: [...entry.parents, entry.name],
|
|
14945
|
-
hash: hash2
|
|
15014
|
+
hash: hash2,
|
|
15015
|
+
renderer: state.doc.renderer?.current
|
|
14946
15016
|
});
|
|
14947
15017
|
res.json({
|
|
14948
15018
|
hash: hash2,
|
|
14949
|
-
url: expectedUrlFor(state.
|
|
15019
|
+
url: expectedUrlFor(state.store, hash2)
|
|
14950
15020
|
});
|
|
14951
15021
|
} catch (err) {
|
|
14952
15022
|
const msg = err instanceof Error ? err.message : String(err);
|
|
@@ -15014,7 +15084,7 @@ async function startServer(state, requestedPort) {
|
|
|
15014
15084
|
if (webDist) {
|
|
15015
15085
|
app.use(express.static(webDist));
|
|
15016
15086
|
app.get("/{*splat}", (_req, res) => {
|
|
15017
|
-
res.sendFile(
|
|
15087
|
+
res.sendFile("index.html", { root: webDist });
|
|
15018
15088
|
});
|
|
15019
15089
|
}
|
|
15020
15090
|
const port = requestedPort === 0 ? await pickRandomFreePort() : await findFreePort(requestedPort);
|
|
@@ -15058,28 +15128,62 @@ function isFree(port) {
|
|
|
15058
15128
|
});
|
|
15059
15129
|
}
|
|
15060
15130
|
|
|
15061
|
-
// src/
|
|
15131
|
+
// src/project.ts
|
|
15132
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
15133
|
+
import { basename, join as join2 } from "path";
|
|
15134
|
+
function resolveProjectId(projectRoot) {
|
|
15135
|
+
const explicit = process.env.SCREENTEST_PROJECT?.trim();
|
|
15136
|
+
if (explicit) return sanitize(explicit);
|
|
15137
|
+
try {
|
|
15138
|
+
const pkg = JSON.parse(readFileSync2(join2(projectRoot, "package.json"), "utf8"));
|
|
15139
|
+
if (typeof pkg.name === "string" && pkg.name.trim()) return sanitize(pkg.name);
|
|
15140
|
+
} catch {
|
|
15141
|
+
}
|
|
15142
|
+
return sanitize(basename(projectRoot));
|
|
15143
|
+
}
|
|
15144
|
+
function sanitize(raw) {
|
|
15145
|
+
const out = raw.replace(/^@/, "").replace(/[^a-zA-Z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
15146
|
+
return out || "project";
|
|
15147
|
+
}
|
|
15148
|
+
|
|
15149
|
+
// src/store-args.ts
|
|
15062
15150
|
var DEFAULT_WORKER_URL = "https://screentests.x-cevek.workers.dev";
|
|
15063
15151
|
var DEFAULT_TOKEN = "SECRET_123";
|
|
15064
|
-
function
|
|
15065
|
-
let docPath = null;
|
|
15066
|
-
let port = Number(process.env.PORT) || 0;
|
|
15067
|
-
let doOpen = true;
|
|
15152
|
+
function parseStoreArgs(rawArgs) {
|
|
15068
15153
|
let workerUrl = process.env.CLOUDFLARE_WORKER_URL || DEFAULT_WORKER_URL;
|
|
15069
15154
|
let token = process.env.CLOUDFLARE_TOKEN || DEFAULT_TOKEN;
|
|
15155
|
+
const rest = [];
|
|
15070
15156
|
for (let i = 0; i < rawArgs.length; i++) {
|
|
15071
15157
|
const a = rawArgs[i];
|
|
15072
|
-
if (a === "--
|
|
15073
|
-
port = Number(rawArgs[++i]);
|
|
15074
|
-
if (!Number.isFinite(port) || port <= 0) throw new Error("--port must be a number");
|
|
15075
|
-
} else if (a === "--no-open") {
|
|
15076
|
-
doOpen = false;
|
|
15077
|
-
} else if (a === "--worker-url") {
|
|
15158
|
+
if (a === "--worker-url") {
|
|
15078
15159
|
workerUrl = rawArgs[++i] ?? "";
|
|
15079
15160
|
if (!workerUrl) throw new Error("--worker-url requires a value");
|
|
15080
15161
|
} else if (a === "--token") {
|
|
15081
15162
|
token = rawArgs[++i] ?? "";
|
|
15082
15163
|
if (!token) throw new Error("--token requires a value");
|
|
15164
|
+
} else {
|
|
15165
|
+
rest.push(a);
|
|
15166
|
+
}
|
|
15167
|
+
}
|
|
15168
|
+
return { store: { workerUrl: workerUrl.replace(/\/+$/, ""), token }, rest };
|
|
15169
|
+
}
|
|
15170
|
+
function storeConfigFor(store, projectRoot) {
|
|
15171
|
+
return { ...store, project: resolveProjectId(projectRoot) };
|
|
15172
|
+
}
|
|
15173
|
+
|
|
15174
|
+
// src/ui.ts
|
|
15175
|
+
function parseUIArgs(rawArgs) {
|
|
15176
|
+
const { store, rest } = parseStoreArgs(rawArgs);
|
|
15177
|
+
let docPath = null;
|
|
15178
|
+
let port = Number(process.env.PORT) || 0;
|
|
15179
|
+
let doOpen = true;
|
|
15180
|
+
for (let i = 0; i < rest.length; i++) {
|
|
15181
|
+
const a = rest[i];
|
|
15182
|
+
if (a === "--port") {
|
|
15183
|
+
port = Number(rest[++i]);
|
|
15184
|
+
if (!Number.isFinite(port) || port <= 0) throw new Error("--port must be a number");
|
|
15185
|
+
} else if (a === "--no-open") {
|
|
15186
|
+
doOpen = false;
|
|
15083
15187
|
} else if (a.startsWith("--")) {
|
|
15084
15188
|
throw new Error(`Unknown flag: ${a}`);
|
|
15085
15189
|
} else if (!docPath) {
|
|
@@ -15089,13 +15193,7 @@ function parseUIArgs(rawArgs) {
|
|
|
15089
15193
|
}
|
|
15090
15194
|
}
|
|
15091
15195
|
if (!docPath) throw new Error("doc-json path is required");
|
|
15092
|
-
return {
|
|
15093
|
-
docPath,
|
|
15094
|
-
port,
|
|
15095
|
-
open: doOpen,
|
|
15096
|
-
workerUrl: workerUrl.replace(/\/+$/, ""),
|
|
15097
|
-
token
|
|
15098
|
-
};
|
|
15196
|
+
return { docPath, port, open: doOpen, store };
|
|
15099
15197
|
}
|
|
15100
15198
|
async function runUI(opts) {
|
|
15101
15199
|
const docAbs = isAbsolute(opts.docPath) ? opts.docPath : resolve5(process.cwd(), opts.docPath);
|
|
@@ -15141,8 +15239,9 @@ async function runUI(opts) {
|
|
|
15141
15239
|
const state = {
|
|
15142
15240
|
doc,
|
|
15143
15241
|
docDir,
|
|
15144
|
-
|
|
15145
|
-
|
|
15242
|
+
// Pre-0.3 doc.json has no `project` block; the cwd is the best guess left,
|
|
15243
|
+
// and it is right whenever the UI is launched from the project.
|
|
15244
|
+
store: storeConfigFor(opts.store, doc.project?.path ?? process.cwd()),
|
|
15146
15245
|
flat: flatResult.flat,
|
|
15147
15246
|
actualWhitelist: flatResult.whitelist
|
|
15148
15247
|
};
|
|
@@ -15168,9 +15267,8 @@ import { spawn } from "child_process";
|
|
|
15168
15267
|
|
|
15169
15268
|
// src/doc-builder.ts
|
|
15170
15269
|
import { spawnSync } from "child_process";
|
|
15171
|
-
import { createHash as createHash2 } from "crypto";
|
|
15172
15270
|
import { promises as fs4 } from "fs";
|
|
15173
|
-
import { join as
|
|
15271
|
+
import { join as join3, relative, sep } from "path";
|
|
15174
15272
|
function detectBranch(projectRoot) {
|
|
15175
15273
|
const r = spawnSync("git", ["-C", projectRoot, "rev-parse", "--abbrev-ref", "HEAD"], {
|
|
15176
15274
|
stdio: "pipe",
|
|
@@ -15180,6 +15278,27 @@ function detectBranch(projectRoot) {
|
|
|
15180
15278
|
const branch = r.stdout.trim();
|
|
15181
15279
|
return branch || null;
|
|
15182
15280
|
}
|
|
15281
|
+
async function readRunRenderer(runFile) {
|
|
15282
|
+
try {
|
|
15283
|
+
const run3 = JSON.parse(await fs4.readFile(runFile, "utf8"));
|
|
15284
|
+
const parsed = rendererSchema.safeParse(run3.renderer);
|
|
15285
|
+
return parsed.success ? parsed.data : null;
|
|
15286
|
+
} catch {
|
|
15287
|
+
return null;
|
|
15288
|
+
}
|
|
15289
|
+
}
|
|
15290
|
+
function baselineRenderer(snap) {
|
|
15291
|
+
const parsed = rendererSchema.safeParse(snap.renderer);
|
|
15292
|
+
return parsed.success ? parsed.data : null;
|
|
15293
|
+
}
|
|
15294
|
+
async function writeSnapshotRenderer(snapshotFile, renderer) {
|
|
15295
|
+
const raw = JSON.parse(await fs4.readFile(snapshotFile, "utf8"));
|
|
15296
|
+
const { renderer: _old, ...rest } = raw;
|
|
15297
|
+
await fs4.writeFile(snapshotFile, JSON.stringify({ renderer, ...rest }, null, 2) + "\n");
|
|
15298
|
+
}
|
|
15299
|
+
function describeRenderer(r) {
|
|
15300
|
+
return `${r.label} [${r.fingerprint}]`;
|
|
15301
|
+
}
|
|
15183
15302
|
function isGroup(node) {
|
|
15184
15303
|
return Array.isArray(node.items);
|
|
15185
15304
|
}
|
|
@@ -15198,7 +15317,7 @@ function indexSnapshot(snap) {
|
|
|
15198
15317
|
else out.set([...prefix, it.name].join("/"), it.hash);
|
|
15199
15318
|
}
|
|
15200
15319
|
};
|
|
15201
|
-
for (const g of snap.groups) walk(g.items ?? [], [g.name]);
|
|
15320
|
+
for (const g of snap.groups ?? []) walk(g.items ?? [], [g.name]);
|
|
15202
15321
|
return out;
|
|
15203
15322
|
}
|
|
15204
15323
|
async function walkActual(dir, prefix = []) {
|
|
@@ -15211,10 +15330,10 @@ async function walkActual(dir, prefix = []) {
|
|
|
15211
15330
|
const out = [];
|
|
15212
15331
|
for (const e of entries) {
|
|
15213
15332
|
if (e.isDirectory()) {
|
|
15214
|
-
out.push(...await walkActual(
|
|
15333
|
+
out.push(...await walkActual(join3(dir, e.name), [...prefix, e.name]));
|
|
15215
15334
|
} else if (e.isFile() && e.name.endsWith(".png")) {
|
|
15216
15335
|
const stem = e.name.slice(0, -4);
|
|
15217
|
-
out.push({ path: [...prefix, stem], absFile:
|
|
15336
|
+
out.push({ path: [...prefix, stem], absFile: join3(dir, e.name) });
|
|
15218
15337
|
}
|
|
15219
15338
|
}
|
|
15220
15339
|
return out;
|
|
@@ -15243,8 +15362,9 @@ function countLeaves(doc) {
|
|
|
15243
15362
|
for (const g of doc.groups) walk(g.items);
|
|
15244
15363
|
return n;
|
|
15245
15364
|
}
|
|
15246
|
-
async function generateDoc(
|
|
15247
|
-
const
|
|
15365
|
+
async function generateDoc(paths, opts = {}) {
|
|
15366
|
+
const { projectRoot, snapshotFile, actualDir, docFile, cacheDir } = paths;
|
|
15367
|
+
const fullRun = opts.fullRun ?? true;
|
|
15248
15368
|
const snap = await readSnapshot(snapshotFile);
|
|
15249
15369
|
const expectedByPath = indexSnapshot(snap);
|
|
15250
15370
|
const actuals = await walkActual(actualDir);
|
|
@@ -15256,7 +15376,7 @@ async function generateDoc(projectRoot, snapshotFile, actualDir, docFile, cacheD
|
|
|
15256
15376
|
const summary = { new: 0, change: 0, deleted: 0, unchanged: 0 };
|
|
15257
15377
|
for (const a of actuals) {
|
|
15258
15378
|
const buf = await fs4.readFile(a.absFile);
|
|
15259
|
-
const hash2 =
|
|
15379
|
+
const hash2 = sha256Hex(buf);
|
|
15260
15380
|
const key = a.path.join("/");
|
|
15261
15381
|
seen.add(key);
|
|
15262
15382
|
const expHash = expectedByPath.get(key);
|
|
@@ -15282,7 +15402,7 @@ async function generateDoc(projectRoot, snapshotFile, actualDir, docFile, cacheD
|
|
|
15282
15402
|
});
|
|
15283
15403
|
summary.change++;
|
|
15284
15404
|
}
|
|
15285
|
-
if (
|
|
15405
|
+
if (fullRun) {
|
|
15286
15406
|
for (const [key, hash2] of expectedByPath) {
|
|
15287
15407
|
if (seen.has(key) || !hash2) continue;
|
|
15288
15408
|
insertLeaf(doc, key.split("/"), {
|
|
@@ -15293,13 +15413,47 @@ async function generateDoc(projectRoot, snapshotFile, actualDir, docFile, cacheD
|
|
|
15293
15413
|
summary.deleted++;
|
|
15294
15414
|
}
|
|
15295
15415
|
}
|
|
15416
|
+
const current = await readRunRenderer(paths.runFile);
|
|
15417
|
+
let baseline = baselineRenderer(snap);
|
|
15418
|
+
const allMatched = fullRun && summary.change === 0 && summary.unchanged > 0;
|
|
15419
|
+
if (current && allMatched && baseline?.fingerprint !== current.fingerprint && !process.env.CI) {
|
|
15420
|
+
await writeSnapshotRenderer(snapshotFile, current);
|
|
15421
|
+
process.stdout.write(
|
|
15422
|
+
`[screentest] recorded renderer ${describeRenderer(current)} in ${relative(projectRoot, snapshotFile)}` + (baseline ? ` (was ${describeRenderer(baseline)})` : "") + ` \u2014 every screenshot matched under it. Commit it with the baselines.
|
|
15423
|
+
`
|
|
15424
|
+
);
|
|
15425
|
+
baseline = current;
|
|
15426
|
+
}
|
|
15427
|
+
const mismatch = !!current && !!baseline && baseline.fingerprint !== current.fingerprint && summary.change > 0 && summary.change >= summary.unchanged;
|
|
15428
|
+
if (current) doc.renderer = { current, baseline, mismatch };
|
|
15296
15429
|
await fs4.mkdir(cacheDir, { recursive: true });
|
|
15297
15430
|
await fs4.writeFile(docFile, JSON.stringify(doc, null, 2));
|
|
15298
15431
|
const total = countLeaves(doc);
|
|
15299
|
-
|
|
15432
|
+
if (mismatch && current && baseline) {
|
|
15433
|
+
process.stderr.write(
|
|
15434
|
+
`
|
|
15435
|
+
[screentest] RENDERER CHANGED \u2014 the diffs below are likely not app changes.
|
|
15436
|
+
[screentest] baselines: ${describeRenderer(baseline)}
|
|
15437
|
+
[screentest] this run: ${describeRenderer(current)}
|
|
15438
|
+
[screentest] Re-run with the baseline renderer, or accept everything to
|
|
15439
|
+
[screentest] move the baselines to this one.
|
|
15440
|
+
|
|
15441
|
+
`
|
|
15442
|
+
);
|
|
15443
|
+
} else if (current && !baseline && summary.change > 0 && summary.unchanged === 0) {
|
|
15444
|
+
process.stderr.write(
|
|
15445
|
+
`
|
|
15446
|
+
[screentest] Every screenshot changed, and ${relative(projectRoot, snapshotFile)}
|
|
15447
|
+
[screentest] records no renderer to compare with. Make sure this run used
|
|
15448
|
+
[screentest] the same renderer as the baselines before accepting.
|
|
15449
|
+
|
|
15450
|
+
`
|
|
15451
|
+
);
|
|
15452
|
+
}
|
|
15453
|
+
const deletedSummary = fullRun ? `deleted: ${summary.deleted}, ` : "";
|
|
15300
15454
|
process.stdout.write(
|
|
15301
15455
|
`Wrote ${docFile}
|
|
15302
|
-
${total} diff${total === 1 ? "" : "s"} to review (new: ${summary.new}, change: ${summary.change}, ${deletedSummary}unchanged: ${summary.unchanged}${
|
|
15456
|
+
${total} diff${total === 1 ? "" : "s"} to review (new: ${summary.new}, change: ${summary.change}, ${deletedSummary}unchanged: ${summary.unchanged}${fullRun ? "" : "; deletion detection disabled \u2014 subset run"})
|
|
15303
15457
|
`
|
|
15304
15458
|
);
|
|
15305
15459
|
return total;
|
|
@@ -15307,25 +15461,26 @@ async function generateDoc(projectRoot, snapshotFile, actualDir, docFile, cacheD
|
|
|
15307
15461
|
|
|
15308
15462
|
// src/runner/paths.ts
|
|
15309
15463
|
import { existsSync as existsSync2 } from "fs";
|
|
15310
|
-
import { isAbsolute as isAbsolute2, join as
|
|
15464
|
+
import { isAbsolute as isAbsolute2, join as join4, resolve as resolve6 } from "path";
|
|
15311
15465
|
function resolveRunnerPaths(cwd = process.cwd()) {
|
|
15312
15466
|
const projectRoot = resolve6(cwd);
|
|
15313
|
-
const cacheDir =
|
|
15467
|
+
const cacheDir = join4(projectRoot, "node_modules", ".cache", "screentest");
|
|
15314
15468
|
return {
|
|
15315
15469
|
projectRoot,
|
|
15316
15470
|
snapshotFile: resolveSnapshotFile(projectRoot),
|
|
15317
15471
|
cacheDir,
|
|
15318
|
-
actualDir:
|
|
15319
|
-
docFile:
|
|
15472
|
+
actualDir: join4(cacheDir, "actual"),
|
|
15473
|
+
docFile: join4(cacheDir, "doc.json"),
|
|
15474
|
+
runFile: join4(cacheDir, "run.json")
|
|
15320
15475
|
};
|
|
15321
15476
|
}
|
|
15322
15477
|
function resolveSnapshotFile(projectRoot) {
|
|
15323
15478
|
const envPath = process.env.SCREENTEST_SNAPSHOT_FILE;
|
|
15324
15479
|
if (envPath) {
|
|
15325
|
-
return isAbsolute2(envPath) ? envPath :
|
|
15480
|
+
return isAbsolute2(envPath) ? envPath : join4(projectRoot, envPath);
|
|
15326
15481
|
}
|
|
15327
|
-
const legacyRoot =
|
|
15328
|
-
const insideTests =
|
|
15482
|
+
const legacyRoot = join4(projectRoot, "snapshot.json");
|
|
15483
|
+
const insideTests = join4(projectRoot, "tests", "snapshot.json");
|
|
15329
15484
|
if (existsSync2(legacyRoot) && !existsSync2(insideTests)) return legacyRoot;
|
|
15330
15485
|
return insideTests;
|
|
15331
15486
|
}
|
|
@@ -15339,13 +15494,7 @@ function run(cmd, argv) {
|
|
|
15339
15494
|
}
|
|
15340
15495
|
async function runReview() {
|
|
15341
15496
|
const paths = resolveRunnerPaths();
|
|
15342
|
-
const total = await generateDoc(
|
|
15343
|
-
paths.projectRoot,
|
|
15344
|
-
paths.snapshotFile,
|
|
15345
|
-
paths.actualDir,
|
|
15346
|
-
paths.docFile,
|
|
15347
|
-
paths.cacheDir
|
|
15348
|
-
);
|
|
15497
|
+
const total = await generateDoc(paths);
|
|
15349
15498
|
if (total === 0) {
|
|
15350
15499
|
process.stdout.write("Nothing to review \u2014 all snapshots match.\n");
|
|
15351
15500
|
return 0;
|
|
@@ -15356,7 +15505,7 @@ async function runReview() {
|
|
|
15356
15505
|
// src/init.ts
|
|
15357
15506
|
import { copyFile, mkdir } from "fs/promises";
|
|
15358
15507
|
import { existsSync as existsSync3 } from "fs";
|
|
15359
|
-
import { dirname as dirname5, join as
|
|
15508
|
+
import { dirname as dirname5, join as join5, resolve as resolve7 } from "path";
|
|
15360
15509
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
15361
15510
|
async function runInit() {
|
|
15362
15511
|
const here = dirname5(fileURLToPath3(import.meta.url));
|
|
@@ -15367,8 +15516,8 @@ async function runInit() {
|
|
|
15367
15516
|
{ from: "example.test.ts", to: "tests/example.test.ts" }
|
|
15368
15517
|
];
|
|
15369
15518
|
for (const { from, to } of targets) {
|
|
15370
|
-
const src =
|
|
15371
|
-
const dst =
|
|
15519
|
+
const src = join5(templatesDir, from);
|
|
15520
|
+
const dst = join5(cwd, to);
|
|
15372
15521
|
if (existsSync3(dst)) {
|
|
15373
15522
|
process.stdout.write(` exists ${to}
|
|
15374
15523
|
`);
|
|
@@ -15401,9 +15550,9 @@ Tip: add a script to package.json so you can just \`npm run screentest\`:
|
|
|
15401
15550
|
// src/daemon.ts
|
|
15402
15551
|
import { spawn as spawn2, spawnSync as spawnSync2 } from "child_process";
|
|
15403
15552
|
import { existsSync as existsSync4 } from "fs";
|
|
15553
|
+
import { request } from "http";
|
|
15404
15554
|
import { dirname as dirname6, resolve as resolve8 } from "path";
|
|
15405
15555
|
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
15406
|
-
import { createConnection } from "net";
|
|
15407
15556
|
var IMAGE_NAME = "cevek/screentest-firefox";
|
|
15408
15557
|
var IMAGE_TAG = "pw-1.60.0";
|
|
15409
15558
|
var FULL_IMAGE = `${IMAGE_NAME}:${IMAGE_TAG}`;
|
|
@@ -15411,23 +15560,27 @@ var CONTAINER_NAME = "screentest-firefox";
|
|
|
15411
15560
|
var PORT = 5180;
|
|
15412
15561
|
var WS_PATH = "screentest-firefox";
|
|
15413
15562
|
var WS_ENDPOINT = `ws://localhost:${PORT}/${WS_PATH}`;
|
|
15563
|
+
var START_TIMEOUT_MS = 3e4;
|
|
15414
15564
|
function run2(cmd, argv) {
|
|
15415
15565
|
return new Promise((resolveExit) => {
|
|
15416
15566
|
const p = spawn2(cmd, argv, { stdio: "inherit" });
|
|
15567
|
+
p.on("error", () => resolveExit(127));
|
|
15417
15568
|
p.on("exit", (code) => resolveExit(code ?? 0));
|
|
15418
15569
|
});
|
|
15419
15570
|
}
|
|
15420
15571
|
function daemonState() {
|
|
15421
15572
|
const ps = spawnSync2(
|
|
15422
15573
|
"docker",
|
|
15423
|
-
["ps", "-a", "-f", `name=^${CONTAINER_NAME}$`, "--format", "{{.
|
|
15574
|
+
["ps", "-a", "-f", `name=^${CONTAINER_NAME}$`, "--format", "{{.State}} {{.Image}}"],
|
|
15424
15575
|
{ stdio: "pipe", encoding: "utf8" }
|
|
15425
15576
|
);
|
|
15426
|
-
|
|
15427
|
-
if (
|
|
15428
|
-
|
|
15429
|
-
|
|
15430
|
-
|
|
15577
|
+
if (ps.error) return { state: "no-docker", reason: ps.error.message };
|
|
15578
|
+
if (ps.status !== 0) return { state: "no-docker", reason: ps.stderr.trim() };
|
|
15579
|
+
const [status = "", image = ""] = ps.stdout.trim().split(" ");
|
|
15580
|
+
if (status === "running") return { state: "running", image: image.trim() };
|
|
15581
|
+
if (status === "paused") return { state: "paused" };
|
|
15582
|
+
if (status === "created" || status === "restarting" || status === "removing") {
|
|
15583
|
+
return { state: "transitional", status };
|
|
15431
15584
|
}
|
|
15432
15585
|
const img = spawnSync2("docker", ["image", "inspect", FULL_IMAGE], { stdio: "pipe" });
|
|
15433
15586
|
if (img.status !== 0) return { state: "missing-image" };
|
|
@@ -15464,75 +15617,115 @@ async function buildImage() {
|
|
|
15464
15617
|
}
|
|
15465
15618
|
return true;
|
|
15466
15619
|
}
|
|
15467
|
-
function
|
|
15620
|
+
function probeServer() {
|
|
15468
15621
|
return new Promise((resolveProbe) => {
|
|
15469
|
-
const
|
|
15470
|
-
|
|
15471
|
-
|
|
15622
|
+
const req = request({ host: "127.0.0.1", port: PORT, path: `/${WS_PATH}`, timeout: 1e3 });
|
|
15623
|
+
req.once("response", (res) => {
|
|
15624
|
+
res.resume();
|
|
15472
15625
|
resolveProbe(true);
|
|
15473
15626
|
});
|
|
15474
|
-
|
|
15627
|
+
req.once("timeout", () => req.destroy());
|
|
15628
|
+
req.once("error", () => resolveProbe(false));
|
|
15629
|
+
req.end();
|
|
15475
15630
|
});
|
|
15476
15631
|
}
|
|
15477
15632
|
async function waitForReady(timeoutMs = 1e4) {
|
|
15478
15633
|
const start = Date.now();
|
|
15479
15634
|
while (Date.now() - start < timeoutMs) {
|
|
15480
|
-
if (await
|
|
15481
|
-
await
|
|
15635
|
+
if (await probeServer()) return true;
|
|
15636
|
+
await sleep(150);
|
|
15482
15637
|
}
|
|
15483
15638
|
return false;
|
|
15484
15639
|
}
|
|
15485
|
-
|
|
15486
|
-
|
|
15487
|
-
|
|
15488
|
-
|
|
15489
|
-
|
|
15490
|
-
|
|
15640
|
+
function sleep(ms) {
|
|
15641
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
15642
|
+
}
|
|
15643
|
+
function assertUsable(s) {
|
|
15644
|
+
if (s.state === "no-docker") {
|
|
15645
|
+
throw new Error(
|
|
15646
|
+
`docker is not available \u2014 screentest renders only in the pinned Firefox
|
|
15647
|
+
container (${FULL_IMAGE}). Start Docker, then re-run.
|
|
15648
|
+
docker said: ${s.reason}
|
|
15649
|
+
`
|
|
15650
|
+
);
|
|
15651
|
+
}
|
|
15652
|
+
if (s.state === "paused") {
|
|
15653
|
+
throw new Error(`screentest-firefox daemon is paused.
|
|
15654
|
+
\u2192 docker unpause ${CONTAINER_NAME}
|
|
15655
|
+
`);
|
|
15656
|
+
}
|
|
15657
|
+
if (s.state === "running" && s.image !== FULL_IMAGE) {
|
|
15658
|
+
throw new Error(
|
|
15659
|
+
`screentest-firefox daemon is running, but on a different image:
|
|
15491
15660
|
running: ${s.image}
|
|
15492
15661
|
this @cevek/screentest wants: ${FULL_IMAGE}
|
|
15493
15662
|
\u2192 stop it first: screentest stop
|
|
15494
15663
|
\u2192 then re-run: screentest serve
|
|
15664
|
+
`
|
|
15665
|
+
);
|
|
15666
|
+
}
|
|
15667
|
+
}
|
|
15668
|
+
function startContainer() {
|
|
15669
|
+
spawnSync2("docker", ["rm", CONTAINER_NAME], { stdio: "pipe" });
|
|
15670
|
+
const r = spawnSync2(
|
|
15671
|
+
"docker",
|
|
15672
|
+
["run", "-d", "--rm", "--name", CONTAINER_NAME, "--network=host", FULL_IMAGE],
|
|
15673
|
+
{ stdio: "pipe", encoding: "utf8" }
|
|
15674
|
+
);
|
|
15675
|
+
return { ok: !r.error && r.status === 0, stderr: r.stderr?.trim() ?? String(r.error) };
|
|
15676
|
+
}
|
|
15677
|
+
async function ensureDaemon() {
|
|
15678
|
+
const deadline = Date.now() + START_TIMEOUT_MS;
|
|
15679
|
+
let started = false;
|
|
15680
|
+
let lastError = "";
|
|
15681
|
+
for (; ; ) {
|
|
15682
|
+
const s = daemonState();
|
|
15683
|
+
assertUsable(s);
|
|
15684
|
+
if (s.state === "running") {
|
|
15685
|
+
if (await waitForReady()) return { started };
|
|
15686
|
+
spawnSync2("docker", ["logs", CONTAINER_NAME], { stdio: "inherit" });
|
|
15687
|
+
throw new Error(
|
|
15688
|
+
`screentest-firefox daemon container is up but never answered on port ${PORT}.
|
|
15495
15689
|
`
|
|
15496
15690
|
);
|
|
15497
|
-
return 1;
|
|
15498
15691
|
}
|
|
15499
|
-
|
|
15500
|
-
|
|
15501
|
-
|
|
15502
|
-
}
|
|
15503
|
-
|
|
15504
|
-
|
|
15505
|
-
|
|
15506
|
-
|
|
15507
|
-
|
|
15692
|
+
if (Date.now() > deadline) {
|
|
15693
|
+
throw new Error(
|
|
15694
|
+
`screentest-firefox daemon did not come up within ${START_TIMEOUT_MS / 1e3}s` + (s.state === "transitional" ? ` (container stuck in "${s.status}")` : "") + (lastError ? `:
|
|
15695
|
+
${lastError}` : "") + `
|
|
15696
|
+
`
|
|
15697
|
+
);
|
|
15698
|
+
}
|
|
15699
|
+
if (s.state === "missing-image" && !await buildImage()) {
|
|
15700
|
+
throw new Error(`could not build ${FULL_IMAGE}
|
|
15508
15701
|
`);
|
|
15509
|
-
|
|
15510
|
-
"
|
|
15511
|
-
|
|
15512
|
-
"--rm",
|
|
15513
|
-
"--name",
|
|
15514
|
-
CONTAINER_NAME,
|
|
15515
|
-
"--network=host",
|
|
15516
|
-
FULL_IMAGE
|
|
15517
|
-
]);
|
|
15518
|
-
if (code !== 0) {
|
|
15519
|
-
process.stderr.write(`docker run failed (exit ${code})
|
|
15702
|
+
}
|
|
15703
|
+
if (s.state === "stopped" || s.state === "missing-image") {
|
|
15704
|
+
if (!started) process.stderr.write(`Starting screentest-firefox daemon (port ${PORT})\u2026
|
|
15520
15705
|
`);
|
|
15521
|
-
|
|
15706
|
+
const r = startContainer();
|
|
15707
|
+
if (r.ok) started = true;
|
|
15708
|
+
else lastError = r.stderr;
|
|
15709
|
+
}
|
|
15710
|
+
await sleep(300);
|
|
15522
15711
|
}
|
|
15523
|
-
|
|
15524
|
-
|
|
15525
|
-
|
|
15526
|
-
|
|
15712
|
+
}
|
|
15713
|
+
async function startDaemon() {
|
|
15714
|
+
try {
|
|
15715
|
+
const { started } = await ensureDaemon();
|
|
15716
|
+
process.stdout.write(
|
|
15717
|
+
`screentest-firefox daemon ${started ? "ready" : "already running"} at ${WS_ENDPOINT}
|
|
15718
|
+
`
|
|
15719
|
+
);
|
|
15720
|
+
return 0;
|
|
15721
|
+
} catch (err) {
|
|
15722
|
+
process.stderr.write(err instanceof Error ? err.message : String(err));
|
|
15527
15723
|
return 1;
|
|
15528
15724
|
}
|
|
15529
|
-
process.stdout.write(`screentest-firefox daemon ready at ${WS_ENDPOINT}
|
|
15530
|
-
`);
|
|
15531
|
-
return 0;
|
|
15532
15725
|
}
|
|
15533
15726
|
async function stopDaemon() {
|
|
15534
15727
|
const s = daemonState();
|
|
15535
|
-
if (s.state !== "running") {
|
|
15728
|
+
if (s.state !== "running" && s.state !== "paused") {
|
|
15536
15729
|
process.stdout.write("screentest-firefox daemon is not running\n");
|
|
15537
15730
|
return 0;
|
|
15538
15731
|
}
|
|
@@ -15542,6 +15735,26 @@ async function stopDaemon() {
|
|
|
15542
15735
|
}
|
|
15543
15736
|
async function printStatus() {
|
|
15544
15737
|
const s = daemonState();
|
|
15738
|
+
if (s.state === "no-docker") {
|
|
15739
|
+
process.stdout.write(
|
|
15740
|
+
`screentest-firefox daemon: unknown (docker is not available: ${s.reason})
|
|
15741
|
+
`
|
|
15742
|
+
);
|
|
15743
|
+
return 1;
|
|
15744
|
+
}
|
|
15745
|
+
if (s.state === "paused") {
|
|
15746
|
+
process.stdout.write(
|
|
15747
|
+
`screentest-firefox daemon: paused
|
|
15748
|
+
\u2192 docker unpause ${CONTAINER_NAME}
|
|
15749
|
+
`
|
|
15750
|
+
);
|
|
15751
|
+
return 0;
|
|
15752
|
+
}
|
|
15753
|
+
if (s.state === "transitional") {
|
|
15754
|
+
process.stdout.write(`screentest-firefox daemon: ${s.status}
|
|
15755
|
+
`);
|
|
15756
|
+
return 0;
|
|
15757
|
+
}
|
|
15545
15758
|
if (s.state === "running") {
|
|
15546
15759
|
const match = s.image === FULL_IMAGE ? "(matches this @cevek/screentest)" : "(MISMATCH!)";
|
|
15547
15760
|
process.stdout.write(
|
|
@@ -15564,19 +15777,295 @@ async function printStatus() {
|
|
|
15564
15777
|
if (s.state === "stopped") {
|
|
15565
15778
|
process.stdout.write(
|
|
15566
15779
|
`screentest-firefox daemon: stopped (image ${FULL_IMAGE} exists)
|
|
15567
|
-
|
|
15780
|
+
starts automatically on the next test run, or now with: screentest serve
|
|
15568
15781
|
`
|
|
15569
15782
|
);
|
|
15570
15783
|
return 0;
|
|
15571
15784
|
}
|
|
15572
15785
|
process.stdout.write(
|
|
15573
15786
|
`screentest-firefox daemon: not installed (image ${FULL_IMAGE} missing)
|
|
15574
|
-
|
|
15787
|
+
built + started automatically on the next test run, or now with: screentest serve
|
|
15575
15788
|
`
|
|
15576
15789
|
);
|
|
15577
15790
|
return 0;
|
|
15578
15791
|
}
|
|
15579
15792
|
|
|
15793
|
+
// src/repair.ts
|
|
15794
|
+
import { promises as fs5 } from "fs";
|
|
15795
|
+
|
|
15796
|
+
// src/concurrency.ts
|
|
15797
|
+
async function mapLimit(items, limit, fn) {
|
|
15798
|
+
const out = new Array(items.length);
|
|
15799
|
+
let next = 0;
|
|
15800
|
+
await Promise.all(
|
|
15801
|
+
Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
15802
|
+
for (; ; ) {
|
|
15803
|
+
const i = next++;
|
|
15804
|
+
if (i >= items.length) return;
|
|
15805
|
+
out[i] = await fn(items[i]);
|
|
15806
|
+
}
|
|
15807
|
+
})
|
|
15808
|
+
);
|
|
15809
|
+
return out;
|
|
15810
|
+
}
|
|
15811
|
+
|
|
15812
|
+
// src/repair.ts
|
|
15813
|
+
var PROBE_CONCURRENCY = 8;
|
|
15814
|
+
function parseRepairArgs(rawArgs) {
|
|
15815
|
+
const { store, rest } = parseStoreArgs(rawArgs);
|
|
15816
|
+
let dryRun = false;
|
|
15817
|
+
for (const a of rest) {
|
|
15818
|
+
if (a === "--dry-run") dryRun = true;
|
|
15819
|
+
else throw new Error(`Unknown argument: ${a}`);
|
|
15820
|
+
}
|
|
15821
|
+
return { store, dryRun };
|
|
15822
|
+
}
|
|
15823
|
+
async function indexActualsByHash(actualDir) {
|
|
15824
|
+
const files = await walkActual(actualDir);
|
|
15825
|
+
const byHash = /* @__PURE__ */ new Map();
|
|
15826
|
+
for (const f of files) {
|
|
15827
|
+
const buf = await fs5.readFile(f.absFile);
|
|
15828
|
+
const hash2 = sha256Hex(buf);
|
|
15829
|
+
if (!byHash.has(hash2)) byHash.set(hash2, f.absFile);
|
|
15830
|
+
}
|
|
15831
|
+
return byHash;
|
|
15832
|
+
}
|
|
15833
|
+
async function runRepair(opts) {
|
|
15834
|
+
const paths = resolveRunnerPaths();
|
|
15835
|
+
const snapshot = await readSnapshot(paths.snapshotFile);
|
|
15836
|
+
const entries = [...indexSnapshot(snapshot)].filter(
|
|
15837
|
+
(e) => typeof e[1] === "string"
|
|
15838
|
+
);
|
|
15839
|
+
if (entries.length === 0) {
|
|
15840
|
+
process.stdout.write(`No hashes in ${paths.snapshotFile}
|
|
15841
|
+
`);
|
|
15842
|
+
return 0;
|
|
15843
|
+
}
|
|
15844
|
+
const cfg = storeConfigFor(opts.store, paths.projectRoot);
|
|
15845
|
+
const byHash = await indexActualsByHash(paths.actualDir);
|
|
15846
|
+
const hashes = [...new Set(entries.map(([, hash2]) => hash2))];
|
|
15847
|
+
process.stdout.write(
|
|
15848
|
+
`${paths.snapshotFile}: ${entries.length} entries, ${hashes.length} unique hashes
|
|
15849
|
+
Local actuals: ${byHash.size} unique hashes
|
|
15850
|
+
Probing ${cfg.workerUrl}/${cfg.project}/ \u2026
|
|
15851
|
+
`
|
|
15852
|
+
);
|
|
15853
|
+
const present = await mapLimit(hashes, PROBE_CONCURRENCY, (h) => existsInStore(cfg, h));
|
|
15854
|
+
const missing = hashes.filter((_, i) => !present[i]);
|
|
15855
|
+
if (missing.length === 0) {
|
|
15856
|
+
process.stdout.write(`All ${hashes.length} blobs present. Nothing to repair.
|
|
15857
|
+
`);
|
|
15858
|
+
return 0;
|
|
15859
|
+
}
|
|
15860
|
+
const recoverable = missing.filter((h) => byHash.has(h));
|
|
15861
|
+
const lost = missing.filter((h) => !byHash.has(h));
|
|
15862
|
+
process.stdout.write(
|
|
15863
|
+
`Missing: ${missing.length} (recoverable ${recoverable.length}, lost ${lost.length})
|
|
15864
|
+
`
|
|
15865
|
+
);
|
|
15866
|
+
if (lost.length > 0) {
|
|
15867
|
+
const nameOf = /* @__PURE__ */ new Map();
|
|
15868
|
+
for (const [name, hash2] of entries) if (!nameOf.has(hash2)) nameOf.set(hash2, name);
|
|
15869
|
+
process.stdout.write(`
|
|
15870
|
+
Gone from the store and not in the local cache:
|
|
15871
|
+
`);
|
|
15872
|
+
for (const h of lost) process.stdout.write(` ${h.slice(0, 12)}\u2026 ${nameOf.get(h) ?? "?"}
|
|
15873
|
+
`);
|
|
15874
|
+
process.stdout.write(
|
|
15875
|
+
`
|
|
15876
|
+
Re-run the tests, then accept these in the review UI \u2014 their baseline
|
|
15877
|
+
content no longer exists anywhere.
|
|
15878
|
+
|
|
15879
|
+
`
|
|
15880
|
+
);
|
|
15881
|
+
}
|
|
15882
|
+
if (opts.dryRun) {
|
|
15883
|
+
process.stdout.write(
|
|
15884
|
+
`Dry run \u2014 would upload ${recoverable.length}. Re-run without --dry-run.
|
|
15885
|
+
`
|
|
15886
|
+
);
|
|
15887
|
+
return lost.length > 0 ? 1 : 0;
|
|
15888
|
+
}
|
|
15889
|
+
let uploaded = 0;
|
|
15890
|
+
const failed = [];
|
|
15891
|
+
await mapLimit(recoverable, PROBE_CONCURRENCY, async (hash2) => {
|
|
15892
|
+
try {
|
|
15893
|
+
const buf = await fs5.readFile(byHash.get(hash2));
|
|
15894
|
+
if (sha256Hex(buf) !== hash2) {
|
|
15895
|
+
failed.push(`${hash2.slice(0, 12)}\u2026: local file changed since scan, skipped`);
|
|
15896
|
+
return;
|
|
15897
|
+
}
|
|
15898
|
+
await uploadPng(cfg, hash2, buf);
|
|
15899
|
+
uploaded++;
|
|
15900
|
+
} catch (err) {
|
|
15901
|
+
failed.push(`${hash2.slice(0, 12)}\u2026: ${err instanceof Error ? err.message : String(err)}`);
|
|
15902
|
+
}
|
|
15903
|
+
});
|
|
15904
|
+
process.stdout.write(`Uploaded ${uploaded}, failed ${failed.length}
|
|
15905
|
+
`);
|
|
15906
|
+
for (const f of failed) process.stdout.write(` ${f}
|
|
15907
|
+
`);
|
|
15908
|
+
return failed.length > 0 || lost.length > 0 ? 1 : 0;
|
|
15909
|
+
}
|
|
15910
|
+
|
|
15911
|
+
// src/gc.ts
|
|
15912
|
+
import { promises as fs6 } from "fs";
|
|
15913
|
+
import { isAbsolute as isAbsolute3, join as join6, relative as relative2, resolve as resolve9 } from "path";
|
|
15914
|
+
var DELETE_CONCURRENCY = 8;
|
|
15915
|
+
function parseGcArgs(rawArgs) {
|
|
15916
|
+
const { store, rest } = parseStoreArgs(rawArgs);
|
|
15917
|
+
const snapshotFiles = [];
|
|
15918
|
+
let confirmed = false;
|
|
15919
|
+
for (let i = 0; i < rest.length; i++) {
|
|
15920
|
+
const a = rest[i];
|
|
15921
|
+
if (a === "--snapshot") {
|
|
15922
|
+
const v = rest[++i];
|
|
15923
|
+
if (!v) throw new Error("--snapshot requires a path");
|
|
15924
|
+
snapshotFiles.push(v);
|
|
15925
|
+
} else if (a === "--yes") {
|
|
15926
|
+
confirmed = true;
|
|
15927
|
+
} else if (a === "--dry-run") {
|
|
15928
|
+
confirmed = false;
|
|
15929
|
+
} else {
|
|
15930
|
+
throw new Error(`Unknown argument: ${a}`);
|
|
15931
|
+
}
|
|
15932
|
+
}
|
|
15933
|
+
return { store, snapshotFiles, confirmed };
|
|
15934
|
+
}
|
|
15935
|
+
async function discoverSnapshotFiles(projectRoot, configured, extra) {
|
|
15936
|
+
const required2 = extra.map((p) => isAbsolute3(p) ? p : resolve9(projectRoot, p));
|
|
15937
|
+
for (const f of required2) {
|
|
15938
|
+
try {
|
|
15939
|
+
await fs6.access(f);
|
|
15940
|
+
} catch {
|
|
15941
|
+
throw new Error(`Snapshot file not found: ${f}`);
|
|
15942
|
+
}
|
|
15943
|
+
}
|
|
15944
|
+
const found = /* @__PURE__ */ new Set([...required2, configured]);
|
|
15945
|
+
for (const dir of [projectRoot, join6(projectRoot, "tests")]) {
|
|
15946
|
+
let entries;
|
|
15947
|
+
try {
|
|
15948
|
+
entries = await fs6.readdir(dir, { withFileTypes: true });
|
|
15949
|
+
} catch {
|
|
15950
|
+
continue;
|
|
15951
|
+
}
|
|
15952
|
+
for (const e of entries) {
|
|
15953
|
+
if (e.isFile() && /^snapshot.*\.json$/.test(e.name)) found.add(join6(dir, e.name));
|
|
15954
|
+
}
|
|
15955
|
+
}
|
|
15956
|
+
const existing = [];
|
|
15957
|
+
for (const f of found) {
|
|
15958
|
+
try {
|
|
15959
|
+
await fs6.access(f);
|
|
15960
|
+
existing.push(f);
|
|
15961
|
+
} catch {
|
|
15962
|
+
}
|
|
15963
|
+
}
|
|
15964
|
+
return existing.sort();
|
|
15965
|
+
}
|
|
15966
|
+
async function readSnapshotStrict(file2) {
|
|
15967
|
+
const raw = await fs6.readFile(file2, "utf8");
|
|
15968
|
+
let parsed;
|
|
15969
|
+
try {
|
|
15970
|
+
parsed = JSON.parse(raw);
|
|
15971
|
+
} catch (err) {
|
|
15972
|
+
throw new Error(
|
|
15973
|
+
`${file2} is not valid JSON (${err instanceof Error ? err.message : String(err)}). Refusing to run \u2014 a file that fails to parse would look like an empty baseline.`
|
|
15974
|
+
);
|
|
15975
|
+
}
|
|
15976
|
+
const result = snapshotDocSchema.safeParse(parsed);
|
|
15977
|
+
if (!result.success) {
|
|
15978
|
+
throw new Error(
|
|
15979
|
+
`${file2} is not a snapshot document: ${result.error.issues.map((i) => `${i.path.join(".")} ${i.message}`).join("; ")}`
|
|
15980
|
+
);
|
|
15981
|
+
}
|
|
15982
|
+
return result.data;
|
|
15983
|
+
}
|
|
15984
|
+
async function runGc(opts) {
|
|
15985
|
+
const paths = resolveRunnerPaths();
|
|
15986
|
+
const cfg = storeConfigFor(opts.store, paths.projectRoot);
|
|
15987
|
+
const snapshotFiles = await discoverSnapshotFiles(
|
|
15988
|
+
paths.projectRoot,
|
|
15989
|
+
paths.snapshotFile,
|
|
15990
|
+
opts.snapshotFiles
|
|
15991
|
+
);
|
|
15992
|
+
if (snapshotFiles.length === 0) {
|
|
15993
|
+
process.stderr.write(
|
|
15994
|
+
`No snapshot files found under ${paths.projectRoot}.
|
|
15995
|
+
Refusing to run: with nothing to compare against, every blob would look
|
|
15996
|
+
unreferenced. Pass --snapshot <path> if they live elsewhere.
|
|
15997
|
+
`
|
|
15998
|
+
);
|
|
15999
|
+
return 1;
|
|
16000
|
+
}
|
|
16001
|
+
const live = /* @__PURE__ */ new Set();
|
|
16002
|
+
process.stdout.write(`Live hashes from:
|
|
16003
|
+
`);
|
|
16004
|
+
for (const file2 of snapshotFiles) {
|
|
16005
|
+
const before = live.size;
|
|
16006
|
+
for (const hash2 of indexSnapshot(await readSnapshotStrict(file2)).values()) {
|
|
16007
|
+
if (typeof hash2 === "string") live.add(hash2);
|
|
16008
|
+
}
|
|
16009
|
+
process.stdout.write(` ${relative2(paths.projectRoot, file2)} (+${live.size - before} new)
|
|
16010
|
+
`);
|
|
16011
|
+
}
|
|
16012
|
+
if (live.size === 0) {
|
|
16013
|
+
process.stderr.write(
|
|
16014
|
+
`
|
|
16015
|
+
Every snapshot file parsed, and not one hash among them.
|
|
16016
|
+
Refusing to delete: an empty baseline set makes the whole store look
|
|
16017
|
+
unreferenced. Accept something first, or narrow with --snapshot.
|
|
16018
|
+
`
|
|
16019
|
+
);
|
|
16020
|
+
return 1;
|
|
16021
|
+
}
|
|
16022
|
+
process.stdout.write(`
|
|
16023
|
+
${live.size} live hashes. Listing ${cfg.workerUrl}/${cfg.project}/ \u2026
|
|
16024
|
+
`);
|
|
16025
|
+
const objects = await listStore(cfg);
|
|
16026
|
+
const unrecognized = [];
|
|
16027
|
+
const garbage = objects.filter((o) => {
|
|
16028
|
+
const hash2 = hashFromKey(cfg.project, o.key);
|
|
16029
|
+
if (hash2 === null) {
|
|
16030
|
+
unrecognized.push(o.key);
|
|
16031
|
+
return false;
|
|
16032
|
+
}
|
|
16033
|
+
return !live.has(hash2);
|
|
16034
|
+
});
|
|
16035
|
+
const bytes = garbage.reduce((n, o) => n + o.size, 0);
|
|
16036
|
+
process.stdout.write(
|
|
16037
|
+
`Stored: ${objects.length} objects. Unreferenced: ${garbage.length} (${(bytes / 1048576).toFixed(1)} MB)
|
|
16038
|
+
`
|
|
16039
|
+
);
|
|
16040
|
+
if (unrecognized.length > 0) {
|
|
16041
|
+
process.stdout.write(
|
|
16042
|
+
`Left alone \u2014 ${unrecognized.length} object(s) under the prefix that are not blob keys:
|
|
16043
|
+
` + unrecognized.slice(0, 10).map((k) => ` ${k}
|
|
16044
|
+
`).join("")
|
|
16045
|
+
);
|
|
16046
|
+
}
|
|
16047
|
+
if (garbage.length === 0) return 0;
|
|
16048
|
+
if (!opts.confirmed) {
|
|
16049
|
+
process.stdout.write(`
|
|
16050
|
+
Dry run \u2014 nothing deleted. Re-run with --yes to delete.
|
|
16051
|
+
`);
|
|
16052
|
+
return 0;
|
|
16053
|
+
}
|
|
16054
|
+
const failed = [];
|
|
16055
|
+
await mapLimit(garbage, DELETE_CONCURRENCY, async (o) => {
|
|
16056
|
+
try {
|
|
16057
|
+
await deleteKey(cfg, o.key);
|
|
16058
|
+
} catch (err) {
|
|
16059
|
+
failed.push(`${o.key}: ${err instanceof Error ? err.message : String(err)}`);
|
|
16060
|
+
}
|
|
16061
|
+
});
|
|
16062
|
+
process.stdout.write(`Deleted ${garbage.length - failed.length}, failed ${failed.length}
|
|
16063
|
+
`);
|
|
16064
|
+
for (const f of failed) process.stdout.write(` ${f}
|
|
16065
|
+
`);
|
|
16066
|
+
return failed.length > 0 ? 1 : 0;
|
|
16067
|
+
}
|
|
16068
|
+
|
|
15580
16069
|
// src/index.ts
|
|
15581
16070
|
function printHelp() {
|
|
15582
16071
|
process.stderr.write(
|
|
@@ -15588,21 +16077,32 @@ function printHelp() {
|
|
|
15588
16077
|
"",
|
|
15589
16078
|
" screentest serve start the shared Firefox browser-server daemon",
|
|
15590
16079
|
" (builds the image on first run). One daemon per",
|
|
15591
|
-
" machine \u2014 used by every project",
|
|
16080
|
+
" machine \u2014 used by every project. Test runs start",
|
|
16081
|
+
" it on their own; `serve` just does it up front",
|
|
15592
16082
|
" screentest stop stop the daemon",
|
|
15593
16083
|
" screentest status show whether the daemon is running",
|
|
15594
16084
|
"",
|
|
15595
16085
|
" screentest review regenerate doc.json from cached actuals + open UI",
|
|
15596
16086
|
" (useful when you closed the UI without accepting)",
|
|
15597
16087
|
"",
|
|
16088
|
+
" screentest repair [--dry-run] [--worker-url URL] [--token TOKEN]",
|
|
16089
|
+
" re-upload snapshot.json hashes whose blob is",
|
|
16090
|
+
" missing from the store, using the cached actual",
|
|
16091
|
+
" with the same content hash. Exits 1 if some are",
|
|
16092
|
+
" gone from both \u2014 those need a re-run + accept",
|
|
16093
|
+
"",
|
|
16094
|
+
" screentest gc [--yes] [--snapshot PATH]\u2026",
|
|
16095
|
+
" delete blobs under this project that no snapshot",
|
|
16096
|
+
" file references any more. Reports only until --yes",
|
|
16097
|
+
"",
|
|
15598
16098
|
" screentest init scaffold vitest.screentest.config.ts and",
|
|
15599
16099
|
" tests/example.test.ts into the current project",
|
|
15600
16100
|
"",
|
|
15601
16101
|
"Running screenshot tests:",
|
|
15602
16102
|
" Add to your package.json scripts (or run directly):",
|
|
15603
16103
|
' "screentest": "vitest run --config vitest.screentest.config.ts"',
|
|
15604
|
-
" The vitest globalSetup we ship handles pre-flight (wipe actualDir,
|
|
15605
|
-
" the daemon) and post-flight (build doc.json, auto-launch the review UI",
|
|
16104
|
+
" The vitest globalSetup we ship handles pre-flight (wipe actualDir, start",
|
|
16105
|
+
" the daemon if needed) and post-flight (build doc.json, auto-launch the review UI",
|
|
15606
16106
|
" if there are diffs).",
|
|
15607
16107
|
"",
|
|
15608
16108
|
"Env vars (UI mode):",
|
|
@@ -15657,6 +16157,16 @@ Or add it as a script in package.json:
|
|
|
15657
16157
|
process.exit(await printStatus());
|
|
15658
16158
|
return;
|
|
15659
16159
|
}
|
|
16160
|
+
if (cmd === "repair") {
|
|
16161
|
+
const code = await runRepair(parseRepairArgs(argv.slice(1)));
|
|
16162
|
+
process.exit(code);
|
|
16163
|
+
return;
|
|
16164
|
+
}
|
|
16165
|
+
if (cmd === "gc") {
|
|
16166
|
+
const code = await runGc(parseGcArgs(argv.slice(1)));
|
|
16167
|
+
process.exit(code);
|
|
16168
|
+
return;
|
|
16169
|
+
}
|
|
15660
16170
|
if (cmd === "init") {
|
|
15661
16171
|
const code = await runInit();
|
|
15662
16172
|
process.exit(code);
|