import test from 'node:test'; import assert from 'node:assert/strict'; import { PufferApi } from '../api.js'; import { validateConfig } from '../config.js'; const response = (status, body) => new Response(body === undefined ? null : JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } }); test('API authenticates, caches token, checks status, starts and stops', async () => { const requests = []; const api = new PufferApi({ baseUrl: 'https://panel.example', clientId: 'id', clientSecret: 'secret', fetchImpl: async (url, options) => { requests.push([url, options]); if (url.endsWith('/oauth2/token')) return response(200, { access_token: 'token', expires_in: 3600 }); if (url.endsWith('/status')) return response(200, { running: false, installing: false }); return response(202); } }); assert.equal(await api.status('valheim1'), false); await api.start('valheim1'); await api.stop('valheim1'); assert.equal(requests.filter(([url]) => url.endsWith('/oauth2/token')).length, 1); assert.equal(requests[1][1].headers.authorization, 'Bearer token'); assert.equal(requests[2][0], 'https://panel.example/api/servers/valheim1/start'); assert.equal(requests[3][0], 'https://panel.example/api/servers/valheim1/stop'); }); test('API refreshes rejected token and retries transient failures', async () => { let count = 0; const api = new PufferApi({ baseUrl: 'https://panel.example', clientId: 'id', clientSecret: 'secret', retries: 2, fetchImpl: async url => { count += 1; if (url.endsWith('/oauth2/token')) return response(200, { access_token: `token-${count}`, expires_in: 3600 }); if (count === 2) return response(401); if (count === 4) throw new Error('temporary network failure'); return response(200, { running: true }); } }); assert.equal(await api.status('game'), true); assert.ok(count >= 5); }); test('stop does not dispatch after token acquisition loses validity', async () => { let resolveToken; const requests = []; let valid = true; const api = new PufferApi({ baseUrl: 'https://panel.example', clientId: 'id', clientSecret: 'secret', fetchImpl: (url, options) => { requests.push([url, options]); if (url.endsWith('/oauth2/token')) return new Promise(resolve => { resolveToken = resolve; }); return response(202); } }); const pending = api.stop('game', () => valid); await Promise.resolve(); valid = false; resolveToken(response(200, { access_token: 'token', expires_in: 3600 })); await pending; assert.equal(requests.length, 1); }); test('stop retry does not dispatch after validity changes', async () => { let valid = true; let posts = 0; const api = new PufferApi({ baseUrl: 'https://panel.example', clientId: 'id', clientSecret: 'secret', retries: 2, fetchImpl: async url => { if (url.endsWith('/oauth2/token')) return response(200, { access_token: 'token', expires_in: 3600 }); posts += 1; valid = false; throw new Error('temporary failure'); } }); await api.stop('game', () => valid); assert.equal(posts, 1); }); test('start does not dispatch after token acquisition loses validity', async () => { let resolveToken; let valid = true; const requests = []; const api = new PufferApi({ baseUrl: 'https://panel.example', clientId: 'id', clientSecret: 'secret', fetchImpl: (url, options) => { requests.push([url, options]); if (url.endsWith('/oauth2/token')) return new Promise(resolve => { resolveToken = resolve; }); return response(202); } }); const pending = api.start('game', () => valid); await Promise.resolve(); valid = false; resolveToken(response(200, { access_token: 'token', expires_in: 3600 })); await pending; assert.equal(requests.length, 1); }); test('API decodes base64 console bytes and sends microsecond cursor unchanged', async () => { const api = new PufferApi({ baseUrl: 'https://panel.example', clientId: 'id', clientSecret: 'secret', fetchImpl: async url => { if (url.endsWith('/oauth2/token')) return response(200, { access_token: 'token', expires_in: 3600 }); assert.match(url, /console\?time=1234567$/); return response(200, { epoch: 1234568, logs: Buffer.from('ready\n').toString('base64') }); } }); const console = await api.console('game', 1234567); assert.equal(console.epoch, 1234568); assert.equal(console.logs.toString(), 'ready\n'); }); test('config rejects self target, unsafe panel URL, duplicate server mapping', () => { const base = { discordToken: 'bot', panelUrl: 'https://games.bugabinga.net', mappings: [{ guildId: '1', voiceChannelId: '2', serverId: 'game', oauthClientId: 'x', oauthClientSecret: 'y' }] }; assert.throws(() => validateConfig({ ...base, mappings: [{ ...base.mappings[0], serverId: '61b39066' }] }), /forbidden/); assert.throws(() => validateConfig({ ...base, panelUrl: 'http://panel.example' }), /HTTPS/); assert.throws(() => validateConfig({ ...base, mappings: [...base.mappings, { ...base.mappings[0], voiceChannelId: '3' }] }), /duplicate PufferPanel server/); const mapping = validateConfig({ ...base, mappings: [{ ...base.mappings[0], displayName: 'Game', statusChannelId: '3', controlRoleName: 'Operators' }] }).mappings[0]; assert.deepEqual({ displayName: mapping.displayName, statusChannelId: mapping.statusChannelId, controlRoleName: mapping.controlRoleName }, { displayName: 'Game', statusChannelId: '3', controlRoleName: 'Operators' }); });