wishforge-ide/scripts/local-web-build.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

99 lines
3.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, mkdir, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises';
import { createServer } from 'node:net';
import os from 'node:os';
import path from 'node:path';
import { buildWithRollback, portOccupied } from './local-web-build.mjs';
async function fixture() {
const root = await mkdtemp(path.join(os.tmpdir(), 'wishforge-web-build-'));
const current = path.join(root, 'apps/web/.next');
await mkdir(path.join(current, 'server'), { recursive: true });
await writeFile(path.join(current, 'BUILD_ID'), 'previous-build');
return { root, current, cleanup: () => rm(root, { recursive: true, force: true }) };
}
const offline = async () => undefined;
test('an occupied port is detected without relying on HTTP auth or page status', async () => {
const server = createServer(socket => socket.end());
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
try {
assert.equal(await portOccupied('127.0.0.1', server.address().port), true);
} finally {
await new Promise(resolve => server.close(resolve));
}
});
test('refuses before touching the active build when offline checks fail', async () => {
const f = await fixture();
try {
let called = false;
await assert.rejects(
buildWithRollback(
f.root,
async () => {
called = true;
},
{
offline: async () => {
throw new Error('still listening');
},
},
),
/still listening/,
);
assert.equal(called, false);
assert.equal(await readFile(path.join(f.current, 'BUILD_ID'), 'utf8'), 'previous-build');
} finally {
await f.cleanup();
}
});
test('a failed build restores the previous artifact and retains failure output in a private directory', async () => {
const f = await fixture();
try {
await assert.rejects(
buildWithRollback(
f.root,
async () => {
await mkdir(f.current);
await writeFile(path.join(f.current, 'partial'), 'new incomplete output');
throw new Error('compiler failed');
},
{ offline },
),
/compiler failed/,
);
assert.equal(await readFile(path.join(f.current, 'BUILD_ID'), 'utf8'), 'previous-build');
const archive = path.join(f.root, '.wishforge/local-poc/web-builds');
assert.equal((await stat(archive)).mode & 0o777, 0o700);
assert.equal((await readdir(archive)).filter(name => name.startsWith('failed-')).length, 1);
await assert.rejects(stat(path.join(f.root, '.wishforge/local-poc/web-build.lock')), { code: 'ENOENT' });
} finally {
await f.cleanup();
}
});
test('a successful build preserves the previous artifact and refuses concurrent builds', async () => {
const f = await fixture();
try {
const result = await buildWithRollback(
f.root,
async () => {
await assert.rejects(
buildWithRollback(f.root, async () => {}, { offline }),
/Another protected web build/,
);
await mkdir(path.join(f.current, 'server'), { recursive: true });
await writeFile(path.join(f.current, 'BUILD_ID'), 'new-build');
},
{ offline },
);
assert.equal(result.buildId, 'new-build');
assert.equal(await readFile(path.join(result.previousBuild, 'BUILD_ID'), 'utf8'), 'previous-build');
} finally {
await f.cleanup();
}
});