feat(scripts): add release smoke tests for the installer and the desktop app (P1.3d)
scripts/release-smoke.mjs runs on every release target: - installer: installs the server archive with packages/launcher into a temporary directory, requires a clean doctor report (only the PATH warning is allowed), starts run on a free port with a temporary data directory, waits for /health, stops it and uninstalls; - desktop: starts the packaged app (an executable or a macOS .app) with a temporary data directory, waits until its sidecar answers /health on 127.0.0.1:3333, then stops the app and the sidecar and checks that the port is released. The process tree is stopped through the process group on macOS and Linux and with taskkill /T on Windows. The tests run both modes against stand-in payloads.
This commit is contained in:
146
scripts/release-smoke.test.ts
Normal file
146
scripts/release-smoke.test.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* scripts/release-smoke.mjs against stand-in payloads. The installer smoke
|
||||
* test installs, checks, runs and removes a packed archive whose service is a
|
||||
* small HTTP server; the desktop smoke test starts a stand-in .app that starts
|
||||
* a stand-in sidecar, and must stop both.
|
||||
*/
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import net from 'node:net';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
|
||||
const ROOT = path.resolve(import.meta.dirname, '..');
|
||||
const SMOKE = path.join(ROOT, 'scripts', 'release-smoke.mjs');
|
||||
const LAUNCHER = path.join(ROOT, 'packages', 'launcher', 'dist', 'cli.js');
|
||||
const TARGETS: Record<string, string> = {
|
||||
'win32-x64': 'windows-x64',
|
||||
'darwin-arm64': 'macos-arm64',
|
||||
'darwin-x64': 'macos-x64',
|
||||
'linux-x64': 'linux-x64',
|
||||
};
|
||||
const target = TARGETS[`${process.platform}-${process.arch}`];
|
||||
|
||||
// Answers /health the way the sidecar does.
|
||||
const healthServer = (port: string) => `
|
||||
const http = require('node:http');
|
||||
const port = Number(${port});
|
||||
http.createServer((request, response) => {
|
||||
response.setHeader('content-type', 'application/json');
|
||||
response.end(JSON.stringify(request.url === '/health' ? { status: 'ok', mode: 'local', port } : {}));
|
||||
}).listen(port, '127.0.0.1');
|
||||
`;
|
||||
|
||||
const temps: string[] = [];
|
||||
function tempDir(prefix: string): string {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
||||
temps.push(dir);
|
||||
return dir;
|
||||
}
|
||||
afterEach(() => {
|
||||
for (const dir of temps.splice(0)) fs.rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 500 });
|
||||
});
|
||||
|
||||
function write(file: string, content: string, mode?: number): void {
|
||||
fs.mkdirSync(path.dirname(file), { recursive: true });
|
||||
fs.writeFileSync(file, content, { mode });
|
||||
}
|
||||
|
||||
function freePort(): Promise<number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const server = net.createServer();
|
||||
server.once('error', reject);
|
||||
server.listen(0, '127.0.0.1', () => {
|
||||
const address = server.address() as net.AddressInfo;
|
||||
server.close(() => resolve(address.port));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function runSmoke(args: string[]) {
|
||||
return spawnSync(process.execPath, [SMOKE, ...args], { encoding: 'utf8', timeout: 120_000, windowsHide: true });
|
||||
}
|
||||
|
||||
describe('release smoke tests', () => {
|
||||
it.skipIf(!target || !fs.existsSync(LAUNCHER))(
|
||||
'installs, checks, runs and uninstalls a server archive',
|
||||
() => {
|
||||
const work = tempDir('waggle-smoke-archive-');
|
||||
const resources = path.join(work, 'resources');
|
||||
const runtime = path.join(resources, process.platform === 'win32' ? 'node.exe' : 'node');
|
||||
fs.mkdirSync(resources, { recursive: true });
|
||||
// The payload runs on its own Node; this test's Node stands in for it.
|
||||
if (process.platform === 'win32') fs.copyFileSync(process.execPath, runtime);
|
||||
else fs.symlinkSync(process.execPath, runtime);
|
||||
write(path.join(resources, 'service.js'), healthServer('process.env.WAGGLE_PORT'));
|
||||
write(
|
||||
path.join(resources, 'node_modules', 'better-sqlite3', 'index.js'),
|
||||
'module.exports = class { prepare() { return { get: () => ({ 1: 1 }) }; } };\n',
|
||||
);
|
||||
write(path.join(work, 'web', 'index.html'), '<!doctype html>');
|
||||
const out = path.join(work, 'out');
|
||||
const pack = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
path.join(ROOT, 'scripts', 'build-server-archive.mjs'),
|
||||
'--resources', resources, '--web', path.join(work, 'web'), '--out', out,
|
||||
],
|
||||
{ encoding: 'utf8' },
|
||||
);
|
||||
expect(pack.status, pack.stderr).toBe(0);
|
||||
const archive = path.join(out, fs.readdirSync(out).find((name) => name.endsWith('.tar.gz'))!);
|
||||
|
||||
const result = runSmoke(['installer', '--archive', archive, '--timeout', '60']);
|
||||
expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);
|
||||
expect(result.stdout).toContain('The installed server answers /health');
|
||||
expect(result.stdout).toContain('Installer smoke test passed');
|
||||
},
|
||||
150_000,
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === 'win32')(
|
||||
'starts a desktop app bundle and stops it together with its sidecar',
|
||||
async () => {
|
||||
const port = await freePort();
|
||||
const work = tempDir('waggle-smoke-desktop-');
|
||||
const contents = path.join(work, 'Stand-in.app', 'Contents');
|
||||
write(
|
||||
path.join(contents, 'Info.plist'),
|
||||
'<plist><dict><key>CFBundleExecutable</key>\n <string>stand-in</string></dict></plist>\n',
|
||||
);
|
||||
write(path.join(work, 'sidecar.js'), healthServer(String(port)));
|
||||
// The app stays alive and owns a child process that serves /health.
|
||||
write(
|
||||
path.join(work, 'app.js'),
|
||||
`require('node:child_process').spawn(process.execPath, [${JSON.stringify(path.join(work, 'sidecar.js'))}], { stdio: 'inherit' });\n`
|
||||
+ 'setInterval(() => {}, 1000);\n',
|
||||
);
|
||||
write(
|
||||
path.join(contents, 'MacOS', 'stand-in'),
|
||||
`#!/bin/sh\nexec ${JSON.stringify(process.execPath)} ${JSON.stringify(path.join(work, 'app.js'))}\n`,
|
||||
0o755,
|
||||
);
|
||||
|
||||
const result = runSmoke(['desktop', '--app', path.join(work, 'Stand-in.app'), '--port', String(port), '--timeout', '30']);
|
||||
expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);
|
||||
expect(result.stdout).toContain(`answers /health on 127.0.0.1:${port}`);
|
||||
expect(result.stdout).toContain('Desktop smoke test passed');
|
||||
|
||||
const probe = net.connect(port, '127.0.0.1');
|
||||
const connected = await new Promise<boolean>((resolve) => {
|
||||
probe.once('connect', () => resolve(true));
|
||||
probe.once('error', () => resolve(false));
|
||||
});
|
||||
probe.destroy();
|
||||
expect(connected).toBe(false);
|
||||
},
|
||||
90_000,
|
||||
);
|
||||
|
||||
it('rejects a desktop run without an app and unknown options', () => {
|
||||
expect(runSmoke(['desktop']).stderr).toContain('desktop needs --app');
|
||||
expect(runSmoke(['installer', '--fast', '1']).stderr).toContain('Unknown option: --fast');
|
||||
expect(runSmoke(['publish']).status).toBe(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user