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([]); }); afterEach(async () => { await fixture.cleanup(); }); describe('host detail instances', () => { it('returns only current instances belonging to the requested host and refreshes their health', async () => { const { createHost } = await import('../../src/lib/server/hosts/index.js'); const host = createHost({ name: 'Selected host' }); const other = createHost({ name: 'Other host' }); const { db } = await import('../../src/lib/server/db/index.js'); const now = new Date().toISOString(); const add = (name: string, hostId: string, retired = false, destroyed = false) => { db().prepare(`INSERT INTO native_instances (id, server, host_id, generation, library_id, image, port, created_at, retired_at) VALUES (?, ?, ?, 1, 'library', 'runtime:test', 28960, ?, ?)`) .run(name, name, hostId, now, retired ? now : null); db().prepare(`INSERT INTO servers (name, komodo_name, container_state, container_checked_at, last_seen_at, last_players, destroyed_at) VALUES (?, ?, 'running', ?, ?, 2, ?)`) .run(name, name, now, now, destroyed ? now : null); }; add('running', host.id); add('unavailable', host.id); add('other-host', other.id); add('retired', host.id, true); add('destroyed', host.id, false, true); db().prepare(`INSERT INTO native_instances (id, server, host_id, library_id, image, port, created_at, retired_at) VALUES ('old-placement', 'other-host', ?, 'library', 'runtime:test', 28960, ?, ?)`) .run(host.id, now, now); db().prepare("UPDATE servers SET container_state='exited' WHERE name='unavailable'").run(); const { GET } = await import('../../src/routes/api/hosts/[id]/+server.js'); const read = async (id: string) => (await GET({ request: new Request(`https://ec.test/api/hosts/${id}`), params: { id } }).json()); const body = await read(host.id); expect(body.instances.map((instance: { name: string }) => instance.name)).toEqual(['running', 'unavailable']); expect(body.instances[0]).toMatchObject({ health: 'online', last_players: 2 }); expect(body.instances[1]).toMatchObject({ health: 'offline', last_players: null }); expect(body.serverNow).toEqual(expect.any(Number)); db().prepare("UPDATE servers SET last_players=0 WHERE name='running'").run(); expect((await read(host.id)).instances[0].last_players).toBe(0); expect((await read(other.id)).instances.map((instance: { name: string }) => instance.name)).toEqual(['other-host']); expect((await read(createHost({ name: 'Empty host' }).id)).instances).toEqual([]); }); });