import Database from 'better-sqlite3'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import schema from '../../src/lib/server/db/schema.sql?raw'; import { newFixture, type Fixture } from '../helpers/fixture.js'; import type { LaunchRecipe } from '../../src/lib/server/fleet/launch-recipes.js'; let fixture: Fixture; beforeEach(async () => { vi.resetModules(); fixture = await newFixture([]); }); afterEach(async () => { vi.restoreAllMocks(); await fixture.cleanup(); }); describe('persisted launch recipe summaries', () => { it('lazily upgrades legacy snapshots once without changing fingerprints or retained bytes', async () => { const recipe: LaunchRecipe = { format: 1, game: 't5', mapSlug: 'theater', mapName: 'Kino der Toten', expectedMap: 'zombie_theater', capturedAt: '2025-01-01T00:00:00.000Z', image: 'plutainer:test', configFile: 'dedicated_sp.cfg', configSource: 'Defaults', files: [{ name: 'Old plugin', kind: 'plugin', path: 'legacy.dll', versionId: 1, version: 1, sha256: 'a'.repeat(64), size: 2 * 1024 ** 2, content: Buffer.alloc(2 * 1024 ** 2, 42).toString('base64') }] }; const legacy = new Database(fixture.dbPath); const encoded = JSON.stringify(recipe, null, 2); try { legacy.exec(schema.replace(' recipe_summary TEXT,\n', '')); legacy.exec(`INSERT INTO server_deployments(id,server,action,state,stage,started_at,triggered_by) VALUES('legacy','retired-server','create','done','Complete','2025-01-01','fixture');`); legacy.prepare('INSERT INTO server_launch_recipes(deployment_id,recipe,blueprint_name) VALUES(?,?,?)') .run('legacy', encoded, 'Old collection'); } finally { legacy.close(); } const { db, closeDb } = await import('../../src/lib/server/db/index.js'); const { recipeSummaryForDeployment, recipeForDeployment, summarizeRecipe } = await import('../../src/lib/server/fleet/launch-recipes.js'); const expected = { recipe: summarizeRecipe(recipe), blueprintName: 'Old collection' }; expect(db().prepare('SELECT recipe_summary FROM server_launch_recipes').get()).toEqual({ recipe_summary: null }); expect(recipeSummaryForDeployment('legacy')).toEqual(expected); closeDb(); const parse = JSON.parse; const parsing = vi.spyOn(JSON, 'parse').mockImplementation((value, reviver) => { if (value.length > 1024 * 1024) throw new Error('Legacy recipe was decoded again.'); return parse(value, reviver); }); try { expect(recipeSummaryForDeployment('legacy')).toEqual(expected); expect(recipeSummaryForDeployment('missing')).toBeNull(); } finally { parsing.mockRestore(); } expect(recipeForDeployment('legacy')).toEqual({ recipe, blueprintName: 'Old collection' }); expect(db().prepare('SELECT recipe FROM server_launch_recipes').get()).toEqual({ recipe: encoded }); }); });