'use strict';
const { describe, it } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const crypto = require('node:crypto');
const { spawnSync } = require('node:child_process');
const root = path.join(__dirname, '..');
const htmlPath = path.join(root, 'build/index.html');
const htaccessPath = path.join(root, 'server/.htaccess');
function sha12(file)
{
const buf = fs.readFileSync(path.join(root, file));
return crypto.createHash('sha256').update(buf).digest('hex').slice(0, 12);
}
function attr(html, attr, file)
{
const re = new RegExp(attr + '="' + file.replace('.', '\\.') + '\\?v=([0-9a-f]{12})"');
const m = html.match(re);
assert.ok(m, attr + ' ' + file + ' is missing a 12-char hash query in index.html');
return m[1];
}
describe('asset cache busting (t1256)', () =>
{
it('stamps content hashes on JS and CSS in index.html', () =>
{
const html = fs.readFileSync(htmlPath, 'utf8');
assert.equal(attr(html, 'href', 'style.css'), sha12('build/style.css'));
assert.equal(attr(html, 'src', 'framework.js'), sha12('build/framework.js'));
assert.equal(attr(html, 'src', 'game.js'), sha12('build/game.js'));
assert.doesNotMatch(html, /(?:href|src)="(?:style\.css|framework\.js|game\.js)"/);
});
it('is idempotent and follows a catalog or JS change', () =>
{
const script = fs.readFileSync(path.join(root, 'src/build.sh'), 'utf8');
assert.match(script, /asset_hash/);
assert.match(script, /game\.js\?v=/);
assert.match(script, /i18n\.json/);
// Restamp in a temp copy so we do not race other tests touching build/.
const tmp = fs.mkdtempSync(path.join(require('node:os').tmpdir(), 'meteors-stamp-'));
const index = path.join(tmp, 'index.html');
fs.writeFileSync(index, '\n'
+ '\n'
+ '\n');
const envScript = `sha256sum() { sha256sum "$@"; }
asset_hash() { sha256sum "$1" | cut -c1-12; }
INDEX="${index}"
GAME_HASH="aaaaaaaaaaaa"
CSS_HASH="bbbbbbbbbbbb"
FW_HASH="cccccccccccc"
sed -i -E "s/href=\\"style\\.css(\\?v=[^\\"]*)?\\"/href=\\"style.css?v=\${CSS_HASH}\\"/" "$INDEX"
sed -i -E "s/src=\\"framework\\.js(\\?v=[^\\"]*)?\\"/src=\\"framework.js?v=\${FW_HASH}\\"/" "$INDEX"
sed -i -E "s/src=\\"game\\.js(\\?v=[^\\"]*)?\\"/src=\\"game.js?v=\${GAME_HASH}\\"/" "$INDEX"
`;
// Drive the same sed the build script uses, twice.
const run = (game, css, fw) =>
{
const r = spawnSync('bash', ['-lc', `
INDEX=${JSON.stringify(index)}
sed -i -E "s/href=\\"style\\.css(\\?v=[^\\"]*)?\\"/href=\\"style.css?v=${css}\\"/" "$INDEX"
sed -i -E "s/src=\\"framework\\.js(\\?v=[^\\"]*)?\\"/src=\\"framework.js?v=${fw}\\"/" "$INDEX"
sed -i -E "s/src=\\"game\\.js(\\?v=[^\\"]*)?\\"/src=\\"game.js?v=${game}\\"/" "$INDEX"
`], { encoding: 'utf8' });
assert.equal(r.status, 0, r.stderr);
return fs.readFileSync(index, 'utf8');
};
const first = run('aaaaaaaaaaaa', 'bbbbbbbbbbbb', 'cccccccccccc');
assert.match(first, /style\.css\?v=bbbbbbbbbbbb/);
assert.match(first, /framework\.js\?v=cccccccccccc/);
assert.match(first, /game\.js\?v=aaaaaaaaaaaa/);
const second = run('dddddddddddd', 'bbbbbbbbbbbb', 'cccccccccccc');
assert.match(second, /game\.js\?v=dddddddddddd/);
assert.doesNotMatch(second, /game\.js\?v=aaaaaaaaaaaa/);
fs.rmSync(tmp, { recursive: true, force: true });
});
it('sets Cache-Control on HTML (no-cache) and versioned static assets', () =>
{
const conf = fs.readFileSync(htaccessPath, 'utf8');
assert.match(conf, /FilesMatch "\\?\.\(html\)\$"/);
assert.match(conf, /Cache-Control "no-cache"/);
assert.match(conf, /FilesMatch "\\?\.\(js\|css\|json\)\$"/);
assert.match(conf, /Cache-Control "public, max-age=31536000, immutable"/);
const router = fs.readFileSync(path.join(root, 'server/router.php'), 'utf8');
assert.match(router, /Cache-Control: no-cache/);
});
});