Files
electric-cherry/tests/unit/script-flow.test.ts
T
joshandClaude Opus 4.7 1afdfddc87 Promote as a real deployment; version state and scope dropdowns
Turns promotion into a first-class deployment (action='promote') so it lands
in the deployments UI with before/after content, per-server restart tracking,
and one-click rollback. Non-beta matching-scope servers get restart_state=
pending so the existing sweep drains and restarts them the same way drift does.

Also adds a per-version state (untested/tested/test-failed) so the upload
flow can visibly signal that a beta must land before promotion, and swaps
the scope-value text box for game/map dropdowns populated from the fleet.
Beta rollback at ready-promote is now labelled Reject and marks the version
test-failed.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-09-12 14:24:35 -04:00

202 lines
9.1 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_kino_1', 't5_ascension_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 } = 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');
// The reconcileBetaState only fires from the beta-runner; short-circuit
// that in the test by flipping beta state manually.
// 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')
});
// 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({
scriptId: script.id,
scriptVersionId: script.current_version_id ?? (await import('../../src/lib/server/fleet/scripts.js')).getLatestVersion(script.id)!.id,
servers: ['t5_kino_1'],
waitForHealth: false,
waitForMatch: false,
triggerKind: 'mark-required'
});
// 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_kino_1', 't5_ascension_1']));
// Deployment row is action='promote' with the new columns populated.
const dep = db().prepare(`SELECT * FROM deployments WHERE id = ?`).get(result.deploymentId) as any;
expect(dep.action).toBe('promote');
expect(dep.script_id).toBe(script.id);
expect(dep.beta_id).toBe(betaId);
expect(dep.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_kino_1']).toBe('done');
expect(byName['t5_ascension_1']).toBe('pending');
// Script's current_version_id and beta state land.
const after = getScript(script.id)!;
expect(after.current_version_id).toBeGreaterThan(0);
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({
scriptId: script.id,
scriptVersionId: v.id,
servers: ['t5_kino_1'],
waitForHealth: false,
waitForMatch: false,
triggerKind: 'mark-required'
});
// 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({
scriptId: script.id,
scriptVersionId: v.id,
servers: ['t5_kino_1'],
waitForHealth: false,
waitForMatch: false,
triggerKind: 'mark-required'
});
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_kino_1', 't5_ascension_1']));
// 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' });
});
});