Files
memind/mindspace-published-script-localize.test.mjs
T
john 1ec971515b
Memind CI / Test, build, and release guards (push) Failing after 3m57s
feat(publish): auto-localize Leaflet CDN to platform assets
Ship Leaflet 1.9.4 under public/assets/leaflet and rewrite common CDN script/stylesheet URLs during publish and public HTML delivery, matching the existing Chart.js guard pattern.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-25 16:05:50 +08:00

79 lines
3.7 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
findExternalScriptSources,
isExternalScriptSrc,
rewriteKnownCdnAssetSources,
rewriteKnownCdnScriptSources,
rewriteKnownCdnStylesheetSources,
} from './mindspace-published-script-localize.mjs';
test('isExternalScriptSrc detects http and protocol-relative urls', () => {
assert.equal(isExternalScriptSrc('https://cdn.jsdelivr.net/npm/chart.js'), true);
assert.equal(isExternalScriptSrc('//cdn.jsdelivr.net/npm/chart.js'), true);
assert.equal(isExternalScriptSrc('/assets/page-data-client.js'), false);
assert.equal(isExternalScriptSrc('assets/chart.umd.min.js'), false);
});
test('rewriteKnownCdnScriptSources rewrites chart.js cdn urls to platform asset', () => {
const source = `<!doctype html><html><body>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.7/dist/chart.umd.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.umd.min.js"></script>
<script src="/assets/page-data-client.js"></script>
</body></html>`;
const result = rewriteKnownCdnScriptSources(source);
assert.equal(result.rewrittenCount, 2);
assert.match(result.html, /src="\/assets\/chart\.umd\.min\.js"/g);
assert.doesNotMatch(result.html, /cdn\.jsdelivr\.net/);
assert.doesNotMatch(result.html, /cdnjs\.cloudflare\.com/);
assert.match(result.html, /src="\/assets\/page-data-client\.js"/);
});
test('findExternalScriptSources lists unresolved external scripts only', () => {
const source = `<html><body>
<script src="https://cdn.example.com/app.js"></script>
<script src="assets/local.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js/dist/chart.umd.min.js"></script>
</body></html>`;
const rewritten = rewriteKnownCdnScriptSources(source).html;
assert.deepEqual(findExternalScriptSources(rewritten), ['https://cdn.example.com/app.js']);
});
test('rewriteKnownCdnStylesheetSources rewrites leaflet css cdn urls to platform asset', () => {
const source = `<!doctype html><html><head>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css">
<link href="//cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.css" rel="stylesheet">
<link rel="stylesheet" href="/assets/local.css">
</head><body></body></html>`;
const result = rewriteKnownCdnStylesheetSources(source);
assert.equal(result.rewrittenCount, 2);
assert.match(result.html, /href="\/assets\/leaflet\/leaflet\.css"/g);
assert.doesNotMatch(result.html, /unpkg\.com\/leaflet/);
assert.doesNotMatch(result.html, /cdn\.jsdelivr\.net\/npm\/leaflet/);
assert.match(result.html, /href="\/assets\/local\.css"/);
});
test('rewriteKnownCdnScriptSources rewrites leaflet js cdn urls to platform asset', () => {
const source = `<!doctype html><html><body>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script src="//cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.js"></script>
</body></html>`;
const result = rewriteKnownCdnScriptSources(source);
assert.equal(result.rewrittenCount, 2);
assert.match(result.html, /src="\/assets\/leaflet\/leaflet\.js"/g);
assert.doesNotMatch(result.html, /unpkg\.com\/leaflet/);
});
test('rewriteKnownCdnAssetSources rewrites leaflet css and js together', () => {
const source = `<!doctype html><html><head>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css">
</head><body>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
</body></html>`;
const result = rewriteKnownCdnAssetSources(source);
assert.equal(result.rewrittenCount, 2);
assert.match(result.html, /href="\/assets\/leaflet\/leaflet\.css"/);
assert.match(result.html, /src="\/assets\/leaflet\/leaflet\.js"/);
assert.deepEqual(findExternalScriptSources(result.html), []);
});