Fix scope restoration for promoted scripts

This commit is contained in:
2026-09-18 08:08:17 -04:00
parent 3c49437bf9
commit b2ec01daf5
4 changed files with 100 additions and 13 deletions
+2 -1
View File
@@ -23,6 +23,7 @@ import {
listPromotedOverlaysForScope, listPromotedOverlaysForScope,
listVersions, listVersions,
serversMatchingScope, serversMatchingScope,
setScope,
type ScopeType, type ScopeType,
type ScriptRow, type ScriptRow,
type ScriptVersionRow type ScriptVersionRow
@@ -1089,6 +1090,7 @@ export function latestVersionForScript(script: ScriptRow): ScriptVersionRow | nu
export function unsetScopeAndDetach(scriptId: number): string[] { export function unsetScopeAndDetach(scriptId: number): string[] {
const script = getScript(scriptId); const script = getScript(scriptId);
if (!script) throw new BetaError(`no such script (id=${scriptId})`, 'no-such-script'); if (!script) throw new BetaError(`no such script (id=${scriptId})`, 'no-such-script');
setScope({ scriptId, scopeType: null, scopeValue: null });
const detached: string[] = []; const detached: string[] = [];
if (script.kind === 'script' || script.kind === 'plugin') { if (script.kind === 'script' || script.kind === 'plugin') {
@@ -1104,6 +1106,5 @@ export function unsetScopeAndDetach(scriptId: number): string[] {
detached.push(server); detached.push(server);
} }
} }
db().prepare('UPDATE scripts SET scope_type = NULL, scope_value = NULL WHERE id = ?').run(scriptId);
return detached; return detached;
} }
+12 -10
View File
@@ -57,7 +57,6 @@ export class ScriptError extends Error {
| 'path-empty' | 'path-empty'
| 'no-such-script' | 'no-such-script'
| 'no-such-version' | 'no-such-version'
| 'scope-requires-beta'
| 'invalid-scope' | 'invalid-scope'
| 'in-use-current' | 'in-use-current'
| 'in-use-beta' | 'in-use-beta'
@@ -290,21 +289,24 @@ export interface SetScopeInput {
scopeValue: string | null; scopeValue: string | null;
} }
// Setting or changing a scope while the script already has a promoted version // Scope edits preserve the promoted version and do not deploy files. An active
// requires a beta first (per plan §"Beta trigger"). Un-scoping is free. // beta must finish first because promotion reads the script's current scope.
export function setScope(input: SetScopeInput): ScriptRow { export function setScope(input: SetScopeInput): ScriptRow {
const script = getScript(input.scriptId); const script = getScript(input.scriptId);
if (!script) throw new ScriptError(`no such script (id=${input.scriptId})`, 'no-such-script'); if (!script) throw new ScriptError(`no such script (id=${input.scriptId})`, 'no-such-script');
validateScope(input.scopeType, input.scopeValue); validateScope(input.scopeType, input.scopeValue);
const changed = const activeBeta = db()
input.scopeType !== script.scope_type || (input.scopeValue ?? null) !== (script.scope_value ?? null); .prepare(
const isSetOrChange = input.scopeType !== null && changed; `SELECT b.id FROM betas b
JOIN beta_scripts bs ON bs.beta_id = b.id
if (isSetOrChange && script.current_version_id !== null) { 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( throw new ScriptError(
'setting or changing scope requires opening a beta first', `rollback or promote beta ${activeBeta.id} before changing scope`,
'scope-requires-beta' 'has-active-beta'
); );
} }
+2 -2
View File
@@ -8,8 +8,8 @@ function parseId(raw: string): number | null {
} }
// POST body: { scopeType: 'game' | 'map' | null, scopeValue?: string } // POST body: { scopeType: 'game' | 'map' | null, scopeValue?: string }
// Setting/changing scope on a script with a current_version_id returns a // Scope edits preserve the promoted version; changes during an active beta
// 409 with kind='scope-requires-beta'. Un-scoping detaches inventory so the // 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 // orphaned files show up as drift on the deployments page for the operator
// to reconcile manually — the same flow as any other drift. // to reconcile manually — the same flow as any other drift.
export async function POST({ params, request }: { params: { id: string }; request: Request }) { export async function POST({ params, request }: { params: { id: string }; request: Request }) {
+84
View File
@@ -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', () => { describe('promote creates a deployment', () => {
it('records deployment_files for every landed server and marks beta servers done, others pending', async () => { it('records deployment_files for every landed server and marks beta servers done, others pending', async () => {
await seedFleet(); await seedFleet();