Starts the fixture provider's loopback endpoint for a named dev instance and records it under the data root. Refuses the default instance, a configured proxy, provider credentials in the environment, a non-loopback bind and a second fixture. fixtureLauncherOptions hands the endpoint to the launcher as its session proxy context.
104 lines
7.2 KiB
JavaScript
104 lines
7.2 KiB
JavaScript
// `mosaic dev fixture`: refusals, the state file's checks, and one real
|
|
// init/status/stop of the loopback endpoint in a scratch HOME.
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { devCommand, fixtureFile, fixtureLauncherOptions, isEndpoint, readFixture } from '../src/dev-fixture.mjs';
|
|
import { FIXTURE_ENTRY, FIXTURE_KEY } from '../../plugins/model-provider/fixture/index.mjs';
|
|
import { io } from './helpers.mjs';
|
|
|
|
// devOwnerInstance reads the instance from MOSAIC_CONFIG under $HOME/.config.
|
|
const HOME = mkdtempSync(join(tmpdir(), 'devfx'));
|
|
process.env.HOME = HOME;
|
|
test.after(() => rmSync(HOME, { recursive: true, force: true }));
|
|
const config = name => join(HOME, '.config', `mosaic-${name}`, 'config.json');
|
|
const envFor = (name = 'fxtest', extra = {}) => ({ PATH: process.env.PATH, HOME, MOSAIC_CONFIG: config(name), ...extra });
|
|
let n = 0;
|
|
const dataRoot = () => { const d = join(HOME, `data${++n}`); mkdirSync(d, { mode: 0o700 }); return d; };
|
|
|
|
async function refused(args, env, system, code, exitCode = 3) {
|
|
const x = io(env);
|
|
await assert.rejects(devCommand(args, x, { system }), e => { assert.equal(e.exitCode, exitCode, e.message); if (code) assert.equal(e.code, code, e.message); return true; });
|
|
return x;
|
|
}
|
|
const INIT = ['fixture', 'init', '--model', 'deterministic'];
|
|
|
|
test('init refuses before it starts anything', async () => {
|
|
const system = { dataRoot: dataRoot() };
|
|
await refused(['fixture', 'init'], envFor(), system, undefined, 4);
|
|
await refused(['fixture', 'init', '--model', 'claude'], envFor(), system, undefined, 4);
|
|
await refused([...INIT, '--bind', '0.0.0.0'], envFor(), system, 'fixture-endpoint-not-loopback');
|
|
await refused(INIT, { PATH: process.env.PATH, HOME }, system, 'fixture-instance-refused');
|
|
await refused(INIT, envFor('dev'), system, 'fixture-instance-refused');
|
|
await refused([...INIT, '--instance', 'other'], envFor(), system, 'fixture-instance-mismatch');
|
|
await refused(INIT, envFor('fxtest', { ANTHROPIC_API_KEY: 'sk-ant-value-never-printed' }), system, 'fixture-credential-present');
|
|
await assert.rejects(devCommand(INIT, io(envFor('fxtest', { ZAI_API_KEY: 'zai-value-never-printed' })), { system }), e => /ZAI_API_KEY/.test(e.message) && !/never-printed/.test(e.message));
|
|
await refused(INIT, envFor(), { ...system, proxy: { url: 'http://127.0.0.1:1' } }, 'fixture-proxy-configured');
|
|
assert.equal(existsSync(join(system.dataRoot, 'dev-fixture')), false);
|
|
});
|
|
|
|
function writeState(root, record, { dirMode = 0o700, fileMode = 0o600 } = {}) {
|
|
const dir = join(root, 'dev-fixture');
|
|
mkdirSync(dir, { recursive: true, mode: 0o700 }); chmodSync(dir, dirMode);
|
|
writeFileSync(fixtureFile(root), JSON.stringify(record), { mode: fileMode }); chmodSync(fixtureFile(root), fileMode);
|
|
}
|
|
const RECORD = { fixtureVersion: 1, model: 'deterministic', endpoint: 'http://127.0.0.1:45678', port: 45678, pid: 999999, instance: 'fxtest', entry: FIXTURE_ENTRY };
|
|
|
|
test('the state file must be private, a fixture record and loopback', () => {
|
|
const unsafe = e => e.code === 'fixture-state-unsafe';
|
|
assert.equal(readFixture(dataRoot()), null);
|
|
let root = dataRoot(); writeState(root, RECORD);
|
|
assert.equal(readFixture(root).port, 45678);
|
|
root = dataRoot(); writeState(root, RECORD, { dirMode: 0o755 }); assert.throws(() => readFixture(root), unsafe);
|
|
root = dataRoot(); writeState(root, RECORD, { fileMode: 0o644 }); assert.throws(() => readFixture(root), unsafe);
|
|
root = dataRoot(); writeState(root, { ...RECORD, endpoint: 'http://0.0.0.0:45678' }); assert.throws(() => readFixture(root), e => e.code === 'fixture-endpoint-not-loopback');
|
|
root = dataRoot(); writeState(root, { ...RECORD, endpoint: 'http://127.0.0.1:1' }); assert.throws(() => readFixture(root), e => e.code === 'fixture-state-invalid');
|
|
root = dataRoot(); writeState(root, { ...RECORD, entry: { ...FIXTURE_ENTRY, upstream: 'anthropic' } }); assert.throws(() => readFixture(root), e => e.code === 'fixture-state-invalid');
|
|
root = dataRoot(); mkdirSync(join(root, 'dev-fixture'), { mode: 0o700 }); writeFileSync(join(root, 'elsewhere.json'), JSON.stringify(RECORD), { mode: 0o600 });
|
|
symlinkSync(join(root, 'elsewhere.json'), fixtureFile(root)); assert.throws(() => readFixture(root), unsafe);
|
|
root = dataRoot(); writeState(root, RECORD); writeFileSync(fixtureFile(root), 'not json'); assert.throws(() => readFixture(root), e => e.code === 'fixture-state-invalid');
|
|
});
|
|
|
|
test('launcher options: none without a fixture, the endpoint with one, refusals around it', () => {
|
|
assert.deepEqual(fixtureLauncherOptions({ system: { dataRoot: dataRoot() }, env: {}, instance: 'fxtest' }), {});
|
|
const root = dataRoot(); writeState(root, RECORD);
|
|
assert.deepEqual(fixtureLauncherOptions({ system: { dataRoot: root }, env: { PATH: '/usr/bin' }, instance: 'fxtest' }), { testProxy: { baseUrl: RECORD.endpoint, key: FIXTURE_KEY } });
|
|
assert.throws(() => fixtureLauncherOptions({ system: { dataRoot: root, proxy: {} }, env: {}, instance: 'fxtest' }), e => e.code === 'fixture-proxy-configured');
|
|
assert.throws(() => fixtureLauncherOptions({ system: { dataRoot: root }, env: { ANTHROPIC_AUTH_TOKEN: 'x' }, instance: 'fxtest' }), e => e.code === 'fixture-credential-present');
|
|
assert.throws(() => fixtureLauncherOptions({ system: { dataRoot: root }, env: {}, instance: 'other' }), e => e.code === 'fixture-instance-mismatch');
|
|
});
|
|
|
|
test('a real init, status and stop on loopback', async t => {
|
|
const system = { dataRoot: dataRoot() };
|
|
const env = envFor();
|
|
const x = io(env);
|
|
await devCommand([...INIT, '--instance', 'fxtest', '--json'], x, { system });
|
|
const receipt = JSON.parse(x.out.text);
|
|
t.after(() => { if (isEndpoint(receipt.pid)) process.kill(receipt.pid, 'SIGKILL'); });
|
|
assert.equal(receipt.receipt, 'dev-fixture-init');
|
|
assert.match(receipt.endpoint, /^http:\/\/127\.0\.0\.1:[0-9]+$/);
|
|
assert.deepEqual([receipt.loopback, receipt.external, receipt.upstream, receipt.instance, receipt.principal, receipt.modelKey], [true, false, 'none', 'fxtest', 'local-dev:local-dev-owner', 'fixture-deterministic']);
|
|
assert.equal(isEndpoint(receipt.pid), true);
|
|
assert.equal(JSON.parse(readFileSync(fixtureFile(system.dataRoot), 'utf8')).pid, receipt.pid);
|
|
assert.doesNotMatch(readFileSync(fixtureFile(system.dataRoot), 'utf8'), new RegExp(FIXTURE_KEY));
|
|
|
|
const s = io(env);
|
|
await devCommand(['fixture', 'status', '--json'], s, { system });
|
|
const status = JSON.parse(s.out.text);
|
|
assert.deepEqual([status.ready, status.alive, status.external, status.upstream, status.proxyConfigured, status.health.upstream], [true, true, false, 'none', false, 'none']);
|
|
const p = io(env);
|
|
await devCommand(['fixture', 'status', '--json'], p, { system: { ...system, proxy: {} } });
|
|
assert.equal(JSON.parse(p.out.text).ready, false);
|
|
|
|
await refused(INIT, env, system, 'fixture-exists');
|
|
|
|
const st = io(env);
|
|
await devCommand(['fixture', 'stop', '--json'], st, { system });
|
|
assert.deepEqual(JSON.parse(st.out.text).signaled, true);
|
|
assert.equal(isEndpoint(receipt.pid), false);
|
|
assert.equal(readFixture(system.dataRoot), null);
|
|
await refused(['fixture', 'status'], env, system, undefined, 2);
|
|
});
|