mirror of
https://github.com/Matysh/houseplan-card
synced 2026-09-29 03:09:36 +00:00
The sidebar page could serve a previous card for hours. It imported the card through the stable facade, `./houseplan-card.js` — a relative specifier, and relative resolution does not inherit a query. A dashboard reaches the same file as `houseplan-card.js?v=1.74.0`, so an upgrade changes its URL and the browser must refetch. The panel always asked for the same address, and entries are served with no Cache-Control at all — only ETag and Last-Modified — so the browser applies heuristic freshness and may answer from cache without asking. A stale 1164-byte loader names a stale chunk, chunks are immutable for a year, and the panel then ran a previous card against the current backend without a single error. The version banner was telling the truth; reloading could not help, because the address never changed. Rollup already emits the right edge: the panel's side-effect import points straight at the shared implementation. The rewrite in entryFallbackPlugin replaced it with the facade for a fallback that the hashed name gives anyway — and better: a chunk the manifest no longer serves now raises the panel's own "House Plan was updated" notice instead of silently working on old code. Two #486 assertions change meaning and are rewritten, not adjusted: the panel no longer routes through the facade, and its initial graph no longer contains it. The invariant they defended — the panel reuses the exact card graph, never a second copy — is now stated over the implementation, and a new test pins that no built entry reaches the card by an address without a version. Issue: #535 User-Visible: yes
114 lines
4.3 KiB
JavaScript
114 lines
4.3 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { createHash } from 'node:crypto';
|
|
import {
|
|
existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync,
|
|
} from 'node:fs';
|
|
import { dirname, join } from 'node:path';
|
|
import { tmpdir } from 'node:os';
|
|
import test from 'node:test';
|
|
|
|
const sha256 = (contents) => createHash('sha256').update(contents).digest('hex');
|
|
|
|
const writeCurrentBundle = (root) => {
|
|
const contents = new Map([
|
|
['houseplan-card.js', 'card facade'],
|
|
['houseplan-panel.js', 'panel shell'],
|
|
['houseplan-assets/card-HASH.js', 'shared card implementation'],
|
|
]);
|
|
for (const [name, value] of contents) {
|
|
const path = join(root, 'dist', name);
|
|
mkdirSync(dirname(path), { recursive: true });
|
|
writeFileSync(path, value);
|
|
}
|
|
const files = [...contents].map(([path, value]) => ({
|
|
path,
|
|
sha256: sha256(value),
|
|
rawBytes: Buffer.byteLength(value),
|
|
gzipBytes: path === 'houseplan-card.js' ? 5 : path === 'houseplan-panel.js' ? 3 : 7,
|
|
isEntry: path === 'houseplan-card.js' || path === 'houseplan-panel.js',
|
|
imports: path === 'houseplan-panel.js'
|
|
? ['houseplan-assets/card-HASH.js']
|
|
: path === 'houseplan-card.js' ? ['houseplan-assets/card-HASH.js'] : [],
|
|
dynamicImports: [],
|
|
}));
|
|
const manifest = {
|
|
schema: 1,
|
|
fingerprint: 'f'.repeat(64),
|
|
entry: 'houseplan-card.js',
|
|
panelEntry: 'houseplan-panel.js',
|
|
initialViewFiles: ['houseplan-assets/card-HASH.js', 'houseplan-card.js'],
|
|
initialViewGzipBytes: 12,
|
|
initialPanelFiles: [
|
|
'houseplan-assets/card-HASH.js', 'houseplan-panel.js',
|
|
],
|
|
initialPanelGzipBytes: 10,
|
|
initialPanelOnlyFiles: ['houseplan-panel.js'],
|
|
initialPanelOnlyGzipBytes: 3,
|
|
files,
|
|
};
|
|
writeFileSync(join(root, 'dist/houseplan-assets.json'), `${JSON.stringify(manifest)}\n`);
|
|
return manifest;
|
|
};
|
|
|
|
const writeLegacyTarget = (root, target) => {
|
|
const targetRoot = join(root, target);
|
|
mkdirSync(join(targetRoot, 'houseplan-assets'), { recursive: true });
|
|
writeFileSync(join(targetRoot, 'houseplan-card.js'), 'old card');
|
|
writeFileSync(join(targetRoot, 'houseplan-legacy.js'), 'old root entry');
|
|
writeFileSync(join(targetRoot, 'houseplan-assets/old-HASH.js'), 'old chunk');
|
|
writeFileSync(join(targetRoot, 'houseplan-assets.json'), JSON.stringify({
|
|
schema: 1,
|
|
fingerprint: 'legacy',
|
|
entry: 'houseplan-card.js',
|
|
files: [
|
|
{ path: 'houseplan-card.js' },
|
|
{ path: 'houseplan-legacy.js' },
|
|
{ path: 'houseplan-assets/old-HASH.js' },
|
|
],
|
|
}));
|
|
};
|
|
|
|
test('#486 bundle sync materializes both entries and removes the legacy inventory', () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'houseplan-bundle-sync-'));
|
|
try {
|
|
mkdirSync(join(root, 'scripts'), { recursive: true });
|
|
for (const name of ['bundle-sync.mjs', 'bundle-tree.mjs']) {
|
|
writeFileSync(
|
|
join(root, 'scripts', name),
|
|
readFileSync(new URL(`../scripts/${name}`, import.meta.url)),
|
|
);
|
|
}
|
|
const manifest = writeCurrentBundle(root);
|
|
for (const target of [
|
|
'custom_components/houseplan/frontend', 'demo/srv/assets',
|
|
]) writeLegacyTarget(root, target);
|
|
|
|
const run = spawnSync(process.execPath, ['scripts/bundle-sync.mjs'], {
|
|
cwd: root, encoding: 'utf8',
|
|
});
|
|
assert.equal(run.status, 0, `${run.stdout}\n${run.stderr}`);
|
|
for (const target of [
|
|
'custom_components/houseplan/frontend', 'demo/srv/assets',
|
|
]) {
|
|
assert.equal(readFileSync(join(root, target, 'houseplan-card.js'), 'utf8'), 'card facade');
|
|
assert.equal(readFileSync(join(root, target, 'houseplan-panel.js'), 'utf8'), 'panel shell');
|
|
assert.deepEqual(
|
|
JSON.parse(readFileSync(join(root, target, 'houseplan-assets.json'), 'utf8')),
|
|
manifest,
|
|
);
|
|
assert.equal(existsSync(join(root, target, 'houseplan-legacy.js')), false);
|
|
assert.equal(existsSync(join(root, target, 'houseplan-assets/old-HASH.js')), false);
|
|
}
|
|
|
|
writeFileSync(join(root, 'dist/houseplan-orphan.js'), 'unlisted entry');
|
|
const orphan = spawnSync(process.execPath, ['scripts/bundle-sync.mjs'], {
|
|
cwd: root, encoding: 'utf8',
|
|
});
|
|
assert.notEqual(orphan.status, 0);
|
|
assert.match(`${orphan.stdout}${orphan.stderr}`, /orphan bundle asset: houseplan-orphan\.js/);
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true });
|
|
}
|
|
});
|