import { getRlsExecutionContext } from "@hivegrid/db/rls-context.server"; import { notify, type NotificationClient } from "@hivegrid/foundation"; import { describe, expect, test } from "vitest"; import { notifyWithTenantRlsContext } from "./notify-rls.js"; // Emulates the FORCE RLS policy on NotificationDelivery (hg_NotificationDelivery_insert): // a tenant-scoped write only passes WITH CHECK when the bound context carries the // row's tenant. Mocking the client without the policy would prove nothing about // RLS (AGENTS.md §RLS), so the fake rejects exactly like Postgres does for the // worker role without a tenant GUC. function createPolicyEnforcingClient(rows: Array<{ tenantId: string | null; userId: string | null; op: string }>): NotificationClient { const assertPolicy = (tenantId: string | null) => { const boundTenantId = getRlsExecutionContext()?.tenantId ?? null; if (tenantId !== null && boundTenantId !== tenantId) { throw new Error('new row violates row-level security policy for table "NotificationDelivery"'); } }; return { notificationDelivery: { async create({ data }) { assertPolicy(data.tenantId); rows.push({ tenantId: data.tenantId, userId: data.userId, op: "create" }); return data; }, async upsert({ create }) { assertPolicy(create.tenantId); rows.push({ tenantId: create.tenantId, userId: create.userId, op: "upsert" }); return create; }, }, }; } describe("notifyWithTenantRlsContext", () => { test("regression: a plain worker-side notify() without bound context fails the policy", async () => { // This is the exact failure mode behind "tasks.reminder.notify-failed" after // the 2026-07-13 app-wide RLS cutover: the sweep called notify() outside any // tenant context, so the FORCE RLS insert was rejected and delivery silently // stopped. The wrapper below exists to close this hole — if this expectation // ever flips, the policy emulation is broken, not fixed. const rows: Array<{ tenantId: string | null; userId: string | null; op: string }> = []; const client = createPolicyEnforcingClient(rows); await expect( notify({ tenantId: "tenant-a", userId: "user-1", messageKey: "tasks.reminder.before", sourceKey: "task-reminder-t1-before" }, client) ).rejects.toThrow(/row-level security/); expect(rows).toEqual([]); }); test("binds the row's tenant for the deduped upsert path", async () => { const rows: Array<{ tenantId: string | null; userId: string | null; op: string }> = []; const client = createPolicyEnforcingClient(rows); await notifyWithTenantRlsContext( { tenantId: "tenant-a", userId: "user-1", messageKey: "tasks.reminder.before", sourceKey: "task-reminder-t1-before" }, client ); expect(rows).toEqual([{ tenantId: "tenant-a", userId: "user-1", op: "upsert" }]); // The binding is scoped to the write; the sweep loop stays context-free. expect(getRlsExecutionContext()).toBeNull(); }); test("binds the row's tenant for the create path (no recipient user)", async () => { // email-domain-probe notifies without a userId -> notify() falls back to create. const rows: Array<{ tenantId: string | null; userId: string | null; op: string }> = []; const client = createPolicyEnforcingClient(rows); await notifyWithTenantRlsContext( { tenantId: "tenant-b", messageKey: "email.domain.notify.verified", sourceKey: "email-domain-verified:d1" }, client ); expect(rows).toEqual([{ tenantId: "tenant-b", userId: null, op: "create" }]); expect(getRlsExecutionContext()).toBeNull(); }); test("keeps concurrent tenant notifications isolated", async () => { const rows: Array<{ tenantId: string | null; userId: string | null; op: string }> = []; const client = createPolicyEnforcingClient(rows); await Promise.all([ notifyWithTenantRlsContext({ tenantId: "tenant-a", userId: "user-a", messageKey: "hr.notify.reminder.due", sourceKey: "hr-reminder:r1" }, client), notifyWithTenantRlsContext({ tenantId: "tenant-b", userId: "user-b", messageKey: "hr.notify.reminder.due", sourceKey: "hr-reminder:r2" }, client), ]); expect(rows.map((row) => row.tenantId).sort()).toEqual(["tenant-a", "tenant-b"]); expect(getRlsExecutionContext()).toBeNull(); }); });