create-disposabl-app 0.17.6 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/README.md +4 -0
  2. package/dist/disposabl.js +42 -9
  3. package/dist/index.js +64 -18
  4. package/dist/templates/default/apps/mobile/README.md +40 -0
  5. package/dist/templates/default/apps/mobile/app.config.js +46 -2
  6. package/dist/templates/default/apps/mobile/package.json +7 -1
  7. package/dist/templates/default/apps/mobile/plugins/with-android-signing.js +23 -1
  8. package/dist/templates/default/apps/mobile/scripts/fresh-android.mjs +24 -0
  9. package/dist/templates/default/apps/mobile/scripts/play-aab.mjs +176 -0
  10. package/dist/templates/default/apps/mobile/scripts/verify-play-aab.mjs +77 -0
  11. package/dist/templates/default/bun.lock +12 -3
  12. package/dist/templates/default/bunfig.toml +1 -0
  13. package/dist/templates/default/package.json +6 -2
  14. package/dist/templates/default/packages/backend/convex/files/guard.ts +2 -0
  15. package/dist/templates/default/packages/backend/convex/http.ts +11 -1
  16. package/dist/templates/default/packages/backend/convex/schema.ts +10 -0
  17. package/dist/templates/default/packages/backend/convex/telemetry.ts +12 -16
  18. package/dist/templates/default/packages/cli/telemetry.ts +40 -3
  19. package/dist/templates/default/packages/disposabl-kit/PAYMENTS.md +2 -0
  20. package/dist/templates/default/packages/disposabl-kit/client/components/account-deletion.tsx +111 -0
  21. package/dist/templates/default/packages/disposabl-kit/client/components/consent-identity.tsx +50 -0
  22. package/dist/templates/default/packages/disposabl-kit/client/components/legal-page.tsx +12 -1
  23. package/dist/templates/default/packages/disposabl-kit/client/lib/telemetry-lease.ts +200 -0
  24. package/dist/templates/default/packages/disposabl-kit/client/lib/telemetry.ts +8 -1
  25. package/dist/templates/default/packages/disposabl-kit/client/routes/consent.tsx +23 -1
  26. package/dist/templates/default/packages/disposabl-kit/client/routes/legal/delete-account.tsx +6 -1
  27. package/dist/templates/default/packages/disposabl-kit/client/routes/legal/support.tsx +33 -0
  28. package/dist/templates/default/packages/disposabl-kit/client/routes/pricing.tsx +13 -0
  29. package/dist/templates/default/packages/disposabl-kit/client/routes/route.tsx +19 -2
  30. package/dist/templates/default/packages/disposabl-kit/client/vite.config.ts +1 -0
  31. package/dist/templates/default/packages/disposabl-kit/legal/README.md +1 -1
  32. package/dist/templates/default/packages/disposabl-kit/legal/config.ts +32 -4
  33. package/dist/templates/default/packages/disposabl-kit/legal/index.ts +2 -2
  34. package/dist/templates/default/packages/disposabl-kit/legal/publisher.ts +6 -0
  35. package/dist/templates/default/packages/disposabl-kit/mobile/billing/account-billing.tsx +13 -9
  36. package/dist/templates/default/packages/disposabl-kit/mobile/billing/paywall.ts +28 -4
  37. package/dist/templates/default/packages/disposabl-kit/mobile/billing/policy.ts +31 -14
  38. package/dist/templates/default/packages/disposabl-kit/mobile/billing/web-checkout.ts +45 -34
  39. package/dist/templates/default/packages/disposabl-kit/mobile/components/settings-links.tsx +14 -0
  40. package/dist/templates/default/packages/disposabl-kit/mobile/expo-plugin.cjs +3 -0
  41. package/dist/templates/default/packages/disposabl-kit/mobile/index.ts +4 -0
  42. package/dist/templates/default/packages/disposabl-kit/mobile/layout.tsx +55 -8
  43. package/dist/templates/default/packages/disposabl-kit/server/billing-functions.ts +147 -124
  44. package/dist/templates/default/packages/disposabl-kit/server/billing-handoff.ts +113 -95
  45. package/dist/templates/default/packages/disposabl-kit/server/files-functions.ts +70 -54
  46. package/dist/templates/default/packages/disposabl-kit/server/http.ts +49 -21
  47. package/dist/templates/default/packages/disposabl-kit/server/lib/platform-spec.ts +25 -1
  48. package/dist/templates/default/packages/disposabl-kit/server/lib/telemetry.ts +375 -69
  49. package/dist/templates/default/packages/sdk/README.md +2 -0
  50. package/dist/templates/default/packages/sdk/src/analytics.ts +7 -2
  51. package/dist/templates/default/packages/skill/SKILL.md +4 -2
  52. package/dist/templates/default/packages/skill/references/store-submissions.md +25 -0
  53. package/dist/templates/default/packages/tests/README.md +28 -1
  54. package/dist/templates/default/packages/tests/account-deletion-ui.test.ts +21 -0
  55. package/dist/templates/default/packages/tests/auth-email-setup.test.ts +45 -0
  56. package/dist/templates/default/packages/tests/components/account-deletion.test.mjs +82 -0
  57. package/dist/templates/default/packages/tests/components/consent-identity.test.mjs +175 -0
  58. package/dist/templates/default/packages/tests/components/mobile-deletion-link.test.mjs +40 -0
  59. package/dist/templates/default/packages/tests/components/mobile-theme.test.mjs +56 -3
  60. package/dist/templates/default/packages/tests/components/pricing-identity.test.mjs +113 -0
  61. package/dist/templates/default/packages/tests/components/support-page.test.mjs +36 -0
  62. package/dist/templates/default/packages/tests/consent-identity.test.ts +13 -0
  63. package/dist/templates/default/packages/tests/desktop-macos-release.test.ts +153 -145
  64. package/dist/templates/default/packages/tests/flows/legal.e2e.ts +40 -3
  65. package/dist/templates/default/packages/tests/helpers/auth-email-setup/probe.test.mjs +44 -0
  66. package/dist/templates/default/packages/tests/helpers/mobile-simulator/preflight.py +80 -0
  67. package/dist/templates/default/packages/tests/helpers/mobile-simulator/test_preflight.py +57 -0
  68. package/dist/templates/default/packages/tests/mobile-android-e2e-network.test.ts +81 -1
  69. package/dist/templates/default/packages/tests/mobile-android-signing.test.ts +52 -0
  70. package/dist/templates/default/packages/tests/mobile-deletion-link.test.ts +21 -0
  71. package/dist/templates/default/packages/tests/mobile-ios-reachability-pin.test.ts +84 -0
  72. package/dist/templates/default/packages/tests/mobile-simulator-preflight.test.ts +16 -0
  73. package/dist/templates/default/packages/tests/native-billing.test.ts +288 -0
  74. package/dist/templates/default/packages/tests/pricing-identity.test.ts +13 -0
  75. package/dist/templates/default/packages/tests/sdk-analytics.test.ts +28 -2
  76. package/dist/templates/default/packages/tests/support-page.test.ts +13 -0
  77. package/dist/templates/default/packages/tests/telemetry.test.ts +389 -18
  78. package/dist/templates/default/packages/tests/ui-telemetry-lease.test.ts +346 -0
  79. package/dist/templates/default/patches/convex-mcp-account-erasure-alpha7.patch +466 -0
  80. package/dist/templates/default/vendor/README.md +61 -1
  81. package/dist/templates/default/vendor/convex-auth-040d8e36-disposabl.1.patch +890 -0
  82. package/dist/templates/default/vendor/convex-auth-040d8e36-disposabl.1.tgz +0 -0
  83. package/dist/templates/default/vendor/convex-auth-040d8e36-disposabl.2.patch +1060 -0
  84. package/dist/templates/default/vendor/convex-auth-040d8e36-disposabl.2.tgz +0 -0
  85. package/dist/templates/default/vendor/convex-auth-437831bd-disposabl.4.tgz +0 -0
  86. package/dist/templates/default/vendor/convex-auth-437831bd-disposabl.5.tgz +0 -0
  87. package/dist/templates/default/vendor/convex-auth-437831bd-disposabl.6.tgz +0 -0
  88. package/dist/templates/default/vendor/convex-auth-437831bd-disposabl.7.tgz +0 -0
  89. package/dist/templates/default/vendor/convex-auth-437831bd-disposabl.8.tgz +0 -0
  90. package/dist/templates/default/vendor/convex-auth-core-erasure-disposabl.6.patch +115 -0
  91. package/dist/templates/default/vendor/convex-auth-identity-cutoff-disposabl.7.patch +442 -0
  92. package/dist/templates/default/vendor/convex-auth-identity-erasure-disposabl.8.md +11 -0
  93. package/dist/templates/default/vendor/convex-auth-identity-erasure-disposabl.8.patch +78 -0
  94. package/dist/templates/default/vendor/convex-auth-marker-disposabl.4.patch +169 -0
  95. package/dist/templates/default/vendor/convex-auth-marker-password-disposabl.5.patch +126 -0
  96. package/package.json +1 -1
