Blame · Line-by-line history
api.test.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| fc1817a | 1 | import { describe, it, expect, beforeAll, afterAll } from "bun:test"; |
| 2 | import { join } from "path"; | |
| 3 | import { rm, mkdir } from "fs/promises"; | |
| 4 | import app from "../app"; | |
| 5 | ||
| 6 | const TEST_REPOS = join(import.meta.dir, "../../.test-repos-api"); | |
| 7 | ||
| 8 | beforeAll(async () => { | |
| 9 | process.env.GIT_REPOS_PATH = TEST_REPOS; | |
| 10 | process.env.DATABASE_URL = process.env.DATABASE_URL || ""; | |
| 11 | await mkdir(TEST_REPOS, { recursive: true }); | |
| 12 | }); | |
| 13 | ||
| 14 | afterAll(async () => { | |
| 15 | await rm(TEST_REPOS, { recursive: true, force: true }); | |
| 16 | }); | |
| 17 | ||
| 18 | describe("API routes", () => { | |
| 19 | it("GET / returns home page", async () => { | |
| 20 | const res = await app.request("/"); | |
| 21 | expect(res.status).toBe(200); | |
| 22 | const text = await res.text(); | |
| 23 | expect(text).toContain("gluecron"); | |
| 24 | }); | |
| 25 | ||
| 26 | it("GET /api/repos/:owner/:name returns 404 for missing repo", async () => { | |
| 27 | // This will fail without DB, but verifies route exists | |
| 28 | const res = await app.request("/api/repos/nobody/nothing"); | |
| 29 | // Without DB connection, this returns 500 or 404 | |
| 30 | expect([404, 500]).toContain(res.status); | |
| 31 | }); | |
| 32 | ||
| 33 | it("POST /api/repos returns 400 without required fields", async () => { | |
| 34 | const res = await app.request("/api/repos", { | |
| 35 | method: "POST", | |
| 36 | headers: { "Content-Type": "application/json" }, | |
| 37 | body: JSON.stringify({}), | |
| 38 | }); | |
| 39 | // Without DB, might be 400 or 500 | |
| 40 | expect([400, 500]).toContain(res.status); | |
| 41 | }); | |
| 42 | }); | |
| 43 | ||
| 44 | describe("Git HTTP routes", () => { | |
| 45 | it("returns 400 for invalid service", async () => { | |
| 46 | const res = await app.request("/test/repo.git/info/refs?service=invalid"); | |
| 47 | expect(res.status).toBe(400); | |
| 48 | }); | |
| 49 | ||
| 50 | it("returns 404 for non-existent repo", async () => { | |
| 51 | const res = await app.request( | |
| 52 | "/nobody/nothing.git/info/refs?service=git-upload-pack" | |
| 53 | ); | |
| 54 | expect(res.status).toBe(404); | |
| 55 | }); | |
| 56 | }); |