railyard/apps/insights/tests/api/shares.spec.ts
caelum0x 1bc04fa81b feat(insights): the Insights fork behind the add-on, site entries, tenant summaries, caps that say what happens (PY1.3)
Railyard side of PY1.3 (RYP-D2, RYP-E1, SC-E3). Acceptance:
1. apps/insights: umami-software/umami v3.4.0 (MIT, commit ec0ff503),
   vendored --depth 1, LICENSE kept, NOTICE added. Fork additions only:
   src/railyard/{ingest,summary,tenancy,api-only}.ts, /v1/ingest/batch (the
   edge collector contract v1, bearer INSIGHTS_INGEST_TOKEN, no IP ever,
   retried batches de-duplicated by migration 90_railyard_ingest_receipts),
   /api/v1/tenants/{org}/summary (bearer INSIGHTS_API_TOKEN), src/proxy.ts
   (INSIGHTS_API_ONLY=1 serves only the Railyard API and the heartbeat).
   Tenant isolation is tested at the API level (an app id owned by another org
   is refused) and at the SQL level (upstream migrations 01-26 plus 90 on a
   throwaway cluster: org B never reads org A's rows, even naming its app).
   Upstream vitest suite: not run on the Mac (pnpm 12.3.4 pinned upstream,
   disk floor); it runs in the image build on box 2 or CI.
2. No upstream product name in anything a customer loads: the page tag, the
   console pages and the fork's additions (tests); the upstream dashboard is
   never served in API-only mode.
3. The console writes the collector's site entry on "Add app", on
   auto-created apps from deploys, and rewrites every entry on add-on, plan or
   pause changes (lib/sites.ts, SITES KV binding). The bridge gains
   `sites` (the project's app, site key, page tag and collector URLs, entry
   written) and the analytics proof is the first event from Insights. The
   console's Insights page shows visitors, events, first event and the
   visit -> signup -> first action funnel.
5. insights-edge/src/caps.ts: at 100% of the cap events are still counted and
   not stored until the next UTC month; the console shows the same message
   from a byte-identical copy (lib/caps.ts, tested).
4 (384 MB at 50 events a second for 10 min) is measured on staging before deploy.

Env: INSIGHTS_API_URL, INSIGHTS_API_TOKEN, SITES KV binding (console);
INSIGHTS_INGEST_TOKEN, INSIGHTS_API_TOKEN, INSIGHTS_API_ONLY (Insights).

Tests: console 54/54 (tsc clean), insights fork 7/7 (incl. SQL isolation),
insights-edge caps + collector + sim 14/14, tests/contract 3/3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 21:23:38 +03:00

271 lines
8.3 KiB
TypeScript

import { expect, test } from './fixtures';
import { ENTITY_TYPE, UNKNOWN_UUID } from './helpers/constants';
import { dateRange } from './helpers/dates';
import { createWebsite, deleteWebsite, uniqueName, uniqueSlug } from './helpers/entities';
test.describe('Shares', () => {
test.describe.configure({ mode: 'serial' });
let websiteId = '';
let shareId = '';
let slug = '';
let websiteShareId = '';
let gatedShareId = '';
let gatedSlug = '';
test.beforeAll(async ({ admin }) => {
websiteId = (await createWebsite(admin)).id;
});
test.afterAll(async ({ admin }) => {
for (const id of [shareId, websiteShareId, gatedShareId]) {
if (id) {
await admin.del(`/api/share/id/${id}`);
}
}
if (websiteId) {
await deleteWebsite(admin, websiteId);
}
});
test('POST /api/share creates a share for an entity', async ({ admin }) => {
const name = uniqueName('share');
slug = uniqueSlug('share');
const response = await admin.post('/api/share', {
entityId: websiteId,
shareType: ENTITY_TYPE.website,
name,
slug,
parameters: { overview: true },
});
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
id: expect.any(String),
entityId: websiteId,
shareType: ENTITY_TYPE.website,
name,
slug,
parameters: { overview: true },
createdAt: expect.any(String),
});
shareId = response.body.id;
});
test('POST /api/share generates a slug when omitted', async ({ admin }) => {
const response = await admin.post('/api/share', {
entityId: websiteId,
shareType: ENTITY_TYPE.website,
name: uniqueName('share'),
parameters: {},
});
expect(response.status).toBe(200);
expect(response.body.slug).toMatch(/^[A-Za-z0-9]{16}$/);
await admin.del(`/api/share/id/${response.body.id}`);
});
test('POST /api/share validates the body and checks entity access', async ({
admin,
user,
viewer,
api,
}) => {
const missing = await admin.post('/api/share', { entityId: websiteId, name: 'x' });
const badId = await admin.post('/api/share', {
entityId: 'nope',
shareType: ENTITY_TYPE.website,
name: 'x',
parameters: {},
});
// Non-admin callers cannot share an entity that does not exist (or is not theirs).
const unknown = await user.post('/api/share', {
entityId: UNKNOWN_UUID,
shareType: ENTITY_TYPE.website,
name: 'x',
parameters: {},
});
const denied = await viewer.post('/api/share', {
entityId: websiteId,
shareType: ENTITY_TYPE.website,
name: 'x',
parameters: {},
});
const anonymous = await api.post('/api/share', {
entityId: websiteId,
shareType: ENTITY_TYPE.website,
name: 'x',
parameters: {},
});
expect(missing.status).toBe(400);
expect(badId.status).toBe(400);
expect(unknown.status).toBe(401);
expect(denied.status).toBe(401);
expect(anonymous.status).toBe(401);
});
test('GET /api/share/{slug} publicly resolves a share to a token', async ({ api, seed }) => {
const response = await api.get(`/api/share/${slug}`);
const seeded = await api.get(`/api/share/${seed.share.slug}`);
const unknown = await api.get('/api/share/no-such-slug');
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
shareId,
shareType: ENTITY_TYPE.website,
websiteId,
parameters: { overview: true },
token: expect.any(String),
});
expect(seeded.status).toBe(200);
expect(seeded.body).toMatchObject({ shareId: seed.share.id, websiteId: seed.website.id });
expect(unknown.status).toBe(404);
});
test('GET /api/share/id/{shareId} returns a share', async ({ admin, viewer, api }) => {
const response = await admin.get(`/api/share/id/${shareId}`);
const denied = await viewer.get(`/api/share/id/${shareId}`);
const anonymous = await api.get(`/api/share/id/${shareId}`);
expect(response.status).toBe(200);
expect(response.body).toMatchObject({ id: shareId, entityId: websiteId, slug });
expect(denied.status).toBe(401);
expect(anonymous.status).toBe(401);
});
test('POST /api/share/id/{shareId} updates a share', async ({ admin, viewer }) => {
const name = uniqueName('renamed');
slug = uniqueSlug('renamed');
const response = await admin.post(`/api/share/id/${shareId}`, {
name,
slug,
parameters: { overview: true, events: true },
});
const partial = await admin.post(`/api/share/id/${shareId}`, { name });
const unknown = await admin.post(`/api/share/id/${UNKNOWN_UUID}`, {
name,
slug: uniqueSlug('x'),
parameters: {},
});
const denied = await viewer.post(`/api/share/id/${shareId}`, {
name,
slug,
parameters: {},
});
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
id: shareId,
name,
slug,
parameters: { overview: true, events: true },
});
expect(partial.status).toBe(400);
expect(unknown.status).toBe(404);
expect(denied.status).toBe(401);
});
test('GET /api/websites/{websiteId}/shares lists website shares', async ({
admin,
viewer,
share,
seed,
}) => {
const response = await admin.get(`/api/websites/${websiteId}/shares`);
const paged = await admin.get(`/api/websites/${websiteId}/shares`, {
params: { page: 1, pageSize: 1 },
});
const denied = await viewer.get(`/api/websites/${websiteId}/shares`);
const shared = await (await share()).get(`/api/websites/${seed.website.id}/shares`);
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
data: expect.any(Array),
count: expect.any(Number),
page: 1,
pageSize: expect.any(Number),
});
expect(response.body.data.map((s: any) => s.id)).toContain(shareId);
expect(paged.status).toBe(200);
expect(paged.body.data).toHaveLength(1);
expect(paged.body.pageSize).toBe(1);
expect(denied.status).toBe(401);
expect(shared.status).toBe(401);
});
test('POST /api/websites/{websiteId}/shares creates a website share', async ({ admin, user }) => {
const name = uniqueName('site-share');
const response = await admin.post(`/api/websites/${websiteId}/shares`, { name });
const invalid = await admin.post(`/api/websites/${websiteId}/shares`, {});
const denied = await user.post(`/api/websites/${websiteId}/shares`, { name });
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
entityId: websiteId,
shareType: ENTITY_TYPE.website,
name,
slug: expect.any(String),
parameters: {},
});
expect(response.body.slug).toHaveLength(16);
expect(invalid.status).toBe(400);
expect(denied.status).toBe(401);
websiteShareId = response.body.id;
});
test('share parameters gate which sections a share token may access', async ({
admin,
api,
share,
seed,
}) => {
gatedSlug = uniqueSlug('gated');
const created = await admin.post('/api/share', {
entityId: seed.website.id,
shareType: ENTITY_TYPE.website,
name: uniqueName('gated'),
slug: gatedSlug,
parameters: { overview: true, events: false },
});
expect(created.status).toBe(200);
gatedShareId = created.body.id;
const resolved = await api.get(`/api/share/${gatedSlug}`);
expect(resolved.status).toBe(200);
expect(resolved.body).toMatchObject({
shareId: gatedShareId,
websiteId: seed.website.id,
parameters: { overview: true, events: false },
});
const client = await share(gatedSlug);
const allowed = await client.get(`/api/websites/${seed.website.id}/active`);
const denied = await client.get(`/api/websites/${seed.website.id}/events`, {
params: dateRange(seed),
});
expect(allowed.status).toBe(200);
expect(denied.status).toBe(401);
});
test('DELETE /api/share/id/{shareId} deletes a share', async ({ admin, viewer, api }) => {
const denied = await viewer.del(`/api/share/id/${shareId}`);
const response = await admin.del(`/api/share/id/${shareId}`);
const gone = await api.get(`/api/share/${slug}`);
expect(denied.status).toBe(401);
expect(response.status).toBe(200);
expect(response.body).toEqual({ ok: true });
expect(gone.status).toBe(404);
shareId = '';
});
});