@@ -1,5 +1,7 @@
1
1
  import { afterEach, test } from "node:test";
2
2
  import assert from "node:assert/strict";
3
+ import { existsSync } from "node:fs";
4
+ import appConfig from "../../disposabl.json";
3
5
  import {
4
6
  captureTelemetry,
5
7
  telemetryEnabled,
@@ -11,8 +13,14 @@ import {
11
13
 
12
14
  const originalFetch = globalThis.fetch;
13
15
  const originalEnv = { ...process.env };
16
+ const originalFeatures = [...appConfig.features];
17
+ // Only the product implements account deletion; generated starters do not.
18
+ const hasDeletionGuard = existsSync(
19
+ new URL("../backend/convex/users/deletion.ts", import.meta.url),
20
+ );
14
21
  afterEach(() => {
15
22
  globalThis.fetch = originalFetch;
23
+ appConfig.features.splice(0, appConfig.features.length, ...originalFeatures);
16
24
  for (const key of Object.keys(process.env)) {
17
25
  if (!(key in originalEnv)) delete process.env[key];
18
26
  }
@@ -115,6 +123,9 @@ test("capture uses stable opaque authenticated identity and approved metadata",
115
123
  });
116
124
 
117
125
  function identityContext() {
126
+ const users = new Map<string, object>(
127
+ ["verified-user", "different-user", "a", "b"].map((id) => [id, {}]),
128
+ );
118
129
  const rows = new Map<
119
130
  string,
120
131
  { _id: string; hash: string; expiresAt: number; userId?: string }
@@ -122,6 +133,7 @@ function identityContext() {
122
133
  const jobs: any[] = [];
123
134
  let bucket: any = null;
124
135
  const db = {
136
+ normalizeId: (_table: string, id: string) => id,
125
137
  query: (table: string) => ({
126
138
  withIndex: (_index: string, predicate: (q: any) => unknown) => {
127
139
  let hash = "";
@@ -153,7 +165,8 @@ function identityContext() {
153
165
  value,
154
166
  );
155
167
  },
156
- get: async (_table: string, id: string) => rows.get(id) ?? null,
168
+ get: async (table: string, id: string) =>
169
+ (table === "users" ? users.get(id) : rows.get(id)) ?? null,
157
170
  delete: async (_table: string, id: string) => {
158
171
  rows.delete(id);
159
172
  },
@@ -181,13 +194,14 @@ function identityContext() {
181
194
  }
182
195
 
183
196
  test("page ingestion issues and binds an anonymous identity only to the verified account", async () => {
184
- const { page, claimIdentity } = await import("../backend/convex/telemetry");
197
+ const { page, bootstrap, claimIdentity } =
198
+ await import("../backend/convex/telemetry");
185
199
  const { default: app } = await import("../../disposabl.json");
186
200
  const originalFeatures = [...app.features];
187
201
  if (!app.features.includes("analytics" as never))
188
202
  (app.features as string[]).push("analytics");
189
203
  process.env.DISPOSABL_ENVIRONMENT = "production";
190
- process.env.POSTHOG_PROJECT_TOKEN = "test-token";
204
+ process.env.POSTHOG_PROJECT_TOKEN = "phc_fixture_public";
191
205
  process.env.POSTHOG_HOST = "https://us.i.posthog.com";
192
206
  delete process.env.E2E_TELEMETRY_DISABLED;
193
207
  delete process.env.POSTHOG_ENABLED;
@@ -203,7 +217,8 @@ test("page ingestion issues and binds an anonymous identity only to the verified
203
217
  getUserIdentity: async () =>
204
218
  user ? { subject: "verified-session" } : null,
205
219
  },
206
- runQuery: async () => ({ userId: user }),
220
+ runQuery: async (_ref: unknown, args: any) =>
221
+ "sessionId" in args ? { userId: user } : false,
207
222
  runMutation: async (_ref: unknown, args: any) =>
208
223
  (claimIdentity as any)._handler(fixture.ctx, args),
209
224
  };
@@ -228,11 +243,28 @@ test("page ingestion issues and binds an anonymous identity only to the verified
228
243
  user = "verified-user";
229
244
  const login = await (page as any)._handler(ctx, request(anonymousId, true));
230
245
  assert.equal((await login.json()).anonymousId, anonymousId);
231
- assert.equal(events[1].event, "$identify");
232
- assert.equal(events[1].properties.$anon_distinct_id, anonymousDistinct);
246
+ assert.equal(events[1].event, "page_view");
247
+ const linkedRow = [...fixture.rows.values()].find(
248
+ (row) => row.userId === user,
249
+ ) as any;
250
+ assert.equal(linkedRow.link.anonymous, anonymousDistinct);
251
+ assert.equal(linkedRow.link.distinct, events[1].properties.distinct_id);
252
+ const reserved = { ...linkedRow.link };
253
+ const jobCountBeforeBootstrap = fixture.jobs.length;
254
+ const bootstrapResult = await (bootstrap as any)._handler(
255
+ ctx,
256
+ new Request("https://example.com/telemetry/bootstrap", {
257
+ method: "POST",
258
+ headers: { Authorization: "Bearer private-token" },
259
+ body: JSON.stringify({ anonymousId }),
260
+ }),
261
+ );
262
+ assert.equal(bootstrapResult.status, 200);
263
+ assert.deepEqual(linkedRow.link, reserved);
264
+ assert.equal(fixture.jobs.length, jobCountBeforeBootstrap);
233
265
  assert.equal(
234
- events[2].properties.distinct_id,
235
- events[1].properties.distinct_id,
266
+ events.filter((event) => event.event === "$identify").length,
267
+ 0,
236
268
  );
237
269
  assert.doesNotMatch(
238
270
  JSON.stringify(events),
@@ -248,16 +280,23 @@ test("page ingestion issues and binds an anonymous identity only to the verified
248
280
  const crypto = await import("node:crypto");
249
281
  const fabricatedDistinct = crypto
250
282
  .createHash("sha256")
251
- .update(`disposabl:test-token:anonymous:${fabricated}`)
283
+ .update(`disposabl:phc_fixture_public:anonymous:${fabricated}`)
252
284
  .digest("hex");
253
- assert.notEqual(events[3].properties.$anon_distinct_id, fabricatedDistinct);
285
+ assert.ok(
286
+ ![...fixture.rows.values()].some(
287
+ (row: any) => row.link?.anonymous === fabricatedDistinct,
288
+ ),
289
+ );
254
290
  user = "different-user";
255
291
  const switched = await (page as any)._handler(
256
292
  ctx,
257
293
  request(anonymousId, true),
258
294
  );
259
295
  assert.notEqual((await switched.json()).anonymousId, anonymousId);
260
- assert.notEqual(events[5].properties.$anon_distinct_id, anonymousDistinct);
296
+ assert.notEqual(
297
+ events[3].properties.distinct_id,
298
+ events[1].properties.distinct_id,
299
+ );
261
300
  fixture.exhaust();
262
301
  const eventCount = events.length;
263
302
  const rowCount = fixture.rows.size;
@@ -293,6 +332,172 @@ test("page ingestion issues and binds an anonymous identity only to the verified
293
332
  }
294
333
  });
295
334
 
335
+ test("bootstrap issues opaque identities, links once, rotates accounts, and fails closed", async () => {
336
+ const { bootstrap, claimIdentity } =
337
+ await import("../backend/convex/telemetry");
338
+ const { default: app } = await import("../../disposabl.json");
339
+ const originalFeatures = [...app.features];
340
+ if (!app.features.includes("analytics" as never))
341
+ (app.features as string[]).push("analytics");
342
+ process.env.DISPOSABL_ENVIRONMENT = "production";
343
+ process.env.POSTHOG_PROJECT_TOKEN = "phc_fixture_public";
344
+ process.env.POSTHOG_HOST = "https://us.i.posthog.com";
345
+ delete process.env.E2E_TELEMETRY_DISABLED;
346
+ delete process.env.POSTHOG_ENABLED;
347
+ const events: any[] = [];
348
+ globalThis.fetch = (async (_url, init) => {
349
+ events.push(JSON.parse(String(init?.body)));
350
+ return new Response(null, { status: 200 });
351
+ }) as typeof fetch;
352
+ const fixture = identityContext();
353
+ let user: string | null = null;
354
+ let deleting = false;
355
+ const ctx = {
356
+ auth: {
357
+ getUserIdentity: async () => (user ? { subject: "session" } : null),
358
+ },
359
+ runQuery: async (_ref: unknown, args: any) =>
360
+ "sessionId" in args ? { userId: user } : deleting,
361
+ runMutation: async (_ref: unknown, args: any) =>
362
+ (claimIdentity as any)._handler(fixture.ctx, args),
363
+ };
364
+ const request = (id?: string, auth = false) =>
365
+ new Request("https://local/telemetry/bootstrap", {
366
+ method: "POST",
367
+ headers: auth ? { Authorization: "Bearer private" } : {},
368
+ body: JSON.stringify(id ? { anonymousId: id } : {}),
369
+ });
370
+ try {
371
+ const first = await (bootstrap as any)._handler(ctx, request());
372
+ assert.equal(first.status, 200);
373
+ assert.equal(first.headers.get("cache-control"), "no-store");
374
+ const anonymous = await first.json();
375
+ assert.deepEqual(
376
+ Object.keys(anonymous).sort(),
377
+ [
378
+ "anonymousId",
379
+ "distinctId",
380
+ "enabled",
381
+ "expiresAt",
382
+ "host",
383
+ "identified",
384
+ "projectToken",
385
+ ].sort(),
386
+ );
387
+ assert.equal(anonymous.identified, false);
388
+ assert.equal(anonymous.projectToken, "phc_fixture_public");
389
+ assert.ok(
390
+ anonymous.expiresAt > Date.now() &&
391
+ anonymous.expiresAt <= Date.now() + 30_000,
392
+ );
393
+ assert.equal(events.length, 0);
394
+ const repeat = await (
395
+ await (bootstrap as any)._handler(ctx, request(anonymous.anonymousId))
396
+ ).json();
397
+ assert.equal(repeat.distinctId, anonymous.distinctId);
398
+ user = "verified-user";
399
+ const linked = await (
400
+ await (bootstrap as any)._handler(
401
+ ctx,
402
+ request(anonymous.anonymousId, true),
403
+ )
404
+ ).json();
405
+ assert.equal(linked.identified, true);
406
+ assert.notEqual(linked.distinctId, anonymous.distinctId);
407
+ assert.equal(linked.anonymousId, anonymous.anonymousId);
408
+ assert.equal(events.length, 0);
409
+ const pending = [...fixture.rows.values()].find(
410
+ (row) => row.userId === "verified-user",
411
+ ) as any;
412
+ assert.equal(pending.link.anonymous, anonymous.distinctId);
413
+ assert.equal(pending.link.distinct, linked.distinctId);
414
+ const jobCount = fixture.jobs.length;
415
+ await (bootstrap as any)._handler(
416
+ ctx,
417
+ request(anonymous.anonymousId, true),
418
+ );
419
+ await Promise.all([
420
+ (bootstrap as any)._handler(ctx, request(anonymous.anonymousId, true)),
421
+ (bootstrap as any)._handler(ctx, request(anonymous.anonymousId, true)),
422
+ ]);
423
+ assert.equal(events.length, 0);
424
+ assert.equal(fixture.jobs.length, jobCount);
425
+ user = "different-user";
426
+ const switched = await (
427
+ await (bootstrap as any)._handler(
428
+ ctx,
429
+ request(anonymous.anonymousId, true),
430
+ )
431
+ ).json();
432
+ assert.notEqual(switched.anonymousId, anonymous.anonymousId);
433
+ assert.notEqual(switched.distinctId, linked.distinctId);
434
+ assert.equal(events.length, 0);
435
+ deleting = true;
436
+ assert.equal(
437
+ (
438
+ await (bootstrap as any)._handler(
439
+ ctx,
440
+ request(switched.anonymousId, true),
441
+ )
442
+ ).status,
443
+ hasDeletionGuard ? 401 : 200,
444
+ );
445
+ deleting = false;
446
+ user = null;
447
+ assert.equal(
448
+ (await (bootstrap as any)._handler(ctx, request(undefined, true))).status,
449
+ 401,
450
+ );
451
+ assert.equal(
452
+ (await (bootstrap as any)._handler(ctx, request("bad"))).status,
453
+ 400,
454
+ );
455
+ for (const extra of [{ userId: "spoof" }, { distinctId: "spoof" }]) {
456
+ const response = await (bootstrap as any)._handler(
457
+ ctx,
458
+ new Request("https://local/telemetry/bootstrap", {
459
+ method: "POST",
460
+ body: JSON.stringify(extra),
461
+ }),
462
+ );
463
+ assert.equal(response.status, 400);
464
+ }
465
+ const failClaim = {
466
+ ...ctx,
467
+ runMutation: async () => {
468
+ throw new Error("private");
469
+ },
470
+ };
471
+ assert.equal(
472
+ (await (bootstrap as any)._handler(failClaim, request())).status,
473
+ 503,
474
+ );
475
+ process.env.POSTHOG_PROJECT_TOKEN = "personal_secret";
476
+ assert.equal(
477
+ (await (bootstrap as any)._handler(ctx, request())).status,
478
+ 204,
479
+ );
480
+ process.env.POSTHOG_PROJECT_TOKEN = "phc_fixture_public";
481
+ fixture.exhaust();
482
+ assert.equal(
483
+ (await (bootstrap as any)._handler(ctx, request())).status,
484
+ 429,
485
+ );
486
+ process.env.E2E_TELEMETRY_DISABLED = "1";
487
+ assert.equal(
488
+ (await (bootstrap as any)._handler(ctx, request())).status,
489
+ 204,
490
+ );
491
+ assert.equal(events.length, 0);
492
+ assert.doesNotMatch(
493
+ JSON.stringify(events),
494
+ /verified-user|different-user|private/,
495
+ );
496
+ } finally {
497
+ app.features.splice(0, app.features.length, ...originalFeatures);
498
+ }
499
+ });
500
+
296
501
  test("capabilities expire, cannot be reused by another account, and schedule bounded cleanup", async () => {
297
502
  const { claimIdentity, expireIdentity } =
298
503
  await import("../backend/convex/telemetry");
@@ -312,6 +517,173 @@ test("capabilities expire, cannot be reused by another account, and schedule bou
312
517
  assert.equal(rows.has(original._id), false);
313
518
  });
314
519
 
520
+ test("link delivery retries one stable event and acknowledges only successful transport", async () => {
521
+ if (!appConfig.features.includes("analytics" as never))
522
+ (appConfig.features as string[]).push("analytics");
523
+ const { claimIdentity, getLink, beginLink, finishLink, deliverLink } =
524
+ await import("../backend/convex/telemetry");
525
+ process.env.DISPOSABL_ENVIRONMENT = "production";
526
+ process.env.POSTHOG_PROJECT_TOKEN = "phc_fixture_public";
527
+ process.env.POSTHOG_HOST = "https://us.i.posthog.com";
528
+ delete process.env.E2E_TELEMETRY_DISABLED;
529
+ delete process.env.POSTHOG_ENABLED;
530
+ const fixture = identityContext();
531
+ await (claimIdentity as any)._handler(fixture.ctx, { newHash: "issued" });
532
+ const row = [...fixture.rows.values()][0] as any;
533
+ const link = {
534
+ anonymous: "anon-hash",
535
+ distinct: "user-hash",
536
+ eventId: "12345678-1234-4123-8123-123456789abc",
537
+ };
538
+ assert.equal(
539
+ await (claimIdentity as any)._handler(fixture.ctx, {
540
+ hash: "issued",
541
+ newHash: "unused",
542
+ userId: "verified-user",
543
+ link,
544
+ }),
545
+ "linked",
546
+ );
547
+ const bodies: any[] = [];
548
+ globalThis.fetch = (async (_url, init) => {
549
+ bodies.push(JSON.parse(String(init?.body)));
550
+ return new Response(null, { status: bodies.length === 1 ? 503 : 200 });
551
+ }) as typeof fetch;
552
+ const actionCtx = {
553
+ runQuery: async (_ref: unknown, args: any) =>
554
+ (getLink as any)._handler(fixture.ctx, args),
555
+ runMutation: async (_ref: unknown, args: any) =>
556
+ ("attempts" in args ? beginLink : (finishLink as any))._handler(
557
+ fixture.ctx,
558
+ args,
559
+ ),
560
+ };
561
+ await (deliverLink as any)._handler(actionCtx, { id: row._id });
562
+ assert.equal(row.link.attempts, 1);
563
+ assert.equal(bodies.length, 1);
564
+ assert.equal(fixture.jobs.length, 4); // expiry, first delivery, watchdog, reserved retry
565
+ await (deliverLink as any)._handler(actionCtx, { id: row._id });
566
+ assert.equal(row.link, undefined);
567
+ assert.equal(bodies.length, 2);
568
+ assert.equal(bodies[0].uuid, link.eventId);
569
+ assert.equal(bodies[1].uuid, link.eventId);
570
+ assert.equal(bodies[0].timestamp, bodies[1].timestamp);
571
+ assert.equal(bodies[0].properties.$anon_distinct_id, link.anonymous);
572
+ await (deliverLink as any)._handler(actionCtx, { id: row._id });
573
+ assert.equal(bodies.length, 2);
574
+ });
575
+
576
+ test("link delivery respects disabled capture and the available owner guard", async () => {
577
+ if (!appConfig.features.includes("analytics" as never))
578
+ (appConfig.features as string[]).push("analytics");
579
+ const { claimIdentity, getLink, beginLink, finishLink, deliverLink } =
580
+ await import("../backend/convex/telemetry");
581
+ process.env.DISPOSABL_ENVIRONMENT = "production";
582
+ process.env.POSTHOG_PROJECT_TOKEN = "phc_fixture_public";
583
+ process.env.POSTHOG_HOST = "https://us.i.posthog.com";
584
+ process.env.E2E_TELEMETRY_DISABLED = "1";
585
+ const fixture = identityContext();
586
+ await (claimIdentity as any)._handler(fixture.ctx, { newHash: "issued" });
587
+ const row = [...fixture.rows.values()][0] as any;
588
+ await (claimIdentity as any)._handler(fixture.ctx, {
589
+ hash: "issued",
590
+ newHash: "new",
591
+ userId: "verified-user",
592
+ link: { anonymous: "a", distinct: "b", eventId: "id" },
593
+ });
594
+ let calls = 0;
595
+ globalThis.fetch = (async () => {
596
+ calls++;
597
+ return new Response(null, { status: 200 });
598
+ }) as typeof fetch;
599
+ let deleting = false;
600
+ const queryCtx = {
601
+ ...fixture.ctx,
602
+ db: {
603
+ ...fixture.ctx.db,
604
+ get: async (table: string, id: string) =>
605
+ table === "users" && deleting
606
+ ? { deletionRequestedAt: 1 }
607
+ : fixture.ctx.db.get(table, id),
608
+ },
609
+ };
610
+ const actionCtx = {
611
+ runQuery: async (_ref: unknown, args: any) =>
612
+ (getLink as any)._handler(queryCtx, args),
613
+ runMutation: async (_ref: unknown, args: any) =>
614
+ ("attempts" in args ? beginLink : (finishLink as any))._handler(
615
+ fixture.ctx,
616
+ args,
617
+ ),
618
+ };
619
+ await (deliverLink as any)._handler(actionCtx, { id: row._id });
620
+ assert.equal(calls, 0);
621
+ assert.equal(row.link.attempts, 1);
622
+ delete process.env.E2E_TELEMETRY_DISABLED;
623
+ deleting = true;
624
+ await (deliverLink as any)._handler(actionCtx, { id: row._id });
625
+ assert.equal(calls, hasDeletionGuard ? 0 : 1);
626
+ assert.equal(row.link?.attempts, hasDeletionGuard ? 1 : undefined);
627
+ });
628
+
629
+ test("link delivery stops after five failed attempts and leaves its pending record", async () => {
630
+ if (!appConfig.features.includes("analytics" as never))
631
+ (appConfig.features as string[]).push("analytics");
632
+ const { claimIdentity, getLink, beginLink, finishLink, deliverLink } =
633
+ await import("../backend/convex/telemetry");
634
+ process.env.DISPOSABL_ENVIRONMENT = "production";
635
+ process.env.POSTHOG_PROJECT_TOKEN = "phc_fixture_public";
636
+ process.env.POSTHOG_HOST = "https://us.i.posthog.com";
637
+ delete process.env.E2E_TELEMETRY_DISABLED;
638
+ delete process.env.POSTHOG_ENABLED;
639
+ const fixture = identityContext();
640
+ await (claimIdentity as any)._handler(fixture.ctx, { newHash: "issued" });
641
+ const row = [...fixture.rows.values()][0] as any;
642
+ await (claimIdentity as any)._handler(fixture.ctx, {
643
+ hash: "issued",
644
+ newHash: "new",
645
+ userId: "verified-user",
646
+ link: { anonymous: "a", distinct: "b", eventId: "id" },
647
+ });
648
+ let calls = 0;
649
+ globalThis.fetch = (async () => {
650
+ calls++;
651
+ return new Response(null, { status: 503 });
652
+ }) as typeof fetch;
653
+ const actionCtx = {
654
+ runQuery: async (_ref: unknown, args: any) =>
655
+ (getLink as any)._handler(fixture.ctx, args),
656
+ runMutation: async (_ref: unknown, args: any) =>
657
+ ("attempts" in args ? beginLink : (finishLink as any))._handler(
658
+ fixture.ctx,
659
+ args,
660
+ ),
661
+ };
662
+ for (let attempt = 0; attempt < 7; attempt++)
663
+ await (deliverLink as any)._handler(actionCtx, { id: row._id });
664
+ assert.equal(calls, 5);
665
+ assert.equal(row.link.attempts, 5);
666
+ const previous = { ...row.link };
667
+ const jobsBefore = fixture.jobs.length;
668
+ const claim = () =>
669
+ (claimIdentity as any)._handler(fixture.ctx, {
670
+ hash: "issued",
671
+ newHash: "unused",
672
+ userId: "verified-user",
673
+ });
674
+ // Current owner refresh cannot create an immediate retry loop.
675
+ await claim();
676
+ assert.equal(fixture.jobs.length, jobsBefore);
677
+ row.link.retryAfter = Date.now() - 1;
678
+ await claim();
679
+ assert.equal(row.link.attempts, 0);
680
+ assert.equal(row.link.eventId, previous.eventId);
681
+ assert.equal(row.link.createdAt, previous.createdAt);
682
+ assert.equal(fixture.jobs.length, jobsBefore + 2);
683
+ await claim();
684
+ assert.equal(fixture.jobs.length, jobsBefore + 2);
685
+ });
686
+
315
687
  test("issuance budget refills at one identity per minute", async () => {
316
688
  const { claimIdentity } = await import("../backend/convex/telemetry");
317
689
  const fixture = identityContext();
@@ -335,7 +707,7 @@ test("issuance budget refills at one identity per minute", async () => {
335
707
  }
336
708
  });
337
709
 
338
- test("anonymous capture and identify use the same opaque identity", async () => {
710
+ test("capture emits only the categorized event; identity merges belong to the durable claim", async () => {
339
711
  process.env.CONVEX_SITE_URL = config.productionConvexSiteUrl;
340
712
  delete process.env.E2E_TELEMETRY_DISABLED;
341
713
  delete process.env.POSTHOG_ENABLED;
@@ -347,13 +719,12 @@ test("anonymous capture and identify use the same opaque identity", async () =>
347
719
  const id = "12345678-1234-4123-8123-123456789abc";
348
720
  await captureTelemetry("page_view", { page: "home" }, undefined, config, id);
349
721
  await captureTelemetry("page_view", { page: "login" }, "user-a", config, id);
350
- assert.equal(
351
- events[0].properties.distinct_id,
352
- events[1].properties.$anon_distinct_id,
722
+ assert.deepEqual(
723
+ events.map((event) => event.event),
724
+ ["page_view", "page_view"],
353
725
  );
354
- assert.equal(events[1].event, "$identify");
355
- assert.equal(
356
- events[2].properties.distinct_id,
726
+ assert.notEqual(
727
+ events[0].properties.distinct_id,
357
728
  events[1].properties.distinct_id,
358
729
  );
359
730
  });