@simplelayer/graniteer-core 0.2.0 → 0.2.2
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/index.js +2 -2
- package/package.json +1 -1
- package/src/agreementCheckout.ts +250 -250
- package/src/agreementPortalEmail.ts +128 -128
- package/src/billingPortalLinkEmail.ts +117 -117
- package/src/bookingAgreementEmail.ts +149 -149
- package/src/bookingEffects.ts +388 -388
- package/src/busClient.ts +268 -268
- package/src/busSignature.ts +71 -71
- package/src/checkoutCoupon.test.ts +675 -675
- package/src/checkoutCoupon.ts +455 -455
- package/src/commonsDeposit.ts +298 -298
- package/src/commonsDepositWebhook.ts +237 -237
- package/src/dayPassConfirmation.ts +230 -230
- package/src/dunningEmails.test.ts +263 -263
- package/src/dunningEmails.ts +203 -203
- package/src/email-templates/brand.ts +93 -93
- package/src/email-templates/magic-link.tsx +59 -59
- package/src/email-templates/recovery.tsx +59 -59
- package/src/email-templates/signup.tsx +67 -67
- package/src/functions/admin-cancel-membership.ts +311 -311
- package/src/functions/admin-change-membership-plan.ts +312 -312
- package/src/functions/admin-charge-guest-print.ts +229 -229
- package/src/functions/admin-correct-booking.ts +333 -333
- package/src/functions/admin-create-booking.ts +460 -460
- package/src/functions/admin-create-commons-event.ts +244 -244
- package/src/functions/admin-debug-boldsign-doc.ts +93 -93
- package/src/functions/admin-door-write.ts +272 -272
- package/src/functions/admin-issue-day-pass.ts +231 -231
- package/src/functions/admin-mark-booking-paid.ts +203 -203
- package/src/functions/admin-mint-billing-portal-link.ts +168 -168
- package/src/functions/admin-mint-portal-link.ts +62 -62
- package/src/functions/admin-photo-upload.ts +128 -128
- package/src/functions/admin-remove-team-member.ts +150 -150
- package/src/functions/admin-request-trial.ts +366 -366
- package/src/functions/admin-resend-commons-deposit-link.ts +323 -323
- package/src/functions/admin-reset-mfa.ts +138 -138
- package/src/functions/admin-retry-invoice-payment.ts +333 -333
- package/src/functions/admin-reverse-coupon-redemption.ts +98 -98
- package/src/functions/admin-revoke-agreement.ts +467 -467
- package/src/functions/admin-send-commons-agreement.ts +214 -214
- package/src/functions/admin-set-credit-amount.ts +123 -123
- package/src/functions/admin-skip-commons-contract.ts +165 -165
- package/src/functions/admin-undo-membership-cancel.ts +144 -144
- package/src/functions/admin-wall-photo-write.ts +141 -141
- package/src/functions/auth-email-hook.ts +259 -259
- package/src/functions/boldsign-sender-identity.ts +114 -114
- package/src/functions/booking-availability.ts +419 -419
- package/src/functions/booking-member-preview.ts +136 -136
- package/src/functions/booking-status.ts +149 -149
- package/src/functions/create-booking.ts +648 -648
- package/src/functions/create-day-pass-checkout.ts +218 -218
- package/src/functions/create-portal-session.ts +142 -142
- package/src/functions/create-subscription-checkout.test.ts +222 -222
- package/src/functions/create-subscription-checkout.ts +440 -440
- package/src/functions/day-pass-status.ts +89 -89
- package/src/functions/diag-webpush-test.ts +45 -45
- package/src/functions/event-access-admin.ts +277 -277
- package/src/functions/event-access-store.ts +334 -309
- package/src/functions/ezeep-print-webhook.ts +213 -213
- package/src/functions/ezeep-usage-reconcile.ts +299 -299
- package/src/functions/fetch-avatar-original.ts +457 -457
- package/src/functions/get-provider-sign-link.ts +155 -155
- package/src/functions/get-signing-link.ts +211 -211
- package/src/functions/guest-print-setup-confirm.ts +128 -128
- package/src/functions/guest-print-submit.ts +149 -149
- package/src/functions/luma-webhook.ts +165 -165
- package/src/functions/mcp.ts +67 -67
- package/src/functions/member-door-action.test.ts +1248 -1248
- package/src/functions/member-door-action.ts +863 -863
- package/src/functions/member-unifi-mapping.test.ts +516 -516
- package/src/functions/member-unifi-mapping.ts +206 -206
- package/src/functions/membership-door-sync.ts +956 -956
- package/src/functions/migration-collision-alert.copy.ts +232 -232
- package/src/functions/migration-collision-alert.test.ts +349 -349
- package/src/functions/migration-collision-alert.ts +232 -232
- package/src/functions/office-assignment-end.ts +146 -146
- package/src/functions/portal-booking-access.ts +217 -95
- package/src/functions/portal-list-invoices.ts +99 -99
- package/src/functions/portal-request-login.ts +219 -192
- package/src/functions/portal-resend-invite.ts +248 -248
- package/src/functions/portal-submit-event.ts +168 -168
- package/src/functions/print-billing-recover.ts +97 -97
- package/src/functions/print-billing-run.ts +183 -183
- package/src/functions/public-facts.ts +168 -168
- package/src/functions/push-send.ts +348 -348
- package/src/functions/push-test.ts +234 -234
- package/src/functions/request-portal-link.ts +211 -211
- package/src/functions/review-ratings.ts +113 -113
- package/src/functions/space-availability-range.acceptance.test.ts +161 -161
- package/src/functions/space-availability-range.ts +142 -142
- package/src/functions/stripe-db-reconcile.ts +319 -319
- package/src/functions/stripe-webhook.moved-regression.test.ts +78 -78
- package/src/functions/stripe-webhook.ts +2595 -2595
- package/src/functions/sync-gcal-blackouts.ts +298 -298
- package/src/functions/timetable-event-register.ts +162 -162
- package/src/functions/timetable-event-status.ts +90 -90
- package/src/functions/timetable-spaces.ts +219 -219
- package/src/functions/touch-pass-reconciler.ts +689 -689
- package/src/functions/touch-pass-signup.ts +478 -478
- package/src/functions/upload-booking-display-image.ts +179 -179
- package/src/functions/virtual-onboarding-purge.ts +134 -134
- package/src/functions/virtual-onboarding-submit.ts +315 -315
- package/src/functions/voice-booking.ts +453 -453
- package/src/functions/voice-day-pass.ts +346 -346
- package/src/functions/voice-inquiry.test.ts +301 -301
- package/src/functions/voice-inquiry.ts +198 -198
- package/src/functions/voice-membership-signup.ts +355 -355
- package/src/functions/voice-tour-availability.ts +204 -204
- package/src/functions/voice-tour-booking.ts +429 -429
- package/src/ghlEventInquiry.ts +143 -143
- package/src/mailArrivalEmail.ts +121 -121
- package/src/manageToken.ts +363 -363
- package/src/membershipBusClient.ts +537 -537
- package/src/membershipWelcome.ts +683 -683
- package/src/migrationCheckout.ts +366 -366
- package/src/paymentLinkEmail.ts +123 -123
- package/src/signedUnpaidReminderEmail.test.ts +246 -246
- package/src/signedUnpaidReminderEmail.ts +132 -132
- package/src/stripeCoworkingPortal.ts +91 -91
- package/src/supabaseTestFakes.ts +131 -131
- package/src/supportTicketEmail.ts +119 -119
- package/src/unifiAccessPolicies.ts +228 -228
- package/src/unifiGroups.ts +129 -129
|
@@ -1,222 +1,222 @@
|
|
|
1
|
-
// Unit tests for the create-subscription-checkout edge function.
|
|
2
|
-
//
|
|
3
|
-
// Run with:
|
|
4
|
-
// deno test --no-check --node-modules-dir=auto --allow-env --allow-net --allow-read \
|
|
5
|
-
// src/functions/create-subscription-checkout.test.ts
|
|
6
|
-
//
|
|
7
|
-
// NO REAL NETWORK, and in particular NO LIVE STRIPE. Every outbound call --
|
|
8
|
-
// PostgREST, the coupon RPCs and api.stripe.com -- is answered by an in-memory
|
|
9
|
-
// `globalThis.fetch` stub keyed off the request URL (the npm:stripe client
|
|
10
|
-
// routes through globalThis.fetch under Deno, so the stub covers it too). The
|
|
11
|
-
// stubbed Supabase host is on the reserved .invalid TLD and the Stripe key is a
|
|
12
|
-
// literal placeholder, so a stub miss cannot reach a real host with real
|
|
13
|
-
// credentials -- an unmatched URL throws and fails the test.
|
|
14
|
-
//
|
|
15
|
-
// Scope: the coupon_code + future start_date refusal. Combining a `discounts`
|
|
16
|
-
// coupon with a future subscription_data.billing_cycle_anchor is UNVERIFIED
|
|
17
|
-
// against real Stripe -- a duration:'once' coupon may be consumed by the
|
|
18
|
-
// placeholder invoice at session creation, leaving the real first charge at the
|
|
19
|
-
// anchor date at full price while the internal redemption already reads
|
|
20
|
-
// 'applied'. Until a live experiment settles that, the pair is refused.
|
|
21
|
-
|
|
22
|
-
import { assert, assertEquals, assertStringIncludes } from 'https://deno.land/std@0.224.0/assert/mod.ts';
|
|
23
|
-
|
|
24
|
-
const SUPABASE_URL = 'https://stub.supabase.test.invalid';
|
|
25
|
-
Deno.env.set('SUPABASE_URL', SUPABASE_URL);
|
|
26
|
-
Deno.env.set('SUPABASE_SERVICE_ROLE_KEY', 'stub-service-role-key');
|
|
27
|
-
Deno.env.set('STRIPE_SECRET_KEY', 'sk_test_stub_never_used');
|
|
28
|
-
|
|
29
|
-
const { handle } = await import('./create-subscription-checkout.ts');
|
|
30
|
-
const { COUPON_START_DATE_CONFLICT_MESSAGE } = await import('npm:@simplelayer/graniteer-core/checkoutCoupon');
|
|
31
|
-
|
|
32
|
-
const OPERATOR = { domain: 'granitecity.io', actorEmail: 'system@granitecity.io' };
|
|
33
|
-
|
|
34
|
-
const PLAN_ID = '11111111-1111-1111-1111-111111111111';
|
|
35
|
-
|
|
36
|
-
// --- Pacific calendar dates, computed the same way the function does ---------
|
|
37
|
-
function pacificToday(): string {
|
|
38
|
-
return new Intl.DateTimeFormat('en-CA', {
|
|
39
|
-
timeZone: 'America/Los_Angeles', year: 'numeric', month: '2-digit', day: '2-digit',
|
|
40
|
-
}).format(new Date());
|
|
41
|
-
}
|
|
42
|
-
function plusDays(dateStr: string, days: number): string {
|
|
43
|
-
const [y, m, d] = dateStr.split('-').map(Number);
|
|
44
|
-
return new Date(Date.UTC(y, m - 1, d + days)).toISOString().slice(0, 10);
|
|
45
|
-
}
|
|
46
|
-
const TODAY = pacificToday();
|
|
47
|
-
const FUTURE = plusDays(TODAY, 5);
|
|
48
|
-
|
|
49
|
-
// --- fetch stub -------------------------------------------------------------
|
|
50
|
-
|
|
51
|
-
interface Recorded { url: string; method: string; body: string }
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* `routes` are matched by substring against the request URL, first match wins.
|
|
55
|
-
* An unmatched URL throws, so nothing can silently escape to the network.
|
|
56
|
-
*/
|
|
57
|
-
function installFetchStub(routes: Array<[match: string, json: unknown]>) {
|
|
58
|
-
const calls: Recorded[] = [];
|
|
59
|
-
const original = globalThis.fetch;
|
|
60
|
-
globalThis.fetch = ((input: string | URL | Request, init?: RequestInit) => {
|
|
61
|
-
const url = typeof input === 'string'
|
|
62
|
-
? input
|
|
63
|
-
: input instanceof URL ? input.href : input.url;
|
|
64
|
-
const method = String(init?.method ?? (input instanceof Request ? input.method : 'GET')).toUpperCase();
|
|
65
|
-
calls.push({ url, method, body: typeof init?.body === 'string' ? init.body : '' });
|
|
66
|
-
for (const [match, json] of routes) {
|
|
67
|
-
if (url.includes(match)) {
|
|
68
|
-
return Promise.resolve(new Response(JSON.stringify(json), {
|
|
69
|
-
status: 200, headers: { 'Content-Type': 'application/json' },
|
|
70
|
-
}));
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return Promise.reject(new Error(`unstubbed fetch: ${method} ${url}`));
|
|
74
|
-
}) as typeof fetch;
|
|
75
|
-
return { calls, restore: () => { globalThis.fetch = original; } };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Ad-hoc-price plan: priced from price_cents, so no Stripe prices.retrieve. */
|
|
79
|
-
const AD_HOC_PLAN = {
|
|
80
|
-
id: PLAN_ID,
|
|
81
|
-
slug: 'workday',
|
|
82
|
-
name: 'Workday',
|
|
83
|
-
category: 'coworking',
|
|
84
|
-
active: true,
|
|
85
|
-
billable: true,
|
|
86
|
-
billing_interval: 'month',
|
|
87
|
-
stripe_price_id: null,
|
|
88
|
-
stripe_product_id: null,
|
|
89
|
-
price_cents: 17900,
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const SUPABASE_ROUTES: Array<[string, unknown]> = [
|
|
93
|
-
['/rest/v1/rpc/check_rate_limit', true],
|
|
94
|
-
['/rest/v1/membership_plans', AD_HOC_PLAN],
|
|
95
|
-
];
|
|
96
|
-
|
|
97
|
-
const STRIPE_ROUTES: Array<[string, unknown]> = [
|
|
98
|
-
['api.stripe.com/v1/customers/search', { object: 'search_result', data: [], has_more: false }],
|
|
99
|
-
['api.stripe.com/v1/customers?', { object: 'list', data: [], has_more: false }],
|
|
100
|
-
['api.stripe.com/v1/customers', { id: 'cus_stub', object: 'customer' }],
|
|
101
|
-
['api.stripe.com/v1/checkout/sessions', {
|
|
102
|
-
id: 'cs_stub', object: 'checkout.session', url: 'https://checkout.stripe.test.invalid/c/pay/cs_stub',
|
|
103
|
-
}],
|
|
104
|
-
];
|
|
105
|
-
|
|
106
|
-
function post(body: Record<string, unknown>): Request {
|
|
107
|
-
return new Request('https://fn.test.invalid/create-subscription-checkout', {
|
|
108
|
-
method: 'POST',
|
|
109
|
-
headers: { 'Content-Type': 'application/json', origin: 'https://granitecity.io' },
|
|
110
|
-
body: JSON.stringify(body),
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const BASE_BODY = { plan_slug: 'workday', email: 'kyle@simplelayer.io', name: 'Kyle Vogt' };
|
|
115
|
-
|
|
116
|
-
// --- the refusal ------------------------------------------------------------
|
|
117
|
-
|
|
118
|
-
Deno.test('coupon_code + a FUTURE start_date -> 400, and no coupon RPC is attempted', async () => {
|
|
119
|
-
// Deliberately no routes at all: the guard must fire before any I/O, so any
|
|
120
|
-
// outbound call is itself the failure.
|
|
121
|
-
const stub = installFetchStub([]);
|
|
122
|
-
try {
|
|
123
|
-
const res = await handle(post({ ...BASE_BODY, start_date: FUTURE, coupon_code: 'GCSTAFFKYLE' }), OPERATOR);
|
|
124
|
-
|
|
125
|
-
assertEquals(res.status, 400);
|
|
126
|
-
const body = await res.json();
|
|
127
|
-
assertEquals(body.error, COUPON_START_DATE_CONFLICT_MESSAGE);
|
|
128
|
-
// Puts the message under the coupon input in the modal.
|
|
129
|
-
assertEquals(body.field, 'coupon_code');
|
|
130
|
-
assertStringIncludes(body.error, 'scheduled start date');
|
|
131
|
-
|
|
132
|
-
assertEquals(stub.calls.length, 0, 'the guard must fire before any network call');
|
|
133
|
-
assertEquals(
|
|
134
|
-
stub.calls.filter((c) => c.url.includes('coupon_')).length,
|
|
135
|
-
0,
|
|
136
|
-
'no coupon RPC may run for a refused combination',
|
|
137
|
-
);
|
|
138
|
-
assertEquals(
|
|
139
|
-
stub.calls.filter((c) => c.url.includes('api.stripe.com')).length,
|
|
140
|
-
0,
|
|
141
|
-
'no Stripe call may run for a refused combination',
|
|
142
|
-
);
|
|
143
|
-
} finally {
|
|
144
|
-
stub.restore();
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
// --- either field alone still works normally --------------------------------
|
|
149
|
-
|
|
150
|
-
Deno.test('coupon_code ALONE still reaches the coupon system', async () => {
|
|
151
|
-
const stub = installFetchStub([
|
|
152
|
-
...SUPABASE_ROUTES,
|
|
153
|
-
['/rest/v1/rpc/coupon_validate', [{ valid: false, coupon_id: null, discount_cents: 0, reason: 'COUPON_EXPIRED' }]],
|
|
154
|
-
]);
|
|
155
|
-
try {
|
|
156
|
-
const res = await handle(post({ ...BASE_BODY, coupon_code: 'GCSTAFFKYLE' }), OPERATOR);
|
|
157
|
-
|
|
158
|
-
// The coupon's OWN verdict comes back -- not the start-date refusal.
|
|
159
|
-
assertEquals(res.status, 400);
|
|
160
|
-
const body = await res.json();
|
|
161
|
-
assertEquals(body.error, 'That code has expired.');
|
|
162
|
-
assert(body.error !== COUPON_START_DATE_CONFLICT_MESSAGE, 'the start-date guard must not fire');
|
|
163
|
-
|
|
164
|
-
assertEquals(
|
|
165
|
-
stub.calls.filter((c) => c.url.includes('/rpc/coupon_validate')).length,
|
|
166
|
-
1,
|
|
167
|
-
'the coupon path must be entered when no start_date is present',
|
|
168
|
-
);
|
|
169
|
-
assertEquals(stub.calls.filter((c) => c.url.includes('api.stripe.com')).length, 0);
|
|
170
|
-
} finally {
|
|
171
|
-
stub.restore();
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
Deno.test('a future start_date ALONE still anchors the billing cycle, no coupon RPC', async () => {
|
|
176
|
-
const stub = installFetchStub([...SUPABASE_ROUTES, ...STRIPE_ROUTES]);
|
|
177
|
-
try {
|
|
178
|
-
const res = await handle(post({ ...BASE_BODY, start_date: FUTURE }), OPERATOR);
|
|
179
|
-
|
|
180
|
-
assertEquals(res.status, 200);
|
|
181
|
-
const body = await res.json();
|
|
182
|
-
assertEquals(body.url, 'https://checkout.stripe.test.invalid/c/pay/cs_stub');
|
|
183
|
-
|
|
184
|
-
const session = stub.calls.find((c) => c.url.includes('/v1/checkout/sessions'));
|
|
185
|
-
assert(session, 'expected a Checkout Session to be created');
|
|
186
|
-
// The pre-existing future-start behavior, unchanged.
|
|
187
|
-
assertStringIncludes(session.body, 'subscription_data[billing_cycle_anchor]=');
|
|
188
|
-
assertStringIncludes(session.body, 'subscription_data[proration_behavior]=none');
|
|
189
|
-
// No coupon -> the pre-existing promotion-code affordance stays on and no
|
|
190
|
-
// `discounts` key is emitted.
|
|
191
|
-
assertStringIncludes(session.body, 'allow_promotion_codes=true');
|
|
192
|
-
assert(!session.body.includes('discounts'), 'no discounts key without a coupon');
|
|
193
|
-
|
|
194
|
-
assertEquals(
|
|
195
|
-
stub.calls.filter((c) => c.url.includes('coupon_')).length,
|
|
196
|
-
0,
|
|
197
|
-
'no coupon RPC may run when no coupon_code was sent',
|
|
198
|
-
);
|
|
199
|
-
} finally {
|
|
200
|
-
stub.restore();
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
Deno.test("coupon_code + TODAY's start_date is allowed -- the guard keys on a FUTURE anchor", async () => {
|
|
205
|
-
// A today-or-past date leaves futureStartUnix null: bill immediately, no
|
|
206
|
-
// billing_cycle_anchor. That is the ordinary path, so a coupon is fine.
|
|
207
|
-
const stub = installFetchStub([
|
|
208
|
-
...SUPABASE_ROUTES,
|
|
209
|
-
['/rest/v1/rpc/coupon_validate', [{ valid: false, coupon_id: null, discount_cents: 0, reason: 'COUPON_EXPIRED' }]],
|
|
210
|
-
]);
|
|
211
|
-
try {
|
|
212
|
-
const res = await handle(post({ ...BASE_BODY, start_date: TODAY, coupon_code: 'GCSTAFFKYLE' }), OPERATOR);
|
|
213
|
-
|
|
214
|
-
assertEquals(res.status, 400);
|
|
215
|
-
const body = await res.json();
|
|
216
|
-
assert(body.error !== COUPON_START_DATE_CONFLICT_MESSAGE, 'a same-day start must not trip the guard');
|
|
217
|
-
assertEquals(body.error, 'That code has expired.');
|
|
218
|
-
assertEquals(stub.calls.filter((c) => c.url.includes('/rpc/coupon_validate')).length, 1);
|
|
219
|
-
} finally {
|
|
220
|
-
stub.restore();
|
|
221
|
-
}
|
|
222
|
-
});
|
|
1
|
+
// Unit tests for the create-subscription-checkout edge function.
|
|
2
|
+
//
|
|
3
|
+
// Run with:
|
|
4
|
+
// deno test --no-check --node-modules-dir=auto --allow-env --allow-net --allow-read \
|
|
5
|
+
// src/functions/create-subscription-checkout.test.ts
|
|
6
|
+
//
|
|
7
|
+
// NO REAL NETWORK, and in particular NO LIVE STRIPE. Every outbound call --
|
|
8
|
+
// PostgREST, the coupon RPCs and api.stripe.com -- is answered by an in-memory
|
|
9
|
+
// `globalThis.fetch` stub keyed off the request URL (the npm:stripe client
|
|
10
|
+
// routes through globalThis.fetch under Deno, so the stub covers it too). The
|
|
11
|
+
// stubbed Supabase host is on the reserved .invalid TLD and the Stripe key is a
|
|
12
|
+
// literal placeholder, so a stub miss cannot reach a real host with real
|
|
13
|
+
// credentials -- an unmatched URL throws and fails the test.
|
|
14
|
+
//
|
|
15
|
+
// Scope: the coupon_code + future start_date refusal. Combining a `discounts`
|
|
16
|
+
// coupon with a future subscription_data.billing_cycle_anchor is UNVERIFIED
|
|
17
|
+
// against real Stripe -- a duration:'once' coupon may be consumed by the
|
|
18
|
+
// placeholder invoice at session creation, leaving the real first charge at the
|
|
19
|
+
// anchor date at full price while the internal redemption already reads
|
|
20
|
+
// 'applied'. Until a live experiment settles that, the pair is refused.
|
|
21
|
+
|
|
22
|
+
import { assert, assertEquals, assertStringIncludes } from 'https://deno.land/std@0.224.0/assert/mod.ts';
|
|
23
|
+
|
|
24
|
+
const SUPABASE_URL = 'https://stub.supabase.test.invalid';
|
|
25
|
+
Deno.env.set('SUPABASE_URL', SUPABASE_URL);
|
|
26
|
+
Deno.env.set('SUPABASE_SERVICE_ROLE_KEY', 'stub-service-role-key');
|
|
27
|
+
Deno.env.set('STRIPE_SECRET_KEY', 'sk_test_stub_never_used');
|
|
28
|
+
|
|
29
|
+
const { handle } = await import('./create-subscription-checkout.ts');
|
|
30
|
+
const { COUPON_START_DATE_CONFLICT_MESSAGE } = await import('npm:@simplelayer/graniteer-core/checkoutCoupon');
|
|
31
|
+
|
|
32
|
+
const OPERATOR = { domain: 'granitecity.io', actorEmail: 'system@granitecity.io' };
|
|
33
|
+
|
|
34
|
+
const PLAN_ID = '11111111-1111-1111-1111-111111111111';
|
|
35
|
+
|
|
36
|
+
// --- Pacific calendar dates, computed the same way the function does ---------
|
|
37
|
+
function pacificToday(): string {
|
|
38
|
+
return new Intl.DateTimeFormat('en-CA', {
|
|
39
|
+
timeZone: 'America/Los_Angeles', year: 'numeric', month: '2-digit', day: '2-digit',
|
|
40
|
+
}).format(new Date());
|
|
41
|
+
}
|
|
42
|
+
function plusDays(dateStr: string, days: number): string {
|
|
43
|
+
const [y, m, d] = dateStr.split('-').map(Number);
|
|
44
|
+
return new Date(Date.UTC(y, m - 1, d + days)).toISOString().slice(0, 10);
|
|
45
|
+
}
|
|
46
|
+
const TODAY = pacificToday();
|
|
47
|
+
const FUTURE = plusDays(TODAY, 5);
|
|
48
|
+
|
|
49
|
+
// --- fetch stub -------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
interface Recorded { url: string; method: string; body: string }
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* `routes` are matched by substring against the request URL, first match wins.
|
|
55
|
+
* An unmatched URL throws, so nothing can silently escape to the network.
|
|
56
|
+
*/
|
|
57
|
+
function installFetchStub(routes: Array<[match: string, json: unknown]>) {
|
|
58
|
+
const calls: Recorded[] = [];
|
|
59
|
+
const original = globalThis.fetch;
|
|
60
|
+
globalThis.fetch = ((input: string | URL | Request, init?: RequestInit) => {
|
|
61
|
+
const url = typeof input === 'string'
|
|
62
|
+
? input
|
|
63
|
+
: input instanceof URL ? input.href : input.url;
|
|
64
|
+
const method = String(init?.method ?? (input instanceof Request ? input.method : 'GET')).toUpperCase();
|
|
65
|
+
calls.push({ url, method, body: typeof init?.body === 'string' ? init.body : '' });
|
|
66
|
+
for (const [match, json] of routes) {
|
|
67
|
+
if (url.includes(match)) {
|
|
68
|
+
return Promise.resolve(new Response(JSON.stringify(json), {
|
|
69
|
+
status: 200, headers: { 'Content-Type': 'application/json' },
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return Promise.reject(new Error(`unstubbed fetch: ${method} ${url}`));
|
|
74
|
+
}) as typeof fetch;
|
|
75
|
+
return { calls, restore: () => { globalThis.fetch = original; } };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Ad-hoc-price plan: priced from price_cents, so no Stripe prices.retrieve. */
|
|
79
|
+
const AD_HOC_PLAN = {
|
|
80
|
+
id: PLAN_ID,
|
|
81
|
+
slug: 'workday',
|
|
82
|
+
name: 'Workday',
|
|
83
|
+
category: 'coworking',
|
|
84
|
+
active: true,
|
|
85
|
+
billable: true,
|
|
86
|
+
billing_interval: 'month',
|
|
87
|
+
stripe_price_id: null,
|
|
88
|
+
stripe_product_id: null,
|
|
89
|
+
price_cents: 17900,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const SUPABASE_ROUTES: Array<[string, unknown]> = [
|
|
93
|
+
['/rest/v1/rpc/check_rate_limit', true],
|
|
94
|
+
['/rest/v1/membership_plans', AD_HOC_PLAN],
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
const STRIPE_ROUTES: Array<[string, unknown]> = [
|
|
98
|
+
['api.stripe.com/v1/customers/search', { object: 'search_result', data: [], has_more: false }],
|
|
99
|
+
['api.stripe.com/v1/customers?', { object: 'list', data: [], has_more: false }],
|
|
100
|
+
['api.stripe.com/v1/customers', { id: 'cus_stub', object: 'customer' }],
|
|
101
|
+
['api.stripe.com/v1/checkout/sessions', {
|
|
102
|
+
id: 'cs_stub', object: 'checkout.session', url: 'https://checkout.stripe.test.invalid/c/pay/cs_stub',
|
|
103
|
+
}],
|
|
104
|
+
];
|
|
105
|
+
|
|
106
|
+
function post(body: Record<string, unknown>): Request {
|
|
107
|
+
return new Request('https://fn.test.invalid/create-subscription-checkout', {
|
|
108
|
+
method: 'POST',
|
|
109
|
+
headers: { 'Content-Type': 'application/json', origin: 'https://granitecity.io' },
|
|
110
|
+
body: JSON.stringify(body),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const BASE_BODY = { plan_slug: 'workday', email: 'kyle@simplelayer.io', name: 'Kyle Vogt' };
|
|
115
|
+
|
|
116
|
+
// --- the refusal ------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
Deno.test('coupon_code + a FUTURE start_date -> 400, and no coupon RPC is attempted', async () => {
|
|
119
|
+
// Deliberately no routes at all: the guard must fire before any I/O, so any
|
|
120
|
+
// outbound call is itself the failure.
|
|
121
|
+
const stub = installFetchStub([]);
|
|
122
|
+
try {
|
|
123
|
+
const res = await handle(post({ ...BASE_BODY, start_date: FUTURE, coupon_code: 'GCSTAFFKYLE' }), OPERATOR);
|
|
124
|
+
|
|
125
|
+
assertEquals(res.status, 400);
|
|
126
|
+
const body = await res.json();
|
|
127
|
+
assertEquals(body.error, COUPON_START_DATE_CONFLICT_MESSAGE);
|
|
128
|
+
// Puts the message under the coupon input in the modal.
|
|
129
|
+
assertEquals(body.field, 'coupon_code');
|
|
130
|
+
assertStringIncludes(body.error, 'scheduled start date');
|
|
131
|
+
|
|
132
|
+
assertEquals(stub.calls.length, 0, 'the guard must fire before any network call');
|
|
133
|
+
assertEquals(
|
|
134
|
+
stub.calls.filter((c) => c.url.includes('coupon_')).length,
|
|
135
|
+
0,
|
|
136
|
+
'no coupon RPC may run for a refused combination',
|
|
137
|
+
);
|
|
138
|
+
assertEquals(
|
|
139
|
+
stub.calls.filter((c) => c.url.includes('api.stripe.com')).length,
|
|
140
|
+
0,
|
|
141
|
+
'no Stripe call may run for a refused combination',
|
|
142
|
+
);
|
|
143
|
+
} finally {
|
|
144
|
+
stub.restore();
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// --- either field alone still works normally --------------------------------
|
|
149
|
+
|
|
150
|
+
Deno.test('coupon_code ALONE still reaches the coupon system', async () => {
|
|
151
|
+
const stub = installFetchStub([
|
|
152
|
+
...SUPABASE_ROUTES,
|
|
153
|
+
['/rest/v1/rpc/coupon_validate', [{ valid: false, coupon_id: null, discount_cents: 0, reason: 'COUPON_EXPIRED' }]],
|
|
154
|
+
]);
|
|
155
|
+
try {
|
|
156
|
+
const res = await handle(post({ ...BASE_BODY, coupon_code: 'GCSTAFFKYLE' }), OPERATOR);
|
|
157
|
+
|
|
158
|
+
// The coupon's OWN verdict comes back -- not the start-date refusal.
|
|
159
|
+
assertEquals(res.status, 400);
|
|
160
|
+
const body = await res.json();
|
|
161
|
+
assertEquals(body.error, 'That code has expired.');
|
|
162
|
+
assert(body.error !== COUPON_START_DATE_CONFLICT_MESSAGE, 'the start-date guard must not fire');
|
|
163
|
+
|
|
164
|
+
assertEquals(
|
|
165
|
+
stub.calls.filter((c) => c.url.includes('/rpc/coupon_validate')).length,
|
|
166
|
+
1,
|
|
167
|
+
'the coupon path must be entered when no start_date is present',
|
|
168
|
+
);
|
|
169
|
+
assertEquals(stub.calls.filter((c) => c.url.includes('api.stripe.com')).length, 0);
|
|
170
|
+
} finally {
|
|
171
|
+
stub.restore();
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
Deno.test('a future start_date ALONE still anchors the billing cycle, no coupon RPC', async () => {
|
|
176
|
+
const stub = installFetchStub([...SUPABASE_ROUTES, ...STRIPE_ROUTES]);
|
|
177
|
+
try {
|
|
178
|
+
const res = await handle(post({ ...BASE_BODY, start_date: FUTURE }), OPERATOR);
|
|
179
|
+
|
|
180
|
+
assertEquals(res.status, 200);
|
|
181
|
+
const body = await res.json();
|
|
182
|
+
assertEquals(body.url, 'https://checkout.stripe.test.invalid/c/pay/cs_stub');
|
|
183
|
+
|
|
184
|
+
const session = stub.calls.find((c) => c.url.includes('/v1/checkout/sessions'));
|
|
185
|
+
assert(session, 'expected a Checkout Session to be created');
|
|
186
|
+
// The pre-existing future-start behavior, unchanged.
|
|
187
|
+
assertStringIncludes(session.body, 'subscription_data[billing_cycle_anchor]=');
|
|
188
|
+
assertStringIncludes(session.body, 'subscription_data[proration_behavior]=none');
|
|
189
|
+
// No coupon -> the pre-existing promotion-code affordance stays on and no
|
|
190
|
+
// `discounts` key is emitted.
|
|
191
|
+
assertStringIncludes(session.body, 'allow_promotion_codes=true');
|
|
192
|
+
assert(!session.body.includes('discounts'), 'no discounts key without a coupon');
|
|
193
|
+
|
|
194
|
+
assertEquals(
|
|
195
|
+
stub.calls.filter((c) => c.url.includes('coupon_')).length,
|
|
196
|
+
0,
|
|
197
|
+
'no coupon RPC may run when no coupon_code was sent',
|
|
198
|
+
);
|
|
199
|
+
} finally {
|
|
200
|
+
stub.restore();
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
Deno.test("coupon_code + TODAY's start_date is allowed -- the guard keys on a FUTURE anchor", async () => {
|
|
205
|
+
// A today-or-past date leaves futureStartUnix null: bill immediately, no
|
|
206
|
+
// billing_cycle_anchor. That is the ordinary path, so a coupon is fine.
|
|
207
|
+
const stub = installFetchStub([
|
|
208
|
+
...SUPABASE_ROUTES,
|
|
209
|
+
['/rest/v1/rpc/coupon_validate', [{ valid: false, coupon_id: null, discount_cents: 0, reason: 'COUPON_EXPIRED' }]],
|
|
210
|
+
]);
|
|
211
|
+
try {
|
|
212
|
+
const res = await handle(post({ ...BASE_BODY, start_date: TODAY, coupon_code: 'GCSTAFFKYLE' }), OPERATOR);
|
|
213
|
+
|
|
214
|
+
assertEquals(res.status, 400);
|
|
215
|
+
const body = await res.json();
|
|
216
|
+
assert(body.error !== COUPON_START_DATE_CONFLICT_MESSAGE, 'a same-day start must not trip the guard');
|
|
217
|
+
assertEquals(body.error, 'That code has expired.');
|
|
218
|
+
assertEquals(stub.calls.filter((c) => c.url.includes('/rpc/coupon_validate')).length, 1);
|
|
219
|
+
} finally {
|
|
220
|
+
stub.restore();
|
|
221
|
+
}
|
|
222
|
+
});
|