wishforge-ide/scripts/configure-local-network.test.mjs
eust-w 6fc7eaf00c ✨ Improve local WishForge reliability and LAN access
Detect actual service and Runner readiness, restore LAN access after network changes, protect production web builds with rollback, honor cancellation during executor startup, and show truthful cached-data and credential states.
2026-10-25 03:29:51 +08:00

68 lines
3.2 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, writeFile, readFile, stat, rm, symlink } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { configureNetwork, networkPlan } from './configure-local-network.mjs';
const interfaces = { en0: [{ internal: false, family: 'IPv4', address: '192.168.6.207' }] };
const source = [
'# Private config stays private',
'WF_DEPLOYMENT_MODE=development',
'WF_LOCAL_ACCESS=true',
'WF_WEB_PORT=3000',
'WF_LOCAL_ALLOWED_ORIGINS=http://localhost:3000,http://127.0.0.1:3000,http://172.20.10.5:3000,https://trusted.example',
'OTHER_TEST_SECRET="fixture-kept-verbatim"',
'',
].join('\n');
test('network change replaces only prior same-port LAN origins and preserves unrelated configuration', () => {
const plan = networkPlan(source, '192.168.6.207', interfaces);
assert.deepEqual(plan.removed, ['http://172.20.10.5:3000']);
assert.deepEqual(plan.origins, [
'http://localhost:3000',
'http://127.0.0.1:3000',
'https://trusted.example',
'http://192.168.6.207:3000',
]);
assert.match(plan.source, /OTHER_TEST_SECRET="fixture-kept-verbatim"/);
assert.equal(networkPlan(plan.source, '192.168.6.207', interfaces).changed, false);
});
test('rejects unassigned or public addresses, disabled authentication, and duplicate/malformed origins', () => {
for (const address of ['192.168.6.208', '8.8.8.8', '0.0.0.0', '192.168.6.207:3000'])
assert.throws(() => networkPlan(source, address, interfaces));
for (const invalid of [
source.replace('WF_LOCAL_ACCESS=true', 'WF_LOCAL_ACCESS=false'),
source.replace('development', 'production'),
`${source}WF_LOCAL_ALLOWED_ORIGINS=http://localhost:3000\n`,
source.replace('https://trusted.example', 'https://user:password@trusted.example'),
])
assert.throws(() => networkPlan(invalid, '192.168.6.207', interfaces));
});
test('preview does not write; apply backs up privately, preserves credentials, and is idempotent', async () => {
const root = await mkdtemp(path.join(tmpdir(), 'wishforge-network-'));
try {
const file = path.join(root, '.env.local');
await writeFile(file, source, { mode: 0o600 });
const options = { root, address: '192.168.6.207', interfaces };
const preview = await configureNetwork(options);
assert.equal(preview.applied, false);
assert.equal(await readFile(file, 'utf8'), source);
assert.equal(JSON.stringify(preview).includes('fixture-kept-verbatim'), false);
const applied = await configureNetwork({ ...options, apply: true });
assert.equal(applied.applied, true);
assert.equal(await readFile(applied.backup, 'utf8'), source);
assert.equal((await stat(applied.backup)).mode & 0o777, 0o600);
assert.equal((await stat(file)).mode & 0o777, 0o600);
assert.equal(await readFile(file, 'utf8'), networkPlan(source, options.address, interfaces).source);
assert.equal((await configureNetwork({ ...options, apply: true })).applied, false);
await rm(file);
await symlink(applied.backup, file);
await assert.rejects(configureNetwork({ ...options, apply: true }));
} finally {
await rm(root, { recursive: true, force: true });
}
});