Persist beta, promotion, drift, and rollback payloads until occupancy and restart-capacity checks pass. Preserve rollback snapshots and campaign ordering, and allow held rollback campaigns to resume safely. Fixes #28 Validation: 805 tests passed, Svelte checks clean, production build passed.
393 lines
19 KiB
TypeScript
393 lines
19 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { newFixture, type Fixture } from '../helpers/fixture.js';
|
|
import { activateFiles } from '../helpers/staged-files.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('editing promoted script scopes', () => {
|
|
async function saveScope(scriptId: number, scopeType: 'game' | 'map' | null, scopeValue: string | null) {
|
|
const { POST } = await import('../../src/routes/api/scripts/[id]/scope/+server.js');
|
|
return POST({
|
|
params: { id: String(scriptId) },
|
|
request: new Request(`http://localhost/api/scripts/${scriptId}/scope`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ scopeType, scopeValue })
|
|
})
|
|
});
|
|
}
|
|
|
|
it.each(['game', 'map'] as const)('restores a cleared %s scope and can promote again', async (scopeType) => {
|
|
await seedFleet();
|
|
const { createScript, getScript } = 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');
|
|
const scopeValue = scopeType === 'game' ? 't5' : 'zombie_theater';
|
|
const { script, version } = createScript({
|
|
name: 'restore scope', targetRelPath: 'restore.gsc',
|
|
scopeType, scopeValue, content: Buffer.from('main(){}\n')
|
|
});
|
|
async function promote() {
|
|
const { betaId } = await openBeta({
|
|
scripts: [{ scriptId: script.id, scriptVersionId: version.id }],
|
|
servers: ['t5_zombie_theater_1'], waitForHealth: false, waitForMatch: false
|
|
});
|
|
await activateFiles('beta', betaId);
|
|
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);
|
|
const result = await promoteBeta(betaId);
|
|
await activateFiles('campaign', result.deploymentId);
|
|
return result;
|
|
}
|
|
await promote();
|
|
|
|
const cleared = await saveScope(script.id, null, null);
|
|
expect(cleared.status).toBe(200);
|
|
expect((await cleared.json()).detached).toContain('t5_zombie_theater_1');
|
|
expect(getScript(script.id)).toMatchObject({ scope_type: null, current_version_id: version.id });
|
|
|
|
const restored = await saveScope(script.id, scopeType, scopeValue);
|
|
expect(restored.status).toBe(200);
|
|
expect((await restored.json()).script).toMatchObject({
|
|
scope_type: scopeType, scope_value: scopeValue, current_version_id: version.id
|
|
});
|
|
// Saving scope must not deploy or reattach the detached inventory.
|
|
expect(db().prepare('SELECT * FROM server_scripts WHERE script_version_id = ?').all(version.id)).toHaveLength(0);
|
|
const result = await promote();
|
|
expect(result.landed.sort()).toEqual(scopeType === 'game'
|
|
? ['t5_zombie_cosmodrome_1', 't5_zombie_theater_1'] : ['t5_zombie_theater_1']);
|
|
|
|
// A promoted script can also change scope without first clearing it.
|
|
const changed = await saveScope(script.id, 'map', 'zombie_cosmodrome');
|
|
expect(changed.status).toBe(200);
|
|
expect(getScript(script.id)).toMatchObject({
|
|
scope_type: 'map', scope_value: 'zombie_cosmodrome', current_version_id: version.id
|
|
});
|
|
});
|
|
|
|
it.each(['running', 'ready-promote'])('rejects scope edits while a beta is %s without detaching inventory', async (state) => {
|
|
await seedFleet();
|
|
const { createScript, getScript } = await import('../../src/lib/server/fleet/scripts.js');
|
|
const { openBeta } = await import('../../src/lib/server/fleet/beta.js');
|
|
const { db } = await import('../../src/lib/server/db/index.js');
|
|
const { script, version } = createScript({
|
|
name: 'active scope', targetRelPath: 'active.gsc',
|
|
scopeType: 'game', scopeValue: 't5', content: Buffer.from('main(){}\n')
|
|
});
|
|
const { betaId } = await openBeta({
|
|
scripts: [{ scriptId: script.id, scriptVersionId: version.id }],
|
|
servers: ['t5_zombie_theater_1'], waitForHealth: false, waitForMatch: false
|
|
});
|
|
await activateFiles('beta', betaId);
|
|
db().prepare('UPDATE betas SET state = ? WHERE id = ?').run(state, betaId);
|
|
const inventory = db().prepare('SELECT * FROM server_scripts').all();
|
|
expect(inventory.length).toBeGreaterThan(0);
|
|
for (const scopeType of [null, 'map'] as const) {
|
|
const response = await saveScope(script.id, scopeType, scopeType ? 'zombie_cosmodrome' : null);
|
|
expect(response.status).toBe(409);
|
|
expect(await response.json()).toMatchObject({ ok: false, kind: 'has-active-beta' });
|
|
expect(getScript(script.id)).toMatchObject({ scope_type: 'game', scope_value: 't5' });
|
|
expect(db().prepare('SELECT * FROM server_scripts').all()).toEqual(inventory);
|
|
}
|
|
});
|
|
});
|
|
|
|
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
|
|
});
|
|
await activateFiles('beta', betaId);
|
|
|
|
// 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
|
|
});
|
|
await activateFiles('beta', betaId);
|
|
|
|
// 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
|
|
});
|
|
await activateFiles('beta', betaId);
|
|
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);
|
|
await activateFiles('campaign', promote.deploymentId);
|
|
|
|
// 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
|
|
});
|
|
await activateFiles('beta', betaId);
|
|
|
|
// 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);
|
|
});
|
|
});
|