Files
kuber/tests/server/version-header.test.ts
T

75 lines
2.6 KiB
TypeScript

import { expect, test } from "bun:test";
import { createApp, execProblem } from "../../server/app";
import { hashToken, MemoryAuthStore } from "../../server/auth";
import { MemoryWorkspaceStore } from "../../server/workspace-store";
import type { LogService } from "../../server/log-service";
import { KUBER_VERSION, KUBER_VERSION_HEADER } from "../../shared/version";
test("returns the bundled version on success, errors, preflight, empty and streaming responses", async () => {
const store = new MemoryAuthStore();
await store.putUser({
username: "viewer",
passwordHash: "hash",
roles: ["viewer"],
});
await store.putSession({
tokenHash: hashToken("token"),
username: "viewer",
authVersion: 1,
expiresAt: "2030-01-01T00:00:00.000Z",
});
const workspaceStore = new MemoryWorkspaceStore({
uid: () => "workspace-uid",
});
await workspaceStore.create({
id: "demo",
source: { uri: "oci://example/demo", digest: "sha256:abc" },
});
const logs = {
async collect() {
return [{ type: "log", message: "hello" }];
},
} as unknown as LogService;
const app = createApp({ store, workspaceStore, logs });
const request = (path: string, init: RequestInit = {}) =>
new Request(`https://kuber.astrxl.dev${path}`, init);
const authenticated = (path: string, init: RequestInit = {}) =>
request(path, {
...init,
headers: {
authorization: "Bearer token",
...Object.fromEntries(new Headers(init.headers)),
},
});
const success = await app(request("/api/v2/health"));
const error = await app(request("/api/v2/me"));
const preflight = await app(
request("/api/v2/health", {
method: "OPTIONS",
headers: { origin: "https://kuber.astrxl.dev" },
}),
);
const stream = await app(authenticated("/api/v2/workspaces/demo/logs"));
const empty = await app(authenticated("/api/v2/logout", { method: "POST" }));
for (const response of [success, error, preflight, empty, stream]) {
expect(response.headers.get(KUBER_VERSION_HEADER)).toBe(KUBER_VERSION);
}
expect([
success.status,
error.status,
preflight.status,
empty.status,
stream.status,
]).toEqual([200, 401, 204, 204, 200]);
expect(stream.headers.get("content-type")).toContain("application/x-ndjson");
expect(await stream.text()).toContain("hello");
});
test("includes the version on exec upgrade errors outside the ordinary API router", () => {
const response = execProblem(new Error("upgrade failed"), "request-id");
expect(response.status).toBe(500);
expect(response.headers.get(KUBER_VERSION_HEADER)).toBe(KUBER_VERSION);
});