Files
STCourses/apps/server/test/server.test.ts
T
2026-10-30 03:05:49 +01:00

37 lines
1.3 KiB
TypeScript

import { afterAll, beforeAll, expect, test } from "bun:test";
import { resolve } from "node:path";
import { loadConfig } from "@stc/core";
import { startServer } from "../src/main";
let s: Awaited<ReturnType<typeof startServer>>;
let base: string;
beforeAll(async () => {
const config = loadConfig({
STC_PORT: "0", STC_DATABASE_URL: "sqlite://:memory:", STC_SECRET: "t",
STC_COURSE_DIRS: resolve(import.meta.dir, "../../../fixtures/courses"),
});
s = await startServer(config, []);
base = s.server.url.toString().replace(/\/$/, "");
});
afterAll(() => s.stop());
test("health reports the fixture course", async () => {
expect(await (await fetch(`${base}/api/health`)).json()).toEqual({ ok: true, courses: 1, db: "sqlite" });
});
test("SPA paths return the HTML shell", async () => {
const r = await fetch(`${base}/some/spa/path`);
expect(r.status).toBe(200);
expect(await r.text()).toContain('<div id="root">');
});
test("unknown API paths are 404, not the SPA", async () => {
expect((await fetch(`${base}/api/nope`)).status).toBe(404);
});
test("media honours Range", async () => {
const r = await fetch(`${base}/api/media/sample/media/clip.mp4`, { headers: { Range: "bytes=0-9" } });
expect(r.status).toBe(206);
expect((await r.arrayBuffer()).byteLength).toBe(10);
});