2026-04-29 23:47:19 +08:00
|
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
2026-06-24 13:30:18 +08:00
|
|
|
import { buildTestApp, loginAsAdmin, type TestApp } from "../test-server.js";
|
2026-04-29 23:47:19 +08:00
|
|
|
|
|
|
|
|
let testApp: TestApp;
|
|
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
testApp = await buildTestApp();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
|
await testApp.cleanup();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("GET /api/v1/config/analytics", () => {
|
|
|
|
|
it("returns 200 without auth (public endpoint)", async () => {
|
|
|
|
|
const res = await testApp.app.inject({
|
|
|
|
|
method: "GET",
|
|
|
|
|
url: "/api/v1/config/analytics",
|
|
|
|
|
});
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns correct AnalyticsConfig shape", async () => {
|
|
|
|
|
const res = await testApp.app.inject({
|
|
|
|
|
method: "GET",
|
|
|
|
|
url: "/api/v1/config/analytics",
|
|
|
|
|
});
|
|
|
|
|
const config = JSON.parse(res.body);
|
|
|
|
|
|
|
|
|
|
expect(config).toHaveProperty("enabled");
|
|
|
|
|
expect(config).toHaveProperty("posthogApiKey");
|
|
|
|
|
expect(config).toHaveProperty("posthogHost");
|
|
|
|
|
expect(config).toHaveProperty("sentryDsn");
|
|
|
|
|
expect(config).toHaveProperty("sampleRate");
|
|
|
|
|
expect(config).toHaveProperty("instanceId");
|
|
|
|
|
|
|
|
|
|
expect(typeof config.enabled).toBe("boolean");
|
|
|
|
|
expect(typeof config.posthogApiKey).toBe("string");
|
|
|
|
|
expect(typeof config.posthogHost).toBe("string");
|
|
|
|
|
expect(typeof config.sentryDsn).toBe("string");
|
|
|
|
|
expect(typeof config.sampleRate).toBe("number");
|
|
|
|
|
expect(typeof config.instanceId).toBe("string");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("instanceId is consistent across requests", async () => {
|
|
|
|
|
const res1 = await testApp.app.inject({ method: "GET", url: "/api/v1/config/analytics" });
|
|
|
|
|
const res2 = await testApp.app.inject({ method: "GET", url: "/api/v1/config/analytics" });
|
|
|
|
|
const c1 = JSON.parse(res1.body);
|
|
|
|
|
const c2 = JSON.parse(res2.body);
|
|
|
|
|
expect(c1.instanceId).toBe(c2.instanceId);
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-24 11:05:39 +08:00
|
|
|
it("config values come from build-time bake (dev defaults to disabled)", async () => {
|
|
|
|
|
const res = await testApp.app.inject({ method: "GET", url: "/api/v1/config/analytics" });
|
|
|
|
|
const config = JSON.parse(res.body);
|
|
|
|
|
// In dev/test the committed baked.ts has enabled: false
|
|
|
|
|
expect(config.enabled).toBe(false);
|
|
|
|
|
expect(config.posthogApiKey).toBe("");
|
|
|
|
|
expect(config.posthogHost).toBe("");
|
|
|
|
|
expect(config.sentryDsn).toBe("");
|
|
|
|
|
expect(config.sampleRate).toBe(0);
|
2026-04-29 23:47:19 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-24 11:05:39 +08:00
|
|
|
describe("PUT /api/v1/user/analytics (removed)", () => {
|
|
|
|
|
it("returns 404 (endpoint no longer exists)", async () => {
|
2026-06-24 13:30:18 +08:00
|
|
|
// Authenticate first: the global auth preHandler answers unauthenticated
|
|
|
|
|
// requests with 401 before routing, so only an authenticated request can
|
|
|
|
|
// reach Fastify's not-found handler and prove the route is gone.
|
|
|
|
|
const token = await loginAsAdmin(testApp.app);
|
2026-06-24 11:05:39 +08:00
|
|
|
const res = await testApp.app.inject({
|
2026-04-29 23:47:19 +08:00
|
|
|
method: "PUT",
|
|
|
|
|
url: "/api/v1/user/analytics",
|
2026-06-24 13:30:18 +08:00
|
|
|
headers: { authorization: `Bearer ${token}` },
|
2026-04-29 23:47:19 +08:00
|
|
|
payload: { enabled: true },
|
|
|
|
|
});
|
2026-06-24 11:05:39 +08:00
|
|
|
expect(res.statusCode).toBe(404);
|
2026-04-29 23:47:19 +08:00
|
|
|
});
|
|
|
|
|
});
|