import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { newFixture, type Fixture } from '../helpers/fixture.js'; let fixture: Fixture; beforeEach(async () => { vi.resetModules(); fixture = await newFixture(['t5_zombie_theater_1', 't5_zombie_cosmodrome_1']); // Provisioning must retain queued work while integrations are unset. delete process.env.KOMODO_ADDRESS; delete process.env.RCON_PASSWORD; }); afterEach(async () => { await fixture.cleanup(); }); describe('sweep', () => { it('emits server state only when state, players, or error changes', async () => { const { sweep } = await import('../../src/lib/server/pipeline/sweep.js'); const { seedServerRegistry } = await import('../../src/lib/server/fleet/servers.js'); const { db } = await import('../../src/lib/server/db/index.js'); const { subscribe } = await import('../../src/lib/server/pipeline/bus.js'); await seedServerRegistry(); db().prepare("INSERT INTO deployments (id, action, started_at, triggered_by) VALUES ('changes', 'test', ?, 'ui')") .run(new Date().toISOString()); db().prepare(`INSERT INTO deployment_servers (deployment_id, server, files_changed, restart_state) VALUES ('changes', 't5_zombie_theater_1', 1, 'pending')`).run(); const events = vi.fn(); const unsubscribe = subscribe(event => { if (event.type === 'server-state') events(event); }); try { await sweep(); expect(events).toHaveBeenCalledTimes(1); await sweep(); expect(events).toHaveBeenCalledTimes(1); // Fresh check timestamps still update the campaign, without an SSE event. const checkedAt = new Date().toISOString(); db().prepare('UPDATE server_deployments SET checked_at = ?').run(checkedAt); await sweep(); expect(events).toHaveBeenCalledTimes(1); expect(db().prepare("SELECT checked_at, attempts FROM deployment_servers WHERE deployment_id = 'changes'").get()) .toEqual({ checked_at: checkedAt, attempts: 1 }); db().prepare('UPDATE server_deployments SET players = 2').run(); await sweep(); expect(events).toHaveBeenCalledTimes(2); expect(events).toHaveBeenLastCalledWith(expect.objectContaining({ state: 'pending', players: 2 })); db().prepare("UPDATE server_deployments SET error = 'Waiting for players'").run(); await sweep(); expect(events).toHaveBeenCalledTimes(3); expect(events).toHaveBeenLastCalledWith(expect.objectContaining({ error: 'Waiting for players' })); db().prepare("UPDATE server_deployments SET state = 'failed'").run(); await sweep(); expect(events).toHaveBeenCalledTimes(4); expect(events).toHaveBeenLastCalledWith(expect.objectContaining({ state: 'failed' })); await sweep(); expect(events).toHaveBeenCalledTimes(4); db().prepare("UPDATE server_deployments SET state = 'done'").run(); await sweep(); expect(events).toHaveBeenCalledTimes(5); expect(events).toHaveBeenLastCalledWith(expect.objectContaining({ state: 'done', error: null })); } finally { unsubscribe(); } }); it('queues pending rows in Provisioning even when integrations are unset', async () => { const { sweep } = await import('../../src/lib/server/pipeline/sweep.js'); const { seedServerRegistry } = await import('../../src/lib/server/fleet/servers.js'); const { db } = await import('../../src/lib/server/db/index.js'); await seedServerRegistry(); const depId = 'dep-test-001'; db().prepare("INSERT INTO deployments (id, action, started_at, triggered_by) VALUES (?, 'test', ?, 'ui')") .run(depId, new Date().toISOString()); db() .prepare( "INSERT INTO deployment_servers (deployment_id, server, files_changed, restart_state, attempts) VALUES (?, ?, 1, 'pending', 0)" ) .run(depId, 't5_zombie_theater_1'); db() .prepare( "INSERT INTO deployment_servers (deployment_id, server, files_changed, restart_state, attempts) VALUES (?, ?, 1, 'pending', 0)" ) .run(depId, 't5_zombie_cosmodrome_1'); await sweep(); const { processProvisioning } = await import('../../src/lib/server/provisioning/worker.js'); await processProvisioning(); await sweep(); const rows = db() .prepare("SELECT restart_state FROM deployment_servers WHERE deployment_id = ?") .all(depId) as Array<{ restart_state: string }>; expect(rows.length).toBe(2); for (const r of rows) expect(r.restart_state).toBe('pending'); const operations = db().prepare('SELECT state, stage FROM server_deployments').all(); expect(operations).toHaveLength(2); for (const operation of operations) expect(operation).toEqual({ state: 'waiting', stage: 'Waiting for Komodo and RCON configuration' }); }); it('is a no-op when nothing is pending', async () => { const { sweep } = await import('../../src/lib/server/pipeline/sweep.js'); const { seedServerRegistry } = await import('../../src/lib/server/fleet/servers.js'); await seedServerRegistry(); const result = await sweep(); expect(result).toEqual({ checked: 0, restarted: 0, healthy: 0, held: 0, failed: 0 }); }); });