From b2ec01daf5b97affbe19639fb11f2dfde3238c17 Mon Sep 17 00:00:00 2001 From: josh Date: Fri, 18 Sep 2026 08:08:17 -0400 Subject: [PATCH] Fix scope restoration for promoted scripts --- src/lib/server/fleet/beta.ts | 3 +- src/lib/server/fleet/scripts.ts | 22 ++--- src/routes/api/scripts/[id]/scope/+server.ts | 4 +- tests/unit/script-flow.test.ts | 84 ++++++++++++++++++++ 4 files changed, 100 insertions(+), 13 deletions(-) diff --git a/src/lib/server/fleet/beta.ts b/src/lib/server/fleet/beta.ts index c69ffc0..52dace0 100644 --- a/src/lib/server/fleet/beta.ts +++ b/src/lib/server/fleet/beta.ts @@ -23,6 +23,7 @@ import { listPromotedOverlaysForScope, listVersions, serversMatchingScope, + setScope, type ScopeType, type ScriptRow, type ScriptVersionRow @@ -1089,6 +1090,7 @@ export function latestVersionForScript(script: ScriptRow): ScriptVersionRow | nu export function unsetScopeAndDetach(scriptId: number): string[] { const script = getScript(scriptId); if (!script) throw new BetaError(`no such script (id=${scriptId})`, 'no-such-script'); + setScope({ scriptId, scopeType: null, scopeValue: null }); const detached: string[] = []; if (script.kind === 'script' || script.kind === 'plugin') { @@ -1104,6 +1106,5 @@ export function unsetScopeAndDetach(scriptId: number): string[] { detached.push(server); } } - db().prepare('UPDATE scripts SET scope_type = NULL, scope_value = NULL WHERE id = ?').run(scriptId); return detached; } diff --git a/src/lib/server/fleet/scripts.ts b/src/lib/server/fleet/scripts.ts index b3208a8..921ff54 100644 --- a/src/lib/server/fleet/scripts.ts +++ b/src/lib/server/fleet/scripts.ts @@ -57,7 +57,6 @@ export class ScriptError extends Error { | 'path-empty' | 'no-such-script' | 'no-such-version' - | 'scope-requires-beta' | 'invalid-scope' | 'in-use-current' | 'in-use-beta' @@ -290,21 +289,24 @@ export interface SetScopeInput { scopeValue: string | null; } -// Setting or changing a scope while the script already has a promoted version -// requires a beta first (per plan §"Beta trigger"). Un-scoping is free. +// Scope edits preserve the promoted version and do not deploy files. An active +// beta must finish first because promotion reads the script's current scope. export function setScope(input: SetScopeInput): ScriptRow { const script = getScript(input.scriptId); if (!script) throw new ScriptError(`no such script (id=${input.scriptId})`, 'no-such-script'); validateScope(input.scopeType, input.scopeValue); - const changed = - input.scopeType !== script.scope_type || (input.scopeValue ?? null) !== (script.scope_value ?? null); - const isSetOrChange = input.scopeType !== null && changed; - - if (isSetOrChange && script.current_version_id !== null) { + const activeBeta = db() + .prepare( + `SELECT b.id FROM betas b + JOIN beta_scripts bs ON bs.beta_id = b.id + WHERE bs.script_id = ? AND b.state IN ('running','ready-promote') LIMIT 1` + ) + .get(input.scriptId) as { id: number } | undefined; + if (activeBeta) { throw new ScriptError( - 'setting or changing scope requires opening a beta first', - 'scope-requires-beta' + `rollback or promote beta ${activeBeta.id} before changing scope`, + 'has-active-beta' ); } diff --git a/src/routes/api/scripts/[id]/scope/+server.ts b/src/routes/api/scripts/[id]/scope/+server.ts index c514490..3b138b7 100644 --- a/src/routes/api/scripts/[id]/scope/+server.ts +++ b/src/routes/api/scripts/[id]/scope/+server.ts @@ -8,8 +8,8 @@ function parseId(raw: string): number | null { } // POST body: { scopeType: 'game' | 'map' | null, scopeValue?: string } -// Setting/changing scope on a script with a current_version_id returns a -// 409 with kind='scope-requires-beta'. Un-scoping detaches inventory so the +// Scope edits preserve the promoted version; changes during an active beta +// return 409 with kind='has-active-beta'. Un-scoping detaches inventory so the // orphaned files show up as drift on the deployments page for the operator // to reconcile manually — the same flow as any other drift. export async function POST({ params, request }: { params: { id: string }; request: Request }) { diff --git a/tests/unit/script-flow.test.ts b/tests/unit/script-flow.test.ts index e29a8ac..844c843 100644 --- a/tests/unit/script-flow.test.ts +++ b/tests/unit/script-flow.test.ts @@ -34,6 +34,90 @@ describe('scope options', () => { }); }); +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 + }); + 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); + return promoteBeta(betaId); + } + 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 + }); + 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();