Files
HealthLog/src/lib/insights/__tests__/cached-text-cipher.test.ts
T
Marc-André Bombeck 9941db1549 refactor(insights): drop the readable cached-text column
Every write has gone to insights_cached_text_encrypted since 0411, and
the boot-time free-text backfill sealed the rows written before it.
Migration 0430 drops insights_cached_text and the backfill index that
served it. A host that skipped the intervening releases loses only a
cache of model output; the next warm writes a fresh, sealed payload.

The cipher module now reads and writes the sealed column only, the
backfill job loses its insights step, and consent withdrawal, the wipe
plan, the backup plan and the session projection follow the schema.
Backups never carried the column, and restore ignores unknown account
settings; a round-trip test now proves an old backup naming the field
still restores cleanly. The access guard pins exact counts for where
the sealed name may appear. Fixtures, the demo seed and the demo
migration script seed the sealed column instead of plaintext.
2026-10-21 19:24:21 +02:00

74 lines
2.2 KiB
TypeScript

import { Buffer } from "node:buffer";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@/lib/logging/context", () => ({ getEvent: () => undefined }));
import { encryptBytes } from "@/lib/crypto";
import {
INSIGHTS_CACHE_CLEARED,
INSIGHTS_CACHE_SELECT,
insightsCacheWrite,
openInsightsCachedText,
sealInsightsCachedText,
} from "../cached-text-cipher";
const PAYLOAD = JSON.stringify({ dailyBriefing: { paragraph: "Sleep held." } });
beforeEach(() => {
vi.stubEnv("ENCRYPTION_KEYS", "");
vi.stubEnv("ENCRYPTION_ACTIVE_KEY_ID", "");
vi.stubEnv("ENCRYPTION_KEY", "b".repeat(64));
});
afterEach(() => vi.unstubAllEnvs());
describe("cached insights payload at rest", () => {
it("writes ciphertext only", () => {
const data = insightsCacheWrite(PAYLOAD);
expect(Object.keys(data)).toEqual(["insightsCachedTextEncrypted"]);
expect(
Buffer.from(data.insightsCachedTextEncrypted).toString("utf8"),
).not.toContain("Sleep held");
expect(openInsightsCachedText(data)).toBe(PAYLOAD);
});
it("reads a sealed value", () => {
expect(
openInsightsCachedText({
insightsCachedTextEncrypted: sealInsightsCachedText("sealed"),
}),
).toBe("sealed");
});
it("does not open a value sealed for another purpose", () => {
const foreign = encryptBytes(Buffer.from(PAYLOAD, "utf8"), "other/v1");
expect(
openInsightsCachedText({
insightsCachedTextEncrypted: new Uint8Array(foreign),
}),
).toBeNull();
});
it("reads an empty row as no cache", () => {
expect(openInsightsCachedText(null)).toBeNull();
expect(openInsightsCachedText({ ...INSIGHTS_CACHE_CLEARED })).toBeNull();
expect(
openInsightsCachedText({ insightsCachedTextEncrypted: new Uint8Array() }),
).toBeNull();
});
it("is fail-closed on write: no key, no payload", () => {
vi.stubEnv("ENCRYPTION_KEY", "");
expect(() => insightsCacheWrite(PAYLOAD)).toThrow();
});
it("clears and selects the sealed column", () => {
expect(INSIGHTS_CACHE_CLEARED).toEqual({
insightsCachedTextEncrypted: null,
});
expect(Object.keys(INSIGHTS_CACHE_SELECT)).toEqual([
"insightsCachedTextEncrypted",
]);
});
});