1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
import { describe, test, expect, beforeAll } from "bun:test";
import app from "$lib/app";
import { auth, optional } from "$lib/auth";
import users from "$routes/users";
import info from "$routes/info";
import invoices from "$routes/invoices";
// Register routes (subset of index.ts — no side effects, no Bun.serve)
app.get("/health", info.health);
app.post("/register", users.create);
app.post("/login", users.login);
app.get("/me", auth, users.me);
app.get("/users/:key", users.get);
app.post("/invoice", optional, invoices.create);
app.get("/invoice/:id", invoices.get);
const post = (path: string, body: any) =>
app.request(path, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
// Use unique username to avoid collisions with other test files
const USERNAME = `apiuser${Date.now()}`;
const PASSWORD = "testpass123";
beforeAll(() => {
// Clear any leftover state from other test files
const store = globalThis.__testStore;
for (const k of Object.keys(store.kvStore)) delete store.kvStore[k];
for (const k of Object.keys(store.listStore)) delete store.listStore[k];
for (const k of Object.keys(store.setStore)) delete store.setStore[k];
});
describe("Health", () => {
test("GET /health returns 200", async () => {
const res = await app.request("/health");
expect(res.status).toBe(200);
const body = await res.json();
expect(body.healthy).toBe(true);
});
});
describe("Registration", () => {
test("creates user with valid credentials", async () => {
const res = await post("/register", {
user: { username: USERNAME, password: PASSWORD },
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.token).toBeDefined();
expect(body.username).toBe(USERNAME);
});
test("rejects duplicate username", async () => {
const res = await post("/register", {
user: { username: USERNAME, password: PASSWORD },
});
expect(res.status).toBe(500);
});
test("rejects missing user object", async () => {
const res = await post("/register", { password: PASSWORD });
expect(res.status).toBe(500);
});
test("rejects invalid username", async () => {
const res = await post("/register", {
user: { username: "ab!@#", password: PASSWORD },
});
expect(res.status).toBe(500);
});
});
describe("Login", () => {
test("succeeds with valid credentials", async () => {
const res = await post("/login", {
username: USERNAME,
password: PASSWORD,
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.token).toBeDefined();
expect(body.user).toBeDefined();
});
test("fails with wrong password", async () => {
const res = await post("/login", {
username: USERNAME,
password: "wrongpass",
});
expect(res.status).toBe(401);
});
test("fails with nonexistent user", async () => {
const res = await post("/login", {
username: "noone",
password: PASSWORD,
});
expect(res.status).toBe(401);
});
});
describe("Authenticated endpoints", () => {
test("GET /me returns user data", async () => {
const res = await app.request("/me");
expect(res.status).toBe(200);
const body = await res.json();
expect(body.username).toBeDefined();
});
});
describe("User lookup", () => {
test("GET /users/:key finds registered user", async () => {
const res = await app.request(`/users/${USERNAME}`);
expect(res.status).toBe(200);
});
});
describe("Routing", () => {
test("unknown route returns 404", async () => {
const res = await app.request("/nonexistent");
expect(res.status).toBe(404);
});
});
|