create-disposabl-app 0.14.18 → 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 (97) 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/.github/scripts/macos-release.sh +81 -0
  5. package/dist/templates/default/.github/scripts/platforms.sh +1 -1
  6. package/dist/templates/default/.github/scripts/release.sh +1 -1
  7. package/dist/templates/default/.github/workflows/pipeline.yml +14 -3
  8. package/dist/templates/default/apps/desktop/README.md +13 -2
  9. package/dist/templates/default/apps/desktop/forge.config.mts +9 -3
  10. package/dist/templates/default/apps/docs/content/getting-started.mdx +1 -1
  11. package/dist/templates/default/apps/mobile/package.json +1 -0
  12. package/dist/templates/default/apps/web/eslint.config.ts +5 -1
  13. package/dist/templates/default/apps/web/public/disposabl-logo-light.svg +8 -0
  14. package/dist/templates/default/apps/web/public/landing/coast.jpg +0 -0
  15. package/dist/templates/default/apps/web/public/landing/community.jpg +0 -0
  16. package/dist/templates/default/apps/web/public/landing/stores/app-store.svg +46 -0
  17. package/dist/templates/default/apps/web/public/landing/stores/google-play.png +0 -0
  18. package/dist/templates/default/apps/web/public/landing/taste/LICENSE +21 -0
  19. package/dist/templates/default/apps/web/public/landing/taste/init.js +3 -0
  20. package/dist/templates/default/apps/web/public/landing/taste/moments/lib.js +189 -0
  21. package/dist/templates/default/apps/web/public/landing/taste/moments/moments.js +109 -0
  22. package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.css +8 -0
  23. package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.js +35 -0
  24. package/dist/templates/default/apps/web/public/landing/taste/motion/motion.css +321 -0
  25. package/dist/templates/default/apps/web/public/landing/taste/motion/motion.js +406 -0
  26. package/dist/templates/default/apps/web/routes.config.ts +14 -0
  27. package/dist/templates/default/apps/web/src/router/__root.tsx +8 -2
  28. package/dist/templates/default/apps/web/tsr.config.json +1 -1
  29. package/dist/templates/default/apps/web/vite.config.ts +10 -2
  30. package/dist/templates/default/bun.lock +6 -0
  31. package/dist/templates/default/bunfig.toml +1 -0
  32. package/dist/templates/default/disposabl.json +1 -1
  33. package/dist/templates/default/packages/backend/convex/authSetup.ts +2 -5
  34. package/dist/templates/default/packages/backend/convex/billing.ts +5 -0
  35. package/dist/templates/default/packages/backend/convex/billingHandoff.ts +6 -0
  36. package/dist/templates/default/packages/backend/convex/convex.config.ts +8 -0
  37. package/dist/templates/default/packages/backend/convex/http.ts +6 -0
  38. package/dist/templates/default/packages/backend/convex/revenuecat.ts +13 -0
  39. package/dist/templates/default/packages/backend/convex/schema.ts +44 -0
  40. package/dist/templates/default/packages/backend/convex/telemetry.ts +41 -5
  41. package/dist/templates/default/packages/backend/generate.ts +6 -0
  42. package/dist/templates/default/packages/cli/auth.ts +36 -3
  43. package/dist/templates/default/packages/cli/build.ts +6 -0
  44. package/dist/templates/default/packages/cli/client.ts +189 -19
  45. package/dist/templates/default/packages/cli/errors.ts +18 -0
  46. package/dist/templates/default/packages/cli/telemetry-build.ts +27 -0
  47. package/dist/templates/default/packages/cli/telemetry.ts +266 -0
  48. package/dist/templates/default/packages/disposabl-kit/PAYMENTS.md +23 -5
  49. package/dist/templates/default/packages/disposabl-kit/client/lib/telemetry.ts +24 -2
  50. package/dist/templates/default/packages/disposabl-kit/client/lib/theme.ts +1 -0
  51. package/dist/templates/default/packages/disposabl-kit/client/routes/billing-login.tsx +92 -0
  52. package/dist/templates/default/packages/disposabl-kit/client/routes/pricing.tsx +113 -43
  53. package/dist/templates/default/packages/disposabl-kit/client/routes/route.tsx +13 -5
  54. package/dist/templates/default/packages/disposabl-kit/client/vite.config.ts +3 -2
  55. package/dist/templates/default/packages/disposabl-kit/lib/telemetry.ts +148 -12
  56. package/dist/templates/default/packages/disposabl-kit/mobile/billing/account-billing.tsx +143 -40
  57. package/dist/templates/default/packages/disposabl-kit/mobile/billing/paywall.ts +83 -0
  58. package/dist/templates/default/packages/disposabl-kit/mobile/billing/policy.ts +125 -0
  59. package/dist/templates/default/packages/disposabl-kit/mobile/billing/session.ts +31 -3
  60. package/dist/templates/default/packages/disposabl-kit/mobile/billing/topup.ts +99 -0
  61. package/dist/templates/default/packages/disposabl-kit/mobile/billing/use-topup.ts +116 -0
  62. package/dist/templates/default/packages/disposabl-kit/mobile/billing/web-checkout.ts +190 -0
  63. package/dist/templates/default/packages/disposabl-kit/mobile/layout.tsx +9 -2
  64. package/dist/templates/default/packages/disposabl-kit/package.json +10 -1
  65. package/dist/templates/default/packages/disposabl-kit/payments/checkout-attempt.ts +64 -0
  66. package/dist/templates/default/packages/disposabl-kit/payments/config.ts +16 -3
  67. package/dist/templates/default/packages/disposabl-kit/server/billing-functions.ts +218 -9
  68. package/dist/templates/default/packages/disposabl-kit/server/billing-handoff.ts +126 -0
  69. package/dist/templates/default/packages/disposabl-kit/server/billing-native-capability.ts +12 -0
  70. package/dist/templates/default/packages/disposabl-kit/server/billing-setup.ts +2 -0
  71. package/dist/templates/default/packages/disposabl-kit/server/billing.ts +31 -0
  72. package/dist/templates/default/packages/disposabl-kit/server/browser-session.ts +27 -0
  73. package/dist/templates/default/packages/disposabl-kit/server/lib/telemetry.ts +173 -59
  74. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-contract.ts +233 -0
  75. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-functions.ts +554 -0
  76. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-validator.ts +76 -0
  77. package/dist/templates/default/packages/reconfigure-skill/SKILL.md +36 -0
  78. package/dist/templates/default/packages/sdk/README.md +21 -0
  79. package/dist/templates/default/packages/sdk/src/analytics.ts +102 -0
  80. package/dist/templates/default/packages/sdk/src/index.ts +2 -0
  81. package/dist/templates/default/packages/skill/SKILL.md +2 -0
  82. package/dist/templates/default/packages/skill/references/agent.md +2 -0
  83. package/dist/templates/default/packages/tests/billing-attempts.test.ts +156 -0
  84. package/dist/templates/default/packages/tests/billing-handoff.test.ts +116 -0
  85. package/dist/templates/default/packages/tests/billing-setup.test.ts +4 -3
  86. package/dist/templates/default/packages/tests/billing.test.ts +48 -4
  87. package/dist/templates/default/packages/tests/ci-platform-selection.test.ts +54 -0
  88. package/dist/templates/default/packages/tests/desktop-macos-release.test.ts +226 -0
  89. package/dist/templates/default/packages/tests/desktop-packaging.test.ts +12 -0
  90. package/dist/templates/default/packages/tests/flows/seo.e2e.ts +3 -1
  91. package/dist/templates/default/packages/tests/native-billing.test.ts +417 -0
  92. package/dist/templates/default/packages/tests/page-telemetry.test.ts +218 -0
  93. package/dist/templates/default/packages/tests/revenuecat-inbox.test.ts +476 -0
  94. package/dist/templates/default/packages/tests/sdk-analytics.test.ts +158 -0
  95. package/dist/templates/default/packages/tests/telemetry.test.ts +272 -55
  96. package/package.json +1 -1
  97. package/dist/templates/default/apps/web/src/routeTree.gen.ts +0 -578
