railyard/apps/insights-edge/test/replay/viewer-js.test.mjs
caelum0x bb856083ad feat(insights-edge): add session replay (rrweb), paid-tier gated
Adopts rrweb + rrweb-player (MIT, vendored as prebuilt UMD bundles via
scripts/vendor-rrweb.mjs, see NOTICE) to add cookieless session replay to
Railyard Insights, gated behind the existing Dodo paid tier.

- src/replay/config.mjs: per-site settings with privacy-first floors/defaults
  (off by default, sample rate, maskAllInputs, maskTextSelector, per-session
  byte/duration caps, monthly session cap) — every cap is clamped server-side
  regardless of what a site's stored config asks for.
- src/replay/ingest.mjs: POST /api/replay chunk ingestion — bot/DNT/GPC drop,
  paid-tier + enabled gate, hostname check, and all caps enforced again on
  the server (not just trusted from the client).
- src/replay/store.mjs: D1 replay_sessions metadata + R2 event chunks
  (<siteKey>/<sessionId>/<seq>.json); wired into the existing site-delete
  flow so deleting a site removes its sessions too.
- src/replay/tracker.mjs, routes.mjs, pages.mjs: the opt-in /replay.js tag,
  the dashboard's settings + session list + player pages (matching the
  existing Insights dashboard design), and the events API — all behind the
  same private dashboard token, so a site owner can only ever see and play
  back their own site's sessions.
- wrangler.jsonc: new R2 binding (railyard-insights-replay).

Tests: 23 new (config validation/clamping, ingestion caps, dashboard auth
scoping, a regression test for the vendored player's actual constructor
shape) + existing 52, all 75 passing. Verified end-to-end locally with
wrangler dev + a real browser: recorded a session, played it back with
rrweb-player, screenshots in /tmp/railyard-replay-*.png.

No deploy, no merge to railyard main from this commit alone (see
plans/railyard.md for the deploy/rollback steps).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 03:06:17 +03:00

20 lines
1.1 KiB
JavaScript

// Regression guard: the vendored rrweb-player UMD bundle (2.1.7) exposes the
// player class as window.rrwebPlayer.Player (a plain `new window.rrwebPlayer(...)`
// throws "is not a constructor" — found only by actually running the viewer in a
// real browser against the real vendored bundle, not by the Miniflare test suite).
import assert from "node:assert/strict";
import { test } from "node:test";
import { REPLAY_JS, REPLAY_VIEWER_JS } from "../../src/replay/tracker.mjs";
test("the player viewer resolves window.rrwebPlayer.Player (or .default), never calls `new window.rrwebPlayer` directly", () => {
assert.match(REPLAY_VIEWER_JS, /rrwebPlayer\.Player\|\|window\.rrwebPlayer\.default/);
assert.doesNotMatch(REPLAY_VIEWER_JS, /new window\.rrwebPlayer\(/);
});
test("the recorder loader checks Do Not Track and Global Privacy Control before any fetch", () => {
const dntCheckIndex = REPLAY_JS.indexOf("doNotTrack");
const firstFetchIndex = REPLAY_JS.indexOf("fetch(");
assert.ok(dntCheckIndex >= 0 && dntCheckIndex < firstFetchIndex, "the DNT/GPC check must run before the first network call");
assert.match(REPLAY_JS, /globalPrivacyControl/);
});