feat(launcher): install the server from a prebuilt archive (P1.3c)
The launcher is now a self-contained installer with install, run, update, doctor and uninstall. Install downloads waggle-server-<version>-<target> .tar.gz, checks its SHA-256, unpacks it into a per-user program directory and writes a waggle-server command that runs the tool on the archive's own Node. Nothing is built on the user's machine and the data directory stays WAGGLE_DATA_DIR or ~/.waggle. scripts/build-server-archive.mjs packs the staged desktop sidecar payload with the built web UI and a manifest. The download location waits for the repository decision; until then install takes --from or WAGGLE_RELEASE_BASE_URL (ADR-0009). Removes install.sh, scripts/waggle-server.sh and their CI job, which the installer replaces, and the bin name clash with @waggle/cli.
This commit is contained in:
@@ -1,858 +0,0 @@
|
||||
/**
|
||||
* 9D-3: npx waggle CLI Launcher — tests.
|
||||
*
|
||||
* Tests argument parsing, version checking, launcher configuration, and the
|
||||
* packed/installed CLI startup path a user gets from npm.
|
||||
*/
|
||||
import { spawn, spawnSync } from 'node:child_process';
|
||||
import type { ChildProcessWithoutNullStreams } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
formatStartupFailure,
|
||||
formatStartupSuccess,
|
||||
parseArgs,
|
||||
} from '../src/cli-core.js';
|
||||
|
||||
const ROOT = path.resolve(import.meta.dirname, '..', '..', '..');
|
||||
const LAUNCHER_DIR = path.join(ROOT, 'packages', 'launcher');
|
||||
|
||||
interface CommandResult {
|
||||
status: number | null;
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
error?: Error;
|
||||
}
|
||||
|
||||
function bin(name: string): string {
|
||||
return process.platform === 'win32' ? `${name}.cmd` : name;
|
||||
}
|
||||
|
||||
function makeHome(): string {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-launcher-cli-'));
|
||||
}
|
||||
|
||||
function run(
|
||||
command: string,
|
||||
args: string[],
|
||||
home: string,
|
||||
extraEnv: NodeJS.ProcessEnv = {},
|
||||
): Promise<CommandResult> {
|
||||
return runInCwd(command, args, ROOT, home, extraEnv);
|
||||
}
|
||||
|
||||
function runInCwd(
|
||||
command: string,
|
||||
args: string[],
|
||||
cwd: string,
|
||||
home: string,
|
||||
extraEnv: NodeJS.ProcessEnv = {},
|
||||
): Promise<CommandResult> {
|
||||
return new Promise((resolve) => {
|
||||
const child = spawn(command, args, {
|
||||
cwd,
|
||||
env: {
|
||||
...process.env,
|
||||
HOME: home,
|
||||
USERPROFILE: home,
|
||||
...extraEnv,
|
||||
},
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
shell: process.platform === 'win32' && command.endsWith('.cmd'),
|
||||
});
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
child.stdout.setEncoding('utf8');
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stdout.on('data', (chunk) => { stdout += chunk; });
|
||||
child.stderr.on('data', (chunk) => { stderr += chunk; });
|
||||
child.once('error', (error) => resolve({ status: null, stdout, stderr, error }));
|
||||
child.once('close', (status) => resolve({ status, stdout, stderr }));
|
||||
});
|
||||
}
|
||||
|
||||
function spawnInCwd(
|
||||
command: string,
|
||||
args: string[],
|
||||
cwd: string,
|
||||
home: string,
|
||||
extraEnv: NodeJS.ProcessEnv = {},
|
||||
): ChildProcessWithoutNullStreams {
|
||||
return spawn(command, args, {
|
||||
cwd,
|
||||
env: {
|
||||
...process.env,
|
||||
HOME: home,
|
||||
USERPROFILE: home,
|
||||
...extraEnv,
|
||||
},
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
shell: process.platform === 'win32' && command.endsWith('.cmd'),
|
||||
// POSIX: a process group of its own, so stopProcess can stop npx together
|
||||
// with the waggle process it starts.
|
||||
detached: process.platform !== 'win32',
|
||||
});
|
||||
}
|
||||
|
||||
async function occupyPort(): Promise<{
|
||||
server: http.Server;
|
||||
port: number;
|
||||
requestCount: () => number;
|
||||
bearerRequestCount: () => number;
|
||||
}> {
|
||||
let requests = 0;
|
||||
let bearerRequests = 0;
|
||||
const server = http.createServer((request, response) => {
|
||||
requests += 1;
|
||||
if (request.headers.authorization?.startsWith('Bearer ')) bearerRequests += 1;
|
||||
response.writeHead(503, { 'content-type': 'application/json' });
|
||||
response.end('{}');
|
||||
});
|
||||
const port = await new Promise<number>((resolve) => {
|
||||
server.listen(0, '127.0.0.1', () => {
|
||||
const addr = server.address();
|
||||
resolve(typeof addr === 'object' && addr ? addr.port : 0);
|
||||
});
|
||||
});
|
||||
return {
|
||||
server,
|
||||
port,
|
||||
requestCount: () => requests,
|
||||
bearerRequestCount: () => bearerRequests,
|
||||
};
|
||||
}
|
||||
|
||||
async function freePort(): Promise<number> {
|
||||
const { server, port } = await occupyPort();
|
||||
await new Promise<void>((resolve) => server.close(() => resolve()));
|
||||
return port;
|
||||
}
|
||||
|
||||
async function extractTarball(file: string, cwd: string): Promise<void> {
|
||||
const tar = await import('tar');
|
||||
await tar.x({ file, cwd });
|
||||
}
|
||||
|
||||
async function waitFor(
|
||||
predicate: () => boolean | Promise<boolean>,
|
||||
timeoutMs: number,
|
||||
): Promise<void> {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
if (await predicate()) return;
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
}
|
||||
throw new Error(`Timed out after ${timeoutMs}ms`);
|
||||
}
|
||||
|
||||
async function waitForReadyFile(
|
||||
child: ChildProcessWithoutNullStreams,
|
||||
readyFile: string,
|
||||
output: () => { stdout: string; stderr: string },
|
||||
timeoutMs: number,
|
||||
): Promise<void> {
|
||||
try {
|
||||
await waitFor(
|
||||
() => fs.existsSync(readyFile) || child.exitCode !== null || child.signalCode !== null,
|
||||
timeoutMs,
|
||||
);
|
||||
} catch (error) {
|
||||
const { stdout, stderr } = output();
|
||||
throw new Error(
|
||||
`${error instanceof Error ? error.message : String(error)} waiting for ${readyFile}`
|
||||
+ `\nexit=${String(child.exitCode)} signal=${String(child.signalCode)}`
|
||||
+ `\nstdout:\n${stdout}\nstderr:\n${stderr}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (!fs.existsSync(readyFile)) {
|
||||
const { stdout, stderr } = output();
|
||||
throw new Error(
|
||||
`Launcher exited before writing ${readyFile}`
|
||||
+ `\nexit=${String(child.exitCode)} signal=${String(child.signalCode)}`
|
||||
+ `\nstdout:\n${stdout}\nstderr:\n${stderr}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForHealth(url: string, timeoutMs: number): Promise<Record<string, unknown>> {
|
||||
let lastError = '';
|
||||
await waitFor(async () => {
|
||||
try {
|
||||
const res = await fetch(url, { signal: AbortSignal.timeout(750) });
|
||||
if (!res.ok) {
|
||||
lastError = `HTTP ${res.status}`;
|
||||
return false;
|
||||
}
|
||||
const body = await res.json() as Record<string, unknown>;
|
||||
if (body.status === 'ok' || body.status === 'degraded') return true;
|
||||
lastError = `unexpected status ${String(body.status)}`;
|
||||
return false;
|
||||
} catch (err) {
|
||||
lastError = err instanceof Error ? err.message : String(err);
|
||||
return false;
|
||||
}
|
||||
}, timeoutMs).catch((err) => {
|
||||
throw new Error(`${err instanceof Error ? err.message : String(err)} waiting for ${url}: ${lastError}`);
|
||||
});
|
||||
|
||||
const res = await fetch(url);
|
||||
return await res.json() as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function processGroupAlive(pid: number): boolean {
|
||||
try {
|
||||
process.kill(-pid, 0);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function stopProcess(child: ChildProcessWithoutNullStreams): Promise<void> {
|
||||
if (!child.pid) return;
|
||||
|
||||
if (process.platform === 'win32') {
|
||||
if (child.exitCode !== null || child.signalCode !== null) return;
|
||||
spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], { stdio: 'ignore' });
|
||||
await Promise.race([
|
||||
new Promise<void>((resolve) => child.once('exit', () => resolve())),
|
||||
new Promise<void>((resolve) => setTimeout(resolve, 5_000)),
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
// npx can exit before the waggle process it started, so signal and wait for
|
||||
// the whole group rather than the direct child alone.
|
||||
const group = child.pid;
|
||||
const signalGroup = (signal: NodeJS.Signals): void => {
|
||||
try { process.kill(-group, signal); } catch { /* the group is already gone */ }
|
||||
};
|
||||
if (!processGroupAlive(group)) return;
|
||||
signalGroup('SIGTERM');
|
||||
try {
|
||||
await waitFor(() => !processGroupAlive(group), 10_000);
|
||||
} catch {
|
||||
signalGroup('SIGKILL');
|
||||
await waitFor(() => !processGroupAlive(group), 5_000);
|
||||
}
|
||||
}
|
||||
|
||||
async function providerFreeEnv(): Promise<NodeJS.ProcessEnv> {
|
||||
return {
|
||||
EMBEDDING_PROVIDER: 'mock',
|
||||
ANTHROPIC_API_KEY: '',
|
||||
OPENAI_API_KEY: '',
|
||||
GEMINI_API_KEY: '',
|
||||
GOOGLE_API_KEY: '',
|
||||
XAI_API_KEY: '',
|
||||
DEEPSEEK_API_KEY: '',
|
||||
MISTRAL_API_KEY: '',
|
||||
DASHSCOPE_API_KEY: '',
|
||||
MINIMAX_API_KEY: '',
|
||||
ZHIPU_API_KEY: '',
|
||||
MOONSHOT_API_KEY: '',
|
||||
PERPLEXITY_API_KEY: '',
|
||||
OPENROUTER_API_KEY: '',
|
||||
OLLAMA_HOST: `http://127.0.0.1:${await freePort()}`,
|
||||
VLLM_HOST: `http://127.0.0.1:${await freePort()}`,
|
||||
};
|
||||
}
|
||||
|
||||
describe('Waggle CLI Launcher', () => {
|
||||
describe('argument parsing', () => {
|
||||
it('uses default port 3333 when no arguments', () => {
|
||||
const result = parseArgs(['node', 'waggle']);
|
||||
expect(result.port).toBe(3333);
|
||||
expect(result.skipLiteLLM).toBe(false);
|
||||
expect(result.noBrowser).toBe(false);
|
||||
expect(result.help).toBe(false);
|
||||
});
|
||||
|
||||
it('parses --port flag', () => {
|
||||
const result = parseArgs(['node', 'waggle', '--port', '4000']);
|
||||
expect(result.port).toBe(4000);
|
||||
});
|
||||
|
||||
it('parses -p shorthand', () => {
|
||||
const result = parseArgs(['node', 'waggle', '-p', '8080']);
|
||||
expect(result.port).toBe(8080);
|
||||
});
|
||||
|
||||
it('rejects invalid port values', () => {
|
||||
const result = parseArgs(['node', 'waggle', '--port', 'abc']);
|
||||
expect(result.port).toBe(3333); // default
|
||||
expect(result.error).toContain('Invalid port');
|
||||
});
|
||||
|
||||
it('rejects out-of-range ports', () => {
|
||||
const neg = parseArgs(['node', 'waggle', '--port', '-1']);
|
||||
expect(neg.port).toBe(3333);
|
||||
expect(neg.error).toContain('Invalid port');
|
||||
|
||||
const big = parseArgs(['node', 'waggle', '--port', '99999']);
|
||||
expect(big.port).toBe(3333);
|
||||
expect(big.error).toContain('Invalid port');
|
||||
});
|
||||
|
||||
it('rejects missing port values', () => {
|
||||
const result = parseArgs(['node', 'waggle', '--port']);
|
||||
expect(result.port).toBe(3333);
|
||||
expect(result.error).toContain('Missing value');
|
||||
});
|
||||
|
||||
it('rejects unknown options before startup', () => {
|
||||
const result = parseArgs(['node', 'waggle', '--wat']);
|
||||
expect(result.error).toContain('Unknown option');
|
||||
});
|
||||
|
||||
it('parses --skip-litellm flag', () => {
|
||||
const result = parseArgs(['node', 'waggle', '--skip-litellm']);
|
||||
expect(result.skipLiteLLM).toBe(true);
|
||||
});
|
||||
|
||||
it('parses --no-open flag', () => {
|
||||
const result = parseArgs(['node', 'waggle', '--no-open']);
|
||||
expect(result.noBrowser).toBe(true);
|
||||
});
|
||||
|
||||
it('parses --help flag', () => {
|
||||
const result = parseArgs(['node', 'waggle', '--help']);
|
||||
expect(result.help).toBe(true);
|
||||
});
|
||||
|
||||
it('parses -h shorthand', () => {
|
||||
const result = parseArgs(['node', 'waggle', '-h']);
|
||||
expect(result.help).toBe(true);
|
||||
});
|
||||
|
||||
it('handles multiple flags together', () => {
|
||||
const result = parseArgs(['node', 'waggle', '--port', '5000', '--skip-litellm', '--no-open']);
|
||||
expect(result.port).toBe(5000);
|
||||
expect(result.skipLiteLLM).toBe(true);
|
||||
expect(result.noBrowser).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Node.js version check', () => {
|
||||
it('current Node version meets minimum requirement (>=18)', () => {
|
||||
const [major] = process.versions.node.split('.').map(Number);
|
||||
expect(major).toBeGreaterThanOrEqual(18);
|
||||
});
|
||||
});
|
||||
|
||||
describe('package configuration', () => {
|
||||
it('package.json has correct bin entry', async () => {
|
||||
const { readFileSync } = await import('node:fs');
|
||||
const { resolve } = await import('node:path');
|
||||
const pkg = JSON.parse(
|
||||
readFileSync(resolve(import.meta.dirname, '..', 'package.json'), 'utf-8')
|
||||
);
|
||||
expect(pkg.name).toBe('@waggle-ai/waggle');
|
||||
expect(pkg.bin).toBeDefined();
|
||||
expect(pkg.bin.waggle).toContain('cli');
|
||||
});
|
||||
});
|
||||
|
||||
describe('startup copy', () => {
|
||||
it('turns port conflicts into actionable CLI guidance', () => {
|
||||
const message = formatStartupFailure(
|
||||
'Port 3333 is already in use. Another Waggle instance may be running.',
|
||||
3333,
|
||||
);
|
||||
|
||||
expect(message).toContain('Failed to start Waggle');
|
||||
expect(message).toContain('Port 3333 is already in use');
|
||||
expect(message).toContain('npx waggle --port 3334');
|
||||
});
|
||||
|
||||
it('explains --no-open success without implying a browser opened', () => {
|
||||
const message = formatStartupSuccess({
|
||||
url: 'http://localhost:3333',
|
||||
llmProvider: 'anthropic-proxy',
|
||||
llmHealth: 'degraded',
|
||||
dataDir: 'C:/tmp/waggle',
|
||||
noBrowser: true,
|
||||
});
|
||||
|
||||
expect(message).toContain('Server: http://localhost:3333');
|
||||
expect(message).toContain('Browser: not opened (--no-open)');
|
||||
expect(message).toContain('Open manually: http://localhost:3333');
|
||||
});
|
||||
});
|
||||
|
||||
describe('runtime help', () => {
|
||||
it('runs built help without starting service setup', async () => {
|
||||
const home = makeHome();
|
||||
try {
|
||||
const build = await run(bin('npm'), ['run', 'build', '--workspace', '@waggle-ai/waggle'], home);
|
||||
expect(build.status).toBe(0);
|
||||
|
||||
const result = await run(process.execPath, [path.join(LAUNCHER_DIR, 'dist', 'cli.js'), '--help'], home);
|
||||
|
||||
expect(result.status).toBe(0);
|
||||
expect(result.stdout).toContain('Usage:');
|
||||
expect(result.stdout).not.toContain('[waggle:service]');
|
||||
expect(result.stderr).toBe('');
|
||||
expect(fs.existsSync(path.join(home, '.waggle'))).toBe(false);
|
||||
} finally {
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('runs built invalid-port validation before starting service setup', async () => {
|
||||
const home = makeHome();
|
||||
try {
|
||||
const build = await run(bin('npm'), ['run', 'build', '--workspace', '@waggle-ai/waggle'], home);
|
||||
expect(build.status).toBe(0);
|
||||
|
||||
const result = await run(process.execPath, [path.join(LAUNCHER_DIR, 'dist', 'cli.js'), '--port', 'abc'], home);
|
||||
|
||||
expect(result.status).toBe(1);
|
||||
expect(result.stderr).toContain('Invalid port: abc');
|
||||
expect(result.stderr).not.toContain('[waggle:service]');
|
||||
expect(fs.existsSync(path.join(home, '.waggle'))).toBe(false);
|
||||
} finally {
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('reports occupied ports with a working --port recovery command', async () => {
|
||||
const home = makeHome();
|
||||
const { server: blocker, port, requestCount, bearerRequestCount } = await occupyPort();
|
||||
try {
|
||||
const build = await run(bin('npm'), ['run', 'build', '--workspace', '@waggle-ai/waggle'], home);
|
||||
expect(build.status).toBe(0);
|
||||
|
||||
const result = await run(
|
||||
process.execPath,
|
||||
[
|
||||
path.join(LAUNCHER_DIR, 'dist', 'cli.js'),
|
||||
'--port',
|
||||
String(port),
|
||||
'--skip-litellm',
|
||||
'--no-open',
|
||||
],
|
||||
home,
|
||||
{ WAGGLE_DATA_DIR: path.join(home, 'data') },
|
||||
);
|
||||
|
||||
expect(result.status).toBe(1);
|
||||
expect(result.stderr).toContain(`Port ${port} is already in use`);
|
||||
expect(result.stderr).toContain(`npx waggle --port ${port + 1}`);
|
||||
expect(bearerRequestCount()).toBe(0);
|
||||
expect(requestCount()).toBe(0);
|
||||
} finally {
|
||||
await new Promise<void>((resolve) => blocker.close(() => resolve()));
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
// Windows has no SIGTERM: Node terminates the process outright there.
|
||||
it.skipIf(process.platform === 'win32')('exits on its own after SIGTERM once the service is ready (UF-041)', async () => {
|
||||
const home = makeHome();
|
||||
let child: ChildProcessWithoutNullStreams | undefined;
|
||||
try {
|
||||
const build = await run(bin('npm'), ['run', 'build', '--workspace', '@waggle-ai/waggle'], home);
|
||||
expect(build.status).toBe(0);
|
||||
|
||||
let output = '';
|
||||
child = spawnInCwd(
|
||||
process.execPath,
|
||||
[path.join(LAUNCHER_DIR, 'dist', 'cli.js'), '--port', String(await freePort()), '--skip-litellm', '--no-open'],
|
||||
ROOT,
|
||||
home,
|
||||
{ WAGGLE_DATA_DIR: path.join(home, 'data'), ...await providerFreeEnv() },
|
||||
);
|
||||
child.stdout.setEncoding('utf8');
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stdout.on('data', (chunk) => { output += chunk; });
|
||||
child.stderr.on('data', (chunk) => { output += chunk; });
|
||||
const exited = new Promise<void>((resolve) => child!.once('exit', () => resolve()));
|
||||
|
||||
await waitFor(() => output.includes('Press Ctrl+C to stop') || child!.exitCode !== null, 90_000);
|
||||
expect(output).toContain('Press Ctrl+C to stop');
|
||||
|
||||
child.kill('SIGTERM');
|
||||
await Promise.race([exited, new Promise<void>((resolve) => setTimeout(resolve, 10_000))]);
|
||||
expect({ exitCode: child.exitCode, signalCode: child.signalCode }).toEqual({ exitCode: 0, signalCode: null });
|
||||
} finally {
|
||||
if (child) await stopProcess(child);
|
||||
fs.rmSync(home, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
|
||||
}
|
||||
}, 120_000);
|
||||
|
||||
it('packs a tarball with a runnable first command', async () => {
|
||||
const home = makeHome();
|
||||
try {
|
||||
const build = await run(bin('npm'), ['run', 'build', '--workspace', '@waggle-ai/waggle'], home);
|
||||
expect(build.status).toBe(0);
|
||||
|
||||
const pack = await run(
|
||||
bin('npm'),
|
||||
['pack', '--workspace', '@waggle-ai/waggle', '--pack-destination', home, '--json'],
|
||||
home,
|
||||
);
|
||||
expect(pack.status).toBe(0);
|
||||
|
||||
const [packResult] = JSON.parse(pack.stdout) as Array<{ filename: string }>;
|
||||
const tarball = path.join(home, packResult.filename);
|
||||
const extractDir = path.join(home, 'packed');
|
||||
fs.mkdirSync(extractDir, { recursive: true });
|
||||
await extractTarball(tarball, extractDir);
|
||||
|
||||
const pkg = JSON.parse(
|
||||
fs.readFileSync(path.join(extractDir, 'package', 'package.json'), 'utf8'),
|
||||
);
|
||||
const distFiles = fs.readdirSync(path.join(extractDir, 'package', 'dist'));
|
||||
const result = await run(
|
||||
process.execPath,
|
||||
[path.join(extractDir, 'package', 'dist', 'cli.js'), '--help'],
|
||||
home,
|
||||
);
|
||||
|
||||
expect(pkg.bin.waggle).toBe('./dist/cli.js');
|
||||
expect({
|
||||
transformersDependency: pkg.dependencies?.['@huggingface/transformers'],
|
||||
bundledNativeBindings: distFiles.filter((file) => /^onnxruntime_binding-.*\.node$/.test(file)),
|
||||
bundledTransformersRuntime: distFiles.filter((file) => /^transformers\.node-.*\.js$/.test(file)),
|
||||
}).toEqual({
|
||||
transformersDependency: '3.8.1',
|
||||
bundledNativeBindings: [],
|
||||
bundledTransformersRuntime: [],
|
||||
});
|
||||
expect(result.status).toBe(0);
|
||||
expect(result.stdout).toContain('Usage:');
|
||||
expect(result.stdout).not.toContain('[waggle:service]');
|
||||
expect(result.stderr).toBe('');
|
||||
expect(fs.existsSync(path.join(home, '.waggle'))).toBe(false);
|
||||
} finally {
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
}, 120_000);
|
||||
|
||||
it('installs the packed launcher and runs npx help plus startup recovery', async () => {
|
||||
const home = makeHome();
|
||||
const { server: blocker, port, requestCount, bearerRequestCount } = await occupyPort();
|
||||
try {
|
||||
const build = await run(bin('npm'), ['run', 'build', '--workspace', '@waggle-ai/waggle'], home);
|
||||
expect(build.status).toBe(0);
|
||||
|
||||
const pack = await run(
|
||||
bin('npm'),
|
||||
['pack', '--workspace', '@waggle-ai/waggle', '--pack-destination', home, '--json'],
|
||||
home,
|
||||
);
|
||||
expect(pack.status).toBe(0);
|
||||
|
||||
const [packResult] = JSON.parse(pack.stdout) as Array<{ filename: string }>;
|
||||
const projectDir = path.join(home, 'project');
|
||||
fs.mkdirSync(projectDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(projectDir, 'package.json'),
|
||||
JSON.stringify({ private: true, type: 'module' }, null, 2),
|
||||
);
|
||||
|
||||
const install = await runInCwd(
|
||||
bin('npm'),
|
||||
[
|
||||
'install',
|
||||
path.join(home, packResult.filename),
|
||||
'--no-audit',
|
||||
'--no-fund',
|
||||
'--prefer-offline',
|
||||
],
|
||||
projectDir,
|
||||
home,
|
||||
);
|
||||
expect(install.status).toBe(0);
|
||||
|
||||
const nativeEmbeddingRuntime = await runInCwd(
|
||||
process.execPath,
|
||||
[
|
||||
'--input-type=module',
|
||||
'--eval',
|
||||
"const runtime = await import('@huggingface/transformers'); if (typeof runtime.pipeline !== 'function') throw new Error('Transformers pipeline API is unavailable');",
|
||||
],
|
||||
projectDir,
|
||||
home,
|
||||
);
|
||||
expect(nativeEmbeddingRuntime.status).toBe(0);
|
||||
expect(nativeEmbeddingRuntime.stderr).toBe('');
|
||||
|
||||
const help = await runInCwd(bin('npx'), ['waggle', '--help'], projectDir, home);
|
||||
expect(help.status).toBe(0);
|
||||
expect(help.stdout).toContain('Usage:');
|
||||
expect(help.stdout).not.toContain('[waggle:service]');
|
||||
expect(help.stderr).toBe('');
|
||||
expect(fs.existsSync(path.join(home, '.waggle'))).toBe(false);
|
||||
|
||||
const startup = await runInCwd(
|
||||
bin('npx'),
|
||||
['waggle', '--port', String(port), '--skip-litellm', '--no-open'],
|
||||
projectDir,
|
||||
home,
|
||||
{ WAGGLE_DATA_DIR: path.join(home, 'data') },
|
||||
);
|
||||
|
||||
expect(startup.status).toBe(1);
|
||||
expect(startup.stderr).toContain(`Port ${port} is already in use`);
|
||||
expect(startup.stderr).toContain(`npx waggle --port ${port + 1}`);
|
||||
expect(fs.existsSync(path.join(home, '.waggle'))).toBe(false);
|
||||
expect(bearerRequestCount()).toBe(0);
|
||||
expect(requestCount()).toBe(0);
|
||||
} finally {
|
||||
await new Promise<void>((resolve) => blocker.close(() => resolve()));
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
}, 240_000);
|
||||
|
||||
it('starts the installed launcher, isolates concurrent histories, and reloads them after restart', async () => {
|
||||
const home = makeHome();
|
||||
const {
|
||||
server: blocker,
|
||||
port: preferredPort,
|
||||
requestCount,
|
||||
bearerRequestCount,
|
||||
} = await occupyPort();
|
||||
let child: ChildProcessWithoutNullStreams | undefined;
|
||||
try {
|
||||
const build = await run(bin('npm'), ['run', 'build', '--workspace', '@waggle-ai/waggle'], home);
|
||||
expect(build.status).toBe(0);
|
||||
|
||||
const pack = await run(
|
||||
bin('npm'),
|
||||
['pack', '--workspace', '@waggle-ai/waggle', '--pack-destination', home, '--json'],
|
||||
home,
|
||||
);
|
||||
expect(pack.status).toBe(0);
|
||||
|
||||
const [packResult] = JSON.parse(pack.stdout) as Array<{ filename: string }>;
|
||||
const projectDir = path.join(home, 'project');
|
||||
fs.mkdirSync(projectDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(projectDir, 'package.json'),
|
||||
JSON.stringify({ private: true, type: 'module' }, null, 2),
|
||||
);
|
||||
|
||||
const install = await runInCwd(
|
||||
bin('npm'),
|
||||
[
|
||||
'install',
|
||||
path.join(home, packResult.filename),
|
||||
'--no-audit',
|
||||
'--no-fund',
|
||||
'--prefer-offline',
|
||||
],
|
||||
projectDir,
|
||||
home,
|
||||
);
|
||||
expect(install.status).toBe(0);
|
||||
|
||||
const dataDir = path.join(home, 'data');
|
||||
const readyFile = path.join(home, 'desktop-ready.json');
|
||||
const restartReadyFile = path.join(home, 'desktop-ready-restart.json');
|
||||
const desktopBootstrapToken = 'launcher-desktop-bootstrap-token-1234567890';
|
||||
const providerEnv = await providerFreeEnv();
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
child = spawnInCwd(
|
||||
bin('npx'),
|
||||
['waggle', '--port', String(preferredPort), '--skip-litellm', '--no-open'],
|
||||
projectDir,
|
||||
home,
|
||||
{
|
||||
WAGGLE_DATA_DIR: dataDir,
|
||||
WAGGLE_DESKTOP_PORT_FALLBACK: '1',
|
||||
WAGGLE_INSTANCE_ID: 'launcher-fallback-health',
|
||||
WAGGLE_DESKTOP_BOOTSTRAP_TOKEN: desktopBootstrapToken,
|
||||
WAGGLE_READY_FILE: readyFile,
|
||||
...providerEnv,
|
||||
},
|
||||
);
|
||||
child.stdout.setEncoding('utf8');
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stdout.on('data', (chunk) => { stdout += chunk; });
|
||||
child.stderr.on('data', (chunk) => { stderr += chunk; });
|
||||
|
||||
await waitForReadyFile(child, readyFile, () => ({ stdout, stderr }), 90_000);
|
||||
const ready = JSON.parse(fs.readFileSync(readyFile, 'utf8')) as {
|
||||
preferredPort: number;
|
||||
port: number;
|
||||
};
|
||||
expect(ready.preferredPort).toBe(preferredPort);
|
||||
expect(ready.port).not.toBe(preferredPort);
|
||||
const baseUrl = `http://127.0.0.1:${ready.port}`;
|
||||
const health = await waitForHealth(`${baseUrl}/health`, 30_000);
|
||||
await waitFor(() => stdout.includes('Press Ctrl+C to stop'), 10_000);
|
||||
|
||||
expect(health.database).toMatchObject({ healthy: true });
|
||||
expect(stdout).toContain(`Server: http://localhost:${ready.port}`);
|
||||
expect(stdout).toContain('Browser: not opened (--no-open)');
|
||||
expect(stdout).toContain(`Open manually: http://localhost:${ready.port}`);
|
||||
expect(stderr).not.toContain('Failed to start Waggle');
|
||||
expect(fs.existsSync(path.join(dataDir, 'waggle.db'))).toBe(true);
|
||||
expect(fs.existsSync(path.join(dataDir, 'memory'))).toBe(true);
|
||||
expect(fs.existsSync(path.join(dataDir, 'personal.mind'))).toBe(false);
|
||||
expect(bearerRequestCount()).toBe(0);
|
||||
expect(requestCount()).toBe(0);
|
||||
|
||||
const tokenResponse = await fetch(`${baseUrl}/api/auth/session-token`, {
|
||||
headers: { 'x-waggle-desktop-bootstrap': desktopBootstrapToken },
|
||||
});
|
||||
expect(tokenResponse.status).toBe(200);
|
||||
const token = (await tokenResponse.json() as { token?: string }).token;
|
||||
if (!token) throw new Error('Installed launcher returned no session token');
|
||||
const headers = {
|
||||
authorization: `Bearer ${token}`,
|
||||
'content-type': 'application/json',
|
||||
};
|
||||
|
||||
const workspaceResponse = await fetch(`${baseUrl}/api/workspaces`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({ name: 'Packed launcher concurrent history', group: 'Test' }),
|
||||
});
|
||||
expect(workspaceResponse.status).toBe(201);
|
||||
const workspaceId = (await workspaceResponse.json() as { id?: string }).id;
|
||||
if (!workspaceId) throw new Error('Installed launcher returned no workspace id');
|
||||
|
||||
const sessionA = 'packed-concurrent-a';
|
||||
const sessionB = 'packed-concurrent-b';
|
||||
const markerA = 'PACKED_CONCURRENT_SESSION_A';
|
||||
const markerB = 'PACKED_CONCURRENT_SESSION_B';
|
||||
const [chatA, chatB] = await Promise.all([
|
||||
fetch(`${baseUrl}/api/chat`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({ message: markerA, workspace: workspaceId, session: sessionA }),
|
||||
}),
|
||||
fetch(`${baseUrl}/api/chat`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({ message: markerB, workspace: workspaceId, session: sessionB }),
|
||||
}),
|
||||
]);
|
||||
const [chatBodyA, chatBodyB] = await Promise.all([chatA.text(), chatB.text()]);
|
||||
for (const [response, body] of [[chatA, chatBodyA], [chatB, chatBodyB]] as const) {
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get('content-type')).toMatch(/^text\/event-stream/);
|
||||
expect(body.match(/^event:\s*done\s*$/gm)).toHaveLength(1);
|
||||
expect(body.match(/^event:\s*error\s*$/gm) ?? []).toHaveLength(0);
|
||||
expect(body).toContain('No AI model is ready');
|
||||
}
|
||||
|
||||
for (const sessionId of [sessionA, sessionB]) {
|
||||
expect(fs.existsSync(path.join(
|
||||
dataDir,
|
||||
'workspaces',
|
||||
workspaceId,
|
||||
'sessions',
|
||||
`${sessionId}.jsonl`,
|
||||
))).toBe(true);
|
||||
}
|
||||
|
||||
await stopProcess(child);
|
||||
child = undefined;
|
||||
|
||||
let restartStdout = '';
|
||||
let restartStderr = '';
|
||||
child = spawnInCwd(
|
||||
bin('npx'),
|
||||
['waggle', '--port', String(preferredPort), '--skip-litellm', '--no-open'],
|
||||
projectDir,
|
||||
home,
|
||||
{
|
||||
WAGGLE_DATA_DIR: dataDir,
|
||||
WAGGLE_DESKTOP_PORT_FALLBACK: '1',
|
||||
WAGGLE_INSTANCE_ID: 'launcher-fallback-history-restart',
|
||||
WAGGLE_DESKTOP_BOOTSTRAP_TOKEN: desktopBootstrapToken,
|
||||
WAGGLE_READY_FILE: restartReadyFile,
|
||||
...providerEnv,
|
||||
},
|
||||
);
|
||||
child.stdout.setEncoding('utf8');
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stdout.on('data', (chunk) => { restartStdout += chunk; });
|
||||
child.stderr.on('data', (chunk) => { restartStderr += chunk; });
|
||||
|
||||
await waitForReadyFile(
|
||||
child,
|
||||
restartReadyFile,
|
||||
() => ({ stdout: restartStdout, stderr: restartStderr }),
|
||||
90_000,
|
||||
);
|
||||
const restartReady = JSON.parse(fs.readFileSync(restartReadyFile, 'utf8')) as {
|
||||
preferredPort: number;
|
||||
port: number;
|
||||
};
|
||||
expect(restartReady.preferredPort).toBe(preferredPort);
|
||||
expect(restartReady.port).not.toBe(preferredPort);
|
||||
const restartBaseUrl = `http://127.0.0.1:${restartReady.port}`;
|
||||
const restartHealth = await waitForHealth(`${restartBaseUrl}/health`, 30_000);
|
||||
await waitFor(() => restartStdout.includes('Press Ctrl+C to stop'), 10_000);
|
||||
expect(restartHealth.database).toMatchObject({ healthy: true });
|
||||
expect(restartStderr).not.toContain('Failed to start Waggle');
|
||||
|
||||
const restartTokenResponse = await fetch(`${restartBaseUrl}/api/auth/session-token`, {
|
||||
headers: { 'x-waggle-desktop-bootstrap': desktopBootstrapToken },
|
||||
});
|
||||
expect(restartTokenResponse.status).toBe(200);
|
||||
const restartToken = (await restartTokenResponse.json() as { token?: string }).token;
|
||||
if (!restartToken) throw new Error('Restarted launcher returned no session token');
|
||||
const restartHeaders = { authorization: `Bearer ${restartToken}` };
|
||||
const [historyResponseA, historyResponseB] = await Promise.all([
|
||||
fetch(
|
||||
`${restartBaseUrl}/api/history?workspace=${encodeURIComponent(workspaceId)}&session=${encodeURIComponent(sessionA)}`,
|
||||
{ headers: restartHeaders },
|
||||
),
|
||||
fetch(
|
||||
`${restartBaseUrl}/api/history?workspace=${encodeURIComponent(workspaceId)}&session=${encodeURIComponent(sessionB)}`,
|
||||
{ headers: restartHeaders },
|
||||
),
|
||||
]);
|
||||
expect(historyResponseA.status).toBe(200);
|
||||
expect(historyResponseB.status).toBe(200);
|
||||
const [historyA, historyB] = await Promise.all([
|
||||
historyResponseA.json(),
|
||||
historyResponseB.json(),
|
||||
]) as Array<{
|
||||
sessionId: string;
|
||||
count: number;
|
||||
messages: Array<{ role: string; content: string }>;
|
||||
}>;
|
||||
|
||||
expect(historyA.sessionId).toBe(sessionA);
|
||||
expect(historyB.sessionId).toBe(sessionB);
|
||||
expect(historyA.count).toBe(2);
|
||||
expect(historyB.count).toBe(2);
|
||||
expect(historyA.messages).toEqual([
|
||||
expect.objectContaining({ role: 'user', content: markerA }),
|
||||
expect.objectContaining({ role: 'assistant', content: expect.stringContaining('No AI model is ready') }),
|
||||
]);
|
||||
expect(historyB.messages).toEqual([
|
||||
expect.objectContaining({ role: 'user', content: markerB }),
|
||||
expect.objectContaining({ role: 'assistant', content: expect.stringContaining('No AI model is ready') }),
|
||||
]);
|
||||
expect(historyA.messages.some(({ content }) => content.includes(markerB))).toBe(false);
|
||||
expect(historyB.messages.some(({ content }) => content.includes(markerA))).toBe(false);
|
||||
expect(bearerRequestCount()).toBe(0);
|
||||
expect(requestCount()).toBe(0);
|
||||
} finally {
|
||||
if (child) await stopProcess(child);
|
||||
await new Promise<void>((resolve) => blocker.close(() => resolve()));
|
||||
// Windows can release SQLite WAL/SHM handles a few milliseconds after
|
||||
// taskkill reports success; retry the isolated temp-home cleanup.
|
||||
fs.rmSync(home, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: 10,
|
||||
retryDelay: 100,
|
||||
});
|
||||
}
|
||||
}, 360_000);
|
||||
});
|
||||
});
|
||||
269
packages/launcher/tests/installer.test.ts
Normal file
269
packages/launcher/tests/installer.test.ts
Normal file
@@ -0,0 +1,269 @@
|
||||
/**
|
||||
* The installer: argument parsing, per-system paths, the server environment,
|
||||
* and install / update / uninstall against an archive packed by
|
||||
* scripts/build-server-archive.mjs.
|
||||
*/
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import { DEFAULT_PORT, parseArgs } from '../src/args.js';
|
||||
import { UserError, doctor, install, uninstall, type Context } from '../src/commands.js';
|
||||
import {
|
||||
assetName,
|
||||
currentHost,
|
||||
defaultBinDir,
|
||||
defaultDataDir,
|
||||
defaultInstallDir,
|
||||
isOnPath,
|
||||
releaseTarget,
|
||||
type Host,
|
||||
} from '../src/platform.js';
|
||||
import { layoutFor, parseChecksum, readRecord, serviceEnvironment, shimContent, shimFile } from '../src/payload.js';
|
||||
|
||||
const ROOT = path.resolve(import.meta.dirname, '..', '..', '..');
|
||||
const VERSION = (JSON.parse(fs.readFileSync(path.join(ROOT, 'packages', 'launcher', 'package.json'), 'utf8')) as {
|
||||
version: string;
|
||||
}).version;
|
||||
|
||||
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 });
|
||||
});
|
||||
|
||||
function host(platform: NodeJS.Platform, env: NodeJS.ProcessEnv = {}): Host {
|
||||
return { platform, arch: 'x64', homedir: platform === 'win32' ? 'C:\\Users\\ana' : '/home/ana', env };
|
||||
}
|
||||
|
||||
describe('parseArgs', () => {
|
||||
it('runs the server when no command is given', () => {
|
||||
expect(parseArgs([])).toEqual({ command: 'run', options: { port: DEFAULT_PORT, open: true, force: false } });
|
||||
});
|
||||
|
||||
it('reads a command and its options', () => {
|
||||
const parsed = parseArgs(['install', '--from', 'a.tar.gz', '--dir', '/opt/w', '--port', '4000', '--no-open', '--force']);
|
||||
expect(parsed.error).toBeUndefined();
|
||||
expect(parsed.command).toBe('install');
|
||||
expect(parsed.options).toMatchObject({ from: 'a.tar.gz', dir: '/opt/w', port: 4000, open: false, force: true });
|
||||
});
|
||||
|
||||
it('rejects bad ports, unknown words and malformed checksums', () => {
|
||||
expect(parseArgs(['--port', '70000']).error).toMatch(/Invalid port/);
|
||||
expect(parseArgs(['--port']).error).toMatch(/Invalid port/);
|
||||
expect(parseArgs(['instal']).error).toMatch(/Unknown command/);
|
||||
expect(parseArgs(['--fast']).error).toMatch(/Unknown option/);
|
||||
expect(parseArgs(['install', '--from']).error).toMatch(/Missing value/);
|
||||
expect(parseArgs(['install', '--sha256', 'abc']).error).toMatch(/64-character/);
|
||||
});
|
||||
|
||||
it('treats --help and --version as commands', () => {
|
||||
expect(parseArgs(['--help']).command).toBe('help');
|
||||
expect(parseArgs(['-v']).command).toBe('version');
|
||||
});
|
||||
});
|
||||
|
||||
describe('platform paths', () => {
|
||||
it('maps v1 systems to release targets and names the asset', () => {
|
||||
expect(releaseTarget({ platform: 'win32', arch: 'x64' })).toBe('windows-x64');
|
||||
expect(releaseTarget({ platform: 'darwin', arch: 'arm64' })).toBe('macos-arm64');
|
||||
expect(releaseTarget({ platform: 'darwin', arch: 'x64' })).toBe('macos-x64');
|
||||
expect(releaseTarget({ platform: 'linux', arch: 'x64' })).toBe('linux-x64');
|
||||
expect(releaseTarget({ platform: 'linux', arch: 'arm64' })).toBeNull();
|
||||
expect(assetName('1.2.3', 'linux-x64')).toBe('waggle-server-1.2.3-linux-x64.tar.gz');
|
||||
});
|
||||
|
||||
it('installs into the per-user program directory of each system', () => {
|
||||
expect(defaultInstallDir(host('win32', { LOCALAPPDATA: 'C:\\Users\\ana\\AppData\\Local' })))
|
||||
.toBe('C:\\Users\\ana\\AppData\\Local\\Waggle\\server');
|
||||
expect(defaultInstallDir(host('darwin'))).toBe('/home/ana/Library/Application Support/Waggle/server');
|
||||
expect(defaultInstallDir(host('linux'))).toBe('/home/ana/.local/share/waggle/server');
|
||||
expect(defaultInstallDir(host('linux', { XDG_DATA_HOME: '/data' }))).toBe('/data/waggle/server');
|
||||
});
|
||||
|
||||
it('keeps the Windows shim inside the install and uses ~/.local/bin elsewhere', () => {
|
||||
expect(defaultBinDir(host('win32'), 'C:\\W\\server')).toBe('C:\\W\\server\\bin');
|
||||
expect(defaultBinDir(host('linux'), '/x')).toBe('/home/ana/.local/bin');
|
||||
});
|
||||
|
||||
it('follows the sidecar data directory rule', () => {
|
||||
expect(defaultDataDir(host('linux'))).toBe('/home/ana/.waggle');
|
||||
expect(defaultDataDir(host('linux', { WAGGLE_DATA_DIR: '/srv/waggle' }))).toBe('/srv/waggle');
|
||||
});
|
||||
|
||||
it('finds a directory on PATH, ignoring case on Windows', () => {
|
||||
expect(isOnPath(host('linux', { PATH: '/usr/bin:/home/ana/.local/bin' }), '/home/ana/.local/bin')).toBe(true);
|
||||
expect(isOnPath(host('linux', { PATH: '/usr/bin' }), '/home/ana/.local/bin')).toBe(false);
|
||||
expect(isOnPath(host('win32', { Path: 'C:\\Windows;c:\\w\\BIN' }), 'C:\\W\\bin')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('payload helpers', () => {
|
||||
it('gives the server the environment the desktop shell gives it', () => {
|
||||
const env = serviceEnvironment({
|
||||
appDir: '/opt/w/app',
|
||||
dataDir: '/home/ana/.waggle',
|
||||
port: 4000,
|
||||
platform: 'linux',
|
||||
baseEnv: { PATH: '/usr/bin', WAGGLE_DATA_DIR: '/stale' },
|
||||
explicitDataDir: false,
|
||||
});
|
||||
expect(env).toMatchObject({
|
||||
WAGGLE_SKIP_LITELLM: '1',
|
||||
WAGGLE_PORT: '4000',
|
||||
WAGGLE_FRONTEND_DIR: '/opt/w/app/web',
|
||||
NODE_PATH: '/opt/w/app/node_modules:/opt/w/app/native',
|
||||
PATH: '/opt/w/app/node_modules/waggle-node-runtime/bin:/opt/w/app:/usr/bin',
|
||||
NPM_CONFIG_PREFIX: '/home/ana/.waggle/npm/prefix',
|
||||
NPM_CONFIG_CACHE: '/home/ana/.waggle/npm/cache',
|
||||
});
|
||||
expect(env.WAGGLE_DATA_DIR).toBeUndefined();
|
||||
|
||||
const windows = serviceEnvironment({
|
||||
appDir: 'C:\\W\\app',
|
||||
dataDir: 'D:\\data',
|
||||
port: 3333,
|
||||
platform: 'win32',
|
||||
baseEnv: { Path: 'C:\\Windows' },
|
||||
explicitDataDir: true,
|
||||
});
|
||||
expect(windows.Path).toBeUndefined();
|
||||
expect(windows.PATH).toBe('C:\\W\\app\\node_modules\\waggle-node-runtime\\bin;C:\\W\\app;C:\\Windows');
|
||||
expect(windows.WAGGLE_DATA_DIR).toBe('D:\\data');
|
||||
});
|
||||
|
||||
it('writes shims that run the copied tool with the payload Node', () => {
|
||||
const unix = layoutFor("/home/ana/it's/server", 'linux');
|
||||
expect(shimContent(unix, 'linux')).toBe(
|
||||
"#!/bin/sh\nexec '/home/ana/it'\\''s/server/app/node' '/home/ana/it'\\''s/server/launcher.mjs' \"$@\"\n",
|
||||
);
|
||||
const windows = layoutFor('C:\\W\\server', 'win32');
|
||||
expect(shimContent(windows, 'win32')).toBe('@echo off\r\n"C:\\W\\server\\app\\node.exe" "C:\\W\\server\\launcher.mjs" %*\r\n');
|
||||
expect(shimFile('C:\\W\\bin', 'waggle-server', 'win32')).toBe('C:\\W\\bin\\waggle-server.cmd');
|
||||
});
|
||||
|
||||
it('reads sha256sum lines and bare digests', () => {
|
||||
const digest = 'A'.repeat(64);
|
||||
expect(parseChecksum(`${digest} waggle.tar.gz\n`)).toBe('a'.repeat(64));
|
||||
expect(parseChecksum(digest)).toBe('a'.repeat(64));
|
||||
expect(parseChecksum('not a digest')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
const target = releaseTarget(currentHost());
|
||||
|
||||
describe.skipIf(!target)('install, update and uninstall from a packed archive', () => {
|
||||
function packArchive(): string {
|
||||
const work = tempDir('waggle-archive-');
|
||||
const resources = path.join(work, 'resources');
|
||||
const web = path.join(work, 'web');
|
||||
fs.mkdirSync(path.join(resources, 'node_modules', 'better-sqlite3'), { recursive: true });
|
||||
fs.mkdirSync(path.join(resources, 'native'), { recursive: true });
|
||||
fs.mkdirSync(web, { recursive: true });
|
||||
fs.writeFileSync(path.join(resources, process.platform === 'win32' ? 'node.exe' : 'node'), 'not a real runtime');
|
||||
fs.writeFileSync(path.join(resources, 'service.js'), 'console.log("service");\n');
|
||||
fs.writeFileSync(path.join(resources, 'service.js.map'), '{}');
|
||||
fs.writeFileSync(path.join(web, 'index.html'), '<!doctype html>');
|
||||
const out = path.join(work, 'out');
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
path.join(ROOT, 'scripts', 'build-server-archive.mjs'),
|
||||
'--resources', resources, '--web', web, '--out', out, '--node-version', '22.0.0',
|
||||
],
|
||||
{ encoding: 'utf8' },
|
||||
);
|
||||
expect(result.status, result.stderr).toBe(0);
|
||||
return path.join(out, assetName(VERSION, target!));
|
||||
}
|
||||
|
||||
function context(lines: string[]): Context {
|
||||
const launcher = path.join(tempDir('waggle-tool-'), 'cli.js');
|
||||
fs.writeFileSync(launcher, '// bundled tool\n');
|
||||
return { host: currentHost(), version: VERSION, launcherSource: launcher, out: (line) => lines.push(line) };
|
||||
}
|
||||
|
||||
it('installs, reinstalls on request and removes the program but not the data', async () => {
|
||||
const archive = packArchive();
|
||||
const dir = path.join(tempDir('waggle-install-'), 'server');
|
||||
const binDir = tempDir('waggle-bin-');
|
||||
const dataDir = tempDir('waggle-data-');
|
||||
fs.writeFileSync(path.join(dataDir, 'waggle.db'), 'user data');
|
||||
const lines: string[] = [];
|
||||
const ctx = context(lines);
|
||||
const opts = { ...parseArgs([]).options, from: archive, dir, binDir, dataDir };
|
||||
|
||||
await install(ctx, opts);
|
||||
const layout = layoutFor(dir, process.platform);
|
||||
expect(fs.readFileSync(path.join(layout.appDir, 'service.js'), 'utf8')).toContain('service');
|
||||
expect(fs.existsSync(path.join(layout.appDir, 'web', 'index.html'))).toBe(true);
|
||||
expect(fs.existsSync(path.join(layout.appDir, 'service.js.map'))).toBe(false);
|
||||
expect(fs.readFileSync(layout.launcherFile, 'utf8')).toBe('// bundled tool\n');
|
||||
const record = readRecord(layout);
|
||||
expect(record).toMatchObject({ version: VERSION, target, source: archive });
|
||||
expect(record?.sha256).toBe(parseChecksum(fs.readFileSync(`${archive}.sha256`, 'utf8')));
|
||||
const shim = shimFile(binDir, 'waggle-server', process.platform);
|
||||
expect(fs.readFileSync(shim, 'utf8')).toContain(layout.launcherFile);
|
||||
expect(record?.shim).toBe(shim);
|
||||
|
||||
lines.length = 0;
|
||||
await install(ctx, { ...opts, from: undefined });
|
||||
expect(lines.join('\n')).toMatch(/already installed/);
|
||||
|
||||
await install(ctx, { ...opts, force: true });
|
||||
expect(fs.existsSync(path.join(dir, 'app.old'))).toBe(false);
|
||||
expect(fs.existsSync(path.join(dir, 'app.new'))).toBe(false);
|
||||
|
||||
// The recorded shim is found without --bin-dir.
|
||||
expect(await uninstall(ctx, { ...opts, binDir: undefined })).toBe(0);
|
||||
expect(fs.existsSync(dir)).toBe(false);
|
||||
expect(fs.existsSync(shim)).toBe(false);
|
||||
expect(fs.readFileSync(path.join(dataDir, 'waggle.db'), 'utf8')).toBe('user data');
|
||||
});
|
||||
|
||||
it('refuses an archive whose checksum does not match and keeps the installed one', async () => {
|
||||
const archive = packArchive();
|
||||
const dir = path.join(tempDir('waggle-install-'), 'server');
|
||||
const ctx = context([]);
|
||||
const opts = { ...parseArgs([]).options, from: archive, dir, binDir: tempDir('waggle-bin-') };
|
||||
await install(ctx, opts);
|
||||
|
||||
await expect(install(ctx, { ...opts, force: true, sha256: 'b'.repeat(64) })).rejects.toThrow(UserError);
|
||||
expect(fs.existsSync(path.join(dir, 'app', 'service.js'))).toBe(true);
|
||||
expect(fs.existsSync(path.join(dir, 'app.new'))).toBe(false);
|
||||
});
|
||||
|
||||
it('reports a runtime that does not start', async () => {
|
||||
const archive = packArchive();
|
||||
const dir = path.join(tempDir('waggle-install-'), 'server');
|
||||
const lines: string[] = [];
|
||||
const ctx = context(lines);
|
||||
const opts = {
|
||||
...parseArgs([]).options,
|
||||
from: archive,
|
||||
dir,
|
||||
binDir: tempDir('waggle-bin-'),
|
||||
dataDir: tempDir('waggle-data-'),
|
||||
port: 1,
|
||||
};
|
||||
await install(ctx, opts);
|
||||
lines.length = 0;
|
||||
|
||||
expect(await doctor(ctx, opts)).toBe(1);
|
||||
const report = lines.join('\n');
|
||||
expect(report).toMatch(/ok +Install: version/);
|
||||
expect(report).toMatch(/FAIL +Node runtime: does not start/);
|
||||
});
|
||||
|
||||
it('removes nothing from a directory that is not an install', async () => {
|
||||
const dir = tempDir('waggle-not-install-');
|
||||
fs.writeFileSync(path.join(dir, 'keep.txt'), 'mine');
|
||||
await expect(uninstall(context([]), { ...parseArgs([]).options, dir })).rejects.toThrow(/not a Waggle server install/);
|
||||
expect(fs.readFileSync(path.join(dir, 'keep.txt'), 'utf8')).toBe('mine');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user