backend-manager 5.9.6 → 5.9.8

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 (215) hide show
  1. package/package.json +5 -3
  2. package/src/manager/events/cron/runner.js +0 -1
  3. package/src/manager/libraries/email/data/disposable-domains.json +21 -0
  4. package/src/test/fixtures/firebase-project/.temp/test-mode.json +1 -1
  5. package/src/test/fixtures/firebase-project/database-debug.log +8 -8
  6. package/src/test/fixtures/firebase-project/firestore-debug.log +55 -59
  7. package/src/test/fixtures/firebase-project/pubsub-debug.log +3 -3
  8. package/templates/_.env +42 -0
  9. package/templates/_.gitignore +60 -0
  10. package/templates/backend-manager-config.json +249 -0
  11. package/templates/database.rules.json +83 -0
  12. package/templates/firebase.json +66 -0
  13. package/templates/firestore.indexes.json +4 -0
  14. package/templates/firestore.rules +112 -0
  15. package/templates/public/404.html +26 -0
  16. package/templates/public/index.html +24 -0
  17. package/templates/remoteconfig.template.json +1 -0
  18. package/templates/storage-lifecycle-config-1-day.json +9 -0
  19. package/templates/storage-lifecycle-config-30-days.json +9 -0
  20. package/templates/storage.rules +8 -0
  21. package/test/_init/accounts-validation.js +58 -0
  22. package/test/_legacy/ai/index.js +231 -0
  23. package/test/_legacy/ai/test.jpg +0 -0
  24. package/test/_legacy/ai/test.pdf +0 -0
  25. package/test/_legacy/index.js +31 -0
  26. package/test/_legacy/payment-resolver/chargebee/orders/refunded.json +0 -0
  27. package/test/_legacy/payment-resolver/chargebee/orders/unpaid.json +98 -0
  28. package/test/_legacy/payment-resolver/chargebee/subscriptions/active.json +578 -0
  29. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-in-trial.json +38 -0
  30. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-skipped-to-active.json +135 -0
  31. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-active-to-cancelled.json +42 -0
  32. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-active.json +44 -0
  33. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-cancelled.json +39 -0
  34. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-refund.json +143 -0
  35. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-suspended.json +48 -0
  36. package/test/_legacy/payment-resolver/coinbase/orders/regular.json +204 -0
  37. package/test/_legacy/payment-resolver/coinbase/subscriptions/cancelled.json +204 -0
  38. package/test/_legacy/payment-resolver/coinbase/subscriptions/paid-2.json +125 -0
  39. package/test/_legacy/payment-resolver/index.js +1558 -0
  40. package/test/_legacy/payment-resolver/paypal/orders/refunded.json +0 -0
  41. package/test/_legacy/payment-resolver/paypal/orders/regular.json +120 -0
  42. package/test/_legacy/payment-resolver/paypal/subscriptions/active-refund-previous-stmnt.json +323 -0
  43. package/test/_legacy/payment-resolver/paypal/subscriptions/active.json +192 -0
  44. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-in-trial.json +125 -0
  45. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-payment-not-complete.json +76 -0
  46. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-payment-overdue-2.json +114 -0
  47. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-payment-overdue.json +114 -0
  48. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-active-to-cancelled.json +111 -0
  49. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-active.json +132 -0
  50. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-cancelled.json +107 -0
  51. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-expired.json +91 -0
  52. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-refund.json +147 -0
  53. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-suspended.json +120 -0
  54. package/test/_legacy/payment-resolver/stripe/orders/refunded.json +0 -0
  55. package/test/_legacy/payment-resolver/stripe/orders/regular.json +91 -0
  56. package/test/_legacy/payment-resolver/stripe/subscriptions/active.json +421 -0
  57. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-in-trial.json +349 -0
  58. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-active-failed-authorization.json +430 -0
  59. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-active.json +319 -0
  60. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-cancelled.json +319 -0
  61. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-refund.json +414 -0
  62. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-suspended.json +319 -0
  63. package/test/_legacy/payment-resolver/stripe/subscriptions/unsure.json +339 -0
  64. package/test/_legacy/test-new +1178 -0
  65. package/test/_legacy/test.md +89 -0
  66. package/test/_legacy/usage.js +175 -0
  67. package/test/_legacy/user.js +31 -0
  68. package/test/ai/tools-live.js +170 -0
  69. package/test/boot/emulator-boots.js +37 -0
  70. package/test/content/blog-generate.js +160 -0
  71. package/test/email/campaign-send.js +45 -0
  72. package/test/email/consent-lifecycle.js +257 -0
  73. package/test/email/feedback-and-plain-send.js +52 -0
  74. package/test/email/fixtures/clean.json +30 -0
  75. package/test/email/fixtures/editorial.json +30 -0
  76. package/test/email/fixtures/field-report.json +53 -0
  77. package/test/email/marketing-lifecycle.js +132 -0
  78. package/test/email/newsletter-generate.js +819 -0
  79. package/test/email/newsletter-templates.js +491 -0
  80. package/test/email/templates.js +207 -0
  81. package/test/email/transactional-send.js +34 -0
  82. package/test/email/transactional.js +560 -0
  83. package/test/email/validation.js +710 -0
  84. package/test/events/auth-delete-race.js +201 -0
  85. package/test/events/payments/journey-payments-cancel-endpoint.js +100 -0
  86. package/test/events/payments/journey-payments-cancel.js +182 -0
  87. package/test/events/payments/journey-payments-discount.js +89 -0
  88. package/test/events/payments/journey-payments-failure.js +146 -0
  89. package/test/events/payments/journey-payments-legacy-product.js +149 -0
  90. package/test/events/payments/journey-payments-one-time-failure.js +111 -0
  91. package/test/events/payments/journey-payments-one-time.js +136 -0
  92. package/test/events/payments/journey-payments-plan-change.js +144 -0
  93. package/test/events/payments/journey-payments-refund-webhook.js +180 -0
  94. package/test/events/payments/journey-payments-suspend.js +181 -0
  95. package/test/events/payments/journey-payments-trial-cancel.js +130 -0
  96. package/test/events/payments/journey-payments-trial.js +171 -0
  97. package/test/events/payments/journey-payments-uid-resolution.js +132 -0
  98. package/test/events/payments/journey-payments-upgrade.js +135 -0
  99. package/test/fixtures/chargebee/invoice-one-time.json +27 -0
  100. package/test/fixtures/chargebee/subscription-active.json +44 -0
  101. package/test/fixtures/chargebee/subscription-cancelled.json +42 -0
  102. package/test/fixtures/chargebee/subscription-in-trial.json +41 -0
  103. package/test/fixtures/chargebee/subscription-legacy-plan.json +41 -0
  104. package/test/fixtures/chargebee/subscription-non-renewing.json +41 -0
  105. package/test/fixtures/chargebee/subscription-paused.json +42 -0
  106. package/test/fixtures/chargebee/webhook-payment-failed.json +51 -0
  107. package/test/fixtures/chargebee/webhook-subscription-created.json +47 -0
  108. package/test/fixtures/paypal/order-approved.json +62 -0
  109. package/test/fixtures/paypal/order-completed.json +110 -0
  110. package/test/fixtures/paypal/subscription-active.json +76 -0
  111. package/test/fixtures/paypal/subscription-cancelled.json +50 -0
  112. package/test/fixtures/paypal/subscription-suspended.json +65 -0
  113. package/test/fixtures/stripe/checkout-session-completed.json +130 -0
  114. package/test/fixtures/stripe/invoice-payment-failed.json +148 -0
  115. package/test/fixtures/stripe/invoice-subscription-payment-failed.json +28 -0
  116. package/test/fixtures/stripe/subscription-active.json +161 -0
  117. package/test/fixtures/stripe/subscription-canceled.json +161 -0
  118. package/test/fixtures/stripe/subscription-trialing.json +161 -0
  119. package/test/functions/admin/database-read.js +134 -0
  120. package/test/functions/admin/database-write.js +159 -0
  121. package/test/functions/admin/edit-post.js +366 -0
  122. package/test/functions/admin/firestore-query.js +207 -0
  123. package/test/functions/admin/firestore-read.js +129 -0
  124. package/test/functions/admin/firestore-write.js +105 -0
  125. package/test/functions/admin/get-stats.js +115 -0
  126. package/test/functions/admin/send-email.js +119 -0
  127. package/test/functions/admin/send-notification.js +208 -0
  128. package/test/functions/admin/write-repo-content.js +223 -0
  129. package/test/functions/general/add-marketing-contact.js +335 -0
  130. package/test/functions/general/fetch-post.js +54 -0
  131. package/test/functions/general/generate-uuid.js +114 -0
  132. package/test/functions/test/authenticate.js +64 -0
  133. package/test/functions/user/create-custom-token.js +73 -0
  134. package/test/functions/user/delete.js +135 -0
  135. package/test/functions/user/get-active-sessions.js +111 -0
  136. package/test/functions/user/get-subscription-info.js +99 -0
  137. package/test/functions/user/regenerate-api-keys.js +156 -0
  138. package/test/functions/user/resolve.js +52 -0
  139. package/test/functions/user/sign-out-all-sessions.js +94 -0
  140. package/test/functions/user/sign-up.js +252 -0
  141. package/test/functions/user/submit-feedback.js +104 -0
  142. package/test/functions/user/validate-settings.js +82 -0
  143. package/test/helpers/ai-request-payload.js +300 -0
  144. package/test/helpers/ai-test-provider.js +202 -0
  145. package/test/helpers/ai-tools-format.js +383 -0
  146. package/test/helpers/content/blog-auto-publisher.js +484 -0
  147. package/test/helpers/content/feed-parser.js +528 -0
  148. package/test/helpers/content/ghostii-blocks.js +134 -0
  149. package/test/helpers/content/ghostii-feed-integration.js +404 -0
  150. package/test/helpers/content/ghostii-write-article.js +243 -0
  151. package/test/helpers/environment.js +230 -0
  152. package/test/helpers/infer-contact.js +113 -0
  153. package/test/helpers/merge-line-files.js +271 -0
  154. package/test/helpers/payment/chargebee/parse-webhook.js +413 -0
  155. package/test/helpers/payment/chargebee/to-unified-one-time.js +147 -0
  156. package/test/helpers/payment/chargebee/to-unified-subscription.js +648 -0
  157. package/test/helpers/payment/paypal/parse-webhook.js +539 -0
  158. package/test/helpers/payment/paypal/to-unified-one-time.js +382 -0
  159. package/test/helpers/payment/paypal/to-unified-subscription.js +820 -0
  160. package/test/helpers/payment/stripe/parse-webhook.js +447 -0
  161. package/test/helpers/payment/stripe/to-unified-one-time.js +306 -0
  162. package/test/helpers/payment/stripe/to-unified-subscription.js +708 -0
  163. package/test/helpers/sanitize.js +222 -0
  164. package/test/helpers/slugify.js +394 -0
  165. package/test/helpers/storage.js +194 -0
  166. package/test/helpers/user.js +755 -0
  167. package/test/helpers/webhook-forward.js +418 -0
  168. package/test/mcp/discovery.js +53 -0
  169. package/test/mcp/oauth.js +161 -0
  170. package/test/mcp/protocol.js +268 -0
  171. package/test/mcp/roles.js +188 -0
  172. package/test/mcp/utils.js +245 -0
  173. package/test/routes/admin/create-post.js +364 -0
  174. package/test/routes/admin/database.js +131 -0
  175. package/test/routes/admin/deduplicate-image-alts.js +190 -0
  176. package/test/routes/admin/email.js +114 -0
  177. package/test/routes/admin/firestore-query.js +204 -0
  178. package/test/routes/admin/firestore.js +127 -0
  179. package/test/routes/admin/infer-contact.js +218 -0
  180. package/test/routes/admin/notification.js +198 -0
  181. package/test/routes/admin/post-resize-image.js +184 -0
  182. package/test/routes/admin/post.js +368 -0
  183. package/test/routes/admin/repo-content.js +223 -0
  184. package/test/routes/admin/stats.js +112 -0
  185. package/test/routes/content/post.js +54 -0
  186. package/test/routes/general/uuid.js +115 -0
  187. package/test/routes/marketing/campaign.js +125 -0
  188. package/test/routes/marketing/contact.js +370 -0
  189. package/test/routes/marketing/email-preferences.js +292 -0
  190. package/test/routes/marketing/push-send.js +32 -0
  191. package/test/routes/marketing/webhook-forward.js +61 -0
  192. package/test/routes/marketing/webhook.js +639 -0
  193. package/test/routes/payments/cancel.js +163 -0
  194. package/test/routes/payments/discount.js +80 -0
  195. package/test/routes/payments/dispute-alert.js +320 -0
  196. package/test/routes/payments/intent.js +336 -0
  197. package/test/routes/payments/portal.js +92 -0
  198. package/test/routes/payments/refund.js +179 -0
  199. package/test/routes/payments/trial-eligibility.js +71 -0
  200. package/test/routes/payments/webhook.js +107 -0
  201. package/test/routes/test/authenticate.js +59 -0
  202. package/test/routes/test/schema.js +554 -0
  203. package/test/routes/test/usage.js +342 -0
  204. package/test/routes/user/api-keys.js +156 -0
  205. package/test/routes/user/delete.js +136 -0
  206. package/test/routes/user/feedback.js +104 -0
  207. package/test/routes/user/sessions.js +199 -0
  208. package/test/routes/user/settings-validate.js +82 -0
  209. package/test/routes/user/signup.js +542 -0
  210. package/test/routes/user/subscription.js +99 -0
  211. package/test/routes/user/token.js +73 -0
  212. package/test/routes/user/user.js +157 -0
  213. package/test/rules/notifications.js +410 -0
  214. package/test/rules/payments-carts.js +371 -0
  215. package/test/rules/user.js +396 -0
