Files
electric-cherry/tests/unit/system-status.test.ts
T

54 lines
2.1 KiB
TypeScript

import { afterEach, beforeEach, expect, it, vi } from 'vitest';
import { newFixture, type Fixture } from '../helpers/fixture.js';
let fixture: Fixture;
beforeEach(async () => {
vi.resetModules();
fixture = await newFixture([]);
vi.stubEnv('RCON_PASSWORD', 'fixture-rcon-secret');
});
afterEach(async () => {
await fixture.cleanup();
vi.unstubAllEnvs();
});
it('directs a fresh controller to host enrollment and verified game files', async () => {
const { GET } = await import('../../src/routes/api/system-status/+server.js');
const response = await GET();
const body = await response.json();
expect(response.headers.get('cache-control')).toBe('no-store');
expect(body.checks.map((check: { id: string }) => check.id)).toEqual([
'hosts',
'runtime',
'game-files',
'rcon',
'keys',
'first-server'
]);
expect(body.checks[0]).toMatchObject({ state: 'unconfigured', href: '/hosts' });
expect(body.checks[2].state).toBe('attention');
expect(body.readyServers).toBe(0);
expect(JSON.stringify(body)).not.toContain('fixture-rcon-secret');
});
it('reports an enrolled but disconnected host as needing attention without issuing commands', async () => {
const { createHost, createEnrollment, enrollHost } =
await import('../../src/lib/server/hosts/index.js');
const { db } = await import('../../src/lib/server/db/index.js');
const host = createHost({ name: 'Game host' });
const enrolled = enrollHost(createEnrollment(host.id).token, { protocolVersion: 1 });
db()
.prepare('UPDATE ec_hosts SET last_heartbeat=? WHERE id=?')
.run('2000-01-01T00:00:00Z', host.id);
const { GET } = await import('../../src/routes/api/system-status/+server.js');
const response = await GET();
const text = await response.text();
const body = JSON.parse(text);
expect(body.checks[0]).toMatchObject({
state: 'attention',
detail: expect.stringContaining('0 connected')
});
expect(text).not.toContain(enrolled.credential);
expect(db().prepare('SELECT id FROM ec_host_operations').all()).toEqual([]);
});