Files
cfw-autumn/server/tests/unit/analytics/select-analytics-store.test.ts

115 lines
3.3 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { ErrCode, RecaseError } from "@autumn/shared";
import { selectAnalyticsStore } from "@/internal/analytics/store/selectAnalyticsStore.js";
const makeCtx = (env?: Record<string, unknown>) =>
({
workerEnv: env as unknown as Env,
logger: { debug: () => undefined },
}) as any;
describe("selectAnalyticsStore", () => {
test("throws unavailable when Cloudflare hot reads are disabled", () => {
expect(() =>
selectAnalyticsStore({
ctx: makeCtx({ CLOUDFLARE_ANALYTICS_HOT_READ_ENABLED: "false" }),
customRange: {
start: Date.parse("2026-06-01T00:00:00.000Z"),
end: Date.parse("2026-06-02T00:00:00.000Z"),
},
now: new Date("2026-06-22T00:00:00.000Z"),
}),
).toThrow(RecaseError);
try {
selectAnalyticsStore({
ctx: makeCtx({ CLOUDFLARE_ANALYTICS_HOT_READ_ENABLED: "false" }),
now: new Date("2026-06-22T00:00:00.000Z"),
});
} catch (error) {
expect((error as RecaseError).code).toBe(
ErrCode.CloudflareAnalyticsUnavailable,
);
}
});
test("uses cloudflare hot store for enabled in-range queries", () => {
const selected = selectAnalyticsStore({
ctx: makeCtx({
CLOUDFLARE_ANALYTICS_HOT_READ_ENABLED: "true",
CLOUDFLARE_ACCOUNT_ID: "account_1",
CLOUDFLARE_API_TOKEN: "token_1",
EVENTS_ANALYTICS: {
writeDataPoint: () => undefined,
} as AnalyticsEngineDataset,
}),
customRange: {
start: Date.parse("2026-06-01T00:00:00.000Z"),
end: Date.parse("2026-06-02T00:00:00.000Z"),
},
now: new Date("2026-06-22T00:00:00.000Z"),
});
expect(selected.kind).toBe("cloudflare-hot");
expect(selected.reason).toBe("inside-hot-range");
});
test("throws unsupported instead of falling back for older ranges", () => {
expect(() =>
selectAnalyticsStore({
ctx: makeCtx({
CLOUDFLARE_ANALYTICS_HOT_READ_ENABLED: "true",
CLOUDFLARE_ACCOUNT_ID: "account_1",
CLOUDFLARE_API_TOKEN: "token_1",
EVENTS_ANALYTICS: {
writeDataPoint: () => undefined,
} as AnalyticsEngineDataset,
}),
customRange: {
start: Date.parse("2026-01-01T00:00:00.000Z"),
end: Date.parse("2026-01-02T00:00:00.000Z"),
},
now: new Date("2026-06-22T00:00:00.000Z"),
}),
).toThrow(RecaseError);
});
test("throws unsupported for filter_by instead of requiring Tinybird", () => {
expect(() =>
selectAnalyticsStore({
ctx: makeCtx({
CLOUDFLARE_ANALYTICS_HOT_READ_ENABLED: "true",
CLOUDFLARE_ACCOUNT_ID: "account_1",
CLOUDFLARE_API_TOKEN: "token_1",
EVENTS_ANALYTICS: {
writeDataPoint: () => undefined,
} as AnalyticsEngineDataset,
}),
requiresCloudflareUnsupportedFeature: true,
now: new Date("2026-06-22T00:00:00.000Z"),
}),
).toThrow(RecaseError);
});
test("handler source does not require Tinybird before selecting a store", async () => {
const { readFileSync } = await import("node:fs");
const { join } = await import("node:path");
const files = [
"handleListEventNames.ts",
"handleInternalAggregateEvents.ts",
];
for (const file of files) {
const source = readFileSync(
join(
import.meta.dir,
`../../../src/internal/analytics/internalHandlers/${file}`,
),
"utf8",
);
expect(source).not.toContain("assertTinybirdAvailable()");
}
});
});