39 lines
1.7 KiB
JavaScript
39 lines
1.7 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs/promises';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import test from 'node:test';
|
|
|
|
import { classifyPublicUrl, inspectRuntimePublicUrls } from './public-url.mjs';
|
|
|
|
test('public URL classifier rejects loopback, private, and unknown TKMind hosts', () => {
|
|
assert.equal(classifyPublicUrl('http://127.0.0.1:8081/file'), 'loopback_or_private_host');
|
|
assert.equal(classifyPublicUrl('http://192.168.1.4/file'), 'loopback_or_private_host');
|
|
assert.equal(classifyPublicUrl('https://typo.tkmind.cn/file'), 'unknown_tkmind_public_host');
|
|
assert.equal(classifyPublicUrl('https://m.tkmind.cn/file'), null);
|
|
assert.equal(classifyPublicUrl('https://plaza.tkmind.cn/file'), null);
|
|
});
|
|
|
|
test('runtime public URL inspection checks rendered documents and canonical production entry', async () => {
|
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'memind-public-url-'));
|
|
try {
|
|
await fs.mkdir(path.join(root, 'dist'), { recursive: true });
|
|
await fs.mkdir(path.join(root, 'scripts'), { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(root, 'dist', 'index.html'),
|
|
'<a href="https://m.tkmind.cn/ok">ok</a><img src="http://127.0.0.1:8081/bad.png">',
|
|
);
|
|
await fs.writeFile(
|
|
path.join(root, 'scripts', 'run-memind-portal-prod.sh'),
|
|
'export H5_PUBLIC_BASE_URL="${H5_PUBLIC_BASE_URL:-https://m.tkmind.cn}"\n',
|
|
);
|
|
assert.deepEqual(await inspectRuntimePublicUrls(root), [{
|
|
file: 'dist/index.html',
|
|
url: 'http://127.0.0.1:8081/bad.png',
|
|
reason: 'loopback_or_private_host',
|
|
}]);
|
|
} finally {
|
|
await fs.rm(root, { recursive: true, force: true });
|
|
}
|
|
});
|