2026-06-28 18:57:53 +08:00
|
|
|
// Proves the server-side invariant: trackEvent does not emit to PostHog when
|
|
|
|
|
// analytics is disabled by the runtime gate. Replaces the old source-string and
|
|
|
|
|
// denylist-regex assertions, which no longer match the gated implementation.
|
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
2026-04-29 23:47:19 +08:00
|
|
|
|
2026-06-28 18:57:53 +08:00
|
|
|
describe("Server-side analytics gating (behavioral)", () => {
|
|
|
|
|
const origEnv = process.env.NODE_ENV;
|
|
|
|
|
const origOverride = process.env.ANALYTICS_BAKED_OVERRIDE;
|
2026-04-29 23:47:19 +08:00
|
|
|
|
2026-06-28 18:57:53 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.resetModules();
|
|
|
|
|
process.env.NODE_ENV = "test";
|
|
|
|
|
});
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
process.env.NODE_ENV = origEnv;
|
|
|
|
|
if (origOverride === undefined) delete process.env.ANALYTICS_BAKED_OVERRIDE;
|
|
|
|
|
else process.env.ANALYTICS_BAKED_OVERRIDE = origOverride;
|
|
|
|
|
vi.restoreAllMocks();
|
2026-04-29 23:47:19 +08:00
|
|
|
});
|
|
|
|
|
|
2026-06-28 18:57:53 +08:00
|
|
|
it("trackEvent does not capture when the gate is off", async () => {
|
|
|
|
|
process.env.ANALYTICS_BAKED_OVERRIDE = "off";
|
|
|
|
|
const capture = vi.fn();
|
|
|
|
|
vi.doMock("posthog-node", () => ({
|
|
|
|
|
PostHog: vi.fn(() => ({ capture, shutdown: vi.fn() })),
|
|
|
|
|
}));
|
|
|
|
|
const { trackEvent } = await import("../../../apps/api/src/lib/analytics.js");
|
|
|
|
|
await trackEvent("tool_used", { tool_id: "resize" }, "did-1");
|
|
|
|
|
expect(capture).not.toHaveBeenCalled();
|
2026-04-29 23:47:19 +08:00
|
|
|
});
|
|
|
|
|
});
|