@@ -0,0 +1,218 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import {
4
+ createPageTelemetry,
5
+ pageCategory,
6
+ } from "../disposabl-kit/lib/telemetry";
7
+
8
+ const id = (n: number) =>
9
+ `00000000-0000-4000-8000-${String(n).padStart(12, "0")}`;
10
+ const token = (session: string, extra = "") =>
11
+ `x.${btoa(JSON.stringify({ sub: session, extra }))}.x`;
12
+ const tick = () => new Promise<void>((resolve) => setTimeout(resolve, 0));
13
+ async function until(check: () => boolean) {
14
+ for (let n = 0; n < 100 && !check(); n++) await tick();
15
+ assert.ok(check(), "telemetry did not settle");
16
+ }
17
+
18
+ test("persisted anonymous history links on login and rotates on logout or account switch", async () => {
19
+ let saved: string | null = null;
20
+ let next = 0;
21
+ const bodies: any[] = [];
22
+ const storage = {
23
+ getItem: () => saved,
24
+ setItem: (_: string, value: string) => {
25
+ saved = value;
26
+ },
27
+ };
28
+ const transport = (async (_url, init) => {
29
+ const body = JSON.parse(String(init?.body));
30
+ bodies.push(body);
31
+ return Response.json({ anonymousId: body.anonymousId ?? id(++next) });
32
+ }) as typeof fetch;
33
+ let telemetry = createPageTelemetry(storage, transport);
34
+ const send = async (session: string | null, path = "/") => {
35
+ const count = bodies.length;
36
+ telemetry.view("https://backend.example", "web", path, session);
37
+ await until(
38
+ () =>
39
+ bodies.length > count &&
40
+ !!saved &&
41
+ JSON.parse(saved).anonymousId !== null,
42
+ );
43
+ };
44
+ await send(null, "/private?secret=foo");
45
+ assert.deepEqual(bodies[0], { platform: "web", page: "other" });
46
+ telemetry = createPageTelemetry(storage, transport); // browser restart
47
+ await send(token("a"), "/login");
48
+ assert.equal(bodies[1].anonymousId, id(1));
49
+ await send(token("a", "refreshed"));
50
+ assert.equal(bodies[2].anonymousId, id(1));
51
+ await send(token("b"));
52
+ assert.equal(bodies[3].anonymousId, undefined);
53
+ await send(null);
54
+ assert.equal(bodies[4].anonymousId, undefined);
55
+ await send(token("c"));
56
+ assert.equal(bodies[5].anonymousId, id(3));
57
+ assert.equal(pageCategory("/dashboard/connections?secret=x"), "connections");
58
+ });
59
+
60
+ test("in-flight account changes discard old IDs and keep at most one pending view", async () => {
61
+ let saved: string | null = null;
62
+ const bodies: any[] = [];
63
+ let release!: (response: Response) => void;
64
+ const storage = {
65
+ getItem: () => saved,
66
+ setItem: (_: string, value: string) => {
67
+ saved = value;
68
+ },
69
+ };
70
+ const transport = (async (_url, init) => {
71
+ const body = JSON.parse(String(init?.body));
72
+ bodies.push(body);
73
+ if (bodies.length === 1)
74
+ return await new Promise<Response>((resolve) => {
75
+ release = resolve;
76
+ });
77
+ return Response.json({ anonymousId: body.anonymousId ?? id(2) });
78
+ }) as typeof fetch;
79
+ const telemetry = createPageTelemetry(storage, transport);
80
+ telemetry.view("https://backend.example", "mobile", "/", token("a"));
81
+ await until(() => bodies.length === 1);
82
+ telemetry.view("https://backend.example", "mobile", "/", null);
83
+ telemetry.view("https://backend.example", "mobile", "/account", token("b"));
84
+ release(Response.json({ anonymousId: id(1) }));
85
+ await until(() => bodies.length === 2);
86
+ assert.equal(bodies[1].anonymousId, undefined);
87
+ assert.equal(bodies[1].page, "account");
88
+ });
89
+
90
+ function sharedTabs() {
91
+ let saved: string | null = null;
92
+ let lock = Promise.resolve();
93
+ let sequence = 0;
94
+ const bodies: any[] = [];
95
+ const storage = {
96
+ getItem: () => saved,
97
+ setItem: (_: string, value: string) => {
98
+ saved = value;
99
+ },
100
+ };
101
+ const exclusive = (work: () => Promise<void>) => {
102
+ const next = lock.then(work);
103
+ lock = next.catch(() => {});
104
+ return next;
105
+ };
106
+ let release: (() => void) | undefined;
107
+ let hold = false;
108
+ const transport = (async (_url, init) => {
109
+ const body = JSON.parse(String(init?.body));
110
+ bodies.push(body);
111
+ const assigned = body.anonymousId ?? id(++sequence);
112
+ if (hold) {
113
+ hold = false;
114
+ await new Promise<void>((resolve) => {
115
+ release = resolve;
116
+ });
117
+ }
118
+ return Response.json({ anonymousId: assigned });
119
+ }) as typeof fetch;
120
+ return {
121
+ bodies,
122
+ storage,
123
+ exclusive,
124
+ transport,
125
+ state: () => saved && JSON.parse(saved),
126
+ hold: () => {
127
+ hold = true;
128
+ },
129
+ release: () => release?.(),
130
+ idle: async () => {
131
+ await tick();
132
+ await lock;
133
+ },
134
+ };
135
+ }
136
+
137
+ test("concurrent browser initialization and account changes do not merge different accounts", async () => {
138
+ const shared = sharedTabs();
139
+ const a = createPageTelemetry(
140
+ shared.storage,
141
+ shared.transport,
142
+ shared.exclusive,
143
+ );
144
+ const b = createPageTelemetry(
145
+ shared.storage,
146
+ shared.transport,
147
+ shared.exclusive,
148
+ );
149
+ shared.hold();
150
+ a.view("https://backend.example", "web", "/", null);
151
+ await until(() => shared.bodies.length === 1);
152
+ b.view("https://backend.example", "web", "/", null);
153
+ await tick();
154
+ assert.equal(shared.bodies.length, 1);
155
+ shared.release();
156
+ await shared.idle();
157
+ assert.equal(shared.bodies[0].anonymousId, undefined);
158
+ assert.equal(shared.bodies[1].anonymousId, id(1));
159
+ a.view("https://backend.example", "web", "/login", token("a"));
160
+ await shared.idle();
161
+ assert.equal(shared.bodies[2].anonymousId, id(1));
162
+ b.view("https://backend.example", "web", "/login", token("b"));
163
+ await shared.idle();
164
+ assert.equal(shared.bodies[3].anonymousId, undefined);
165
+ assert.equal(shared.state()?.session, "b");
166
+ assert.equal(shared.state()?.anonymousId, id(2));
167
+ });
168
+
169
+ test("logout waits for another tab's response before rotating shared identity", async () => {
170
+ const shared = sharedTabs();
171
+ const a = createPageTelemetry(
172
+ shared.storage,
173
+ shared.transport,
174
+ shared.exclusive,
175
+ );
176
+ const b = createPageTelemetry(
177
+ shared.storage,
178
+ shared.transport,
179
+ shared.exclusive,
180
+ );
181
+ a.view("https://backend.example", "web", "/", token("a"));
182
+ await shared.idle();
183
+ shared.hold();
184
+ b.view("https://backend.example", "web", "/account", token("a"));
185
+ await until(() => shared.bodies.length === 2);
186
+ a.view("https://backend.example", "web", "/", null);
187
+ await tick();
188
+ assert.equal(shared.bodies.length, 2);
189
+ shared.release();
190
+ await shared.idle();
191
+ assert.equal(shared.bodies[2].anonymousId, undefined);
192
+ assert.equal(shared.state()?.session, null);
193
+ assert.equal(shared.state()?.anonymousId, id(2));
194
+ b.view("https://backend.example", "web", "/login", token("b"));
195
+ await shared.idle();
196
+ // This tab did not observe logout, so it conservatively rotates again.
197
+ assert.equal(shared.bodies[3].anonymousId, undefined);
198
+ assert.equal(shared.state()?.session, "b");
199
+ });
200
+
201
+ test("a failed storage write cannot link an identity that other tabs still see", async () => {
202
+ let calls = 0;
203
+ const existing = JSON.stringify({ anonymousId: id(1), session: null });
204
+ const storage = {
205
+ getItem: () => existing,
206
+ setItem: () => {
207
+ throw new Error("storage unavailable");
208
+ },
209
+ };
210
+ const transport = (async () => {
211
+ calls++;
212
+ return Response.json({ anonymousId: id(1) });
213
+ }) as typeof fetch;
214
+ const telemetry = createPageTelemetry(storage, transport);
215
+ telemetry.view("https://backend.example", "web", "/login", token("a"));
216
+ await tick();
217
+ assert.equal(calls, 0);
218
+ });
@@ -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
+ });