Full server lifecycle management from Electric Cherry: pick game/mode/map/pluto key in the Fleet page, EC provisions the deployment via Komodo, allocates a port in 28960-28980, creates the 0o777 host dir, and starts the container. Destroy button tears everything down (Komodo Destroy+Delete, rm -rf host dir, transactional DB cleanup) and surfaces any partial-failure warnings inline. Same phase cuts over to the new naming scheme (t5_zombie_cosmodrome_1 etc.). Komodo entity names now match EC server names 1:1 (no more _/- transform), and the map catalog carries friendly/slug/canonical for each of the 11 t5 zombie maps. Server rows gain game_mode, map_slug, host_dir, config_file, game_id, image, created_by, and destroyed_at columns for provisioning + audit. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
299 lines
14 KiB
TypeScript
299 lines
14 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { newFixture, type Fixture } from '../helpers/fixture.js';
|
|
|
|
let fixture: Fixture;
|
|
|
|
// Fresh module registry per test — otherwise the db() singleton leaks across
|
|
// fixtures and hits a released sqlite handle.
|
|
beforeEach(async () => {
|
|
vi.resetModules();
|
|
fixture = await newFixture(['t5_zombie_theater_1', 't5_zombie_cosmodrome_1']);
|
|
delete process.env.KOMODO_ADDRESS;
|
|
delete process.env.RCON_PASSWORD;
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await fixture.cleanup();
|
|
});
|
|
|
|
async function seedFleet(): Promise<void> {
|
|
const { seedServerRegistry } = await import('../../src/lib/server/fleet/servers.js');
|
|
await seedServerRegistry();
|
|
}
|
|
|
|
describe('scope options', () => {
|
|
it('lists distinct games and maps by game from the fleet', async () => {
|
|
await seedFleet();
|
|
const { listDistinctGames, listMapsByGame } = await import('../../src/lib/server/fleet/scripts.js');
|
|
|
|
// Both fixture servers are t5, so games is a single-entry list.
|
|
expect(listDistinctGames()).toEqual(['t5']);
|
|
const byGame = listMapsByGame();
|
|
expect(byGame.t5).toEqual(expect.arrayContaining(['zombie_theater', 'zombie_cosmodrome']));
|
|
expect(byGame.t5.length).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('promote creates a deployment', () => {
|
|
it('records deployment_files for every landed server and marks beta servers done, others pending', async () => {
|
|
await seedFleet();
|
|
const { createScript, getScript, getLatestVersion } = await import('../../src/lib/server/fleet/scripts.js');
|
|
const { openBeta, promoteBeta } = await import('../../src/lib/server/fleet/beta.js');
|
|
const { db } = await import('../../src/lib/server/db/index.js');
|
|
|
|
// A game-scoped script matches both fixture servers.
|
|
const { script } = createScript({
|
|
name: 'hud tweaks',
|
|
targetRelPath: 'hud.gsc',
|
|
scopeType: 'game',
|
|
scopeValue: 't5',
|
|
content: Buffer.from('main(){}\n')
|
|
});
|
|
const version = getLatestVersion(script.id)!;
|
|
|
|
// Open the beta on ONE server; promotion should still write to the
|
|
// other server (via scope) and mark it pending for restart.
|
|
const { betaId } = await openBeta({
|
|
scripts: [{ scriptId: script.id, scriptVersionId: version.id }],
|
|
servers: ['t5_zombie_theater_1'],
|
|
waitForHealth: false,
|
|
waitForMatch: false
|
|
});
|
|
|
|
// Force all beta_servers to 'ready' and beta to 'ready-promote'.
|
|
db().prepare(`UPDATE beta_servers SET stage = 'ready', restarted_at = ? WHERE beta_id = ?`)
|
|
.run(new Date().toISOString(), betaId);
|
|
db().prepare(`UPDATE betas SET state = 'ready-promote' WHERE id = ?`).run(betaId);
|
|
|
|
const result = await promoteBeta(betaId);
|
|
|
|
expect(result.deploymentId).toMatch(/^dep-.*-promote-\d+$/);
|
|
expect(new Set(result.landed)).toEqual(new Set(['t5_zombie_theater_1', 't5_zombie_cosmodrome_1']));
|
|
expect(result.scriptIds).toEqual([script.id]);
|
|
|
|
// Deployment row is action='promote'. The multi-script schema stores
|
|
// scripts in deployment_scripts, not the legacy singular columns.
|
|
const dep = db().prepare(`SELECT * FROM deployments WHERE id = ?`).get(result.deploymentId) as any;
|
|
expect(dep.action).toBe('promote');
|
|
expect(dep.beta_id).toBe(betaId);
|
|
expect(dep.script_id).toBeNull(); // new promotes populate deployment_scripts
|
|
|
|
const depScripts = db().prepare(`SELECT * FROM deployment_scripts WHERE deployment_id = ?`).all(result.deploymentId) as any[];
|
|
expect(depScripts.length).toBe(1);
|
|
expect(depScripts[0].script_id).toBe(script.id);
|
|
expect(depScripts[0].script_version_id).toBe(version.id);
|
|
expect(depScripts[0].prior_version_id).toBeNull(); // never promoted before
|
|
|
|
// deployment_files has one row per landed server.
|
|
const files = db().prepare(`SELECT * FROM deployment_files WHERE deployment_id = ?`).all(result.deploymentId) as any[];
|
|
expect(files.length).toBe(2);
|
|
for (const f of files) {
|
|
expect(f.verb).toBe('added');
|
|
expect(f.rel_path).toBe('hud.gsc');
|
|
expect(f.sha256_after).toBeTruthy();
|
|
}
|
|
|
|
// Beta server row is 'done' (beta already restarted it); non-beta scope
|
|
// server row is 'pending' (sweep will drain-and-restart).
|
|
const servers = db()
|
|
.prepare(`SELECT * FROM deployment_servers WHERE deployment_id = ? ORDER BY server`)
|
|
.all(result.deploymentId) as any[];
|
|
const byName = Object.fromEntries(servers.map((s) => [s.server, s.restart_state]));
|
|
expect(byName['t5_zombie_theater_1']).toBe('done');
|
|
expect(byName['t5_zombie_cosmodrome_1']).toBe('pending');
|
|
|
|
// Script's current_version_id and beta state land.
|
|
const after = getScript(script.id)!;
|
|
expect(after.current_version_id).toBe(version.id);
|
|
const beta = db().prepare(`SELECT state FROM betas WHERE id = ?`).get(betaId) as any;
|
|
expect(beta.state).toBe('promoted');
|
|
});
|
|
});
|
|
|
|
describe('rollback beta on ready-promote marks version test-failed', () => {
|
|
it('flips script_versions.state to test-failed when the version has no other promoting beta', async () => {
|
|
await seedFleet();
|
|
const { createScript, getLatestVersion } = await import('../../src/lib/server/fleet/scripts.js');
|
|
const { openBeta, rollbackBeta } = await import('../../src/lib/server/fleet/beta.js');
|
|
const { db } = await import('../../src/lib/server/db/index.js');
|
|
|
|
const { script } = createScript({
|
|
name: 'flag experiment',
|
|
targetRelPath: 'flag.gsc',
|
|
scopeType: 'map',
|
|
scopeValue: 'zombie_theater',
|
|
content: Buffer.from('main(){}\n')
|
|
});
|
|
const v = getLatestVersion(script.id)!;
|
|
|
|
const { betaId } = await openBeta({
|
|
scripts: [{ scriptId: script.id, scriptVersionId: v.id }],
|
|
servers: ['t5_zombie_theater_1'],
|
|
waitForHealth: false,
|
|
waitForMatch: false
|
|
});
|
|
|
|
// Force ready-promote so the state machine gate is closed.
|
|
db().prepare(`UPDATE beta_servers SET stage = 'ready' WHERE beta_id = ?`).run(betaId);
|
|
db().prepare(`UPDATE betas SET state = 'ready-promote' WHERE id = ?`).run(betaId);
|
|
// Simulate the beta-runner's tested-state transition.
|
|
db().prepare(`UPDATE script_versions SET state = 'tested' WHERE id = ?`).run(v.id);
|
|
|
|
await rollbackBeta(betaId);
|
|
|
|
const after = db().prepare(`SELECT state FROM script_versions WHERE id = ?`).get(v.id) as any;
|
|
expect(after.state).toBe('test-failed');
|
|
});
|
|
});
|
|
|
|
describe('rollback promote deployment', () => {
|
|
it('restores current_version_id + files and creates an action=rollback deployment with pending servers', async () => {
|
|
await seedFleet();
|
|
const { createScript, getScript, getLatestVersion } = await import('../../src/lib/server/fleet/scripts.js');
|
|
const { openBeta, promoteBeta } = await import('../../src/lib/server/fleet/beta.js');
|
|
const { rollbackPromoteDeployment } = await import('../../src/lib/server/fleet/rollback.js');
|
|
const { db } = await import('../../src/lib/server/db/index.js');
|
|
|
|
const { script } = createScript({
|
|
name: 'roll me back',
|
|
targetRelPath: 'rb.gsc',
|
|
scopeType: 'game',
|
|
scopeValue: 't5',
|
|
content: Buffer.from('main(){}\n')
|
|
});
|
|
const v = getLatestVersion(script.id)!;
|
|
|
|
const { betaId } = await openBeta({
|
|
scripts: [{ scriptId: script.id, scriptVersionId: v.id }],
|
|
servers: ['t5_zombie_theater_1'],
|
|
waitForHealth: false,
|
|
waitForMatch: false
|
|
});
|
|
db().prepare(`UPDATE beta_servers SET stage = 'ready', restarted_at = ? WHERE beta_id = ?`)
|
|
.run(new Date().toISOString(), betaId);
|
|
db().prepare(`UPDATE betas SET state = 'ready-promote' WHERE id = ?`).run(betaId);
|
|
const promote = await promoteBeta(betaId);
|
|
|
|
// Sanity: script is promoted.
|
|
expect(getScript(script.id)!.current_version_id).toBe(v.id);
|
|
|
|
const result = await rollbackPromoteDeployment(promote.deploymentId);
|
|
expect(result.deploymentId).toMatch(/^dep-.*-rollback-/);
|
|
expect(new Set(result.affectedServers)).toEqual(new Set(['t5_zombie_theater_1', 't5_zombie_cosmodrome_1']));
|
|
expect(result.scriptIds).toEqual([script.id]);
|
|
|
|
// Script's current_version_id reverted (was null before the promote).
|
|
expect(getScript(script.id)!.current_version_id).toBeNull();
|
|
|
|
// Original promote is now marked rolled_back_by.
|
|
const promoteRow = db().prepare(`SELECT rolled_back_by FROM deployments WHERE id = ?`).get(promote.deploymentId) as any;
|
|
expect(promoteRow.rolled_back_by).toBe(result.deploymentId);
|
|
|
|
// Rollback deployment_servers rows are pending.
|
|
const rbServers = db()
|
|
.prepare(`SELECT restart_state FROM deployment_servers WHERE deployment_id = ?`)
|
|
.all(result.deploymentId) as any[];
|
|
expect(rbServers.length).toBe(2);
|
|
for (const s of rbServers) expect(s.restart_state).toBe('pending');
|
|
|
|
// Second rollback attempt is rejected.
|
|
await expect(rollbackPromoteDeployment(promote.deploymentId)).rejects.toMatchObject({ kind: 'already-rolled-back' });
|
|
});
|
|
});
|
|
|
|
describe('multi-script beta', () => {
|
|
it('open + promote + rollback bundle two scripts as one atomic unit', async () => {
|
|
await seedFleet();
|
|
const {
|
|
createScript,
|
|
getScript,
|
|
getLatestVersion
|
|
} = await import('../../src/lib/server/fleet/scripts.js');
|
|
const {
|
|
openBeta,
|
|
promoteBeta,
|
|
listBetaScripts
|
|
} = await import('../../src/lib/server/fleet/beta.js');
|
|
const { rollbackPromoteDeployment } = await import('../../src/lib/server/fleet/rollback.js');
|
|
const { heldByBeta } = await import('../../src/lib/server/fleet/servers.js');
|
|
const { db } = await import('../../src/lib/server/db/index.js');
|
|
|
|
// Two scripts, both scope=game=t5 so they both land on both fixture servers.
|
|
const a = createScript({
|
|
name: 'hud',
|
|
targetRelPath: 'hud.gsc',
|
|
scopeType: 'game',
|
|
scopeValue: 't5',
|
|
content: Buffer.from('hud(){}\n')
|
|
});
|
|
const b = createScript({
|
|
name: 'flag',
|
|
targetRelPath: 'flag.gsc',
|
|
scopeType: 'game',
|
|
scopeValue: 't5',
|
|
content: Buffer.from('flag(){}\n')
|
|
});
|
|
const va = getLatestVersion(a.script.id)!;
|
|
const vb = getLatestVersion(b.script.id)!;
|
|
|
|
const { betaId } = await openBeta({
|
|
scripts: [
|
|
{ scriptId: a.script.id, scriptVersionId: va.id },
|
|
{ scriptId: b.script.id, scriptVersionId: vb.id }
|
|
],
|
|
servers: ['t5_zombie_theater_1', 't5_zombie_cosmodrome_1'],
|
|
waitForHealth: false,
|
|
waitForMatch: false
|
|
});
|
|
|
|
// Both scripts are joined to the beta.
|
|
const bundle = listBetaScripts(betaId);
|
|
expect(bundle.length).toBe(2);
|
|
expect(new Set(bundle.map((r) => r.script_id))).toEqual(new Set([a.script.id, b.script.id]));
|
|
|
|
// Both target rel paths are held on both servers.
|
|
const held1 = heldByBeta('t5_zombie_theater_1');
|
|
expect(held1.get('hud.gsc')).toBe(betaId);
|
|
expect(held1.get('flag.gsc')).toBe(betaId);
|
|
|
|
// Force ready-promote and simulate tested transition (the beta-runner
|
|
// would do this for real).
|
|
db().prepare(`UPDATE beta_servers SET stage = 'ready', restarted_at = ? WHERE beta_id = ?`)
|
|
.run(new Date().toISOString(), betaId);
|
|
db().prepare(`UPDATE betas SET state = 'ready-promote' WHERE id = ?`).run(betaId);
|
|
db().prepare(`UPDATE script_versions SET state = 'tested' WHERE id IN (?, ?)`).run(va.id, vb.id);
|
|
|
|
const promote = await promoteBeta(betaId);
|
|
expect(new Set(promote.scriptIds)).toEqual(new Set([a.script.id, b.script.id]));
|
|
|
|
// ONE deployment, TWO deployment_scripts rows.
|
|
const dep = db().prepare(`SELECT * FROM deployments WHERE id = ?`).get(promote.deploymentId) as any;
|
|
expect(dep.action).toBe('promote');
|
|
expect(dep.beta_id).toBe(betaId);
|
|
const depScripts = db()
|
|
.prepare(`SELECT * FROM deployment_scripts WHERE deployment_id = ? ORDER BY script_id`)
|
|
.all(promote.deploymentId) as any[];
|
|
expect(depScripts.length).toBe(2);
|
|
|
|
// Two files per server (hud + flag) → 4 deployment_files rows.
|
|
const files = db().prepare(`SELECT * FROM deployment_files WHERE deployment_id = ?`).all(promote.deploymentId) as any[];
|
|
expect(files.length).toBe(4);
|
|
|
|
// Both scripts' current_version_id updated.
|
|
expect(getScript(a.script.id)!.current_version_id).toBe(va.id);
|
|
expect(getScript(b.script.id)!.current_version_id).toBe(vb.id);
|
|
|
|
// Rollback the whole promote — both scripts revert together.
|
|
const rb = await rollbackPromoteDeployment(promote.deploymentId);
|
|
expect(new Set(rb.scriptIds)).toEqual(new Set([a.script.id, b.script.id]));
|
|
expect(getScript(a.script.id)!.current_version_id).toBeNull();
|
|
expect(getScript(b.script.id)!.current_version_id).toBeNull();
|
|
|
|
// The rollback deployment mirrors both scripts.
|
|
const rbScripts = db()
|
|
.prepare(`SELECT * FROM deployment_scripts WHERE deployment_id = ?`)
|
|
.all(rb.deploymentId) as any[];
|
|
expect(rbScripts.length).toBe(2);
|
|
});
|
|
});
|