import { afterEach, describe, expect, it, vi } from "vitest"; import { runWithContext } from "../observability/context.js"; import { NatsClient } from "./nats.js"; import { AGENT_ALERT_SUBJECT, DISCORD_MEMBER_LOOKUP_SUBJECT } from "./nats-subjects.js"; import { checkPublishAllowed } from "./nats-skill-acl.js"; describe("NATS skill ACL", () => { afterEach(() => { vi.unstubAllEnvs(); }); it("allows declared publish subjects", () => { expect(checkPublishAllowed("onboarding", DISCORD_MEMBER_LOOKUP_SUBJECT)).toBe( true, ); expect(checkPublishAllowed("supervisor", AGENT_ALERT_SUBJECT)).toBe(true); expect(checkPublishAllowed("onboarding", AGENT_ALERT_SUBJECT)).toBe(false); }); it("enforces denied publish subjects when enabled", async () => { vi.stubEnv("AGENT_NATS_ACL_MODE", "enforce"); const client = new NatsClient(); await expect( runWithContext( { chatroomId: "room-1", requestId: "req-1", skill: "onboarding", startedAt: "2026-05-18T00:00:00.000Z", trigger: "user-message", userId: "user-1", }, () => client.publish(AGENT_ALERT_SUBJECT, { ok: true }), ), ).rejects.toThrow( `NATS publish denied for skill onboarding on subject ${AGENT_ALERT_SUBJECT}`, ); }); });