@@ -0,0 +1,413 @@
1
+ /**
2
+ * Test: Chargebee parseWebhook()
3
+ * Unit tests for the Chargebee webhook processor's event categorization and routing
4
+ *
5
+ * Verifies that parseWebhook() correctly determines category, resourceType, resourceId,
6
+ * and uid for each supported event type.
7
+ */
8
+ const chargebeeProcessor = require('../../../../src/manager/routes/payments/webhook/processors/chargebee.js');
9
+
10
+ // Chargebee webhook fixtures
11
+ const FIXTURE_SUB_CREATED = require('../../../fixtures/chargebee/webhook-subscription-created.json');
12
+ const FIXTURE_PAYMENT_FAILED = require('../../../fixtures/chargebee/webhook-payment-failed.json');
13
+
14
+ function parseWebhook(event) {
15
+ return chargebeeProcessor.parseWebhook({ body: event });
16
+ }
17
+
18
+ module.exports = {
19
+ description: 'Chargebee parseWebhook() event categorization',
20
+ type: 'group',
21
+
22
+ tests: [
23
+ // ─── isSupported() ───
24
+
25
+ {
26
+ name: 'supports-subscription-created',
27
+ async run({ assert }) {
28
+ assert.ok(chargebeeProcessor.isSupported('subscription_created'), 'Should support subscription_created');
29
+ },
30
+ },
31
+
32
+ {
33
+ name: 'supports-subscription-cancelled',
34
+ async run({ assert }) {
35
+ assert.ok(chargebeeProcessor.isSupported('subscription_cancelled'), 'Should support subscription_cancelled');
36
+ },
37
+ },
38
+
39
+ {
40
+ name: 'supports-subscription-activated',
41
+ async run({ assert }) {
42
+ assert.ok(chargebeeProcessor.isSupported('subscription_activated'), 'Should support subscription_activated');
43
+ },
44
+ },
45
+
46
+ {
47
+ name: 'supports-subscription-changed',
48
+ async run({ assert }) {
49
+ assert.ok(chargebeeProcessor.isSupported('subscription_changed'), 'Should support subscription_changed');
50
+ },
51
+ },
52
+
53
+ {
54
+ name: 'supports-subscription-renewed',
55
+ async run({ assert }) {
56
+ assert.ok(chargebeeProcessor.isSupported('subscription_renewed'), 'Should support subscription_renewed');
57
+ },
58
+ },
59
+
60
+ {
61
+ name: 'supports-subscription-reactivated',
62
+ async run({ assert }) {
63
+ assert.ok(chargebeeProcessor.isSupported('subscription_reactivated'), 'Should support subscription_reactivated');
64
+ },
65
+ },
66
+
67
+ {
68
+ name: 'supports-cancellation-scheduled',
69
+ async run({ assert }) {
70
+ assert.ok(chargebeeProcessor.isSupported('subscription_cancellation_scheduled'), 'Should support subscription_cancellation_scheduled');
71
+ },
72
+ },
73
+
74
+ {
75
+ name: 'supports-cancellation-removed',
76
+ async run({ assert }) {
77
+ assert.ok(chargebeeProcessor.isSupported('subscription_scheduled_cancellation_removed'), 'Should support scheduled_cancellation_removed');
78
+ },
79
+ },
80
+
81
+ {
82
+ name: 'supports-payment-failed',
83
+ async run({ assert }) {
84
+ assert.ok(chargebeeProcessor.isSupported('payment_failed'), 'Should support payment_failed');
85
+ },
86
+ },
87
+
88
+ {
89
+ name: 'supports-payment-refunded',
90
+ async run({ assert }) {
91
+ assert.ok(chargebeeProcessor.isSupported('payment_refunded'), 'Should support payment_refunded');
92
+ },
93
+ },
94
+
95
+ {
96
+ name: 'supports-invoice-generated',
97
+ async run({ assert }) {
98
+ assert.ok(chargebeeProcessor.isSupported('invoice_generated'), 'Should support invoice_generated');
99
+ },
100
+ },
101
+
102
+ {
103
+ name: 'rejects-unsupported-event',
104
+ async run({ assert }) {
105
+ assert.equal(chargebeeProcessor.isSupported('customer_created'), false, 'Should not support customer_created');
106
+ assert.equal(chargebeeProcessor.isSupported('coupon_created'), false, 'Should not support coupon_created');
107
+ assert.equal(chargebeeProcessor.isSupported('subscription_deleted'), false, 'Should not support subscription_deleted');
108
+ },
109
+ },
110
+
111
+ // ─── Validation ───
112
+
113
+ {
114
+ name: 'throws-on-empty-body',
115
+ async run({ assert }) {
116
+ try {
117
+ parseWebhook(null);
118
+ assert.fail('Should have thrown');
119
+ } catch (e) {
120
+ assert.match(e.message, /Invalid/, 'Should mention invalid payload');
121
+ }
122
+ },
123
+ },
124
+
125
+ {
126
+ name: 'throws-on-missing-id',
127
+ async run({ assert }) {
128
+ try {
129
+ parseWebhook({ event_type: 'subscription_created', content: {} });
130
+ assert.fail('Should have thrown');
131
+ } catch (e) {
132
+ assert.match(e.message, /Invalid/, 'Should mention invalid payload');
133
+ }
134
+ },
135
+ },
136
+
137
+ {
138
+ name: 'throws-on-missing-event-type',
139
+ async run({ assert }) {
140
+ try {
141
+ parseWebhook({ id: 'ev_123', content: {} });
142
+ assert.fail('Should have thrown');
143
+ } catch (e) {
144
+ assert.match(e.message, /Invalid/, 'Should mention invalid payload');
145
+ }
146
+ },
147
+ },
148
+
149
+ // ─── Subscription events ───
150
+
151
+ {
152
+ name: 'subscription-created-category',
153
+ async run({ assert }) {
154
+ const result = parseWebhook({
155
+ id: 'ev_sub_created',
156
+ event_type: 'subscription_created',
157
+ content: {
158
+ subscription: {
159
+ id: 'sub_123',
160
+ meta_data: '{"uid":"user-abc"}',
161
+ },
162
+ customer: { id: 'cust_123' },
163
+ },
164
+ });
165
+
166
+ assert.equal(result.category, 'subscription', 'Category should be subscription');
167
+ assert.equal(result.resourceType, 'subscription', 'Resource type should be subscription');
168
+ assert.equal(result.resourceId, 'sub_123', 'Resource ID should be subscription ID');
169
+ assert.equal(result.uid, 'user-abc', 'UID should come from meta_data');
170
+ assert.equal(result.eventId, 'ev_sub_created', 'Event ID should match');
171
+ assert.equal(result.eventType, 'subscription_created', 'Event type should match');
172
+ },
173
+ },
174
+
175
+ {
176
+ name: 'subscription-cancelled-category',
177
+ async run({ assert }) {
178
+ const result = parseWebhook({
179
+ id: 'ev_sub_cancelled',
180
+ event_type: 'subscription_cancelled',
181
+ content: {
182
+ subscription: {
183
+ id: 'sub_456',
184
+ meta_data: '{"uid":"user-def"}',
185
+ },
186
+ },
187
+ });
188
+
189
+ assert.equal(result.category, 'subscription', 'Category should be subscription');
190
+ assert.equal(result.resourceId, 'sub_456', 'Resource ID should match');
191
+ assert.equal(result.uid, 'user-def', 'UID should match');
192
+ },
193
+ },
194
+
195
+ {
196
+ name: 'subscription-cancellation-scheduled-category',
197
+ async run({ assert }) {
198
+ const result = parseWebhook({
199
+ id: 'ev_cancel_scheduled',
200
+ event_type: 'subscription_cancellation_scheduled',
201
+ content: {
202
+ subscription: {
203
+ id: 'sub_789',
204
+ meta_data: '{"uid":"user-ghi"}',
205
+ status: 'non_renewing',
206
+ },
207
+ },
208
+ });
209
+
210
+ assert.equal(result.category, 'subscription', 'Category should be subscription');
211
+ assert.equal(result.uid, 'user-ghi', 'UID should match');
212
+ },
213
+ },
214
+
215
+ {
216
+ name: 'subscription-event-uid-from-legacy-cf',
217
+ async run({ assert }) {
218
+ const result = parseWebhook({
219
+ id: 'ev_legacy_uid',
220
+ event_type: 'subscription_renewed',
221
+ content: {
222
+ subscription: {
223
+ id: 'sub_legacy',
224
+ cf_uid: 'legacy-user-123',
225
+ },
226
+ },
227
+ });
228
+
229
+ assert.equal(result.uid, 'legacy-user-123', 'UID should fall back to cf_uid');
230
+ },
231
+ },
232
+
233
+ {
234
+ name: 'subscription-event-null-uid-when-missing',
235
+ async run({ assert }) {
236
+ const result = parseWebhook({
237
+ id: 'ev_no_uid',
238
+ event_type: 'subscription_created',
239
+ content: {
240
+ subscription: { id: 'sub_no_uid' },
241
+ },
242
+ });
243
+
244
+ assert.equal(result.uid, null, 'UID should be null when not available');
245
+ assert.equal(result.category, 'subscription', 'Category should still be subscription');
246
+ },
247
+ },
248
+
249
+ // ─── payment_failed ───
250
+
251
+ {
252
+ name: 'payment-failed-subscription-category',
253
+ async run({ assert }) {
254
+ const result = parseWebhook({
255
+ id: 'ev_pay_fail',
256
+ event_type: 'payment_failed',
257
+ content: {
258
+ subscription: {
259
+ id: 'sub_fail',
260
+ meta_data: '{"uid":"user-fail"}',
261
+ },
262
+ invoice: { id: 'inv_fail', subscription_id: 'sub_fail' },
263
+ },
264
+ });
265
+
266
+ assert.equal(result.category, 'subscription', 'With subscription → subscription category');
267
+ assert.equal(result.resourceType, 'subscription', 'Should fetch subscription');
268
+ assert.equal(result.resourceId, 'sub_fail', 'Resource ID should be subscription ID');
269
+ assert.equal(result.uid, 'user-fail', 'UID from subscription');
270
+ },
271
+ },
272
+
273
+ {
274
+ name: 'payment-failed-one-time-category',
275
+ async run({ assert }) {
276
+ const result = parseWebhook({
277
+ id: 'ev_pay_fail_onetime',
278
+ event_type: 'payment_failed',
279
+ content: {
280
+ invoice: { id: 'inv_onetime_fail' },
281
+ customer: { id: 'cust_onetime' },
282
+ },
283
+ });
284
+
285
+ assert.equal(result.category, 'one-time', 'No subscription → one-time');
286
+ assert.equal(result.resourceType, 'invoice', 'Should fetch invoice');
287
+ assert.equal(result.resourceId, 'inv_onetime_fail', 'Resource ID should be invoice ID');
288
+ },
289
+ },
290
+
291
+ // ─── payment_refunded ───
292
+
293
+ {
294
+ name: 'payment-refunded-subscription-category',
295
+ async run({ assert }) {
296
+ const result = parseWebhook({
297
+ id: 'ev_refund',
298
+ event_type: 'payment_refunded',
299
+ content: {
300
+ subscription: {
301
+ id: 'sub_refund',
302
+ meta_data: '{"uid":"user-refund"}',
303
+ },
304
+ },
305
+ });
306
+
307
+ assert.equal(result.category, 'subscription', 'With subscription → subscription');
308
+ assert.equal(result.resourceId, 'sub_refund', 'Resource ID should be subscription');
309
+ assert.equal(result.uid, 'user-refund', 'UID from subscription');
310
+ },
311
+ },
312
+
313
+ {
314
+ name: 'payment-refunded-no-subscription-skipped',
315
+ async run({ assert }) {
316
+ const result = parseWebhook({
317
+ id: 'ev_refund_no_sub',
318
+ event_type: 'payment_refunded',
319
+ content: {
320
+ transaction: { id: 'txn_refund' },
321
+ },
322
+ });
323
+
324
+ assert.equal(result.category, null, 'No subscription → null (skipped)');
325
+ },
326
+ },
327
+
328
+ // ─── invoice_generated ───
329
+
330
+ {
331
+ name: 'invoice-generated-non-recurring-one-time',
332
+ async run({ assert }) {
333
+ const result = parseWebhook({
334
+ id: 'ev_inv_gen',
335
+ event_type: 'invoice_generated',
336
+ content: {
337
+ invoice: { id: 'inv_onetime_001' },
338
+ customer: { id: 'cust_onetime' },
339
+ },
340
+ });
341
+
342
+ assert.equal(result.category, 'one-time', 'Non-recurring invoice → one-time');
343
+ assert.equal(result.resourceType, 'invoice', 'Should fetch invoice');
344
+ assert.equal(result.resourceId, 'inv_onetime_001', 'Resource ID should be invoice ID');
345
+ },
346
+ },
347
+
348
+ {
349
+ name: 'invoice-generated-recurring-skipped',
350
+ async run({ assert }) {
351
+ const result = parseWebhook({
352
+ id: 'ev_inv_recurring',
353
+ event_type: 'invoice_generated',
354
+ content: {
355
+ invoice: { id: 'inv_recurring', subscription_id: 'sub_existing' },
356
+ },
357
+ });
358
+
359
+ assert.equal(result.category, null, 'Recurring invoice → null (skipped)');
360
+ },
361
+ },
362
+
363
+ // ─── Fixture tests ───
364
+
365
+ {
366
+ name: 'fixture-subscription-created',
367
+ async run({ assert }) {
368
+ const result = parseWebhook(FIXTURE_SUB_CREATED);
369
+
370
+ assert.equal(result.eventId, FIXTURE_SUB_CREATED.id, 'Event ID matches fixture');
371
+ assert.equal(result.eventType, 'subscription_created', 'Event type matches');
372
+ assert.equal(result.category, 'subscription', 'Category is subscription');
373
+ assert.equal(result.resourceType, 'subscription', 'Resource type is subscription');
374
+ assert.equal(result.resourceId, 'cb_sub_active_001', 'Resource ID from content.subscription.id');
375
+ assert.equal(result.uid, 'test-uid-001', 'UID from meta_data');
376
+ },
377
+ },
378
+
379
+ {
380
+ name: 'fixture-payment-failed',
381
+ async run({ assert }) {
382
+ const result = parseWebhook(FIXTURE_PAYMENT_FAILED);
383
+
384
+ assert.equal(result.eventId, FIXTURE_PAYMENT_FAILED.id, 'Event ID matches fixture');
385
+ assert.equal(result.eventType, 'payment_failed', 'Event type matches');
386
+ assert.equal(result.category, 'subscription', 'Category is subscription (has subscription)');
387
+ assert.equal(result.resourceType, 'subscription', 'Resource type is subscription');
388
+ assert.equal(result.resourceId, 'cb_sub_active_001', 'Resource ID from content.subscription.id');
389
+ assert.equal(result.uid, 'test-uid-001', 'UID from subscription meta_data');
390
+ },
391
+ },
392
+
393
+ // ─── raw passthrough ───
394
+
395
+ {
396
+ name: 'raw-contains-full-event',
397
+ async run({ assert }) {
398
+ const event = {
399
+ id: 'ev_raw',
400
+ event_type: 'subscription_changed',
401
+ content: {
402
+ subscription: { id: 'sub_raw', meta_data: '{"uid":"user-raw"}' },
403
+ },
404
+ extra_field: 'preserved',
405
+ };
406
+
407
+ const result = parseWebhook(event);
408
+ assert.equal(result.raw, event, 'Raw should be the full event object');
409
+ assert.equal(result.raw.extra_field, 'preserved', 'Extra fields preserved in raw');
410
+ },
411
+ },
412
+ ],
413
+ };
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Test: Chargebee toUnifiedOneTime()
3
+ * Unit tests for the Chargebee library's invoice → unified one-time transformation
4
+ */
5
+ const Chargebee = require('../../../../src/manager/libraries/payment/processors/chargebee.js');
6
+
7
+ const FIXTURE_INVOICE = require('../../../fixtures/chargebee/invoice-one-time.json');
8
+
9
+ const MOCK_CONFIG = {
10
+ payment: {
11
+ products: [
12
+ { id: 'basic', name: 'Basic', type: 'subscription' },
13
+ {
14
+ id: 'credits-100', name: '100 Credits', type: 'one-time',
15
+ prices: { once: 9.99 },
16
+ },
17
+ ],
18
+ },
19
+ };
20
+
21
+ function toUnifiedOneTime(rawResource, options) {
22
+ return Chargebee.toUnifiedOneTime(rawResource, { config: MOCK_CONFIG, ...options });
23
+ }
24
+
25
+ module.exports = {
26
+ description: 'Chargebee toUnifiedOneTime() transformation',
27
+ type: 'group',
28
+
29
+ tests: [
30
+ {
31
+ name: 'status-paid-to-completed',
32
+ async run({ assert }) {
33
+ const result = toUnifiedOneTime({ status: 'paid' });
34
+ assert.equal(result.status, 'completed', 'paid → completed');
35
+ },
36
+ },
37
+
38
+ {
39
+ name: 'status-not-paid-to-failed',
40
+ async run({ assert }) {
41
+ const result = toUnifiedOneTime({ status: 'not_paid' });
42
+ assert.equal(result.status, 'failed', 'not_paid → failed');
43
+ },
44
+ },
45
+
46
+ {
47
+ name: 'status-payment-due-to-failed',
48
+ async run({ assert }) {
49
+ const result = toUnifiedOneTime({ status: 'payment_due' });
50
+ assert.equal(result.status, 'failed', 'payment_due → failed');
51
+ },
52
+ },
53
+
54
+ {
55
+ name: 'status-unknown-passthrough',
56
+ async run({ assert }) {
57
+ const result = toUnifiedOneTime({ status: 'pending' });
58
+ assert.equal(result.status, 'pending', 'Other statuses pass through');
59
+ },
60
+ },
61
+
62
+ {
63
+ name: 'processor-always-chargebee',
64
+ async run({ assert }) {
65
+ const result = toUnifiedOneTime({});
66
+ assert.equal(result.payment.processor, 'chargebee', 'Processor should be chargebee');
67
+ },
68
+ },
69
+
70
+ {
71
+ name: 'resource-id-from-invoice-id',
72
+ async run({ assert }) {
73
+ const result = toUnifiedOneTime({ id: 'inv_123' });
74
+ assert.equal(result.payment.resourceId, 'inv_123', 'resourceId from invoice id');
75
+ },
76
+ },
77
+
78
+ {
79
+ name: 'order-id-from-meta-data',
80
+ async run({ assert }) {
81
+ const result = toUnifiedOneTime({ meta_data: '{"orderId":"1234-5678-9012"}' });
82
+ assert.equal(result.payment.orderId, '1234-5678-9012', 'orderId from meta_data');
83
+ },
84
+ },
85
+
86
+ {
87
+ name: 'order-id-from-cf-legacy',
88
+ async run({ assert }) {
89
+ const result = toUnifiedOneTime({ cf_clientorderid: 'legacy-order' });
90
+ assert.equal(result.payment.orderId, 'legacy-order', 'orderId from cf_clientorderid');
91
+ },
92
+ },
93
+
94
+ {
95
+ name: 'product-resolves-from-meta-data',
96
+ async run({ assert }) {
97
+ const result = toUnifiedOneTime({
98
+ meta_data: '{"productId":"credits-100"}',
99
+ status: 'paid',
100
+ });
101
+ assert.equal(result.product.id, 'credits-100', 'Product resolved from meta_data');
102
+ assert.equal(result.product.name, '100 Credits', 'Product name resolved');
103
+ },
104
+ },
105
+
106
+ {
107
+ name: 'product-unknown-when-no-meta-data',
108
+ async run({ assert }) {
109
+ const result = toUnifiedOneTime({});
110
+ assert.equal(result.product.id, 'unknown', 'No productId → unknown');
111
+ },
112
+ },
113
+
114
+ {
115
+ name: 'event-metadata-passed-through',
116
+ async run({ assert }) {
117
+ const result = toUnifiedOneTime({}, { eventName: 'invoice_generated', eventId: 'ev_inv' });
118
+ assert.equal(result.payment.updatedBy.event.name, 'invoice_generated', 'Event name');
119
+ assert.equal(result.payment.updatedBy.event.id, 'ev_inv', 'Event ID');
120
+ },
121
+ },
122
+
123
+ {
124
+ name: 'fixture-invoice-one-time',
125
+ async run({ assert }) {
126
+ const result = toUnifiedOneTime(FIXTURE_INVOICE);
127
+ assert.equal(result.status, 'completed', 'Fixture paid → completed');
128
+ assert.equal(result.payment.processor, 'chargebee', 'Processor is chargebee');
129
+ assert.equal(result.payment.resourceId, FIXTURE_INVOICE.id, 'Resource ID matches fixture');
130
+ assert.equal(result.payment.orderId, '6789-0123-4567', 'Order ID from meta_data');
131
+ assert.equal(result.product.id, 'credits-100', 'Product from meta_data productId');
132
+ },
133
+ },
134
+
135
+ {
136
+ name: 'unified-shape-complete',
137
+ async run({ assert }) {
138
+ const result = toUnifiedOneTime(FIXTURE_INVOICE);
139
+ assert.ok(result.product, 'Should have product');
140
+ assert.ok(result.status, 'Should have status');
141
+ assert.ok(result.payment, 'Should have payment');
142
+ assert.ok(result.payment.updatedBy, 'Should have updatedBy');
143
+ assert.ok(result.payment.updatedBy.date, 'Should have updatedBy.date');
144
+ },
145
+ },
146
+ ],
147
+ };