|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { Hono } from "hono"; |
| 3 | + |
| 4 | +process.env.DATABASE_URL = ":memory:"; |
| 5 | +const { dropRouter } = await import("../src/routes/drop"); |
| 6 | +const { queries } = await import("../src/services/db"); |
| 7 | + |
| 8 | +const app = new Hono(); |
| 9 | +app.route("/api", dropRouter); |
| 10 | + |
| 11 | +// Seed data |
| 12 | +queries.insertDrop.run({ |
| 13 | + $id: "stats-1", $rootHash: "0xs1", $fileName: "a.txt", |
| 14 | + $fileSize: 1000, $mimeType: "text/plain", $passwordHash: null, |
| 15 | + $maxDownloads: null, $expiresAt: null, $ipAddress: null, |
| 16 | +}); |
| 17 | +queries.insertDrop.run({ |
| 18 | + $id: "stats-2", $rootHash: "0xs2", $fileName: "b.txt", |
| 19 | + $fileSize: 2000, $mimeType: "text/plain", $passwordHash: null, |
| 20 | + $maxDownloads: null, $expiresAt: null, $ipAddress: null, |
| 21 | +}); |
| 22 | +queries.incrementDownloads.run("stats-1"); |
| 23 | +queries.incrementDownloads.run("stats-1"); |
| 24 | +queries.incrementDownloads.run("stats-2"); |
| 25 | + |
| 26 | +describe("GET /api/stats", () => { |
| 27 | + test("returns aggregate statistics", async () => { |
| 28 | + const res = await app.request("/api/stats"); |
| 29 | + expect(res.status).toBe(200); |
| 30 | + const json = await res.json(); |
| 31 | + expect(json.totalDrops).toBeGreaterThanOrEqual(2); |
| 32 | + expect(json.totalSize).toBeGreaterThanOrEqual(3000); |
| 33 | + expect(json.totalDownloads).toBeGreaterThanOrEqual(3); |
| 34 | + }); |
| 35 | + |
| 36 | + test("returns numeric values", async () => { |
| 37 | + const json = await (await app.request("/api/stats")).json(); |
| 38 | + expect(typeof json.totalDrops).toBe("number"); |
| 39 | + expect(typeof json.totalSize).toBe("number"); |
| 40 | + expect(typeof json.totalDownloads).toBe("number"); |
| 41 | + }); |
| 42 | +}); |
0 commit comments