Files
liqa/server/test/client-static.spec.ts
ars9 718a12c2f4
CI / changes (pull_request) Successful in 6s
CI / version (pull_request) Skipped
CI / server (pull_request) Successful in 1m30s
CI / client (pull_request) Successful in 1m31s
CI / image (pull_request) Skipped
feat(logs): collapse large run log data and serve client manifest
- collapse run log data blocks whose pretty-printed JSON exceeds a byte
  or line limit, with a per-block toggle and an expand/collapse-all
  button next to the Logs heading
- align data blocks and toggles with the context/message column
- add a "Copy logs" button that copies the filtered logs as plain text
  with every data block expanded
- add GET /manifest.json with the app name, version and run log data
  limits (RUN_LOG_DATA_COLLAPSE_BYTES / RUN_LOG_DATA_COLLAPSE_LINES)
- load the manifest in the client stale-while-revalidate: localStorage
  copy first, refetched per page load and on tab focus after a while
- show the server version in the sidebar footer, with a reload hint
  when it differs from the client build

Closes #3
2026-10-02 14:05:49 +03:00

154 lines
4.4 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" };
}
@Get("manifest.json")
manifest() {
return { version: "1.0.0" };
}
@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 },
{ 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"), "<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("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();
});
});