create-disposabl-app 0.15.0 → 0.16.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 (90) hide show
  1. package/README.md +15 -1
  2. package/dist/disposabl.js +357 -50
  3. package/dist/index.js +8973 -154
  4. package/dist/templates/default/apps/desktop/forge.config.mts +2 -1
  5. package/dist/templates/default/apps/docs/content/getting-started.mdx +1 -1
  6. package/dist/templates/default/apps/mobile/package.json +1 -0
  7. package/dist/templates/default/apps/web/eslint.config.ts +5 -1
  8. package/dist/templates/default/apps/web/public/disposabl-logo-light.svg +8 -0
  9. package/dist/templates/default/apps/web/public/landing/coast.jpg +0 -0
  10. package/dist/templates/default/apps/web/public/landing/community.jpg +0 -0
  11. package/dist/templates/default/apps/web/public/landing/stores/app-store.svg +46 -0
  12. package/dist/templates/default/apps/web/public/landing/stores/google-play.png +0 -0
  13. package/dist/templates/default/apps/web/public/landing/taste/LICENSE +21 -0
  14. package/dist/templates/default/apps/web/public/landing/taste/init.js +3 -0
  15. package/dist/templates/default/apps/web/public/landing/taste/moments/lib.js +189 -0
  16. package/dist/templates/default/apps/web/public/landing/taste/moments/moments.js +109 -0
  17. package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.css +8 -0
  18. package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.js +35 -0
  19. package/dist/templates/default/apps/web/public/landing/taste/motion/motion.css +321 -0
  20. package/dist/templates/default/apps/web/public/landing/taste/motion/motion.js +406 -0
  21. package/dist/templates/default/apps/web/routes.config.ts +14 -0
  22. package/dist/templates/default/apps/web/src/router/__root.tsx +8 -2
  23. package/dist/templates/default/apps/web/tsr.config.json +1 -1
  24. package/dist/templates/default/apps/web/vite.config.ts +10 -2
  25. package/dist/templates/default/bun.lock +6 -0
  26. package/dist/templates/default/bunfig.toml +1 -0
  27. package/dist/templates/default/disposabl.json +1 -1
  28. package/dist/templates/default/packages/backend/convex/authSetup.ts +2 -5
  29. package/dist/templates/default/packages/backend/convex/billing.ts +5 -0
  30. package/dist/templates/default/packages/backend/convex/billingHandoff.ts +6 -0
  31. package/dist/templates/default/packages/backend/convex/convex.config.ts +8 -0
  32. package/dist/templates/default/packages/backend/convex/http.ts +6 -0
  33. package/dist/templates/default/packages/backend/convex/revenuecat.ts +13 -0
  34. package/dist/templates/default/packages/backend/convex/schema.ts +44 -0
  35. package/dist/templates/default/packages/backend/convex/telemetry.ts +41 -5
  36. package/dist/templates/default/packages/backend/generate.ts +6 -0
  37. package/dist/templates/default/packages/cli/auth.ts +36 -3
  38. package/dist/templates/default/packages/cli/build.ts +6 -0
  39. package/dist/templates/default/packages/cli/client.ts +189 -19
  40. package/dist/templates/default/packages/cli/errors.ts +18 -0
  41. package/dist/templates/default/packages/cli/telemetry-build.ts +27 -0
  42. package/dist/templates/default/packages/cli/telemetry.ts +266 -0
  43. package/dist/templates/default/packages/disposabl-kit/PAYMENTS.md +23 -5
  44. package/dist/templates/default/packages/disposabl-kit/client/lib/telemetry.ts +24 -2
  45. package/dist/templates/default/packages/disposabl-kit/client/lib/theme.ts +1 -0
  46. package/dist/templates/default/packages/disposabl-kit/client/routes/billing-login.tsx +92 -0
  47. package/dist/templates/default/packages/disposabl-kit/client/routes/pricing.tsx +113 -43
  48. package/dist/templates/default/packages/disposabl-kit/client/routes/route.tsx +13 -5
  49. package/dist/templates/default/packages/disposabl-kit/client/vite.config.ts +3 -2
  50. package/dist/templates/default/packages/disposabl-kit/lib/telemetry.ts +148 -12
  51. package/dist/templates/default/packages/disposabl-kit/mobile/billing/account-billing.tsx +143 -40
  52. package/dist/templates/default/packages/disposabl-kit/mobile/billing/paywall.ts +83 -0
  53. package/dist/templates/default/packages/disposabl-kit/mobile/billing/policy.ts +125 -0
  54. package/dist/templates/default/packages/disposabl-kit/mobile/billing/session.ts +31 -3
  55. package/dist/templates/default/packages/disposabl-kit/mobile/billing/topup.ts +99 -0
  56. package/dist/templates/default/packages/disposabl-kit/mobile/billing/use-topup.ts +116 -0
  57. package/dist/templates/default/packages/disposabl-kit/mobile/billing/web-checkout.ts +190 -0
  58. package/dist/templates/default/packages/disposabl-kit/mobile/layout.tsx +9 -2
  59. package/dist/templates/default/packages/disposabl-kit/package.json +10 -1
  60. package/dist/templates/default/packages/disposabl-kit/payments/checkout-attempt.ts +64 -0
  61. package/dist/templates/default/packages/disposabl-kit/payments/config.ts +16 -3
  62. package/dist/templates/default/packages/disposabl-kit/server/billing-functions.ts +218 -9
  63. package/dist/templates/default/packages/disposabl-kit/server/billing-handoff.ts +126 -0
  64. package/dist/templates/default/packages/disposabl-kit/server/billing-native-capability.ts +12 -0
  65. package/dist/templates/default/packages/disposabl-kit/server/billing-setup.ts +2 -0
  66. package/dist/templates/default/packages/disposabl-kit/server/billing.ts +31 -0
  67. package/dist/templates/default/packages/disposabl-kit/server/browser-session.ts +27 -0
  68. package/dist/templates/default/packages/disposabl-kit/server/lib/telemetry.ts +173 -59
  69. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-contract.ts +233 -0
  70. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-functions.ts +554 -0
  71. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-validator.ts +76 -0
  72. package/dist/templates/default/packages/reconfigure-skill/SKILL.md +36 -0
  73. package/dist/templates/default/packages/sdk/README.md +21 -0
  74. package/dist/templates/default/packages/sdk/src/analytics.ts +102 -0
  75. package/dist/templates/default/packages/sdk/src/index.ts +2 -0
  76. package/dist/templates/default/packages/skill/SKILL.md +2 -0
  77. package/dist/templates/default/packages/skill/references/agent.md +2 -0
  78. package/dist/templates/default/packages/tests/billing-attempts.test.ts +156 -0
  79. package/dist/templates/default/packages/tests/billing-handoff.test.ts +116 -0
  80. package/dist/templates/default/packages/tests/billing-setup.test.ts +4 -3
  81. package/dist/templates/default/packages/tests/billing.test.ts +48 -4
  82. package/dist/templates/default/packages/tests/desktop-packaging.test.ts +12 -0
  83. package/dist/templates/default/packages/tests/flows/seo.e2e.ts +3 -1
  84. package/dist/templates/default/packages/tests/native-billing.test.ts +417 -0
  85. package/dist/templates/default/packages/tests/page-telemetry.test.ts +218 -0
  86. package/dist/templates/default/packages/tests/revenuecat-inbox.test.ts +476 -0
  87. package/dist/templates/default/packages/tests/sdk-analytics.test.ts +158 -0
  88. package/dist/templates/default/packages/tests/telemetry.test.ts +272 -55
  89. package/package.json +1 -1
  90. package/dist/templates/default/apps/web/src/routeTree.gen.ts +0 -578
