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.
- package/README.md +15 -1
- package/dist/disposabl.js +357 -50
- package/dist/index.js +8973 -154
- package/dist/templates/default/apps/desktop/forge.config.mts +2 -1
- package/dist/templates/default/apps/docs/content/getting-started.mdx +1 -1
- package/dist/templates/default/apps/mobile/package.json +1 -0
- package/dist/templates/default/apps/web/eslint.config.ts +5 -1
- package/dist/templates/default/apps/web/public/disposabl-logo-light.svg +8 -0
- package/dist/templates/default/apps/web/public/landing/coast.jpg +0 -0
- package/dist/templates/default/apps/web/public/landing/community.jpg +0 -0
- package/dist/templates/default/apps/web/public/landing/stores/app-store.svg +46 -0
- package/dist/templates/default/apps/web/public/landing/stores/google-play.png +0 -0
- package/dist/templates/default/apps/web/public/landing/taste/LICENSE +21 -0
- package/dist/templates/default/apps/web/public/landing/taste/init.js +3 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/lib.js +189 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/moments.js +109 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.css +8 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.js +35 -0
- package/dist/templates/default/apps/web/public/landing/taste/motion/motion.css +321 -0
- package/dist/templates/default/apps/web/public/landing/taste/motion/motion.js +406 -0
- package/dist/templates/default/apps/web/routes.config.ts +14 -0
- package/dist/templates/default/apps/web/src/router/__root.tsx +8 -2
- package/dist/templates/default/apps/web/tsr.config.json +1 -1
- package/dist/templates/default/apps/web/vite.config.ts +10 -2
- package/dist/templates/default/bun.lock +6 -0
- package/dist/templates/default/bunfig.toml +1 -0
- package/dist/templates/default/disposabl.json +1 -1
- package/dist/templates/default/packages/backend/convex/authSetup.ts +2 -5
- package/dist/templates/default/packages/backend/convex/billing.ts +5 -0
- package/dist/templates/default/packages/backend/convex/billingHandoff.ts +6 -0
- package/dist/templates/default/packages/backend/convex/convex.config.ts +8 -0
- package/dist/templates/default/packages/backend/convex/http.ts +6 -0
- package/dist/templates/default/packages/backend/convex/revenuecat.ts +13 -0
- package/dist/templates/default/packages/backend/convex/schema.ts +44 -0
- package/dist/templates/default/packages/backend/convex/telemetry.ts +41 -5
- package/dist/templates/default/packages/backend/generate.ts +6 -0
- package/dist/templates/default/packages/cli/auth.ts +36 -3
- package/dist/templates/default/packages/cli/build.ts +6 -0
- package/dist/templates/default/packages/cli/client.ts +189 -19
- package/dist/templates/default/packages/cli/errors.ts +18 -0
- package/dist/templates/default/packages/cli/telemetry-build.ts +27 -0
- package/dist/templates/default/packages/cli/telemetry.ts +266 -0
- package/dist/templates/default/packages/disposabl-kit/PAYMENTS.md +23 -5
- package/dist/templates/default/packages/disposabl-kit/client/lib/telemetry.ts +24 -2
- package/dist/templates/default/packages/disposabl-kit/client/lib/theme.ts +1 -0
- package/dist/templates/default/packages/disposabl-kit/client/routes/billing-login.tsx +92 -0
- package/dist/templates/default/packages/disposabl-kit/client/routes/pricing.tsx +113 -43
- package/dist/templates/default/packages/disposabl-kit/client/routes/route.tsx +13 -5
- package/dist/templates/default/packages/disposabl-kit/client/vite.config.ts +3 -2
- package/dist/templates/default/packages/disposabl-kit/lib/telemetry.ts +148 -12
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/account-billing.tsx +143 -40
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/paywall.ts +83 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/policy.ts +125 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/session.ts +31 -3
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/topup.ts +99 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/use-topup.ts +116 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/web-checkout.ts +190 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/layout.tsx +9 -2
- package/dist/templates/default/packages/disposabl-kit/package.json +10 -1
- package/dist/templates/default/packages/disposabl-kit/payments/checkout-attempt.ts +64 -0
- package/dist/templates/default/packages/disposabl-kit/payments/config.ts +16 -3
- package/dist/templates/default/packages/disposabl-kit/server/billing-functions.ts +218 -9
- package/dist/templates/default/packages/disposabl-kit/server/billing-handoff.ts +126 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing-native-capability.ts +12 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing-setup.ts +2 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing.ts +31 -0
- package/dist/templates/default/packages/disposabl-kit/server/browser-session.ts +27 -0
- package/dist/templates/default/packages/disposabl-kit/server/lib/telemetry.ts +173 -59
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-contract.ts +233 -0
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-functions.ts +554 -0
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-validator.ts +76 -0
- package/dist/templates/default/packages/reconfigure-skill/SKILL.md +36 -0
- package/dist/templates/default/packages/sdk/README.md +21 -0
- package/dist/templates/default/packages/sdk/src/analytics.ts +102 -0
- package/dist/templates/default/packages/sdk/src/index.ts +2 -0
- package/dist/templates/default/packages/skill/SKILL.md +2 -0
- package/dist/templates/default/packages/skill/references/agent.md +2 -0
- package/dist/templates/default/packages/tests/billing-attempts.test.ts +156 -0
- package/dist/templates/default/packages/tests/billing-handoff.test.ts +116 -0
- package/dist/templates/default/packages/tests/billing-setup.test.ts +4 -3
- package/dist/templates/default/packages/tests/billing.test.ts +48 -4
- package/dist/templates/default/packages/tests/desktop-packaging.test.ts +12 -0
- package/dist/templates/default/packages/tests/flows/seo.e2e.ts +3 -1
- package/dist/templates/default/packages/tests/native-billing.test.ts +417 -0
- package/dist/templates/default/packages/tests/page-telemetry.test.ts +218 -0
- package/dist/templates/default/packages/tests/revenuecat-inbox.test.ts +476 -0
- package/dist/templates/default/packages/tests/sdk-analytics.test.ts +158 -0
- package/dist/templates/default/packages/tests/telemetry.test.ts +272 -55
- package/package.json +1 -1
- package/dist/templates/default/apps/web/src/routeTree.gen.ts +0 -578
|
@@ -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
|
+
});
|
|
@@ -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
|
+
});
|