mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
The operator switches become master, Coach, Daily Briefing, Status notes and Reading documents. Correlations goes: it gated statistics, not anything a model writes. Reading documents covers the document vault, lab scans and medication extraction. The Coach module's operator availability now follows the Coach switch, so there is one place to turn the Coach off server-wide. The migration folds a stored Coach availability of false into the switch, and gives everyone who hid the Coach the AI analysis opt-out, since hiding the Coach no longer stops nightly briefing and status notes. A switch read that fails now fails closed. The insights module is described as AI analysis in every locale, and the switch guard now keeps both retired switches out and freezes the callers of the old operator-only gate.
149 lines
3.8 KiB
TypeScript
149 lines
3.8 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { NextRequest } from "next/server";
|
|
|
|
vi.mock("@/lib/auth/session", () => ({ getSession: vi.fn() }));
|
|
|
|
vi.mock("@/lib/auth/audit", () => ({
|
|
auditLog: vi.fn().mockResolvedValue(undefined),
|
|
}));
|
|
|
|
vi.mock("@/lib/logging/transports", () => ({ emitIfSampled: vi.fn() }));
|
|
|
|
vi.mock("@/lib/db-compat", () => ({
|
|
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
|
|
}));
|
|
|
|
vi.mock("next/headers", () => ({
|
|
headers: vi.fn(async () => ({ get: () => null })),
|
|
cookies: vi.fn(async () => ({
|
|
get: () => undefined,
|
|
set: () => {},
|
|
delete: () => {},
|
|
})),
|
|
}));
|
|
|
|
vi.mock("@/lib/db", () => ({
|
|
prisma: {
|
|
appSettings: {
|
|
findUnique: vi.fn(),
|
|
},
|
|
},
|
|
}));
|
|
|
|
import { GET } from "../route";
|
|
import { getSession } from "@/lib/auth/session";
|
|
import { prisma } from "@/lib/db";
|
|
|
|
const SESSION_OK = {
|
|
session: { id: "sess-1", expiresAt: new Date(Date.now() + 3_600_000) },
|
|
user: { id: "user-1", username: "testuser", role: "USER" as const },
|
|
};
|
|
|
|
function req(): NextRequest {
|
|
return new NextRequest("http://localhost/api/feature-flags", {
|
|
method: "GET",
|
|
});
|
|
}
|
|
|
|
const FIND = prisma.appSettings.findUnique as ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
vi.resetAllMocks();
|
|
});
|
|
|
|
describe("GET /api/feature-flags", () => {
|
|
it("returns 401 when unauthenticated", async () => {
|
|
vi.mocked(getSession).mockResolvedValue(null);
|
|
const res = await GET(req());
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it("returns the all-on default shape for a fresh install", async () => {
|
|
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
|
|
FIND.mockResolvedValue(null);
|
|
const res = await GET(req());
|
|
expect(res.status).toBe(200);
|
|
|
|
const body = (await res.json()) as {
|
|
data: {
|
|
assistant: {
|
|
enabled: boolean;
|
|
coach: boolean;
|
|
briefing: boolean;
|
|
insightStatus: boolean;
|
|
documentAi: boolean;
|
|
};
|
|
};
|
|
error: null;
|
|
};
|
|
|
|
expect(body.error).toBeNull();
|
|
expect(body.data.assistant).toEqual({
|
|
enabled: true,
|
|
coach: true,
|
|
briefing: true,
|
|
insightStatus: true,
|
|
documentAi: true,
|
|
});
|
|
});
|
|
|
|
it("forces every sub-flag false when the master is off", async () => {
|
|
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
|
|
FIND.mockResolvedValue({
|
|
assistantEnabled: false,
|
|
assistantCoachEnabled: true,
|
|
assistantBriefingEnabled: true,
|
|
assistantInsightStatusEnabled: true,
|
|
assistantDocumentAiEnabled: true,
|
|
});
|
|
|
|
const res = await GET(req());
|
|
expect(res.status).toBe(200);
|
|
|
|
const body = (await res.json()) as {
|
|
data: { assistant: Record<string, boolean> };
|
|
};
|
|
|
|
expect(body.data.assistant).toEqual({
|
|
enabled: false,
|
|
coach: false,
|
|
briefing: false,
|
|
insightStatus: false,
|
|
documentAi: false,
|
|
});
|
|
});
|
|
|
|
it("reads each sub-flag column", async () => {
|
|
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
|
|
FIND.mockResolvedValue({
|
|
assistantEnabled: true,
|
|
assistantCoachEnabled: false,
|
|
assistantBriefingEnabled: true,
|
|
assistantInsightStatusEnabled: false,
|
|
assistantDocumentAiEnabled: true,
|
|
});
|
|
|
|
const res = await GET(req());
|
|
expect(res.status).toBe(200);
|
|
|
|
const body = (await res.json()) as {
|
|
data: { assistant: Record<string, boolean> };
|
|
};
|
|
|
|
expect(body.data.assistant).toEqual({
|
|
enabled: true,
|
|
coach: false,
|
|
briefing: true,
|
|
insightStatus: false,
|
|
documentAi: true,
|
|
});
|
|
});
|
|
|
|
it("attaches a private 60-second Cache-Control header", async () => {
|
|
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
|
|
FIND.mockResolvedValue(null);
|
|
const res = await GET(req());
|
|
expect(res.headers.get("Cache-Control")).toBe("private, max-age=60");
|
|
});
|
|
});
|