wishforge-ide/scripts/local-web-build.test.mjs
eust-w 8fc4ab8e47 🐛 Keep only the web builds a rollback can use
Every protected web build archived the build it replaced and never
removed one, at 50 to 200 MB each: the local POC had collected 284 MB
in three backups. A successful build now keeps the build it replaced,
which the receipt names as the rollback target, one earlier build and
the newest failed build; the receipt lists what it removed. Removal is
best effort: a directory it cannot remove waits for the next build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 05:06:46 +08:00

132 lines
4.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, mkdir, readFile, readdir, rm, stat, utimes, 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();
}
});
test('a successful build keeps the build it replaced, one earlier build and the newest failed build', async () => {
const f = await fixture();
try {
const archive = path.join(f.root, '.wishforge/local-poc/web-builds');
const day = 24 * 60 * 60;
const now = Date.now() / 1000;
// Archived builds of earlier days, oldest first.
const archived = ['previous-a', 'previous-b', 'previous-c', 'failed-a', 'failed-b'];
for (const [index, name] of archived.entries()) {
await mkdir(path.join(archive, name), { recursive: true });
await utimes(path.join(archive, name), now - (10 - index) * day, now - (10 - index) * day);
}
const result = await buildWithRollback(
f.root,
async () => {
await mkdir(path.join(f.current, 'server'), { recursive: true });
await writeFile(path.join(f.current, 'BUILD_ID'), 'new-build');
},
{ offline },
);
const kept = (await readdir(archive)).sort();
assert.deepEqual(kept, ['failed-b', 'previous-c', path.basename(result.previousBuild)].sort());
assert.deepEqual(result.prunedBuilds.map(location => path.basename(location)).sort(), [
'failed-a',
'previous-a',
'previous-b',
]);
assert.equal(await readFile(path.join(result.previousBuild, 'BUILD_ID'), 'utf8'), 'previous-build');
} finally {
await f.cleanup();
}
});