@@ -0,0 +1,476 @@
1
+ import { describe, test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import {
4
+ balanceId,
5
+ balanceRequest,
6
+ parseRevenueCatEvent,
7
+ revenueCatConfigs,
8
+ selectRevenueCatRoute,
9
+ resolveRevenueCatRoute,
10
+ parseTransfer,
11
+ type PurchaseEvent,
12
+ } from "../disposabl-kit/server/revenuecat-contract";
13
+ import {
14
+ receive,
15
+ claim,
16
+ complete,
17
+ confirmFullRefund,
18
+ receiveTransfer,
19
+ webhookAuthorized,
20
+ webhook,
21
+ dispatchBalance,
22
+ } from "../disposabl-kit/server/revenuecat-functions";
23
+
24
+ import { creditQuantity } from "../disposabl-kit/payments/config";
25
+ import { paymentsEnabled } from "../disposabl-kit/features";
26
+
27
+ const config = {
28
+ authorization: "Bearer test",
29
+ project: "proj",
30
+ app: "app",
31
+ environment: "SANDBOX" as const,
32
+ product: "topup",
33
+ store: "APP_STORE" as const,
34
+ autumnKey: "key",
35
+ };
36
+ const event: PurchaseEvent = {
37
+ deliveryId: "event-1",
38
+ project: "proj",
39
+ app: "app",
40
+ environment: "SANDBOX",
41
+ store: "APP_STORE",
42
+ product: "topup",
43
+ customer: "user-1",
44
+ transaction: "tx-1",
45
+ type: "purchase",
46
+ };
47
+ const call = (fn: unknown, ctx: unknown, args: unknown): Promise<any> =>
48
+ (fn as any)._handler(ctx, args);
49
+
50
+ // This fixture exercises mutation handlers and their scheduled operations. Convex's
51
+ // serializable mutations, not this fixture, provide transaction conflict handling.
52
+ function fixture() {
53
+ const tables: Record<string, any[]> = {
54
+ revenueCatPurchases: [],
55
+ revenueCatDeliveries: [],
56
+ revenueCatTransfers: [],
57
+ revenueCatTransferHolds: [],
58
+ };
59
+ const jobs: any[] = [];
60
+ const db = {
61
+ normalizeId: (_table: string, id: string) =>
62
+ id.startsWith("user-") ? id : null,
63
+ get: async (_table: string, id: string) =>
64
+ id.startsWith("user-") ? { _id: id } : null,
65
+ query: (table: string) => ({
66
+ withIndex: (_index: string, filter: (q: any) => any) => {
67
+ const fields: Record<string, unknown> = {};
68
+ const q = {
69
+ eq: (name: string, value: unknown) => {
70
+ fields[name] = value;
71
+ return q;
72
+ },
73
+ };
74
+ filter(q);
75
+ return {
76
+ take: async (n: number) =>
77
+ tables[table]!.filter((row) =>
78
+ Object.entries(fields).every(
79
+ ([key, value]) => row[key] === value,
80
+ ),
81
+ ).slice(0, n),
82
+ unique: async () =>
83
+ tables[table]!.find((row) =>
84
+ Object.entries(fields).every(
85
+ ([key, value]) => row[key] === value,
86
+ ),
87
+ ) ?? null,
88
+ };
89
+ },
90
+ }),
91
+ insert: async (table: string, value: any) => {
92
+ const id = `${table}-${tables[table]!.length}`;
93
+ tables[table]!.push({ ...value, _id: id });
94
+ return id;
95
+ },
96
+ patch: async (table: string, id: string, value: any) =>
97
+ Object.assign(
98
+ tables[table]!.find((row) => row._id === id),
99
+ value,
100
+ ),
101
+ };
102
+ return {
103
+ tables,
104
+ jobs,
105
+ ctx: {
106
+ db,
107
+ scheduler: {
108
+ runAfter: async (_delay: number, _ref: unknown, args: unknown) =>
109
+ jobs.push(args),
110
+ },
111
+ },
112
+ };
113
+ }
114
+ const scopedClaim = (balanceId: string) => ({
115
+ balanceId,
116
+ project: config.project,
117
+ app: config.app,
118
+ environment: config.environment,
119
+ store: config.store,
120
+ product: config.product,
121
+ });
122
+
123
+ describe("RevenueCat consumable inbox", () => {
124
+ test("validated routes select both stores and reject ambiguous or removed configuration", () => {
125
+ const env = {
126
+ REVENUECAT_CONSUMABLE_OWNER: "custom",
127
+ REVENUECAT_WEBHOOK_AUTH: "Bearer test",
128
+ REVENUECAT_PROJECT_ID: "proj",
129
+ REVENUECAT_ENVIRONMENT: "SANDBOX",
130
+ AUTUMN_SECRET_KEY: "key",
131
+ REVENUECAT_ROUTES: JSON.stringify([
132
+ { app: "app", store: "APP_STORE", product: "topup" },
133
+ { app: "android", store: "PLAY_STORE", product: "coins" },
134
+ ]),
135
+ };
136
+ const configs = revenueCatConfigs(true, env)!;
137
+ assert.equal(configs.length, 2);
138
+ const payload = (app: string, store: string, product: string) => ({
139
+ api_version: "1.0",
140
+ event: {
141
+ id: "id",
142
+ app_id: app,
143
+ store,
144
+ product_id: product,
145
+ environment: "SANDBOX",
146
+ app_user_id: "user-1",
147
+ transaction_id: "tx",
148
+ type: "NON_RENEWING_PURCHASE",
149
+ },
150
+ });
151
+ assert.equal(
152
+ selectRevenueCatRoute(payload("app", "APP_STORE", "topup"), configs)?.app,
153
+ "app",
154
+ );
155
+ assert.equal(
156
+ selectRevenueCatRoute(payload("android", "PLAY_STORE", "coins"), configs)
157
+ ?.app,
158
+ "android",
159
+ );
160
+ assert.equal(
161
+ selectRevenueCatRoute(payload("android", "APP_STORE", "coins"), configs),
162
+ null,
163
+ );
164
+ assert.equal(resolveRevenueCatRoute(event, configs.slice(1)), null);
165
+ assert.equal(
166
+ resolveRevenueCatRoute(event, [configs[0]!, configs[0]!]),
167
+ null,
168
+ );
169
+ assert.equal(
170
+ revenueCatConfigs(true, {
171
+ ...env,
172
+ REVENUECAT_ROUTES: JSON.stringify([
173
+ { app: "app", store: "APP_STORE", product: "topup" },
174
+ { app: "app", store: "PLAY_STORE", product: "coins" },
175
+ ]),
176
+ }),
177
+ null,
178
+ );
179
+ assert.equal(
180
+ parseTransfer(
181
+ {
182
+ api_version: "1.0",
183
+ event: {
184
+ id: "t",
185
+ type: "TRANSFER",
186
+ app_id: "app",
187
+ transferred_from: ["user-1"],
188
+ transferred_to: ["user-2"],
189
+ },
190
+ },
191
+ configs[0]!,
192
+ ),
193
+ null,
194
+ );
195
+ });
196
+ test("transfer holds affect the other app in the same project and environment", async () => {
197
+ const f = fixture();
198
+ const android = {
199
+ ...event,
200
+ app: "android",
201
+ store: "PLAY_STORE",
202
+ product: "coins",
203
+ };
204
+ const id = await balanceId(android);
205
+ await call(receive, f.ctx, { event: android, balanceId: id });
206
+ await call(receiveTransfer, f.ctx, {
207
+ project: "proj",
208
+ app: "app",
209
+ environment: "SANDBOX",
210
+ deliveryId: "transfer",
211
+ from: ["user-1"],
212
+ to: ["user-2"],
213
+ });
214
+ assert.equal(
215
+ await call(claim, f.ctx, {
216
+ ...scopedClaim(id),
217
+ app: "android",
218
+ store: "PLAY_STORE",
219
+ product: "coins",
220
+ }),
221
+ null,
222
+ );
223
+ });
224
+ test("lost provider response and crash after success never dispatch again", async () => {
225
+ for (const failure of ["lost response", "crash"]) {
226
+ const f = fixture();
227
+ const id = await balanceId(event);
228
+ await call(receive, f.ctx, { event, balanceId: id });
229
+ let calls = 0;
230
+ const fetcher = (async () => {
231
+ calls++;
232
+ if (failure === "lost response")
233
+ throw new Error("connection lost after commit");
234
+ return Response.json({ success: true });
235
+ }) as typeof fetch;
236
+ const claimOperation = (scope: unknown) => call(claim, f.ctx, scope);
237
+ const completeOperation = () => {
238
+ throw new Error("worker crashed");
239
+ };
240
+ await dispatchBalance(
241
+ config,
242
+ id,
243
+ claimOperation,
244
+ completeOperation,
245
+ fetcher,
246
+ );
247
+ await dispatchBalance(
248
+ config,
249
+ id,
250
+ claimOperation,
251
+ completeOperation,
252
+ fetcher,
253
+ );
254
+ assert.equal(calls, 1);
255
+ assert.equal(f.tables.revenueCatPurchases![0].state, "grant_unknown");
256
+ }
257
+ });
258
+ test(
259
+ "payments-off starter rejects webhook before any mutation",
260
+ { skip: paymentsEnabled },
261
+ async () => {
262
+ const response = await call(
263
+ webhook,
264
+ {
265
+ runMutation: () => {
266
+ throw new Error("unexpected mutation");
267
+ },
268
+ },
269
+ new Request("https://example.test/revenuecat/webhook", {
270
+ method: "POST",
271
+ }),
272
+ );
273
+ assert.equal(response.status, 503);
274
+ },
275
+ );
276
+ test("webhook authorization requires the exact configured header", async () => {
277
+ assert.equal(
278
+ await webhookAuthorized(
279
+ new Request("https://example.test"),
280
+ config.authorization,
281
+ ),
282
+ false,
283
+ );
284
+ assert.equal(
285
+ await webhookAuthorized(
286
+ new Request("https://example.test", {
287
+ headers: { authorization: "Bearer wrong" },
288
+ }),
289
+ config.authorization,
290
+ ),
291
+ false,
292
+ );
293
+ assert.equal(
294
+ await webhookAuthorized(
295
+ new Request("https://example.test", {
296
+ headers: { authorization: config.authorization },
297
+ }),
298
+ config.authorization,
299
+ ),
300
+ true,
301
+ );
302
+ });
303
+ test("transfer holds only affected customers until operator review", async () => {
304
+ const f = fixture();
305
+ const id = await balanceId(event);
306
+ await call(receive, f.ctx, { event, balanceId: id });
307
+ const transfer = {
308
+ project: "proj",
309
+ app: "app",
310
+ environment: "SANDBOX",
311
+ deliveryId: "transfer",
312
+ from: ["user-1"],
313
+ to: ["user-2"],
314
+ };
315
+ await call(receiveTransfer, f.ctx, transfer);
316
+ await call(receiveTransfer, f.ctx, transfer);
317
+ assert.equal(await call(claim, f.ctx, scopedClaim(id)), null);
318
+ const other = {
319
+ ...event,
320
+ customer: "user-3",
321
+ transaction: "other",
322
+ deliveryId: "other",
323
+ };
324
+ const otherId = await balanceId(other);
325
+ await call(receive, f.ctx, { event: other, balanceId: otherId });
326
+ assert.equal(
327
+ (await call(claim, f.ctx, scopedClaim(otherId))).kind,
328
+ "grant",
329
+ );
330
+ });
331
+ test("disabled and incomplete configuration cannot read or dispatch payments", () => {
332
+ assert.equal(revenueCatConfigs(false, {}), null);
333
+ assert.equal(
334
+ revenueCatConfigs(true, { REVENUECAT_CONSUMABLE_OWNER: "custom" }),
335
+ null,
336
+ );
337
+ });
338
+ test("validates scope, customer and transaction without trusting a client", () => {
339
+ const payload = {
340
+ api_version: "1.0",
341
+ event: {
342
+ id: "id",
343
+ app_id: "app",
344
+ app_user_id: "user-1",
345
+ original_app_user_id: "user-1",
346
+ transaction_id: "t",
347
+ product_id: "topup",
348
+ environment: "SANDBOX",
349
+ store: "APP_STORE",
350
+ type: "NON_RENEWING_PURCHASE",
351
+ },
352
+ };
353
+ assert.equal(parseRevenueCatEvent(payload, config)?.type, "purchase");
354
+ for (const change of [
355
+ { app_id: "other" },
356
+ { environment: "PRODUCTION" },
357
+ { product_id: "other" },
358
+ { store: "PLAY_STORE" },
359
+ { transaction_id: "" },
360
+ { app_user_id: "$RCAnonymousID:x" },
361
+ { original_app_user_id: "user-2" },
362
+ { project_id: "other" },
363
+ ]) {
364
+ assert.equal(
365
+ parseRevenueCatEvent(
366
+ { ...payload, event: { ...payload.event, ...change } },
367
+ config,
368
+ ),
369
+ null,
370
+ );
371
+ }
372
+ });
373
+ test("replays and account switches cannot create another grant; different transactions can", async () => {
374
+ const f = fixture();
375
+ const id = await balanceId(event);
376
+ await call(receive, f.ctx, { event, balanceId: id });
377
+ await call(receive, f.ctx, { event, balanceId: id });
378
+ await call(receive, f.ctx, {
379
+ event: { ...event, deliveryId: "event-2" },
380
+ balanceId: id,
381
+ });
382
+ assert.equal(
383
+ await call(receive, f.ctx, {
384
+ event: { ...event, deliveryId: "event-3", customer: "user-2" },
385
+ balanceId: id,
386
+ }),
387
+ "operator_review",
388
+ );
389
+ const second = { ...event, deliveryId: "event-4", transaction: "tx-2" };
390
+ await call(receive, f.ctx, {
391
+ event: second,
392
+ balanceId: await balanceId(second),
393
+ });
394
+ assert.equal(f.tables.revenueCatPurchases!.length, 2);
395
+ assert.equal(f.jobs.length, 2);
396
+ assert.deepEqual(balanceRequest(event.customer, id, "grant"), {
397
+ customer_id: event.customer,
398
+ feature_id: "credits",
399
+ balance_id: id,
400
+ included_grant: creditQuantity("topup"),
401
+ });
402
+ assert.notEqual(await balanceId(event), await balanceId(second));
403
+ });
404
+ test("dispatch claim survives response loss or worker crash and cannot be claimed twice", async () => {
405
+ const f = fixture();
406
+ const id = await balanceId(event);
407
+ await call(receive, f.ctx, { event, balanceId: id });
408
+ assert.equal(
409
+ await call(claim, f.ctx, {
410
+ ...scopedClaim(id),
411
+ environment: "PRODUCTION",
412
+ }),
413
+ null,
414
+ );
415
+ assert.deepEqual(await call(claim, f.ctx, scopedClaim(id)), {
416
+ customer: event.customer,
417
+ kind: "grant",
418
+ });
419
+ await call(receive, f.ctx, {
420
+ event: { ...event, deliveryId: "retry" },
421
+ balanceId: id,
422
+ });
423
+ assert.equal(await call(claim, f.ctx, scopedClaim(id)), null);
424
+ assert.equal(f.tables.revenueCatPurchases![0].state, "grant_unknown");
425
+ await call(complete, f.ctx, { balanceId: id, kind: "grant" });
426
+ assert.equal(f.tables.revenueCatPurchases![0].state, "granted");
427
+ });
428
+ test("refund ordering prevents a late grant and holds granted refunds for review", async () => {
429
+ const f = fixture();
430
+ const id = await balanceId(event);
431
+ await call(receive, f.ctx, {
432
+ event: { ...event, type: "cancel" },
433
+ balanceId: id,
434
+ });
435
+ await call(receive, f.ctx, {
436
+ event: { ...event, deliveryId: "purchase" },
437
+ balanceId: id,
438
+ });
439
+ assert.equal(await call(claim, f.ctx, scopedClaim(id)), null);
440
+ assert.equal(f.tables.revenueCatPurchases![0].state, "tombstone");
441
+ const g = fixture();
442
+ await call(receive, g.ctx, { event, balanceId: id });
443
+ await call(claim, g.ctx, scopedClaim(id));
444
+ await call(receive, g.ctx, {
445
+ event: { ...event, deliveryId: "cancel", type: "cancel" },
446
+ balanceId: id,
447
+ });
448
+ await call(complete, g.ctx, { balanceId: id, kind: "grant" });
449
+ assert.equal(g.tables.revenueCatPurchases![0].state, "refund_review");
450
+ assert.equal(
451
+ await call(confirmFullRefund, g.ctx, {
452
+ balanceId: id,
453
+ evidence: "provider case 1",
454
+ }),
455
+ true,
456
+ );
457
+ assert.deepEqual(await call(claim, g.ctx, scopedClaim(id)), {
458
+ customer: event.customer,
459
+ kind: "refund",
460
+ });
461
+ assert.deepEqual(balanceRequest(event.customer, id, "refund"), {
462
+ customer_id: event.customer,
463
+ feature_id: "credits",
464
+ balance_id: id,
465
+ remaining: 0,
466
+ });
467
+ assert.equal(await call(claim, g.ctx, scopedClaim(id)), null);
468
+ assert.equal(g.tables.revenueCatPurchases![0].state, "refund_unknown");
469
+ await call(receive, g.ctx, {
470
+ event: { ...event, deliveryId: "refund-replay", type: "cancel" },
471
+ balanceId: id,
472
+ });
473
+ assert.equal(await call(claim, g.ctx, scopedClaim(id)), null);
474
+ assert.equal(g.tables.revenueCatPurchases![0].state, "refund_unknown");
475
+ });
476
+ });
@@ -0,0 +1,158 @@
1
+ import { afterEach, beforeEach, test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { createSdkAnalytics } from "../sdk/src/analytics";
4
+
5
+ const inheritedFlags = {
6
+ E2E_TELEMETRY_DISABLED: process.env.E2E_TELEMETRY_DISABLED,
7
+ DO_NOT_TRACK: process.env.DO_NOT_TRACK,
8
+ };
9
+ beforeEach(() => {
10
+ // These tests use in-memory sinks only; each opt-out is exercised explicitly.
11
+ delete process.env.E2E_TELEMETRY_DISABLED;
12
+ delete process.env.DO_NOT_TRACK;
13
+ });
14
+ afterEach(() => {
15
+ for (const [key, value] of Object.entries(inheritedFlags)) {
16
+ if (value === undefined) delete process.env[key];
17
+ else process.env[key] = value;
18
+ }
19
+ });
20
+
21
+ test("SDK analytics respects opt-out, production and E2E gates", async () => {
22
+ const events: unknown[] = [];
23
+ const capture = (event: unknown) => {
24
+ events.push(event);
25
+ };
26
+ for (const config of [
27
+ { enabled: false, environment: "production" as const },
28
+ { enabled: true, environment: "development" as const },
29
+ { enabled: true, environment: "test" as const },
30
+ { enabled: true, environment: "production" as const, e2e: true },
31
+ ])
32
+ await createSdkAnalytics({ ...config, capture }).track(
33
+ "appsList",
34
+ async () => ({}),
35
+ );
36
+ const old = process.env.E2E_TELEMETRY_DISABLED;
37
+ process.env.E2E_TELEMETRY_DISABLED = "1";
38
+ try {
39
+ await createSdkAnalytics({
40
+ enabled: true,
41
+ environment: "production",
42
+ capture,
43
+ }).track("appsList", async () => ({}));
44
+ } finally {
45
+ if (old === undefined) delete process.env.E2E_TELEMETRY_DISABLED;
46
+ else process.env.E2E_TELEMETRY_DISABLED = old;
47
+ }
48
+ assert.deepEqual(events, []);
49
+ });
50
+
51
+ test("SDK analytics allows only operation and outcome, preserving results and errors", async () => {
52
+ const events: unknown[] = [];
53
+ const analytics = createSdkAnalytics({
54
+ environment: "production",
55
+ capture: (event) => {
56
+ events.push(event);
57
+ },
58
+ });
59
+ const result = {
60
+ data: { secret: "private" },
61
+ response: { url: "https://private" },
62
+ };
63
+ assert.equal(await analytics.track("appsList", async () => result), result);
64
+ await analytics.track("appsCreate", async () => ({ error: "private" }));
65
+ const error = new Error("private");
66
+ await assert.rejects(
67
+ analytics.track("appsGet", async () => {
68
+ throw error;
69
+ }),
70
+ (e) => e === error,
71
+ );
72
+ await analytics.track(
73
+ "https://private/?token=secret" as never,
74
+ async () => ({}),
75
+ );
76
+ assert.deepEqual(events, [
77
+ { operation: "appsList", outcome: "success" },
78
+ { operation: "appsCreate", outcome: "error" },
79
+ { operation: "appsGet", outcome: "error" },
80
+ ]);
81
+ });
82
+
83
+ test("sync and async capture failures never affect calls", async () => {
84
+ for (const capture of [
85
+ () => {
86
+ throw new Error("private");
87
+ },
88
+ async () => {
89
+ throw new Error("private");
90
+ },
91
+ ]) {
92
+ const analytics = createSdkAnalytics({
93
+ enabled: true,
94
+ environment: "production",
95
+ capture,
96
+ });
97
+ assert.equal(await analytics.track("appsList", async () => 1), 1);
98
+ }
99
+ });
100
+
101
+ test("DO_NOT_TRACK blocks capture, including after creating the hook", async () => {
102
+ const events: unknown[] = [];
103
+ const analytics = createSdkAnalytics({
104
+ enabled: true,
105
+ environment: "production",
106
+ capture: (event) => {
107
+ events.push(event);
108
+ },
109
+ });
110
+ const previous = process.env.DO_NOT_TRACK;
111
+ process.env.DO_NOT_TRACK = "1";
112
+ try {
113
+ await analytics.track("appsList", async () => ({}));
114
+ assert.deepEqual(events, []);
115
+ } finally {
116
+ if (previous === undefined) delete process.env.DO_NOT_TRACK;
117
+ else process.env.DO_NOT_TRACK = previous;
118
+ }
119
+ });
120
+
121
+ test("generated SDK accepts only explicitly allowed safe operation IDs", async () => {
122
+ const events: unknown[] = [];
123
+ const analytics = createSdkAnalytics({
124
+ enabled: true,
125
+ environment: "production",
126
+ operations: ["notesList", "notesCreate", "https://private/path"] as const,
127
+ capture: (event) => {
128
+ events.push(event);
129
+ },
130
+ });
131
+ await analytics.track("notesList", async () => ({}));
132
+ await analytics.track("appsList" as never, async () => ({}));
133
+ await analytics.track("https://private/path", async () => ({}));
134
+ assert.deepEqual(events, [{ operation: "notesList", outcome: "success" }]);
135
+ });
136
+
137
+ test("browser doNotTrack blocks the owner sink", async () => {
138
+ const original = Object.getOwnPropertyDescriptor(globalThis, "navigator");
139
+ const events: unknown[] = [];
140
+ Object.defineProperty(globalThis, "navigator", {
141
+ configurable: true,
142
+ value: { doNotTrack: "1" },
143
+ });
144
+ try {
145
+ const analytics = createSdkAnalytics({
146
+ enabled: true,
147
+ environment: "production",
148
+ capture: (event) => {
149
+ events.push(event);
150
+ },
151
+ });
152
+ await analytics.track("appsList", async () => ({}));
153
+ assert.deepEqual(events, []);
154
+ } finally {
155
+ if (original) Object.defineProperty(globalThis, "navigator", original);
156
+ else delete (globalThis as { navigator?: unknown }).navigator;
157
+ }
158
+ });