Fix scope restoration for promoted scripts
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user