Something went wrong. Try again.
fossabot clone for streamplace superbot.mmatt.net
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { Db } from '../src/server/db/db.ts'import { Store } from '../src/server/db/store.ts'import { ChannelRuntime } from '../src/server/bot/runtime.ts'import type { Platform } from '../src/server/bot/platform.ts'import type { Deps } from '../src/server/http/context.ts'import { hookRoutes } from '../src/server/http/hooks.ts'
const CHANNEL = 'did:plc:streamer'
function setup() { const store = new Store(new Db(':memory:')) store.upsertChannel({ did: CHANNEL, handle: 'streamer.bsky.social', displayName: 'Streamer', avatar: '' }) store.setSingleton(CHANNEL, 'settings', { messageGapMs: 250 }) const sent: string[] = [] const platform = { botDid: () => 'did:plc:bot', canSend: () => true, send: async (_c: string, text: string) => { sent.push(text) return { uri: 'at://x/y/z', cid: 'c' } }, } as unknown as Platform const rt = new ChannelRuntime(store.getChannel(CHANNEL)!, store, platform) const manager = { runtimeFor: (id: string) => (id === CHANNEL ? rt : undefined) } const app = hookRoutes({ store, manager } as unknown as Deps) return { store, app, sent, rt }}
describe('API hooks', () => { beforeEach(() => { vi.stubGlobal('fetch', async () => { throw new Error('offline') }) }) afterEach(() => vi.unstubAllGlobals())
it('runs a command from a GET with the key in the URL (Stream Deck)', async () => { const { store, app, sent } = setup() const { key } = store.createApiKey(CHANNEL) const res = await app.request(`/command?key=${key}&command=${encodeURIComponent('!count deaths +1')}`) expect(res.status).toBe(200) expect(await res.json()).toMatchObject({ ok: true }) expect(store.counterGet(CHANNEL, 'count:deaths')).toBe(1) expect(sent).toEqual(['deaths is now 1.']) expect(store.getApiKey(CHANNEL)?.lastUsedAt).toBeTruthy() })
it('runs a command from a POST with a bearer token, as JSON or plain text', async () => { const { store, app } = setup() const { key } = store.createApiKey(CHANNEL) const json = await app.request('/command', { method: 'POST', headers: { authorization: `Bearer ${key}`, 'content-type': 'application/json' }, body: JSON.stringify({ command: '!count deaths +2' }) }) expect(json.status).toBe(200) const text = await app.request('/command', { method: 'POST', headers: { 'x-api-key': key, 'content-type': 'text/plain' }, body: '!count deaths +1' }) expect(text.status).toBe(200) expect(store.counterGet(CHANNEL, 'count:deaths')).toBe(3) })
it('rejects missing, wrong, replaced and revoked keys', async () => { const { store, app } = setup() expect((await app.request('/command?command=!count%20a%20%2B1')).status).toBe(401) expect((await app.request('/command?key=sb_nope&command=!count%20a%20%2B1')).status).toBe(401) const old = store.createApiKey(CHANNEL).key const current = store.createApiKey(CHANNEL).key expect((await app.request(`/command?key=${old}&command=!count%20a%20%2B1`)).status).toBe(401) expect((await app.request(`/command?key=${current}&command=!count%20a%20%2B1`)).status).toBe(200) store.deleteApiKey(CHANNEL) expect((await app.request(`/command?key=${current}&command=!count%20a%20%2B1`)).status).toBe(401) expect(store.counterGet(CHANNEL, 'count:a')).toBe(1) })
it('explains empty and unknown commands, and a bot that left the channel', async () => { const { store, app } = setup() const { key } = store.createApiKey(CHANNEL) expect((await app.request(`/command?key=${key}`)).status).toBe(400) const unknown = await app.request(`/command?key=${key}&command=!nope`) expect(unknown.status).toBe(404) expect((await unknown.json()).error).toContain('!nope') store.updateChannel(CHANNEL, { bot_joined: 0 }) expect((await app.request(`/command?key=${key}&command=!count%20a%20%2B1`)).status).toBe(409) })
it('rate limits each channel', async () => { const { store, app, rt } = setup() const { key } = store.createApiKey(CHANNEL) // Muted, so the test doesn't wait out the send gap 60 times. Counters still count. store.updateChannel(CHANNEL, { bot_muted: 1 }) rt.reload() const statuses: number[] = [] for (let i = 0; i < 61; i++) statuses.push((await app.request(`/command?key=${key}&command=!count%20a%20%2B1`)).status) expect(statuses.slice(0, 60).every((s) => s === 200)).toBe(true) expect(statuses[60]).toBe(429) expect(store.counterGet(CHANNEL, 'count:a')).toBe(60) })
it('stores only a hash of the key', () => { const { store } = setup() const { key } = store.createApiKey(CHANNEL) const row = store.db.get<{ hash: string; hint: string }>('SELECT hash, hint FROM api_keys WHERE channel_id = ?', CHANNEL)! expect(row.hash).not.toContain(key) expect(key.endsWith(row.hint)).toBe(true) store.deleteChannel(CHANNEL) expect(store.db.all('SELECT * FROM api_keys')).toEqual([]) })})