- serve the built client from the server via serve-static (CLIENT_DIR), falling back to index.html outside /api, /mcp, /docs and /health - replace server and client Dockerfiles and nginx with one root Dockerfile and a single liqa compose service on port 13000 - use Playwright's chromium-headless-shell with only its required libs and DejaVu/Liberation fonts instead of Debian chromium and Noto - clear the npm cache in the runtime image - bump version to 1.19.0
142 lines
4.1 KiB
TypeScript
142 lines
4.1 KiB
TypeScript
/**
|
|
* 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" };
|
|
}
|
|
|
|
@Post("mcp")
|
|
mcp() {
|
|
return { jsonrpc: "2.0" };
|
|
}
|
|
}
|
|
|
|
async function buildApp(clientDir: string): Promise<INestApplication> {
|
|
// 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 },
|
|
],
|
|
});
|
|
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"), "<html>liqa</html>");
|
|
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("<html>liqa</html>");
|
|
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("<html>liqa</html>");
|
|
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</html>");
|
|
});
|
|
|
|
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</html>");
|
|
});
|
|
|
|
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();
|
|
});
|
|
});
|