create-disposabl-app 0.15.0 → 0.17.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 (101) hide show
  1. package/README.md +26 -9
  2. package/dist/disposabl.js +357 -50
  3. package/dist/index.js +9026 -160
  4. package/dist/templates/default/.github/scripts/mobile.sh +3 -2
  5. package/dist/templates/default/.github/scripts/release.sh +1 -0
  6. package/dist/templates/default/.github/scripts/verify-android-abis.mjs +22 -0
  7. package/dist/templates/default/apps/desktop/forge.config.mts +2 -1
  8. package/dist/templates/default/apps/docs/content/getting-started.mdx +1 -1
  9. package/dist/templates/default/apps/mobile/app.config.js +18 -9
  10. package/dist/templates/default/apps/mobile/package.json +1 -0
  11. package/dist/templates/default/apps/mobile/plugins/with-e2e-network.js +41 -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 +2 -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/android-architectures.ts +35 -0
  50. package/dist/templates/default/packages/disposabl-kit/client/lib/telemetry.ts +24 -2
  51. package/dist/templates/default/packages/disposabl-kit/client/lib/theme.ts +1 -0
  52. package/dist/templates/default/packages/disposabl-kit/client/routes/billing-login.tsx +92 -0
  53. package/dist/templates/default/packages/disposabl-kit/client/routes/pricing.tsx +113 -43
  54. package/dist/templates/default/packages/disposabl-kit/client/routes/route.tsx +13 -5
  55. package/dist/templates/default/packages/disposabl-kit/client/vite.config.ts +3 -2
  56. package/dist/templates/default/packages/disposabl-kit/lib/telemetry.ts +148 -12
  57. package/dist/templates/default/packages/disposabl-kit/mobile/billing/account-billing.tsx +143 -40
  58. package/dist/templates/default/packages/disposabl-kit/mobile/billing/paywall.ts +83 -0
  59. package/dist/templates/default/packages/disposabl-kit/mobile/billing/policy.ts +125 -0
  60. package/dist/templates/default/packages/disposabl-kit/mobile/billing/session.ts +31 -3
  61. package/dist/templates/default/packages/disposabl-kit/mobile/billing/topup.ts +99 -0
  62. package/dist/templates/default/packages/disposabl-kit/mobile/billing/use-topup.ts +116 -0
  63. package/dist/templates/default/packages/disposabl-kit/mobile/billing/web-checkout.ts +190 -0
  64. package/dist/templates/default/packages/disposabl-kit/mobile/expo-plugin.cjs +23 -1
  65. package/dist/templates/default/packages/disposabl-kit/mobile/layout.tsx +9 -2
  66. package/dist/templates/default/packages/disposabl-kit/package.json +10 -1
  67. package/dist/templates/default/packages/disposabl-kit/payments/checkout-attempt.ts +64 -0
  68. package/dist/templates/default/packages/disposabl-kit/payments/config.ts +16 -3
  69. package/dist/templates/default/packages/disposabl-kit/server/billing-functions.ts +218 -9
  70. package/dist/templates/default/packages/disposabl-kit/server/billing-handoff.ts +126 -0
  71. package/dist/templates/default/packages/disposabl-kit/server/billing-native-capability.ts +12 -0
  72. package/dist/templates/default/packages/disposabl-kit/server/billing-setup.ts +2 -0
  73. package/dist/templates/default/packages/disposabl-kit/server/billing.ts +31 -0
  74. package/dist/templates/default/packages/disposabl-kit/server/browser-session.ts +27 -0
  75. package/dist/templates/default/packages/disposabl-kit/server/lib/telemetry.ts +173 -59
  76. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-contract.ts +233 -0
  77. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-functions.ts +554 -0
  78. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-validator.ts +76 -0
  79. package/dist/templates/default/packages/reconfigure-skill/SKILL.md +38 -0
  80. package/dist/templates/default/packages/sdk/README.md +21 -0
  81. package/dist/templates/default/packages/sdk/src/analytics.ts +102 -0
  82. package/dist/templates/default/packages/sdk/src/index.ts +2 -0
  83. package/dist/templates/default/packages/skill/SKILL.md +2 -0
  84. package/dist/templates/default/packages/skill/references/agent.md +2 -0
  85. package/dist/templates/default/packages/tests/android-architectures.test.ts +43 -0
  86. package/dist/templates/default/packages/tests/android-ota-artifacts.test.ts +5 -1
  87. package/dist/templates/default/packages/tests/android-ota-selection.test.ts +7 -2
  88. package/dist/templates/default/packages/tests/billing-attempts.test.ts +156 -0
  89. package/dist/templates/default/packages/tests/billing-handoff.test.ts +116 -0
  90. package/dist/templates/default/packages/tests/billing-setup.test.ts +4 -3
  91. package/dist/templates/default/packages/tests/billing.test.ts +48 -4
  92. package/dist/templates/default/packages/tests/desktop-packaging.test.ts +12 -0
  93. package/dist/templates/default/packages/tests/flows/seo.e2e.ts +3 -1
  94. package/dist/templates/default/packages/tests/mobile-android-e2e-network.test.ts +112 -0
  95. package/dist/templates/default/packages/tests/native-billing.test.ts +417 -0
  96. package/dist/templates/default/packages/tests/page-telemetry.test.ts +218 -0
  97. package/dist/templates/default/packages/tests/revenuecat-inbox.test.ts +476 -0
  98. package/dist/templates/default/packages/tests/sdk-analytics.test.ts +158 -0
  99. package/dist/templates/default/packages/tests/telemetry.test.ts +272 -55
  100. package/package.json +1 -1
  101. package/dist/templates/default/apps/web/src/routeTree.gen.ts +0 -578
