- tests/int/analytics.int.spec.ts: envelope validation, bot filtering, rate limiting, sessionization, retroactive attribution, rollup idempotency, retention purges (11 tests) - tests/e2e/analytics.e2e.spec.ts: dev-flag guest pageview lands in the test DB, collector accepts beacons and rejects garbage, dashboard renders for a superuser - playwright.config.ts: enable NEXT_PUBLIC_ANALYTICS_DEV for the e2e webServer
73 lines
2.8 KiB
TypeScript
73 lines
2.8 KiB
TypeScript
import { expect, test } from "@playwright/test";
|
|
import { Client } from "pg";
|
|
|
|
/**
|
|
* First-party analytics e2e smoke (Phase 1).
|
|
*
|
|
* Runs with NEXT_PUBLIC_ANALYTICS_DEV=1 (set in playwright.config.ts), which
|
|
* enables the tracker in the dev server. Verifies the three surfaces end to
|
|
* end: the client SDK lands a guest pageview row, the collector endpoint
|
|
* accepts beacon batches (and rejects garbage), and the admin dashboard
|
|
* renders for a superuser.
|
|
*/
|
|
|
|
async function countDevPageviews(path: string): Promise<number> {
|
|
// global-setup points DATABASE_URI at the test database before workers run,
|
|
// so read it directly (calling resolveTestDatabaseUri() again would append
|
|
// a second _test suffix).
|
|
const client = new Client({ connectionString: process.env.DATABASE_URI });
|
|
await client.connect();
|
|
try {
|
|
const result = await client.query(
|
|
"SELECT COUNT(*)::int AS c FROM analytics_events WHERE type = 'pageview' AND path = $1 AND env = 'dev'",
|
|
[path],
|
|
);
|
|
return result.rows[0].c as number;
|
|
} finally {
|
|
await client.end();
|
|
}
|
|
}
|
|
|
|
test.describe("First-party analytics", () => {
|
|
test("the tracker records a guest pageview end to end", async ({ page }) => {
|
|
await page.goto("/");
|
|
await expect(page.getByRole("heading", { level: 1 })).toContainText("Polaris Task Force");
|
|
|
|
// The SDK flushes on the first route change, so the row should land fast.
|
|
await expect
|
|
.poll(() => countDevPageviews("/"), { timeout: 20_000, intervals: [500] })
|
|
.toBeGreaterThan(0);
|
|
});
|
|
|
|
test("the collector accepts beacon batches and rejects malformed ones", async ({ request }) => {
|
|
const accepted = await request.post("/api/collect", {
|
|
data: {
|
|
sid: null,
|
|
aid: "e2e-smoke",
|
|
events: [{ type: "pageview", path: "/e2e-collector-smoke" }],
|
|
},
|
|
});
|
|
expect(accepted.status()).toBe(204);
|
|
|
|
const rejected = await request.post("/api/collect", { data: { events: [] } });
|
|
expect(rejected.status()).toBe(400);
|
|
});
|
|
|
|
test("the admin analytics dashboard renders for a superuser", async ({ page }) => {
|
|
await page.goto("/login");
|
|
await page.getByLabel("Username").fill("dev");
|
|
await page.getByLabel("Password").fill("Test123");
|
|
await page.getByRole("button", { name: "Log in" }).click();
|
|
await page.waitForURL((url) => !url.pathname.startsWith("/login"), { timeout: 30_000 });
|
|
|
|
await page.goto("/admin/analytics");
|
|
await expect(page.getByRole("heading", { name: "Usage Analytics" })).toBeVisible({
|
|
timeout: 60_000,
|
|
});
|
|
|
|
// The six report tabs render; hop to one to prove tab navigation works.
|
|
await expect(page.getByRole("link", { name: "Minigames" })).toBeVisible();
|
|
await page.getByRole("link", { name: "Sessions" }).click();
|
|
await expect(page.getByText(/^Sessions \(/)).toBeVisible();
|
|
});
|
|
});
|