@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.
Files changed (124) hide show
  1. package/index.js +2 -2
  2. package/package.json +1 -1
  3. package/src/agreementCheckout.ts +250 -250
  4. package/src/agreementPortalEmail.ts +128 -128
  5. package/src/billingPortalLinkEmail.ts +117 -117
  6. package/src/bookingAgreementEmail.ts +149 -149
  7. package/src/bookingEffects.ts +388 -388
  8. package/src/busClient.ts +268 -268
  9. package/src/busSignature.ts +71 -71
  10. package/src/checkoutCoupon.test.ts +675 -675
  11. package/src/checkoutCoupon.ts +455 -455
  12. package/src/commonsDeposit.ts +298 -298
  13. package/src/commonsDepositWebhook.ts +237 -237
  14. package/src/dayPassConfirmation.ts +230 -230
  15. package/src/dunningEmails.test.ts +263 -263
  16. package/src/dunningEmails.ts +203 -203
  17. package/src/email-templates/brand.ts +93 -93
  18. package/src/email-templates/magic-link.tsx +59 -59
  19. package/src/email-templates/recovery.tsx +59 -59
  20. package/src/email-templates/signup.tsx +67 -67
  21. package/src/functions/admin-cancel-membership.ts +311 -311
  22. package/src/functions/admin-change-membership-plan.ts +312 -312
  23. package/src/functions/admin-charge-guest-print.ts +229 -229
  24. package/src/functions/admin-correct-booking.ts +333 -333
  25. package/src/functions/admin-create-booking.ts +460 -460
  26. package/src/functions/admin-create-commons-event.ts +244 -244
  27. package/src/functions/admin-debug-boldsign-doc.ts +93 -93
  28. package/src/functions/admin-door-write.ts +272 -272
  29. package/src/functions/admin-issue-day-pass.ts +231 -231
  30. package/src/functions/admin-mark-booking-paid.ts +203 -203
  31. package/src/functions/admin-mint-billing-portal-link.ts +168 -168
  32. package/src/functions/admin-mint-portal-link.ts +62 -62
  33. package/src/functions/admin-photo-upload.ts +128 -128
  34. package/src/functions/admin-remove-team-member.ts +150 -150
  35. package/src/functions/admin-request-trial.ts +366 -366
  36. package/src/functions/admin-resend-commons-deposit-link.ts +323 -323
  37. package/src/functions/admin-reset-mfa.ts +138 -138
  38. package/src/functions/admin-retry-invoice-payment.ts +333 -333
  39. package/src/functions/admin-reverse-coupon-redemption.ts +98 -98
  40. package/src/functions/admin-revoke-agreement.ts +467 -467
  41. package/src/functions/admin-send-commons-agreement.ts +214 -214
  42. package/src/functions/admin-set-credit-amount.ts +123 -123
  43. package/src/functions/admin-skip-commons-contract.ts +165 -165
  44. package/src/functions/admin-undo-membership-cancel.ts +144 -144
  45. package/src/functions/admin-wall-photo-write.ts +141 -141
  46. package/src/functions/auth-email-hook.ts +259 -259
  47. package/src/functions/boldsign-sender-identity.ts +114 -114
  48. package/src/functions/booking-availability.ts +419 -419
  49. package/src/functions/booking-member-preview.ts +136 -136
  50. package/src/functions/booking-status.ts +149 -149
  51. package/src/functions/create-booking.ts +648 -648
  52. package/src/functions/create-day-pass-checkout.ts +218 -218
  53. package/src/functions/create-portal-session.ts +142 -142
  54. package/src/functions/create-subscription-checkout.test.ts +222 -222
  55. package/src/functions/create-subscription-checkout.ts +440 -440
  56. package/src/functions/day-pass-status.ts +89 -89
  57. package/src/functions/diag-webpush-test.ts +45 -45
  58. package/src/functions/event-access-admin.ts +277 -277
  59. package/src/functions/event-access-store.ts +334 -309
  60. package/src/functions/ezeep-print-webhook.ts +213 -213
  61. package/src/functions/ezeep-usage-reconcile.ts +299 -299
  62. package/src/functions/fetch-avatar-original.ts +457 -457
  63. package/src/functions/get-provider-sign-link.ts +155 -155
  64. package/src/functions/get-signing-link.ts +211 -211
  65. package/src/functions/guest-print-setup-confirm.ts +128 -128
  66. package/src/functions/guest-print-submit.ts +149 -149
  67. package/src/functions/luma-webhook.ts +165 -165
  68. package/src/functions/mcp.ts +67 -67
  69. package/src/functions/member-door-action.test.ts +1248 -1248
  70. package/src/functions/member-door-action.ts +863 -863
  71. package/src/functions/member-unifi-mapping.test.ts +516 -516
  72. package/src/functions/member-unifi-mapping.ts +206 -206
  73. package/src/functions/membership-door-sync.ts +956 -956
  74. package/src/functions/migration-collision-alert.copy.ts +232 -232
  75. package/src/functions/migration-collision-alert.test.ts +349 -349
  76. package/src/functions/migration-collision-alert.ts +232 -232
  77. package/src/functions/office-assignment-end.ts +146 -146
  78. package/src/functions/portal-booking-access.ts +217 -95
  79. package/src/functions/portal-list-invoices.ts +99 -99
  80. package/src/functions/portal-request-login.ts +219 -192
  81. package/src/functions/portal-resend-invite.ts +248 -248
  82. package/src/functions/portal-submit-event.ts +168 -168
  83. package/src/functions/print-billing-recover.ts +97 -97
  84. package/src/functions/print-billing-run.ts +183 -183
  85. package/src/functions/public-facts.ts +168 -168
  86. package/src/functions/push-send.ts +348 -348
  87. package/src/functions/push-test.ts +234 -234
  88. package/src/functions/request-portal-link.ts +211 -211
  89. package/src/functions/review-ratings.ts +113 -113
  90. package/src/functions/space-availability-range.acceptance.test.ts +161 -161
  91. package/src/functions/space-availability-range.ts +142 -142
  92. package/src/functions/stripe-db-reconcile.ts +319 -319
  93. package/src/functions/stripe-webhook.moved-regression.test.ts +78 -78
  94. package/src/functions/stripe-webhook.ts +2595 -2595
  95. package/src/functions/sync-gcal-blackouts.ts +298 -298
  96. package/src/functions/timetable-event-register.ts +162 -162
  97. package/src/functions/timetable-event-status.ts +90 -90
  98. package/src/functions/timetable-spaces.ts +219 -219
  99. package/src/functions/touch-pass-reconciler.ts +689 -689
  100. package/src/functions/touch-pass-signup.ts +478 -478
  101. package/src/functions/upload-booking-display-image.ts +179 -179
  102. package/src/functions/virtual-onboarding-purge.ts +134 -134
  103. package/src/functions/virtual-onboarding-submit.ts +315 -315
  104. package/src/functions/voice-booking.ts +453 -453
  105. package/src/functions/voice-day-pass.ts +346 -346
  106. package/src/functions/voice-inquiry.test.ts +301 -301
  107. package/src/functions/voice-inquiry.ts +198 -198
  108. package/src/functions/voice-membership-signup.ts +355 -355
  109. package/src/functions/voice-tour-availability.ts +204 -204
  110. package/src/functions/voice-tour-booking.ts +429 -429
  111. package/src/ghlEventInquiry.ts +143 -143
  112. package/src/mailArrivalEmail.ts +121 -121
  113. package/src/manageToken.ts +363 -363
  114. package/src/membershipBusClient.ts +537 -537
  115. package/src/membershipWelcome.ts +683 -683
  116. package/src/migrationCheckout.ts +366 -366
  117. package/src/paymentLinkEmail.ts +123 -123
  118. package/src/signedUnpaidReminderEmail.test.ts +246 -246
  119. package/src/signedUnpaidReminderEmail.ts +132 -132
  120. package/src/stripeCoworkingPortal.ts +91 -91
  121. package/src/supabaseTestFakes.ts +131 -131
  122. package/src/supportTicketEmail.ts +119 -119
  123. package/src/unifiAccessPolicies.ts +228 -228
  124. package/src/unifiGroups.ts +129 -129