@@ -0,0 +1,112 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { createRequire } from "node:module";
4
+
5
+ const require = createRequire(import.meta.url);
6
+ const configure = require("../../apps/mobile/app.config.js");
7
+ const network = require("../../apps/mobile/plugins/with-e2e-network.js");
8
+ const keys = [
9
+ "EXPO_PUBLIC_E2E",
10
+ "E2E_ANDROID_LOOPBACK",
11
+ "EXPO_PUBLIC_CONVEX_URL",
12
+ "E2E_ANDROID_APP_ID",
13
+ "E2E_ANDROID_SCHEME",
14
+ "DISPOSABL_MOBILE_CHANNEL",
15
+ ] as const;
16
+
17
+ function withEnv(values: Record<string, string>, run: () => void) {
18
+ const old = Object.fromEntries(keys.map((key) => [key, process.env[key]]));
19
+ for (const key of keys) delete process.env[key];
20
+ Object.assign(process.env, values);
21
+ try {
22
+ run();
23
+ } finally {
24
+ for (const key of keys) {
25
+ if (old[key] === undefined) delete process.env[key];
26
+ else process.env[key] = old[key];
27
+ }
28
+ }
29
+ }
30
+
31
+ test("production and E2E-off configurations retain default network security", () => {
32
+ for (const e2e of [undefined, "false"])
33
+ withEnv(
34
+ {
35
+ DISPOSABL_MOBILE_CHANNEL: "production",
36
+ E2E_ANDROID_LOOPBACK: "true",
37
+ E2E_ANDROID_APP_ID: "app.example.override",
38
+ E2E_ANDROID_SCHEME: "override",
39
+ ...(e2e ? { EXPO_PUBLIC_E2E: e2e } : {}),
40
+ },
41
+ () => {
42
+ const config = configure({
43
+ config: {
44
+ android: { package: "app.example.production" },
45
+ scheme: "original",
46
+ },
47
+ });
48
+ assert.equal(config.android.package, "app.example.production");
49
+ assert.ok(config.scheme.includes("original"));
50
+ assert.ok(!config.scheme.includes("override"));
51
+ assert.ok(!config.plugins.includes("./plugins/with-e2e-network.js"));
52
+ assert.notEqual(config.updates.enabled, false);
53
+ assert.ok(
54
+ !config.plugins.some(
55
+ (plugin: unknown) =>
56
+ Array.isArray(plugin) && plugin[0] === "expo-dev-client",
57
+ ),
58
+ );
59
+ },
60
+ );
61
+ });
62
+
63
+ test("E2E loopback configuration has isolated identity, scheme, updates and network", () => {
64
+ withEnv(
65
+ {
66
+ EXPO_PUBLIC_E2E: "true",
67
+ E2E_ANDROID_LOOPBACK: "true",
68
+ EXPO_PUBLIC_CONVEX_URL: "http://10.0.2.2:3310",
69
+ E2E_ANDROID_APP_ID: "app.disposabl.androidround10.preview",
70
+ E2E_ANDROID_SCHEME: "disposabl-androidround10-preview",
71
+ },
72
+ () => {
73
+ const config = configure({ config: {} });
74
+ assert.equal(
75
+ config.android.package,
76
+ "app.disposabl.androidround10.preview",
77
+ );
78
+ assert.deepEqual(config.scheme, ["disposabl-androidround10-preview"]);
79
+ assert.equal(config.updates.enabled, false);
80
+ assert.ok(config.plugins.includes("./plugins/with-e2e-network.js"));
81
+ assert.equal(
82
+ config.plugins.find(
83
+ (plugin: unknown) =>
84
+ Array.isArray(plugin) && plugin[0] === "expo-dev-client",
85
+ )[1].addGeneratedScheme,
86
+ false,
87
+ );
88
+ assert.doesNotThrow(() => network({}));
89
+ for (const url of [
90
+ "http://example.com:3310",
91
+ "https://10.0.2.2:3310",
92
+ "http://127.0.0.1:3310",
93
+ ]) {
94
+ process.env.EXPO_PUBLIC_CONVEX_URL = url;
95
+ assert.throws(() => network({}));
96
+ }
97
+ process.env.EXPO_PUBLIC_CONVEX_URL = "http://10.0.2.2:3310";
98
+ delete process.env.EXPO_PUBLIC_E2E;
99
+ assert.throws(() => network({}));
100
+ },
101
+ );
102
+ });
103
+
104
+ test("E2E without loopback opt-in does not add cleartext policy", () => {
105
+ withEnv({ EXPO_PUBLIC_E2E: "true" }, () => {
106
+ assert.ok(
107
+ !configure({ config: {} }).plugins.includes(
108
+ "./plugins/with-e2e-network.js",
109
+ ),
110
+ );
111
+ });
112
+ });
@@ -3,6 +3,11 @@ import assert from "node:assert/strict";
3
3
  import { BillingSession } from "../disposabl-kit/mobile/billing/session";
