Files
SnapOtter/tests/unit/api/analytics-no-leak.test.ts
T
SnapOtterandGitHub 63a03d26f2 feat: pipeline templates, analytics opt-out, 83 conversion presets, positioning + e2e modernization
Lands five integrated branches: pipeline templates (#355), analytics opt-out (#354), 83 conversion presets bringing the catalog to 240 tools (#356), self-hosted positioning (#353), and e2e modernization (#351).

Integration fixes: aligned stale web analytics tests with the opt-out/allow-list model, closed 3 CodeQL incomplete-sanitization alerts in the i18n generator, resolved settings/index/docs/format-matrix conflicts, and corrected tool counts to 240.
2026-06-28 18:57:53 +08:00

32 lines
1.2 KiB
TypeScript

// 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";
describe("Server-side analytics gating (behavioral)", () => {
const origEnv = process.env.NODE_ENV;
const origOverride = process.env.ANALYTICS_BAKED_OVERRIDE;
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();
});
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();
});
});