/** * Tests serving the built client alongside the server routes: static files, * the index.html fallback for client-side routes, and that server routes are * never answered with index.html. */ import { Controller, Get, INestApplication, Module, Post, RequestMethod, } from "@nestjs/common"; import { ServeStaticModule } from "@nestjs/serve-static"; import { NestFactory } from "@nestjs/core"; import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import request from "supertest"; import { clientStaticOptions } from "../src/common/client-static"; @Controller("things") class ThingsController { @Get() list() { return [{ id: 1 }]; } } @Controller() class RootRoutesController { @Get("health") health() { return { status: "ok" }; } @Get("manifest.json") manifest() { return { version: "1.0.0" }; } @Post("mcp") mcp() { return { jsonrpc: "2.0" }; } } async function buildApp(clientDir: string): Promise { // NestFactory (as in main.ts) rather than Test.createTestingModule: the // serve-static module picks its HTTP loader while the module compiles, // which needs the HTTP adapter to exist already. @Module({ imports: [ServeStaticModule.forRoot(...clientStaticOptions(clientDir))], controllers: [ThingsController, RootRoutesController], }) class ClientStaticTestModule {} const app = await NestFactory.create(ClientStaticTestModule, { logger: false, }); // Same prefix setup as main.ts app.setGlobalPrefix("api/v1", { exclude: [ { path: "health", method: RequestMethod.ALL }, { path: "mcp", method: RequestMethod.ALL }, { path: "manifest.json", method: RequestMethod.ALL }, ], }); await app.init(); return app; } describe("Client static serving", () => { let clientDir: string; let app: INestApplication; beforeAll(async () => { clientDir = fs.mkdtempSync(path.join(os.tmpdir(), "liqa-client-")); fs.mkdirSync(path.join(clientDir, "assets")); fs.writeFileSync(path.join(clientDir, "index.html"), "liqa"); fs.writeFileSync( path.join(clientDir, "assets", "app-abc123.js"), "console.log(1);", ); app = await buildApp(clientDir); }); afterAll(async () => { await app.close(); fs.rmSync(clientDir, { recursive: true, force: true }); }); it("serves index.html at / without caching", async () => { const res = await request(app.getHttpServer()).get("/").expect(200); expect(res.text).toBe("liqa"); expect(res.headers["cache-control"]).toBe("no-cache"); }); it("serves index.html for client-side routes", async () => { const res = await request(app.getHttpServer()) .get("/scenarios/123/runs/456") .expect(200); expect(res.text).toBe("liqa"); expect(res.headers["cache-control"]).toBe("no-cache"); }); it("serves hashed assets as immutable", async () => { const res = await request(app.getHttpServer()) .get("/assets/app-abc123.js") .expect(200); expect(res.text).toBe("console.log(1);"); expect(res.headers["cache-control"]).toContain("immutable"); }); it("leaves API routes to the server", async () => { const res = await request(app.getHttpServer()) .get("/api/v1/things") .expect(200); expect(res.body).toEqual([{ id: 1 }]); }); it("returns the server's 404 for unknown API routes, not index.html", async () => { const res = await request(app.getHttpServer()) .get("/api/v1/nope") .expect(404); expect(res.text).not.toContain("liqa"); }); it("leaves /health and /mcp to the server", async () => { await request(app.getHttpServer()) .get("/health") .expect(200, { status: "ok" }); await request(app.getHttpServer()) .post("/mcp") .expect(201, { jsonrpc: "2.0" }); const res = await request(app.getHttpServer()).get("/mcp"); expect(res.text).not.toContain("liqa"); }); it("leaves /manifest.json to the server", async () => { await request(app.getHttpServer()) .get("/manifest.json") .expect(200, { version: "1.0.0" }); }); it("serves nothing when no client directory is configured", async () => { const bare = await buildApp(""); await request(bare.getHttpServer()).get("/").expect(404); await request(bare.getHttpServer()).get("/api/v1/things").expect(200); await bare.close(); }); });