4
4
  import { pricing } from "../disposabl-kit/payments/config";
5
5
  import { monthlyPlanFromCustomer } from "../disposabl-kit/server/billing";
6
+ import {
7
+ paymentOptions,
8
+ storeEligibility,
9
+ preflightWebMonthly,
10
+ } from "../disposabl-kit/mobile/billing/policy";
6
11
 
7
12
  function deferred() {
8
13
  let resolve!: () => void;
@@ -14,6 +19,148 @@ function deferred() {
14
19
  };
15
20
  }
16
21
 
22
+ test("retained gate and account keep separate leases across focus and in-flight work", async () => {
23
+ const started = deferred();
24
+ const finish = deferred();
25
+ const session = new BillingSession(async () => ({ configure() {} }) as never);
26
+ const gate = session.acquire("alice");
27
+ const purchase = session.run("alice", gate, "key", async () => {
28
+ started.resolve();
29
+ await finish.promise;
30
+ return "result";
31
+ });
32
+ await started.promise;
33
+ const account = session.acquire("alice");
34
+ assert.equal(session.current("alice", gate), true);
35
+ assert.equal(session.current("alice", account), true);
36
+ assert.equal(session.beginTransaction(), true);
37
+ assert.equal(session.beginTransaction(), false);
38
+ session.endTransaction();
39
+ session.release("alice", account);
40
+ assert.equal(session.current("alice", gate), true);
41
+ finish.resolve();
42
+ assert.equal(await purchase, "result");
43
+ const returnedAccount = session.acquire("alice");
44
+ const pending = session.run("alice", returnedAccount, "key", async () => {
45
+ await finish.promise;
46
+ return true;
47
+ });
48
+ session.release("alice", returnedAccount);
49
+ await assert.rejects(pending, /account changed/);
50
+ assert.equal(session.current("alice", gate), true);
51
+ const bob = session.acquire("bob");
52
+ assert.equal(session.current("alice", gate), false);
53
+ session.release("alice", gate);
54
+ assert.equal(session.current("bob", bob), true);
55
+ });
56
+
57
+ test("web monthly preflight rechecks storefront, account, and both subscription sources", async () => {
58
+ let current = true;
59
+ let country = "USA";
60
+ let active: string[] = [];
61
+ const sdk = {
62
+ canMakePayments: async () => true,
63
+ getStorefront: async () => ({ countryCode: country }),
64
+ getCustomerInfo: async () => ({ activeSubscriptions: active }),
65
+ getOfferings: async () => ({
66
+ current: {
67
+ availablePackages: [
68
+ { product: { identifier: "monthly", subscriptionPeriod: "P1M" } },
69
+ ],
70
+ },
71
+ }),
72
+ };
73
+ const snapshot = async () => ({
74
+ balance: 0,
75
+ hasMonthlyPlan: false,
76
+ nativeSubscriptionsEnabled: true,
77
+ monthlyCheckoutPending: false,
78
+ mobileWebCheckoutEnabled: true,
79
+ customerId: "alice",
80
+ });
81
+ const run = () =>
82
+ preflightWebMonthly(
83
+ sdk as never,
84
+ "ios",
85
+ "monthly",
86
+ "alice",
87
+ snapshot,
88
+ () => current,
89
+ );
90
+ await run();
91
+ country = "CAN";
92
+ await assert.rejects(run(), /unavailable/);
93
+ country = "USA";
94
+ active = ["monthly"];
95
+ await assert.rejects(run(), /existing_subscription/);
96
+ active = [];
97
+ current = false;
98
+ await assert.rejects(run(), /Account changed/);
99
+ });
100
+
101
+ test("account switch invalidates all retained views while work is in flight", async () => {
102
+ const started = deferred();
103
+ const finish = deferred();
104
+ const session = new BillingSession(async () => ({ configure() {} }) as never);
105
+ const gate = session.acquire("alice");
106
+ const account = session.acquire("alice");
107
+ const pending = session.run("alice", gate, "key", async () => {
108
+ started.resolve();
109
+ await finish.promise;
110
+ });
111
+ const rejected = assert.rejects(pending, /account changed/);
112
+ await started.promise;
113
+ const bob = session.acquire("bob");
114
+ assert.equal(session.current("alice", account), false);
115
+ finish.resolve();
116
+ await rejected;
117
+ assert.equal(session.current("bob", bob), true);
118
+ });
119
+
120
+ test("storefront policy fails closed and Android never offers web", async () => {
121
+ const base = {
122
+ platform: "ios",
123
+ storefront: "USA",
124
+ paymentsEnabled: true,
125
+ webCheckoutEnabled: true,
126
+ nativeReady: true,
127
+ canMakePayments: true,
128
+ };
129
+ assert.deepEqual(paymentOptions(base), { native: true, web: true });
130
+ for (const change of [
131
+ { storefront: null },
132
+ { storefront: "CAN" },
133
+ { platform: "android" },
134
+ { webCheckoutEnabled: false },
135
+ { nativeReady: false },
136
+ { canMakePayments: false },
137
+ { paymentsEnabled: false },
138
+ ])
139
+ assert.equal(paymentOptions({ ...base, ...change }).web, false);
140
+ assert.deepEqual(
141
+ await storeEligibility(
142
+ {
143
+ canMakePayments: async () => true,
144
+ getStorefront: async () => ({ countryCode: "USA" }),
145
+ } as never,
146
+ "ios",
147
+ ),
148
+ { canMakePayments: true, storefront: "USA" },
149
+ );
150
+ assert.deepEqual(
151
+ await storeEligibility(
152
+ {
153
+ canMakePayments: async () => true,
154
+ getStorefront: async () => {
155
+ throw new Error("unknown");
156
+ },
157
+ } as never,
158
+ "ios",
159
+ ),
160
+ { canMakePayments: true, storefront: null },
161
+ );
162
+ });
163
+
17
164
  test("monthly state includes scheduled plans and fails closed for unknown responses", () => {
18
165
  assert.equal(monthlyPlanFromCustomer({ subscriptions: [] }), false);
19
166
  for (const status of ["active", "scheduled"])
@@ -139,3 +286,273 @@ test("a failed login cannot leave another user's identity trusted", async () =>
139
286
  await session.run("alice", session.select("alice"), "public", async () => {});
140
287
  assert.deepEqual(logins, ["bob", "alice"]);
141
288
  });
289
+
290
+ test("monthly paywall permits only a verified current offering and fresh customer", async () => {
291
+ const { presentMonthlyPaywall, validMonthlyOffering } =
292
+ await import("../disposabl-kit/mobile/billing/paywall");
293
+ const monthly = {
294
+ product: { identifier: "monthly", subscriptionPeriod: "P1M" },
295
+ };
296
+ const topup = { product: { identifier: "topup", subscriptionPeriod: null } };
297
+ assert.equal(validMonthlyOffering([monthly], "monthly"), true);
298
+ assert.equal(validMonthlyOffering([monthly, topup], "monthly"), false);
299
+ assert.equal(validMonthlyOffering([topup], "monthly"), false);
300
+ assert.equal(validMonthlyOffering([], "monthly"), false);
301
+ const snapshot = async () => ({
302
+ balance: 0,
303
+ hasMonthlyPlan: false,
304
+ nativeSubscriptionsEnabled: true,
305
+ monthlyCheckoutPending: false,
306
+ });
307
+ let shown = 0;
308
+ const sdk = {
309
+ canMakePayments: async () => true,
310
+ getCustomerInfo: async () => ({ activeSubscriptions: [] }),
311
+ getOfferings: async () => ({ current: { availablePackages: [monthly] } }),
312
+ };
313
+ const show = async () => {
314
+ shown++;
315
+ return "cancelled" as const;
316
+ };
317
+ assert.equal(
318
+ await presentMonthlyPaywall(
319
+ sdk as never,
320
+ snapshot,
321
+ () => true,
322
+ "monthly",
323
+ show,
324
+ ),
325
+ "cancelled",
326
+ );
327
+ assert.equal(shown, 1);
328
+ for (const monthlyCheckoutPending of [true, undefined]) {
329
+ await assert.rejects(
330
+ presentMonthlyPaywall(
331
+ sdk as never,
332
+ async () =>
333
+ ({ ...(await snapshot()), monthlyCheckoutPending }) as never,
334
+ () => true,
335
+ "monthly",
336
+ show,
337
+ ),
338
+ /website checkout/,
339
+ );
340
+ }
341
+ await assert.rejects(
342
+ presentMonthlyPaywall(
343
+ { ...sdk, canMakePayments: async () => false } as never,
344
+ snapshot,
345
+ () => true,
346
+ "monthly",
347
+ show,
348
+ ),
349
+ /restricted/,
350
+ );
351
+ await assert.rejects(
352
+ presentMonthlyPaywall(
353
+ sdk as never,
354
+ async () => ({ ...(await snapshot()), hasMonthlyPlan: true }),
355
+ () => true,
356
+ "monthly",
357
+ show,
358
+ ),
359
+ /existing_subscription/,
360
+ );
361
+ await assert.rejects(
362
+ presentMonthlyPaywall(sdk as never, snapshot, () => false, "monthly", show),
363
+ /Account changed/,
364
+ );
365
+ await assert.rejects(
366
+ presentMonthlyPaywall(
367
+ {
368
+ ...sdk,
369
+ getOfferings: async () => ({
370
+ current: { availablePackages: [monthly, topup] },
371
+ }),
372
+ } as never,
373
+ snapshot,
374
+ () => true,
375
+ "monthly",
376
+ show,
377
+ ),
378
+ /unavailable/,
379
+ );
380
+ assert.equal(shown, 1);
381
+ });
382
+
383
+ // Consumable top-ups share the session lease but do not depend on monthly checkout.
384
+ test("native top-up fails closed and checks identity at purchase", async () => {
385
+ const { nativeTopupRoutes } =
386
+ await import("../disposabl-kit/server/billing-native-capability");
387
+ const { loadTopup, matchingTopupRoute, purchaseTopup } =
388
+ await import("../disposabl-kit/mobile/billing/topup");
389
+ const env = {
390
+ REVENUECAT_CONSUMABLE_OWNER: "custom",
391
+ REVENUECAT_CONSUMABLE_INTEGRATION_VERIFIED: "true",
392
+ REVENUECAT_WEBHOOK_AUTH: "auth",
393
+ REVENUECAT_PROJECT_ID: "project",
394
+ AUTUMN_SECRET_KEY: "key",
395
+ REVENUECAT_ENVIRONMENT: "SANDBOX",
396
+ REVENUECAT_ROUTES: JSON.stringify([
397
+ { app: "apple", store: "APP_STORE", product: "coins" },
398
+ ]),
399
+ };
400
+ assert.deepEqual(nativeTopupRoutes(false, env), []);
401
+ for (const change of [
402
+ { REVENUECAT_CONSUMABLE_OWNER: "managed" },
403
+ { REVENUECAT_CONSUMABLE_INTEGRATION_VERIFIED: "false" },
404
+ { REVENUECAT_ROUTES: "[]" },
405
+ { REVENUECAT_WEBHOOK_AUTH: "" },
406
+ ])
407
+ assert.deepEqual(nativeTopupRoutes(true, { ...env, ...change }), []);
408
+ const snapshot = {
409
+ customerId: "user",
410
+ nativeTopupRoutes: nativeTopupRoutes(true, env),
411
+ };
412
+ assert.equal(matchingTopupRoute(snapshot, "ios", "apple", "coins"), true);
413
+ assert.equal(
414
+ matchingTopupRoute(snapshot, "android", "apple", "coins"),
415
+ false,
416
+ );
417
+ const product = {
418
+ identifier: "coins",
419
+ productCategory: "NON_SUBSCRIPTION",
420
+ productType: "CONSUMABLE",
421
+ priceString: "$5.00",
422
+ };
423
+ let purchases = 0;
424
+ const sdk = {
425
+ PRODUCT_CATEGORY: { NON_SUBSCRIPTION: "NON_SUBSCRIPTION" },
426
+ getProducts: async () => [product],
427
+ getAppUserID: async () => "user",
428
+ canMakePayments: async () => true,
429
+ purchaseStoreProduct: async () => {
430
+ purchases++;
431
+ },
432
+ };
433
+ for (const bad of [
434
+ { ...product, identifier: "other" },
435
+ { ...product, productCategory: "SUBSCRIPTION" },
436
+ { ...product, productType: "UNKNOWN" },
437
+ ])
438
+ assert.equal(
439
+ await loadTopup(
440
+ { ...sdk, getProducts: async () => [bad] } as never,
441
+ "ios",
442
+ "coins",
443
+ ),
444
+ null,
445
+ );
446
+ assert.ok(
447
+ await loadTopup(
448
+ {
449
+ ...sdk,
450
+ getProducts: async () => [{ ...product, productType: "UNKNOWN" }],
451
+ } as never,
452
+ "android",
453
+ "coins",
454
+ ),
455
+ );
456
+ const run = (overrides = {}, state = snapshot, current = () => true) =>
457
+ purchaseTopup(
458
+ { ...sdk, ...overrides } as never,
459
+ async () => state,
460
+ current,
461
+ "user",
462
+ "ios",
463
+ "apple",
464
+ "coins",
465
+ );
466
+ await assert.rejects(run({ getAppUserID: async () => "other" }));
467
+ await assert.rejects(run({ canMakePayments: async () => false }));
468
+ await assert.rejects(run({}, { ...snapshot, nativeTopupRoutes: [] }));
469
+ await assert.rejects(run({}, snapshot, () => false));
470
+ let current = true;
471
+ await assert.rejects(
472
+ run(
473
+ {
474
+ getProducts: async () => {
475
+ current = false;
476
+ return [product];
477
+ },
478
+ },
479
+ snapshot,
480
+ () => current,
481
+ ),
482
+ );
483
+ assert.equal(purchases, 0);
484
+ await run();
485
+ assert.equal(purchases, 1);
486
+ });
487
+
488
+ test("web top-up preflight rechecks storefront, product, route and account independently of monthly plan", async () => {
489
+ const { preflightWebTopup } =
490
+ await import("../disposabl-kit/mobile/billing/policy");
491
+ const product = {
492
+ identifier: "coins",
493
+ productCategory: "NON_SUBSCRIPTION",
494
+ productType: "CONSUMABLE",
495
+ priceString: "$5",
496
+ };
497
+ const sdk = {
498
+ canMakePayments: async () => true,
499
+ getStorefront: async () => ({ countryCode: "USA" }),
500
+ getAppUserID: async () => "user",
501
+ getProducts: async () => [product],
502
+ PRODUCT_CATEGORY: { NON_SUBSCRIPTION: "NON_SUBSCRIPTION" },
503
+ };
504
+ const state = {
505
+ customerId: "user",
506
+ mobileWebCheckoutEnabled: true,
507
+ nativeTopupRoutes: [{ app: "apple", store: "APP_STORE", product: "coins" }],
508
+ hasMonthlyPlan: true,
509
+ monthlyCheckoutPending: true,
510
+ };
511
+ const run = (
512
+ changes = {},
513
+ snapshot = state,
514
+ platform = "ios",
515
+ current = () => true,
516
+ ) =>
517
+ preflightWebTopup(
518
+ { ...sdk, ...changes } as never,
519
+ platform,
520
+ "apple",
521
+ "coins",
522
+ "user",
523
+ async () => snapshot,
524
+ current,
525
+ );
526
+ await run();
527
+ for (const changes of [
528
+ { canMakePayments: async () => false },
529
+ { getStorefront: async () => null },
530
+ { getStorefront: async () => ({ countryCode: "CAN" }) },
531
+ {
532
+ getStorefront: async () => {
533
+ throw Error();
534
+ },
535
+ },
536
+ { getAppUserID: async () => "other" },
537
+ { getProducts: async () => [{ ...product, productType: "UNKNOWN" }] },
538
+ ])
539
+ await assert.rejects(run(changes));
540
+ await assert.rejects(run({}, { ...state, nativeTopupRoutes: [] }));
541
+ await assert.rejects(run({}, { ...state, customerId: "other" }));
542
+ await assert.rejects(run({}, { ...state, mobileWebCheckoutEnabled: false }));
543
+ await assert.rejects(run({}, state, "android"));
544
+ let current = true;
545
+ await assert.rejects(
546
+ run(
547
+ {
548
+ getProducts: async () => {
549
+ current = false;
550
+ return [product];
551
+ },
552
+ },
553
+ state,
554
+ "ios",
555
+ () => current,
556
+ ),
557
+ );
558
+ });