Files
dashcaddy/status/tests/build-determinism.test.js
T
Hermes 4d97a11978
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
fix(build): frontend build determinism across line endings (DC-119) [glm-grade=A]
The deploy host showed phantom dist drift on every git pull: committed
dist bundles could not be reproduced on DNS2. Root cause (verified with
esbuild 0.25.12 probes): the production transform uses sourcemap:'both',
whose inline map base64-embeds RAW source bytes as sourcesContent — a CRLF
working copy (Windows dev, core.autocrlf=true) vs an LF checkout produces
different dist bytes and a different sw.js cache tag.

- build.js: normalizeSource (\r\n -> \n) on every source read (bundle
  inputs + sw.js read); exported + require.main guard so tests can import
  it without triggering a build
- .gitattributes: * text=auto eol=lf (git-layer kill of the CRLF vector)
  + binary exclusions; 9 CRLF-in-index asset files renormalized
- tests/build-determinism.test.js: 3-case pin (byte-identity after
  normalization, divergence pre-normalization, CR-strip contract) importing
  the ACTUAL normalizeSource from build.js
- package.json: declare jsdom devDependency — 2 committed test files
  require('jsdom') but it was never declared, so fresh-checkout
  npm test failed (it only passed where a stray ancestor node_modules
  happened to contain it)
- dist/features.js + sw.js: canonical deterministic rebuild
  (cache tag 3354f5fd96 -> d39ab69dd4)

Verified: status 54/54, API 2858/2858 (128 suites); CRLF-sim tree and LF
tree of same HEAD produce byte-identical dist artifacts (sha256-equal).
Judge: glm-5.3 cold round 1 = A, round 2 (post-fold) = A clean, 0 blocking.
2026-08-23 16:34:49 -07:00

128 lines
4.6 KiB
JavaScript

'use strict';
/**
* DC-119: frontend build determinism across line endings.
*
* The frontend build (status/build.js) concatenates raw source files and
* minifies them with esbuild using sourcemap:'both' — the inline map
* base64-embeds the raw source bytes (sourcesContent), CRs included. A CRLF
* working copy (Windows dev tree, core.autocrlf=true) vs an LF working copy
* (DNS2 Linux checkout) of the SAME commit therefore produces different
* dist bytes and a different sw.js cache tag — so the committed dist could
* never be reproduced on the deploy host, showing up as permanent phantom
* drift on `git pull` in /opt/dashcaddy (the recurring "pre-pull drift"
* stashes).
*
* build.js now normalizes every source read to LF (\r\n -> \n) before
* concatenation. This test pins that behavior at the transform level: the
* SAME input, CRLF vs LF, must produce byte-identical minified output, and
* the normalization regex used by build.js must strip all CR bytes.
*
* It deliberately does NOT shell out to `node build.js` (slow, writes
* dist/) — it exercises the exact transform + normalization logic inline.
*/
const test = require('node:test');
const assert = require('node:assert');
// Same devDependency esbuild the build itself uses.
const esbuild = require('esbuild');
// DC-119: import THE ACTUAL normalization from build.js — not a local
// re-implementation — so this test fails if the build's regex ever changes.
const { normalizeSource: normalize } = require('../build.js');
// Representative source: top-level names, nested scopes, strings with
// escapes, template literals, regex literals, comments — the constructs
// whose minified renames shifted pre-fix.
const SAMPLE = `// feature module
const logoCustomization = {
position: 'left',
cacheTag: 'dashcaddy-shell-abc123',
};
function applyPosition(position, elem) {
const normalized = position || logoCustomization.position;
elem.setAttribute('data-logo-pos', normalized);
return normalized.replace(/_/g, ' ');
}
const summarize = (items) => {
let total = 0;
for (const item of items) {
total += item.count ?? 0;
}
return \`total: \${total} (\${items.length} items)\`;
};
async function loadConfig(url) {
try {
const res = await fetch(url);
if (!res.ok) throw new Error('HTTP ' + res.status);
return await res.json();
} catch (err) {
console.warn('load failed:', err.message);
return null;
}
}
module.exports = { applyPosition, summarize, loadConfig };
`;
// Production transform options — MUST mirror build.js. The CRLF divergence
// lives in the INLINE SOURCEMAP: sourcemap:'both' embeds the raw source as
// base64 sourcesContent, so CRLF bytes survive into dist and shift both the
// bundle bytes and the sw.js content-hash cache tag.
async function minify(source) {
const { code } = await esbuild.transform(source, {
minify: true,
target: 'es2020',
sourcemap: 'both',
});
return code;
}
test('DC-119: CRLF and LF inputs produce byte-identical minified output', async () => {
const lf = SAMPLE;
const crlf = SAMPLE.replace(/\n/g, '\r\n');
// Sanity: the two raw inputs really do differ.
assert.notEqual(lf, crlf, 'fixture setup: CRLF variant must differ from LF');
const outLf = await minify(normalize(lf));
const outCrlf = await minify(normalize(crlf));
assert.strictEqual(
outCrlf,
outLf,
'minified output must be byte-identical after CRLF->LF normalization'
);
});
test('DC-119: without normalization, CRLF vs LF differ (documents the bug)', async () => {
const lf = SAMPLE;
const crlf = SAMPLE.replace(/\n/g, '\r\n');
const outLf = await minify(lf);
const outCrlf = await minify(crlf);
// Documents WHY the normalization exists: the inline sourcemap's
// sourcesContent base64-encodes the raw bytes, CRs included. If esbuild
// ever normalizes sourcesContent itself, this may flip to equal — then
// the normalization is redundant but harmless; update the DC-119 comment
// in build.js when that happens.
assert.notEqual(
outCrlf,
outLf,
'expected CRLF/LF divergence pre-normalization (inline sourcemap sourcesContent); if equal, esbuild changed behavior — update the DC-119 comment in build.js'
);
});
test('DC-119: normalization strips every CR from CRLF input and leaves LF untouched', () => {
const crlf = 'line1\r\nline2\r\n';
const lf = 'line1\nline2\n';
// Lone \r (old-Mac style) is NOT produced by git autocrlf and is NOT
// claimed to be handled — assert only the CRLF contract.
assert.strictEqual(normalize(crlf), 'line1\nline2\n');
assert.strictEqual(normalize(lf), 'line1\nline2\n');
assert.ok(!normalize(crlf).includes('\r'), 'no CR may survive normalization');
});