import { describe , expect , it } from 'bun:test' import { Elysia } from 'elysia' import { treaty } from '@elysiajs/eden' import app from '../app.ts' if (!process.env.WL_FIFO_PATH) throw new Error("WL_FIFO_PATH is missing in env."); if (!process.env.WL_CREDENTIALS) throw new Error("WL_CREDENTIALS is missing in env."); function getCredentialsFromEnv(envValue?: string): { username: string; password: string } { if (!envValue) throw new Error("WL_CREDENTIALS is not set"); const firstCredential = envValue.split(";")[0]; // Get the first username:password pair const [username, password] = firstCredential.split(":"); if (!username || !password) throw new Error("Invalid credentials format"); return { username, password }; } const { username, password } = getCredentialsFromEnv(process.env.WL_CREDENTIALS) const opts = { headers: { authorization: "Basic " + btoa(username + ':' + password) } } const api = treaty(app) describe ('Elysia', () => { it('return a health response', async () => { const { data, status } = await api.health.get() expect(status).toBe(200) expect(data).toBe("OK") }) it('return a version', async () => { const { data, status } = await api.version.get(opts) expect(status).toBe(200) expect(data).toContain("version") }) it('return a whitelist', async () => { const { data, status } = await api.whitelist.get(opts) expect(status).toBe(200) expect(data).toContain("07b4516336e4afe9232c73bc312642590a7d7e95") }) it('writes a new info_hash to a fifo', async () => { const fifoFilePath = process.env.WL_FIFO_PATH! const fifo = Bun.file(fifoFilePath) const fifoExists = await fifo.exists(); // create fifo if it doesn't exist if (!fifoExists) { await Bun.spawn(["mkfifo", fifoFilePath]).exited; } // Start a process to read from the FIFO const reader = Bun.spawn(["cat", fifoFilePath], { stdout: "pipe" }); const { data, status } = await api.whitelist.post("3aa5ad5e62eaffd148cff3dbe93ff2e1e9cbcf01", opts) const text = await new Response(reader.stdout).text(); expect(text).toBe("3aa5ad5e62eaffd148cff3dbe93ff2e1e9cbcf01\n") expect(status).toBe(200) expect(data).toContain("3aa5ad5e62eaffd148cff3dbe93ff2e1e9cbcf01") }) it('returns 401 when username/password is missing from GET /whitelist ', async () => { const { status } = await api.whitelist.get() expect(status).toBe(401) }) it('returns 401 when username/password is missing from POST /whitelist ', async () => { const { status } = await api.whitelist.post() expect(status).toBe(401) }) })