Something went wrong. Try again.
Stable episode delivery and analytics on pod.place
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142import { describe, expect, test } from "bun:test";import { BufferedAnalyticsSink, classifyUserAgent, pseudonymizeIp } from "./analytics";import { SqliteStore } from "./store";import { SHOW_URI } from "./testing/fixtures";import type { AnalyticsEvent } from "./types";
const secret = "test-secret".repeat(5);const timestamp = "2026-01-01T12:00:00.000Z";const event: AnalyticsEvent = { resolverId: "id", showUri: SHOW_URI, mappingVersion: 1, timestamp, method: "GET", userAgentClass: "ios", rangeKind: "probe" };
describe("minimal opt-in analytics", () => { test("IP pseudonyms rotate daily, are show-scoped, and normalize addresses", () => { const hash = pseudonymizeIp(secret, "8.8.8.8", SHOW_URI, timestamp); expect(hash).toBe(pseudonymizeIp(secret, "::ffff:8.8.8.8", SHOW_URI, timestamp)); expect(hash).not.toBe(pseudonymizeIp(secret, "8.8.8.8", SHOW_URI + "2", timestamp)); expect(hash).not.toBe(pseudonymizeIp(secret, "8.8.8.8", SHOW_URI, "2026-01-02T00:00:00.000Z")); expect(pseudonymizeIp(secret, "invalid", SHOW_URI, timestamp)).toBeUndefined(); }); test("bot classification wins over mobile device classification", () => { expect(classifyUserAgent("iPhone crawler")).toBe("bot"); }); test("enqueue does not write synchronously; batches and retention are enforced", async () => { const store = new SqliteStore(":memory:"); try { const sink = new BufferedAnalyticsSink(store, 7); await sink.record(event); expect(store.eventCount()).toBe(0); sink.flush(Date.parse(timestamp)); expect(store.eventCount()).toBe(1); sink.flush(Date.parse(timestamp) + 8 * 86_400_000); expect(store.eventCount()).toBe(0); } finally { store.close(); } }); test("buffer overflow is bounded and counted", async () => { const store = new SqliteStore(":memory:"); try { const sink = new BufferedAnalyticsSink(store, 7, 1); await sink.record(event); await sink.record(event); expect(sink.dropped).toBe(1); sink.flush(Date.parse(timestamp)); expect(store.eventCount()).toBe(1); } finally { store.close(); } });});