Files
minabot/src/discord/registration.test.ts
2026-09-08 08:17:26 +02:00

19 lines
1.4 KiB
TypeScript

import { expect, test } from 'bun:test';
import { registerCommands } from './registration';
const command = { name: 'today', description: 'Today', type: 1, contexts: [0, 1], integration_types: [0] };
test('registers global commands individually without removing other application commands', async () => {
const calls: { url: string; init?: RequestInit }[] = [];
expect(await registerCommands('111111111111111111', 'fake-token', [command], async (url, init) => { calls.push({ url, init }); return Response.json({}); })).toEqual(['today']);
expect(calls[0]!.url).toBe('https://discord.com/api/v10/applications/111111111111111111/commands');
expect(calls[0]!.init!.method).toBe('POST');
expect(JSON.parse(calls[0]!.init!.body as string).contexts).toEqual([0, 1]);
});
test('rejects missing DM support before sending any request', async () => {
let called = false;
await expect(registerCommands('111111111111111111', 'fake-token', [{ ...command, contexts: [0] }], async () => { called = true; return Response.json({}); })).rejects.toThrow('bot DMs');
expect(called).toBe(false);
});
test('stops on rate limits and does not expose response bodies', async () => {
await expect(registerCommands('111111111111111111', 'fake-token', [command], async () => new Response('private provider data', { status: 429, headers: { 'Retry-After': '5' } }))).rejects.toThrow('HTTP 429');
});