@@ -1,246 +1,246 @@
1
- // Unit tests for signedUnpaidReminderEmail.ts: sendSignedUnpaidReminderEmail's
2
- // GHL upsert+send outcomes, escaping, and content shape.
3
- //
4
- // Run with:
5
- // deno test --node-modules-dir=auto --allow-env --allow-net --allow-read \
6
- // src/signedUnpaidReminderEmail.test.ts
7
- //
8
- // NO REAL NETWORK, NO REAL DB: fetch is a global stub (same style as
9
- // dunningEmails.test.ts) and the Supabase client is a minimal in-memory stub
10
- // that only supports what sendSignedUnpaidReminderEmail actually calls
11
- // (ops_events inserts via logOps).
12
- import { assert, assertEquals } from 'https://deno.land/std@0.224.0/assert/mod.ts';
13
- import { sendSignedUnpaidReminderEmail } from './signedUnpaidReminderEmail.ts';
14
-
15
- const EMAIL = 'tenant@example.com';
16
- const PORTAL_URL = 'https://granitecity.io/agreement-portal?token=abc.def';
17
- const OPERATOR = { name: 'Granite City Workspaces', shortName: 'Granite City', domain: 'granitecity.io', supportEmail: 'info@granitecity.io' };
18
-
19
- function withEnv(overrides: Record<string, string | undefined>, fn: () => Promise<void>): Promise<void> {
20
- const prior: Record<string, string | undefined> = {};
21
- for (const k of Object.keys(overrides)) prior[k] = Deno.env.get(k);
22
- for (const [k, v] of Object.entries(overrides)) {
23
- if (v === undefined) Deno.env.delete(k); else Deno.env.set(k, v);
24
- }
25
- return fn().finally(() => {
26
- for (const [k, v] of Object.entries(prior)) {
27
- if (v === undefined) Deno.env.delete(k); else Deno.env.set(k, v);
28
- }
29
- });
30
- }
31
-
32
- // deno-lint-ignore no-explicit-any
33
- function stubClient(): { client: any; opsEvents: Array<Record<string, unknown>> } {
34
- const opsEvents: Array<Record<string, unknown>> = [];
35
- const builder = (table: string) => ({
36
- insert: (p: Record<string, unknown>) => {
37
- if (table === 'ops_events') opsEvents.push(p);
38
- return Promise.resolve({ data: null, error: null });
39
- },
40
- });
41
- return { client: { from: (table: string) => builder(table) }, opsEvents };
42
- }
43
-
44
- Deno.test('sendSignedUnpaidReminderEmail: GHL not configured -> skipped, no network call', async () => {
45
- await withEnv({ GHL_API_KEY: undefined, GHL_LOCATION_ID: undefined }, async () => {
46
- const stub = stubClient();
47
- let fetchCalled = false;
48
- const original = globalThis.fetch;
49
- // deno-lint-ignore no-explicit-any
50
- (globalThis as any).fetch = async () => { fetchCalled = true; return new Response('{}', { status: 200 }); };
51
- let result: string;
52
- try {
53
- result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
54
- } finally {
55
- globalThis.fetch = original;
56
- }
57
- assertEquals(result, 'skipped');
58
- assert(!fetchCalled, 'must not call GHL when unconfigured');
59
- assertEquals(stub.opsEvents.length, 1);
60
- assertEquals(stub.opsEvents[0].kind, 'agreement_signed_unpaid_reminder_skipped');
61
- });
62
- });
63
-
64
- Deno.test('sendSignedUnpaidReminderEmail: happy path -> sent, correct tags/contactId/portal link', async () => {
65
- await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
66
- const stub = stubClient();
67
- const calls: Array<{ url: string; body: Record<string, unknown> }> = [];
68
- const original = globalThis.fetch;
69
- // deno-lint-ignore no-explicit-any
70
- (globalThis as any).fetch = async (input: string | URL | Request, init?: RequestInit) => {
71
- const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
72
- const body = init?.body ? JSON.parse(init.body as string) : {};
73
- calls.push({ url, body });
74
- if (url.endsWith('/contacts/upsert')) {
75
- return new Response(JSON.stringify({ contact: { id: 'contact-1' } }), { status: 200 });
76
- }
77
- return new Response(JSON.stringify({ ok: true }), { status: 200 });
78
- };
79
- let result: string;
80
- try {
81
- result = await sendSignedUnpaidReminderEmail(stub.client, {
82
- email: EMAIL, name: 'Jamie Rivera', portalUrl: PORTAL_URL, officeNumber: '204',
83
- }, OPERATOR);
84
- } finally {
85
- globalThis.fetch = original;
86
- }
87
- assertEquals(result, 'sent');
88
- assertEquals(calls.length, 2);
89
- assertEquals(calls[0].url, 'https://services.leadconnectorhq.com/contacts/upsert');
90
- assertEquals(calls[0].body.firstName, 'Jamie');
91
- assertEquals(calls[0].body.lastName, 'Rivera');
92
- assert((calls[0].body.tags as string[]).includes('Signed Unpaid Reminder'));
93
- assertEquals(calls[1].url, 'https://services.leadconnectorhq.com/conversations/messages');
94
- assertEquals(calls[1].body.contactId, 'contact-1');
95
- assert((calls[1].body.html as string).includes(PORTAL_URL));
96
- assert((calls[1].body.html as string).includes('office 204'));
97
- assert(!(calls[1].body.html as string).includes('${'), 'no unresolved template literal should reach the sent HTML');
98
- assertEquals(stub.opsEvents.length, 1);
99
- assertEquals(stub.opsEvents[0].kind, 'agreement_signed_unpaid_reminder_sent');
100
- });
101
- });
102
-
103
- Deno.test('sendSignedUnpaidReminderEmail: no name -> greets "there", CRM name omitted (never overwritten with a placeholder)', async () => {
104
- await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
105
- const stub = stubClient();
106
- const calls: Array<{ url: string; body: Record<string, unknown> }> = [];
107
- const original = globalThis.fetch;
108
- // deno-lint-ignore no-explicit-any
109
- (globalThis as any).fetch = async (input: string | URL | Request, init?: RequestInit) => {
110
- const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
111
- const body = init?.body ? JSON.parse(init.body as string) : {};
112
- calls.push({ url, body });
113
- if (url.endsWith('/contacts/upsert')) {
114
- return new Response(JSON.stringify({ contact: { id: 'contact-1' } }), { status: 200 });
115
- }
116
- return new Response(JSON.stringify({ ok: true }), { status: 200 });
117
- };
118
- try {
119
- await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, name: null, portalUrl: PORTAL_URL }, OPERATOR);
120
- } finally {
121
- globalThis.fetch = original;
122
- }
123
- assertEquals(calls[0].body.firstName, undefined);
124
- assertEquals(calls[0].body.lastName, undefined);
125
- assert((calls[1].body.html as string).includes('Hi there,'));
126
- });
127
- });
128
-
129
- Deno.test('sendSignedUnpaidReminderEmail: name containing HTML is escaped in the sent body', async () => {
130
- await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
131
- const stub = stubClient();
132
- const calls: Array<{ url: string; body: Record<string, unknown> }> = [];
133
- const original = globalThis.fetch;
134
- // deno-lint-ignore no-explicit-any
135
- (globalThis as any).fetch = async (input: string | URL | Request, init?: RequestInit) => {
136
- const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
137
- const body = init?.body ? JSON.parse(init.body as string) : {};
138
- calls.push({ url, body });
139
- if (url.endsWith('/contacts/upsert')) {
140
- return new Response(JSON.stringify({ contact: { id: 'contact-1' } }), { status: 200 });
141
- }
142
- return new Response(JSON.stringify({ ok: true }), { status: 200 });
143
- };
144
- try {
145
- await sendSignedUnpaidReminderEmail(stub.client, {
146
- email: EMAIL, name: '<script>alert(1)</script>', portalUrl: PORTAL_URL,
147
- }, OPERATOR);
148
- } finally {
149
- globalThis.fetch = original;
150
- }
151
- assert(!(calls[1].body.html as string).includes('<script>alert(1)</script>'));
152
- assert((calls[1].body.html as string).includes('&lt;script&gt;'));
153
- });
154
- });
155
-
156
- Deno.test('sendSignedUnpaidReminderEmail: GHL upsert 500 -> failed_retryable', async () => {
157
- await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
158
- const stub = stubClient();
159
- const original = globalThis.fetch;
160
- // deno-lint-ignore no-explicit-any
161
- (globalThis as any).fetch = async () => new Response('server error', { status: 500 });
162
- let result: string;
163
- try {
164
- result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
165
- } finally {
166
- globalThis.fetch = original;
167
- }
168
- assertEquals(result, 'failed_retryable');
169
- assertEquals(stub.opsEvents[0].kind, 'agreement_signed_unpaid_reminder_failed');
170
- assertEquals(stub.opsEvents[0].severity, 'warning');
171
- });
172
- });
173
-
174
- Deno.test('sendSignedUnpaidReminderEmail: GHL upsert 400 -> failed_permanent', async () => {
175
- await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
176
- const stub = stubClient();
177
- const original = globalThis.fetch;
178
- // deno-lint-ignore no-explicit-any
179
- (globalThis as any).fetch = async () => new Response('bad request', { status: 400 });
180
- let result: string;
181
- try {
182
- result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
183
- } finally {
184
- globalThis.fetch = original;
185
- }
186
- assertEquals(result, 'failed_permanent');
187
- assertEquals(stub.opsEvents[0].kind, 'agreement_signed_unpaid_reminder_failed');
188
- assertEquals(stub.opsEvents[0].severity, 'error');
189
- });
190
- });
191
-
192
- Deno.test('sendSignedUnpaidReminderEmail: upsert ok but conversations/messages send 429 -> failed_retryable', async () => {
193
- await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
194
- const stub = stubClient();
195
- const original = globalThis.fetch;
196
- // deno-lint-ignore no-explicit-any
197
- (globalThis as any).fetch = async (input: string | URL | Request) => {
198
- const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
199
- if (url.endsWith('/contacts/upsert')) {
200
- return new Response(JSON.stringify({ contact: { id: 'contact-1' } }), { status: 200 });
201
- }
202
- return new Response('rate limited', { status: 429 });
203
- };
204
- let result: string;
205
- try {
206
- result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
207
- } finally {
208
- globalThis.fetch = original;
209
- }
210
- assertEquals(result, 'failed_retryable');
211
- });
212
- });
213
-
214
- Deno.test('sendSignedUnpaidReminderEmail: upsert response with no contact id -> failed_permanent', async () => {
215
- await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
216
- const stub = stubClient();
217
- const original = globalThis.fetch;
218
- // deno-lint-ignore no-explicit-any
219
- (globalThis as any).fetch = async () => new Response(JSON.stringify({}), { status: 200 });
220
- let result: string;
221
- try {
222
- result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
223
- } finally {
224
- globalThis.fetch = original;
225
- }
226
- assertEquals(result, 'failed_permanent');
227
- });
228
- });
229
-
230
- Deno.test('sendSignedUnpaidReminderEmail: AbortError (timeout) -> failed_retryable', async () => {
231
- await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
232
- const stub = stubClient();
233
- const original = globalThis.fetch;
234
- // deno-lint-ignore no-explicit-any
235
- (globalThis as any).fetch = async () => {
236
- const e = new Error('aborted'); e.name = 'AbortError'; throw e;
237
- };
238
- let result: string;
239
- try {
240
- result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
241
- } finally {
242
- globalThis.fetch = original;
243
- }
244
- assertEquals(result, 'failed_retryable');
245
- });
246
- });
1
+ // Unit tests for signedUnpaidReminderEmail.ts: sendSignedUnpaidReminderEmail's
2
+ // GHL upsert+send outcomes, escaping, and content shape.
3
+ //
4
+ // Run with:
5
+ // deno test --node-modules-dir=auto --allow-env --allow-net --allow-read \
6
+ // src/signedUnpaidReminderEmail.test.ts
7
+ //
8
+ // NO REAL NETWORK, NO REAL DB: fetch is a global stub (same style as
9
+ // dunningEmails.test.ts) and the Supabase client is a minimal in-memory stub
10
+ // that only supports what sendSignedUnpaidReminderEmail actually calls
11
+ // (ops_events inserts via logOps).
12
+ import { assert, assertEquals } from 'https://deno.land/std@0.224.0/assert/mod.ts';
13
+ import { sendSignedUnpaidReminderEmail } from './signedUnpaidReminderEmail.ts';
14
+
15
+ const EMAIL = 'tenant@example.com';
16
+ const PORTAL_URL = 'https://granitecity.io/agreement-portal?token=abc.def';
17
+ const OPERATOR = { name: 'Granite City Workspaces', shortName: 'Granite City', domain: 'granitecity.io', supportEmail: 'info@granitecity.io' };
18
+
19
+ function withEnv(overrides: Record<string, string | undefined>, fn: () => Promise<void>): Promise<void> {
20
+ const prior: Record<string, string | undefined> = {};
21
+ for (const k of Object.keys(overrides)) prior[k] = Deno.env.get(k);
22
+ for (const [k, v] of Object.entries(overrides)) {
23
+ if (v === undefined) Deno.env.delete(k); else Deno.env.set(k, v);
24
+ }
25
+ return fn().finally(() => {
26
+ for (const [k, v] of Object.entries(prior)) {
27
+ if (v === undefined) Deno.env.delete(k); else Deno.env.set(k, v);
28
+ }
29
+ });
30
+ }
31
+
32
+ // deno-lint-ignore no-explicit-any
33
+ function stubClient(): { client: any; opsEvents: Array<Record<string, unknown>> } {
34
+ const opsEvents: Array<Record<string, unknown>> = [];
35
+ const builder = (table: string) => ({
36
+ insert: (p: Record<string, unknown>) => {
37
+ if (table === 'ops_events') opsEvents.push(p);
38
+ return Promise.resolve({ data: null, error: null });
39
+ },
40
+ });
41
+ return { client: { from: (table: string) => builder(table) }, opsEvents };
42
+ }
43
+
44
+ Deno.test('sendSignedUnpaidReminderEmail: GHL not configured -> skipped, no network call', async () => {
45
+ await withEnv({ GHL_API_KEY: undefined, GHL_LOCATION_ID: undefined }, async () => {
46
+ const stub = stubClient();
47
+ let fetchCalled = false;
48
+ const original = globalThis.fetch;
49
+ // deno-lint-ignore no-explicit-any
50
+ (globalThis as any).fetch = async () => { fetchCalled = true; return new Response('{}', { status: 200 }); };
51
+ let result: string;
52
+ try {
53
+ result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
54
+ } finally {
55
+ globalThis.fetch = original;
56
+ }
57
+ assertEquals(result, 'skipped');
58
+ assert(!fetchCalled, 'must not call GHL when unconfigured');
59
+ assertEquals(stub.opsEvents.length, 1);
60
+ assertEquals(stub.opsEvents[0].kind, 'agreement_signed_unpaid_reminder_skipped');
61
+ });
62
+ });
63
+
64
+ Deno.test('sendSignedUnpaidReminderEmail: happy path -> sent, correct tags/contactId/portal link', async () => {
65
+ await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
66
+ const stub = stubClient();
67
+ const calls: Array<{ url: string; body: Record<string, unknown> }> = [];
68
+ const original = globalThis.fetch;
69
+ // deno-lint-ignore no-explicit-any
70
+ (globalThis as any).fetch = async (input: string | URL | Request, init?: RequestInit) => {
71
+ const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
72
+ const body = init?.body ? JSON.parse(init.body as string) : {};
73
+ calls.push({ url, body });
74
+ if (url.endsWith('/contacts/upsert')) {
75
+ return new Response(JSON.stringify({ contact: { id: 'contact-1' } }), { status: 200 });
76
+ }
77
+ return new Response(JSON.stringify({ ok: true }), { status: 200 });
78
+ };
79
+ let result: string;
80
+ try {
81
+ result = await sendSignedUnpaidReminderEmail(stub.client, {
82
+ email: EMAIL, name: 'Jamie Rivera', portalUrl: PORTAL_URL, officeNumber: '204',
83
+ }, OPERATOR);
84
+ } finally {
85
+ globalThis.fetch = original;
86
+ }
87
+ assertEquals(result, 'sent');
88
+ assertEquals(calls.length, 2);
89
+ assertEquals(calls[0].url, 'https://services.leadconnectorhq.com/contacts/upsert');
90
+ assertEquals(calls[0].body.firstName, 'Jamie');
91
+ assertEquals(calls[0].body.lastName, 'Rivera');
92
+ assert((calls[0].body.tags as string[]).includes('Signed Unpaid Reminder'));
93
+ assertEquals(calls[1].url, 'https://services.leadconnectorhq.com/conversations/messages');
94
+ assertEquals(calls[1].body.contactId, 'contact-1');
95
+ assert((calls[1].body.html as string).includes(PORTAL_URL));
96
+ assert((calls[1].body.html as string).includes('office 204'));
97
+ assert(!(calls[1].body.html as string).includes('${'), 'no unresolved template literal should reach the sent HTML');
98
+ assertEquals(stub.opsEvents.length, 1);
99
+ assertEquals(stub.opsEvents[0].kind, 'agreement_signed_unpaid_reminder_sent');
100
+ });
101
+ });
102
+
103
+ Deno.test('sendSignedUnpaidReminderEmail: no name -> greets "there", CRM name omitted (never overwritten with a placeholder)', async () => {
104
+ await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
105
+ const stub = stubClient();
106
+ const calls: Array<{ url: string; body: Record<string, unknown> }> = [];
107
+ const original = globalThis.fetch;
108
+ // deno-lint-ignore no-explicit-any
109
+ (globalThis as any).fetch = async (input: string | URL | Request, init?: RequestInit) => {
110
+ const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
111
+ const body = init?.body ? JSON.parse(init.body as string) : {};
112
+ calls.push({ url, body });
113
+ if (url.endsWith('/contacts/upsert')) {
114
+ return new Response(JSON.stringify({ contact: { id: 'contact-1' } }), { status: 200 });
115
+ }
116
+ return new Response(JSON.stringify({ ok: true }), { status: 200 });
117
+ };
118
+ try {
119
+ await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, name: null, portalUrl: PORTAL_URL }, OPERATOR);
120
+ } finally {
121
+ globalThis.fetch = original;
122
+ }
123
+ assertEquals(calls[0].body.firstName, undefined);
124
+ assertEquals(calls[0].body.lastName, undefined);
125
+ assert((calls[1].body.html as string).includes('Hi there,'));
126
+ });
127
+ });
128
+
129
+ Deno.test('sendSignedUnpaidReminderEmail: name containing HTML is escaped in the sent body', async () => {
130
+ await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
131
+ const stub = stubClient();
132
+ const calls: Array<{ url: string; body: Record<string, unknown> }> = [];
133
+ const original = globalThis.fetch;
134
+ // deno-lint-ignore no-explicit-any
135
+ (globalThis as any).fetch = async (input: string | URL | Request, init?: RequestInit) => {
136
+ const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
137
+ const body = init?.body ? JSON.parse(init.body as string) : {};
138
+ calls.push({ url, body });
139
+ if (url.endsWith('/contacts/upsert')) {
140
+ return new Response(JSON.stringify({ contact: { id: 'contact-1' } }), { status: 200 });
141
+ }
142
+ return new Response(JSON.stringify({ ok: true }), { status: 200 });
143
+ };
144
+ try {
145
+ await sendSignedUnpaidReminderEmail(stub.client, {
146
+ email: EMAIL, name: '<script>alert(1)</script>', portalUrl: PORTAL_URL,
147
+ }, OPERATOR);
148
+ } finally {
149
+ globalThis.fetch = original;
150
+ }
151
+ assert(!(calls[1].body.html as string).includes('<script>alert(1)</script>'));
152
+ assert((calls[1].body.html as string).includes('&lt;script&gt;'));
153
+ });
154
+ });
155
+
156
+ Deno.test('sendSignedUnpaidReminderEmail: GHL upsert 500 -> failed_retryable', async () => {
157
+ await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
158
+ const stub = stubClient();
159
+ const original = globalThis.fetch;
160
+ // deno-lint-ignore no-explicit-any
161
+ (globalThis as any).fetch = async () => new Response('server error', { status: 500 });
162
+ let result: string;
163
+ try {
164
+ result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
165
+ } finally {
166
+ globalThis.fetch = original;
167
+ }
168
+ assertEquals(result, 'failed_retryable');
169
+ assertEquals(stub.opsEvents[0].kind, 'agreement_signed_unpaid_reminder_failed');
170
+ assertEquals(stub.opsEvents[0].severity, 'warning');
171
+ });
172
+ });
173
+
174
+ Deno.test('sendSignedUnpaidReminderEmail: GHL upsert 400 -> failed_permanent', async () => {
175
+ await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
176
+ const stub = stubClient();
177
+ const original = globalThis.fetch;
178
+ // deno-lint-ignore no-explicit-any
179
+ (globalThis as any).fetch = async () => new Response('bad request', { status: 400 });
180
+ let result: string;
181
+ try {
182
+ result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
183
+ } finally {
184
+ globalThis.fetch = original;
185
+ }
186
+ assertEquals(result, 'failed_permanent');
187
+ assertEquals(stub.opsEvents[0].kind, 'agreement_signed_unpaid_reminder_failed');
188
+ assertEquals(stub.opsEvents[0].severity, 'error');
189
+ });
190
+ });
191
+
192
+ Deno.test('sendSignedUnpaidReminderEmail: upsert ok but conversations/messages send 429 -> failed_retryable', async () => {
193
+ await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
194
+ const stub = stubClient();
195
+ const original = globalThis.fetch;
196
+ // deno-lint-ignore no-explicit-any
197
+ (globalThis as any).fetch = async (input: string | URL | Request) => {
198
+ const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
199
+ if (url.endsWith('/contacts/upsert')) {
200
+ return new Response(JSON.stringify({ contact: { id: 'contact-1' } }), { status: 200 });
201
+ }
202
+ return new Response('rate limited', { status: 429 });
203
+ };
204
+ let result: string;
205
+ try {
206
+ result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
207
+ } finally {
208
+ globalThis.fetch = original;
209
+ }
210
+ assertEquals(result, 'failed_retryable');
211
+ });
212
+ });
213
+
214
+ Deno.test('sendSignedUnpaidReminderEmail: upsert response with no contact id -> failed_permanent', async () => {
215
+ await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
216
+ const stub = stubClient();
217
+ const original = globalThis.fetch;
218
+ // deno-lint-ignore no-explicit-any
219
+ (globalThis as any).fetch = async () => new Response(JSON.stringify({}), { status: 200 });
220
+ let result: string;
221
+ try {
222
+ result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
223
+ } finally {
224
+ globalThis.fetch = original;
225
+ }
226
+ assertEquals(result, 'failed_permanent');
227
+ });
228
+ });
229
+
230
+ Deno.test('sendSignedUnpaidReminderEmail: AbortError (timeout) -> failed_retryable', async () => {
231
+ await withEnv({ GHL_API_KEY: 'key123', GHL_LOCATION_ID: 'loc123' }, async () => {
232
+ const stub = stubClient();
233
+ const original = globalThis.fetch;
234
+ // deno-lint-ignore no-explicit-any
235
+ (globalThis as any).fetch = async () => {
236
+ const e = new Error('aborted'); e.name = 'AbortError'; throw e;
237
+ };
238
+ let result: string;
239
+ try {
240
+ result = await sendSignedUnpaidReminderEmail(stub.client, { email: EMAIL, portalUrl: PORTAL_URL }, OPERATOR);
241
+ } finally {
242
+ globalThis.fetch = original;
243
+ }
244
+ assertEquals(result, 'failed_retryable');
245
+ });
246
+ });