digital-tools 2.1.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (293) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +2 -0
  3. package/dist/client.d.ts +109 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +69 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/define.d.ts +2 -2
  8. package/dist/define.d.ts.map +1 -1
  9. package/dist/define.js +22 -20
  10. package/dist/define.js.map +1 -1
  11. package/dist/function-ref.d.ts +229 -0
  12. package/dist/function-ref.d.ts.map +1 -0
  13. package/dist/function-ref.js +28 -0
  14. package/dist/function-ref.js.map +1 -0
  15. package/dist/function-sugar.d.ts +57 -0
  16. package/dist/function-sugar.d.ts.map +1 -0
  17. package/dist/function-sugar.js +79 -0
  18. package/dist/function-sugar.js.map +1 -0
  19. package/dist/index.d.ts +10 -3
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +24 -4
  22. package/dist/index.js.map +1 -1
  23. package/dist/providers/analytics/mixpanel.d.ts.map +1 -1
  24. package/dist/providers/analytics/mixpanel.js +21 -18
  25. package/dist/providers/analytics/mixpanel.js.map +1 -1
  26. package/dist/providers/calendar/cal-com.d.ts.map +1 -1
  27. package/dist/providers/calendar/cal-com.js +10 -10
  28. package/dist/providers/calendar/cal-com.js.map +1 -1
  29. package/dist/providers/calendar/google-calendar.d.ts.map +1 -1
  30. package/dist/providers/calendar/google-calendar.js +4 -4
  31. package/dist/providers/calendar/google-calendar.js.map +1 -1
  32. package/dist/providers/crm/hubspot.d.ts.map +1 -1
  33. package/dist/providers/crm/hubspot.js +107 -85
  34. package/dist/providers/crm/hubspot.js.map +1 -1
  35. package/dist/providers/development/github.d.ts.map +1 -1
  36. package/dist/providers/development/github.js +40 -43
  37. package/dist/providers/development/github.js.map +1 -1
  38. package/dist/providers/ecommerce/shopify.d.ts.map +1 -1
  39. package/dist/providers/ecommerce/shopify.js +79 -62
  40. package/dist/providers/ecommerce/shopify.js.map +1 -1
  41. package/dist/providers/email/resend.d.ts.map +1 -1
  42. package/dist/providers/email/resend.js +20 -16
  43. package/dist/providers/email/resend.js.map +1 -1
  44. package/dist/providers/email/sendgrid.d.ts.map +1 -1
  45. package/dist/providers/email/sendgrid.js +12 -9
  46. package/dist/providers/email/sendgrid.js.map +1 -1
  47. package/dist/providers/finance/stripe.d.ts.map +1 -1
  48. package/dist/providers/finance/stripe.js +44 -42
  49. package/dist/providers/finance/stripe.js.map +1 -1
  50. package/dist/providers/forms/typeform.d.ts.map +1 -1
  51. package/dist/providers/forms/typeform.js +68 -58
  52. package/dist/providers/forms/typeform.js.map +1 -1
  53. package/dist/providers/knowledge/notion.d.ts.map +1 -1
  54. package/dist/providers/knowledge/notion.js +75 -41
  55. package/dist/providers/knowledge/notion.js.map +1 -1
  56. package/dist/providers/marketing/mailchimp.d.ts.map +1 -1
  57. package/dist/providers/marketing/mailchimp.js +74 -61
  58. package/dist/providers/marketing/mailchimp.js.map +1 -1
  59. package/dist/providers/media/cloudinary.d.ts.map +1 -1
  60. package/dist/providers/media/cloudinary.js +30 -28
  61. package/dist/providers/media/cloudinary.js.map +1 -1
  62. package/dist/providers/messaging/slack.d.ts.map +1 -1
  63. package/dist/providers/messaging/slack.js +75 -58
  64. package/dist/providers/messaging/slack.js.map +1 -1
  65. package/dist/providers/messaging/twilio-sms.d.ts.map +1 -1
  66. package/dist/providers/messaging/twilio-sms.js +33 -15
  67. package/dist/providers/messaging/twilio-sms.js.map +1 -1
  68. package/dist/providers/project-management/linear.d.ts.map +1 -1
  69. package/dist/providers/project-management/linear.js +31 -27
  70. package/dist/providers/project-management/linear.js.map +1 -1
  71. package/dist/providers/spreadsheet/google-sheets.d.ts.map +1 -1
  72. package/dist/providers/spreadsheet/google-sheets.js +21 -18
  73. package/dist/providers/spreadsheet/google-sheets.js.map +1 -1
  74. package/dist/providers/spreadsheet/xlsx.d.ts.map +1 -1
  75. package/dist/providers/spreadsheet/xlsx.js +4 -4
  76. package/dist/providers/spreadsheet/xlsx.js.map +1 -1
  77. package/dist/providers/storage/index.js +1 -0
  78. package/dist/providers/storage/index.js.map +1 -1
  79. package/dist/providers/storage/s3.d.ts.map +1 -1
  80. package/dist/providers/storage/s3.js +36 -27
  81. package/dist/providers/storage/s3.js.map +1 -1
  82. package/dist/providers/support/zendesk.d.ts.map +1 -1
  83. package/dist/providers/support/zendesk.js +24 -25
  84. package/dist/providers/support/zendesk.js.map +1 -1
  85. package/dist/providers/tasks/todoist.d.ts.map +1 -1
  86. package/dist/providers/tasks/todoist.js +18 -18
  87. package/dist/providers/tasks/todoist.js.map +1 -1
  88. package/dist/providers/video-conferencing/google-meet.d.ts.map +1 -1
  89. package/dist/providers/video-conferencing/google-meet.js +11 -11
  90. package/dist/providers/video-conferencing/google-meet.js.map +1 -1
  91. package/dist/providers/video-conferencing/jitsi.js +14 -14
  92. package/dist/providers/video-conferencing/jitsi.js.map +1 -1
  93. package/dist/providers/video-conferencing/teams.d.ts.map +1 -1
  94. package/dist/providers/video-conferencing/teams.js +9 -7
  95. package/dist/providers/video-conferencing/teams.js.map +1 -1
  96. package/dist/providers/video-conferencing/zoom.d.ts.map +1 -1
  97. package/dist/providers/video-conferencing/zoom.js +26 -24
  98. package/dist/providers/video-conferencing/zoom.js.map +1 -1
  99. package/dist/tools/data.d.ts.map +1 -1
  100. package/dist/tools/data.js +5 -12
  101. package/dist/tools/data.js.map +1 -1
  102. package/dist/tools/index.d.ts +1 -0
  103. package/dist/tools/index.d.ts.map +1 -1
  104. package/dist/tools/index.js +1 -0
  105. package/dist/tools/index.js.map +1 -1
  106. package/dist/tools/system.d.ts +289 -0
  107. package/dist/tools/system.d.ts.map +1 -0
  108. package/dist/tools/system.js +752 -0
  109. package/dist/tools/system.js.map +1 -0
  110. package/dist/tools/web.d.ts.map +1 -1
  111. package/dist/tools/web.js +22 -10
  112. package/dist/tools/web.js.map +1 -1
  113. package/dist/track-record.d.ts +101 -0
  114. package/dist/track-record.d.ts.map +1 -0
  115. package/dist/track-record.js +17 -0
  116. package/dist/track-record.js.map +1 -0
  117. package/dist/types.d.ts +210 -9
  118. package/dist/types.d.ts.map +1 -1
  119. package/dist/verb-registration.d.ts +122 -0
  120. package/dist/verb-registration.d.ts.map +1 -0
  121. package/dist/verb-registration.js +176 -0
  122. package/dist/verb-registration.js.map +1 -0
  123. package/dist/worker.d.ts +93 -0
  124. package/dist/worker.d.ts.map +1 -0
  125. package/dist/worker.js +315 -0
  126. package/dist/worker.js.map +1 -0
  127. package/dist/wrap.d.ts +89 -0
  128. package/dist/wrap.d.ts.map +1 -0
  129. package/dist/wrap.js +225 -0
  130. package/dist/wrap.js.map +1 -0
  131. package/package.json +21 -4
  132. package/src/client.ts +136 -0
  133. package/src/define.ts +31 -37
  134. package/src/function-ref.ts +264 -0
  135. package/src/function-sugar.ts +134 -0
  136. package/src/index.ts +132 -10
  137. package/src/providers/analytics/mixpanel.ts +19 -18
  138. package/src/providers/calendar/cal-com.ts +29 -18
  139. package/src/providers/calendar/google-calendar.ts +20 -14
  140. package/src/providers/crm/hubspot.ts +225 -99
  141. package/src/providers/development/github.ts +206 -135
  142. package/src/providers/ecommerce/shopify.ts +250 -89
  143. package/src/providers/email/resend.ts +101 -28
  144. package/src/providers/email/sendgrid.ts +12 -9
  145. package/src/providers/finance/stripe.ts +128 -49
  146. package/src/providers/forms/typeform.ts +74 -58
  147. package/src/providers/knowledge/notion.ts +340 -88
  148. package/src/providers/marketing/mailchimp.ts +86 -70
  149. package/src/providers/media/cloudinary.ts +99 -41
  150. package/src/providers/messaging/slack.ts +283 -85
  151. package/src/providers/messaging/twilio-sms.ts +35 -15
  152. package/src/providers/project-management/linear.ts +143 -55
  153. package/src/providers/spreadsheet/google-sheets.ts +222 -56
  154. package/src/providers/spreadsheet/xlsx.ts +47 -16
  155. package/src/providers/storage/s3.ts +119 -47
  156. package/src/providers/support/zendesk.ts +196 -46
  157. package/src/providers/tasks/todoist.ts +20 -26
  158. package/src/providers/video-conferencing/google-meet.ts +17 -20
  159. package/src/providers/video-conferencing/jitsi.ts +14 -14
  160. package/src/providers/video-conferencing/teams.ts +14 -13
  161. package/src/providers/video-conferencing/zoom.ts +54 -49
  162. package/src/tools/data.ts +6 -16
  163. package/src/tools/index.ts +1 -0
  164. package/src/tools/system.ts +887 -0
  165. package/src/tools/web.ts +22 -10
  166. package/src/track-record.ts +106 -0
  167. package/src/types.ts +241 -13
  168. package/src/verb-registration.ts +197 -0
  169. package/src/worker.ts +370 -0
  170. package/src/wrap.ts +260 -0
  171. package/test/client.test.ts +146 -0
  172. package/test/communication-tools-extended.test.ts +734 -0
  173. package/test/data-tools-extended.test.ts +743 -0
  174. package/test/define-extended.test.ts +819 -0
  175. package/test/define.test.ts +150 -41
  176. package/test/entities.test.ts +623 -0
  177. package/test/extended-entities.test.ts +1228 -0
  178. package/test/provider-implementations.test.ts +725 -0
  179. package/test/provider-registry-extended.test.ts +583 -0
  180. package/test/providers/google-sheets.test.ts +851 -0
  181. package/test/providers/helpers.ts +554 -0
  182. package/test/providers/hubspot.test.ts +576 -0
  183. package/test/providers/slack.test.ts +932 -0
  184. package/test/providers/stripe.test.ts +701 -0
  185. package/test/providers.test.ts +578 -0
  186. package/test/system-tools-extended.test.ts +632 -0
  187. package/test/system.test.ts +673 -0
  188. package/test/tools.test.ts +15 -11
  189. package/test/types.test.ts +402 -0
  190. package/test/verb-registration.test.ts +395 -0
  191. package/test/web-tools.test.ts +553 -0
  192. package/test/worker-extended.test.ts +699 -0
  193. package/test/worker.test.ts +576 -0
  194. package/test/wrap.test.ts +366 -0
  195. package/tsconfig.json +3 -13
  196. package/vitest.config.ts +37 -0
  197. package/wrangler.jsonc +9 -0
  198. package/.turbo/turbo-build.log +0 -5
  199. package/dist/providers/voice/vapi.d.ts +0 -27
  200. package/dist/providers/voice/vapi.d.ts.map +0 -1
  201. package/dist/providers/voice/vapi.js +0 -440
  202. package/dist/providers/voice/vapi.js.map +0 -1
  203. package/src/define.js +0 -267
  204. package/src/entities/advertising.js +0 -999
  205. package/src/entities/ai.js +0 -756
  206. package/src/entities/analytics.js +0 -1588
  207. package/src/entities/automation.js +0 -601
  208. package/src/entities/communication.js +0 -1150
  209. package/src/entities/crm.js +0 -1386
  210. package/src/entities/design.js +0 -546
  211. package/src/entities/development.js +0 -2212
  212. package/src/entities/document.js +0 -874
  213. package/src/entities/ecommerce.js +0 -1429
  214. package/src/entities/experiment.js +0 -1039
  215. package/src/entities/finance.js +0 -3478
  216. package/src/entities/forms.js +0 -1892
  217. package/src/entities/hr.js +0 -661
  218. package/src/entities/identity.js +0 -997
  219. package/src/entities/index.js +0 -282
  220. package/src/entities/infrastructure.js +0 -1153
  221. package/src/entities/knowledge.js +0 -1438
  222. package/src/entities/marketing.js +0 -1610
  223. package/src/entities/media.js +0 -1634
  224. package/src/entities/notification.js +0 -1199
  225. package/src/entities/presentation.js +0 -1274
  226. package/src/entities/productivity.js +0 -1317
  227. package/src/entities/project-management.js +0 -1136
  228. package/src/entities/recruiting.js +0 -736
  229. package/src/entities/shipping.js +0 -509
  230. package/src/entities/signature.js +0 -1102
  231. package/src/entities/site.js +0 -222
  232. package/src/entities/spreadsheet.js +0 -1341
  233. package/src/entities/storage.js +0 -1198
  234. package/src/entities/support.js +0 -1166
  235. package/src/entities/video-conferencing.js +0 -1750
  236. package/src/entities/video.js +0 -950
  237. package/src/entities.js +0 -1663
  238. package/src/index.js +0 -74
  239. package/src/providers/analytics/index.js +0 -17
  240. package/src/providers/analytics/mixpanel.js +0 -255
  241. package/src/providers/calendar/cal-com.js +0 -303
  242. package/src/providers/calendar/google-calendar.js +0 -335
  243. package/src/providers/calendar/index.js +0 -20
  244. package/src/providers/crm/hubspot.js +0 -566
  245. package/src/providers/crm/index.js +0 -17
  246. package/src/providers/development/github.js +0 -472
  247. package/src/providers/development/index.js +0 -17
  248. package/src/providers/ecommerce/index.js +0 -17
  249. package/src/providers/ecommerce/shopify.js +0 -378
  250. package/src/providers/email/index.js +0 -20
  251. package/src/providers/email/resend.js +0 -258
  252. package/src/providers/email/sendgrid.js +0 -161
  253. package/src/providers/finance/index.js +0 -17
  254. package/src/providers/finance/stripe.js +0 -549
  255. package/src/providers/forms/index.js +0 -17
  256. package/src/providers/forms/typeform.js +0 -500
  257. package/src/providers/index.js +0 -123
  258. package/src/providers/knowledge/index.js +0 -17
  259. package/src/providers/knowledge/notion.js +0 -389
  260. package/src/providers/marketing/index.js +0 -17
  261. package/src/providers/marketing/mailchimp.js +0 -443
  262. package/src/providers/media/cloudinary.js +0 -318
  263. package/src/providers/media/index.js +0 -17
  264. package/src/providers/messaging/index.js +0 -20
  265. package/src/providers/messaging/slack.js +0 -393
  266. package/src/providers/messaging/twilio-sms.js +0 -249
  267. package/src/providers/project-management/index.js +0 -17
  268. package/src/providers/project-management/linear.js +0 -575
  269. package/src/providers/registry.js +0 -86
  270. package/src/providers/spreadsheet/google-sheets.js +0 -375
  271. package/src/providers/spreadsheet/index.js +0 -20
  272. package/src/providers/spreadsheet/xlsx.js +0 -423
  273. package/src/providers/storage/index.js +0 -24
  274. package/src/providers/storage/s3.js +0 -419
  275. package/src/providers/support/index.js +0 -17
  276. package/src/providers/support/zendesk.js +0 -373
  277. package/src/providers/tasks/index.js +0 -17
  278. package/src/providers/tasks/todoist.js +0 -286
  279. package/src/providers/types.js +0 -9
  280. package/src/providers/video-conferencing/google-meet.js +0 -286
  281. package/src/providers/video-conferencing/index.js +0 -31
  282. package/src/providers/video-conferencing/jitsi.js +0 -254
  283. package/src/providers/video-conferencing/teams.js +0 -270
  284. package/src/providers/video-conferencing/zoom.js +0 -332
  285. package/src/registry.js +0 -128
  286. package/src/tools/communication.js +0 -184
  287. package/src/tools/data.js +0 -205
  288. package/src/tools/index.js +0 -11
  289. package/src/tools/web.js +0 -137
  290. package/src/types.js +0 -10
  291. package/test/define.test.js +0 -306
  292. package/test/registry.test.js +0 -357
  293. package/test/tools.test.js +0 -363
@@ -0,0 +1,701 @@
1
+ /**
2
+ * Stripe Finance Provider Tests
3
+ *
4
+ * Tests for the Stripe payment provider implementation covering:
5
+ * - Provider initialization with API keys
6
+ * - Invoice creation and management
7
+ * - Payment processing
8
+ * - Customer management
9
+ * - Refund operations
10
+ * - Error handling
11
+ */
12
+
13
+ import { describe, it, expect, beforeEach, afterEach, vi, type MockInstance } from 'vitest'
14
+ import { createStripeProvider, stripeInfo } from '../../src/providers/finance/stripe.js'
15
+ import type { FinanceProvider } from '../../src/providers/types.js'
16
+ import {
17
+ setupMockFetch,
18
+ resetMockFetch,
19
+ mockJsonResponse,
20
+ mockErrorResponse,
21
+ mockNetworkError,
22
+ getLastFetchCall,
23
+ getFetchCall,
24
+ parseFetchFormBody,
25
+ stripeMocks,
26
+ createTestLineItems,
27
+ } from './helpers.js'
28
+
29
+ describe('Stripe Finance Provider', () => {
30
+ let mockFetch: MockInstance
31
+ let provider: FinanceProvider
32
+
33
+ beforeEach(() => {
34
+ mockFetch = setupMockFetch()
35
+ })
36
+
37
+ afterEach(() => {
38
+ resetMockFetch(mockFetch)
39
+ })
40
+
41
+ // ===========================================================================
42
+ // Provider Initialization Tests
43
+ // ===========================================================================
44
+
45
+ describe('initialization', () => {
46
+ it('should have correct provider info', () => {
47
+ const provider = createStripeProvider({})
48
+ expect(provider.info).toBe(stripeInfo)
49
+ expect(provider.info.id).toBe('finance.stripe')
50
+ expect(provider.info.name).toBe('Stripe')
51
+ expect(provider.info.category).toBe('finance')
52
+ })
53
+
54
+ it('should require API key for initialization', async () => {
55
+ provider = createStripeProvider({})
56
+ await expect(provider.initialize({})).rejects.toThrow('Stripe API key is required')
57
+ })
58
+
59
+ it('should initialize successfully with valid API key', async () => {
60
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
61
+ await expect(provider.initialize({ apiKey: 'sk_test_123' })).resolves.toBeUndefined()
62
+ })
63
+
64
+ it('should include requiredConfig in provider info', () => {
65
+ provider = createStripeProvider({})
66
+ expect(provider.info.requiredConfig).toContain('apiKey')
67
+ })
68
+
69
+ it('should include optionalConfig in provider info', () => {
70
+ provider = createStripeProvider({})
71
+ expect(provider.info.optionalConfig).toContain('webhookSecret')
72
+ })
73
+ })
74
+
75
+ // ===========================================================================
76
+ // Health Check Tests
77
+ // ===========================================================================
78
+
79
+ describe('healthCheck', () => {
80
+ beforeEach(async () => {
81
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
82
+ await provider.initialize({ apiKey: 'sk_test_123' })
83
+ })
84
+
85
+ it('should return healthy status on successful balance check', async () => {
86
+ mockFetch.mockResolvedValueOnce(
87
+ mockJsonResponse({ available: [{ amount: 1000 }], pending: [] })
88
+ )
89
+
90
+ const health = await provider.healthCheck()
91
+
92
+ expect(health.healthy).toBe(true)
93
+ expect(health.message).toBe('Connected')
94
+ expect(health.latencyMs).toBeGreaterThanOrEqual(0)
95
+ expect(health.checkedAt).toBeInstanceOf(Date)
96
+ })
97
+
98
+ it('should call balance endpoint for health check', async () => {
99
+ mockFetch.mockResolvedValueOnce(mockJsonResponse({}))
100
+
101
+ await provider.healthCheck()
102
+
103
+ const { url } = getLastFetchCall(mockFetch)
104
+ expect(url).toContain('/balance')
105
+ })
106
+
107
+ it('should return unhealthy status on API error', async () => {
108
+ mockFetch.mockResolvedValueOnce(
109
+ mockErrorResponse(stripeMocks.error('Invalid API Key', 'authentication_error'), 401)
110
+ )
111
+
112
+ const health = await provider.healthCheck()
113
+
114
+ expect(health.healthy).toBe(false)
115
+ expect(health.message).toBe('Invalid API Key')
116
+ })
117
+
118
+ it('should return unhealthy status on network error', async () => {
119
+ mockFetch.mockRejectedValueOnce(mockNetworkError('Network unavailable'))
120
+
121
+ const health = await provider.healthCheck()
122
+
123
+ expect(health.healthy).toBe(false)
124
+ expect(health.message).toBe('Network unavailable')
125
+ })
126
+ })
127
+
128
+ // ===========================================================================
129
+ // Invoice Operations Tests
130
+ // ===========================================================================
131
+
132
+ describe('createInvoice', () => {
133
+ beforeEach(async () => {
134
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
135
+ await provider.initialize({ apiKey: 'sk_test_123' })
136
+ })
137
+
138
+ it('should create invoice with line items', async () => {
139
+ mockFetch
140
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
141
+ .mockResolvedValueOnce(mockJsonResponse({})) // line item 1
142
+ .mockResolvedValueOnce(mockJsonResponse({})) // line item 2
143
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
144
+
145
+ const result = await provider.createInvoice({
146
+ customerId: 'cus_123',
147
+ lineItems: createTestLineItems(2),
148
+ currency: 'usd',
149
+ })
150
+
151
+ expect(result.id).toBe('inv_123')
152
+ expect(result.customerId).toBe('cus_123')
153
+ })
154
+
155
+ it('should use form-encoded body for Stripe API', async () => {
156
+ mockFetch
157
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
158
+ .mockResolvedValueOnce(mockJsonResponse({}))
159
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
160
+
161
+ await provider.createInvoice({
162
+ customerId: 'cus_123',
163
+ lineItems: [{ description: 'Item', quantity: 1, unitPrice: 100 }],
164
+ currency: 'usd',
165
+ })
166
+
167
+ const { options } = getFetchCall(mockFetch, 0)
168
+ expect(options?.headers).toHaveProperty('Content-Type', 'application/x-www-form-urlencoded')
169
+ })
170
+
171
+ it('should include customer ID in request', async () => {
172
+ mockFetch
173
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
174
+ .mockResolvedValueOnce(mockJsonResponse({}))
175
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
176
+
177
+ await provider.createInvoice({
178
+ customerId: 'cus_456',
179
+ lineItems: [{ description: 'Item', quantity: 1, unitPrice: 100 }],
180
+ })
181
+
182
+ // Parse the first call (invoice creation)
183
+ const { options } = getFetchCall(mockFetch, 0)
184
+ const params = new URLSearchParams(options?.body as string)
185
+ expect(params.get('customer')).toBe('cus_456')
186
+ })
187
+
188
+ it('should convert unit price to cents', async () => {
189
+ mockFetch
190
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
191
+ .mockResolvedValueOnce(mockJsonResponse({}))
192
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
193
+
194
+ await provider.createInvoice({
195
+ customerId: 'cus_123',
196
+ lineItems: [{ description: 'Item', quantity: 1, unitPrice: 49.99 }],
197
+ })
198
+
199
+ // Check the invoiceitem call
200
+ const { options } = getFetchCall(mockFetch, 1)
201
+ const params = new URLSearchParams(options?.body as string)
202
+ expect(params.get('unit_amount')).toBe('4999')
203
+ })
204
+
205
+ it('should set due date when provided', async () => {
206
+ mockFetch
207
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
208
+ .mockResolvedValueOnce(mockJsonResponse({}))
209
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
210
+
211
+ const dueDate = new Date('2024-12-31')
212
+ await provider.createInvoice({
213
+ customerId: 'cus_123',
214
+ lineItems: [{ description: 'Item', quantity: 1, unitPrice: 100 }],
215
+ dueDate,
216
+ })
217
+
218
+ // Parse the first call (invoice creation)
219
+ const { options } = getFetchCall(mockFetch, 0)
220
+ const params = new URLSearchParams(options?.body as string)
221
+ expect(parseInt(params.get('due_date')!)).toBe(Math.floor(dueDate.getTime() / 1000))
222
+ })
223
+
224
+ it('should include memo as description', async () => {
225
+ mockFetch
226
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
227
+ .mockResolvedValueOnce(mockJsonResponse({}))
228
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
229
+
230
+ await provider.createInvoice({
231
+ customerId: 'cus_123',
232
+ lineItems: [{ description: 'Item', quantity: 1, unitPrice: 100 }],
233
+ memo: 'Thank you for your business',
234
+ })
235
+
236
+ // Parse the first call (invoice creation)
237
+ const { options } = getFetchCall(mockFetch, 0)
238
+ const params = new URLSearchParams(options?.body as string)
239
+ expect(params.get('description')).toBe('Thank you for your business')
240
+ })
241
+ })
242
+
243
+ describe('getInvoice', () => {
244
+ beforeEach(async () => {
245
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
246
+ await provider.initialize({ apiKey: 'sk_test_123' })
247
+ })
248
+
249
+ it('should retrieve invoice by ID', async () => {
250
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123')))
251
+
252
+ const invoice = await provider.getInvoice('inv_123')
253
+
254
+ expect(invoice).not.toBeNull()
255
+ expect(invoice?.id).toBe('inv_123')
256
+ })
257
+
258
+ it('should return null for non-existent invoice', async () => {
259
+ mockFetch.mockResolvedValueOnce(mockErrorResponse(stripeMocks.error('No such invoice'), 404))
260
+
261
+ const invoice = await provider.getInvoice('inv_nonexistent')
262
+
263
+ expect(invoice).toBeNull()
264
+ })
265
+
266
+ it('should convert amounts from cents to dollars', async () => {
267
+ mockFetch.mockResolvedValueOnce(
268
+ mockJsonResponse(stripeMocks.invoice('inv_123', { subtotal: 5000, total: 5250 }))
269
+ )
270
+
271
+ const invoice = await provider.getInvoice('inv_123')
272
+
273
+ expect(invoice?.subtotal).toBe(50)
274
+ expect(invoice?.total).toBe(52.5)
275
+ })
276
+
277
+ it('should parse status correctly', async () => {
278
+ mockFetch.mockResolvedValueOnce(
279
+ mockJsonResponse(stripeMocks.invoice('inv_123', { status: 'paid' }))
280
+ )
281
+
282
+ const invoice = await provider.getInvoice('inv_123')
283
+
284
+ expect(invoice?.status).toBe('paid')
285
+ })
286
+ })
287
+
288
+ describe('listInvoices', () => {
289
+ beforeEach(async () => {
290
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
291
+ await provider.initialize({ apiKey: 'sk_test_123' })
292
+ })
293
+
294
+ it('should return paginated invoices', async () => {
295
+ const invoices = [stripeMocks.invoice('inv_1'), stripeMocks.invoice('inv_2')]
296
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.listResponse(invoices, true)))
297
+
298
+ const result = await provider.listInvoices()
299
+
300
+ expect(result.items).toHaveLength(2)
301
+ expect(result.hasMore).toBe(true)
302
+ })
303
+
304
+ it('should apply pagination options', async () => {
305
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.listResponse([])))
306
+
307
+ await provider.listInvoices({ limit: 25, cursor: 'inv_last' })
308
+
309
+ const { url } = getLastFetchCall(mockFetch)
310
+ expect(url).toContain('limit=25')
311
+ expect(url).toContain('starting_after=inv_last')
312
+ })
313
+
314
+ it('should filter by customer ID', async () => {
315
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.listResponse([])))
316
+
317
+ await provider.listInvoices({ customerId: 'cus_123' })
318
+
319
+ const { url } = getLastFetchCall(mockFetch)
320
+ expect(url).toContain('customer=cus_123')
321
+ })
322
+
323
+ it('should filter by status', async () => {
324
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.listResponse([])))
325
+
326
+ await provider.listInvoices({ status: 'open' })
327
+
328
+ const { url } = getLastFetchCall(mockFetch)
329
+ expect(url).toContain('status=open')
330
+ })
331
+ })
332
+
333
+ describe('sendInvoice', () => {
334
+ beforeEach(async () => {
335
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
336
+ await provider.initialize({ apiKey: 'sk_test_123' })
337
+ })
338
+
339
+ it('should finalize and send draft invoice', async () => {
340
+ mockFetch
341
+ .mockResolvedValueOnce(
342
+ mockJsonResponse(stripeMocks.invoice('inv_123', { status: 'draft' }))
343
+ )
344
+ .mockResolvedValueOnce(mockJsonResponse(stripeMocks.invoice('inv_123', { status: 'open' })))
345
+ .mockResolvedValueOnce(mockJsonResponse({}))
346
+
347
+ const result = await provider.sendInvoice!('inv_123')
348
+
349
+ expect(result).toBe(true)
350
+ expect(mockFetch).toHaveBeenCalledTimes(3)
351
+ })
352
+
353
+ it('should return false on failure', async () => {
354
+ mockFetch.mockResolvedValueOnce(
355
+ mockErrorResponse(stripeMocks.error('Invoice not found'), 404)
356
+ )
357
+
358
+ const result = await provider.sendInvoice!('inv_nonexistent')
359
+
360
+ expect(result).toBe(false)
361
+ })
362
+ })
363
+
364
+ describe('voidInvoice', () => {
365
+ beforeEach(async () => {
366
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
367
+ await provider.initialize({ apiKey: 'sk_test_123' })
368
+ })
369
+
370
+ it('should void invoice successfully', async () => {
371
+ mockFetch.mockResolvedValueOnce(
372
+ mockJsonResponse(stripeMocks.invoice('inv_123', { status: 'void' }))
373
+ )
374
+
375
+ const result = await provider.voidInvoice!('inv_123')
376
+
377
+ expect(result).toBe(true)
378
+ const { url } = getLastFetchCall(mockFetch)
379
+ expect(url).toContain('/invoices/inv_123/void')
380
+ })
381
+ })
382
+
383
+ // ===========================================================================
384
+ // Payment Operations Tests
385
+ // ===========================================================================
386
+
387
+ describe('createPayment', () => {
388
+ beforeEach(async () => {
389
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
390
+ await provider.initialize({ apiKey: 'sk_test_123' })
391
+ })
392
+
393
+ it('should create payment intent', async () => {
394
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.paymentIntent('pi_123')))
395
+
396
+ const result = await provider.createPayment({
397
+ amount: 100,
398
+ currency: 'usd',
399
+ paymentMethod: 'pm_123',
400
+ })
401
+
402
+ expect(result.id).toBe('pi_123')
403
+ expect(result.amount).toBe(100)
404
+ expect(result.status).toBe('succeeded')
405
+ })
406
+
407
+ it('should convert amount to cents', async () => {
408
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.paymentIntent('pi_123')))
409
+
410
+ await provider.createPayment({
411
+ amount: 49.99,
412
+ currency: 'usd',
413
+ paymentMethod: 'pm_123',
414
+ })
415
+
416
+ const body = parseFetchFormBody(mockFetch)
417
+ expect(body.amount).toBe('4999')
418
+ })
419
+
420
+ it('should include customer ID when provided', async () => {
421
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.paymentIntent('pi_123')))
422
+
423
+ await provider.createPayment({
424
+ amount: 100,
425
+ currency: 'usd',
426
+ paymentMethod: 'pm_123',
427
+ customerId: 'cus_123',
428
+ })
429
+
430
+ const body = parseFetchFormBody(mockFetch)
431
+ expect(body.customer).toBe('cus_123')
432
+ })
433
+
434
+ it('should set confirm to true', async () => {
435
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.paymentIntent('pi_123')))
436
+
437
+ await provider.createPayment({
438
+ amount: 100,
439
+ currency: 'usd',
440
+ paymentMethod: 'pm_123',
441
+ })
442
+
443
+ const body = parseFetchFormBody(mockFetch)
444
+ expect(body.confirm).toBe('true')
445
+ })
446
+ })
447
+
448
+ describe('getPayment', () => {
449
+ beforeEach(async () => {
450
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
451
+ await provider.initialize({ apiKey: 'sk_test_123' })
452
+ })
453
+
454
+ it('should retrieve payment intent by ID', async () => {
455
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.paymentIntent('pi_123')))
456
+
457
+ const payment = await provider.getPayment('pi_123')
458
+
459
+ expect(payment).not.toBeNull()
460
+ expect(payment?.id).toBe('pi_123')
461
+ })
462
+
463
+ it('should return null for non-existent payment', async () => {
464
+ mockFetch.mockResolvedValueOnce(
465
+ mockErrorResponse(stripeMocks.error('No such payment_intent'), 404)
466
+ )
467
+
468
+ const payment = await provider.getPayment('pi_nonexistent')
469
+
470
+ expect(payment).toBeNull()
471
+ })
472
+
473
+ it('should map payment status correctly', async () => {
474
+ mockFetch.mockResolvedValueOnce(
475
+ mockJsonResponse(stripeMocks.paymentIntent('pi_123', { status: 'requires_payment_method' }))
476
+ )
477
+
478
+ const payment = await provider.getPayment('pi_123')
479
+
480
+ expect(payment?.status).toBe('pending')
481
+ })
482
+ })
483
+
484
+ describe('listPayments', () => {
485
+ beforeEach(async () => {
486
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
487
+ await provider.initialize({ apiKey: 'sk_test_123' })
488
+ })
489
+
490
+ it('should return paginated payment intents', async () => {
491
+ const intents = [stripeMocks.paymentIntent('pi_1'), stripeMocks.paymentIntent('pi_2')]
492
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.listResponse(intents)))
493
+
494
+ const result = await provider.listPayments()
495
+
496
+ expect(result.items).toHaveLength(2)
497
+ })
498
+ })
499
+
500
+ describe('refundPayment', () => {
501
+ beforeEach(async () => {
502
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
503
+ await provider.initialize({ apiKey: 'sk_test_123' })
504
+ })
505
+
506
+ it('should create full refund', async () => {
507
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.refund('re_123', 'pi_123')))
508
+
509
+ const refund = await provider.refundPayment!('pi_123')
510
+
511
+ expect(refund.id).toBe('re_123')
512
+ expect(refund.paymentId).toBe('pi_123')
513
+ })
514
+
515
+ it('should create partial refund', async () => {
516
+ mockFetch.mockResolvedValueOnce(
517
+ mockJsonResponse(stripeMocks.refund('re_123', 'pi_123', { amount: 2500 }))
518
+ )
519
+
520
+ const refund = await provider.refundPayment!('pi_123', 25)
521
+
522
+ const body = parseFetchFormBody(mockFetch)
523
+ expect(body.amount).toBe('2500')
524
+ expect(refund.amount).toBe(25)
525
+ })
526
+ })
527
+
528
+ // ===========================================================================
529
+ // Customer Operations Tests
530
+ // ===========================================================================
531
+
532
+ describe('createCustomer', () => {
533
+ beforeEach(async () => {
534
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
535
+ await provider.initialize({ apiKey: 'sk_test_123' })
536
+ })
537
+
538
+ it('should create customer with basic info', async () => {
539
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.customer('cus_123')))
540
+
541
+ const result = await provider.createCustomer!({
542
+ name: 'John Doe',
543
+ email: 'john@example.com',
544
+ })
545
+
546
+ expect(result.id).toBe('cus_123')
547
+ expect(result.name).toBe('John Doe')
548
+ })
549
+
550
+ it('should include address when provided', async () => {
551
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.customer('cus_123')))
552
+
553
+ await provider.createCustomer!({
554
+ name: 'John Doe',
555
+ address: {
556
+ line1: '123 Main St',
557
+ line2: 'Suite 100',
558
+ city: 'San Francisco',
559
+ state: 'CA',
560
+ postalCode: '94105',
561
+ country: 'US',
562
+ },
563
+ })
564
+
565
+ const body = parseFetchFormBody(mockFetch)
566
+ expect(body['address[line1]']).toBe('123 Main St')
567
+ expect(body['address[city]']).toBe('San Francisco')
568
+ expect(body['address[postal_code]']).toBe('94105')
569
+ })
570
+ })
571
+
572
+ describe('getCustomer', () => {
573
+ beforeEach(async () => {
574
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
575
+ await provider.initialize({ apiKey: 'sk_test_123' })
576
+ })
577
+
578
+ it('should retrieve customer by ID', async () => {
579
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.customer('cus_123')))
580
+
581
+ const customer = await provider.getCustomer!('cus_123')
582
+
583
+ expect(customer).not.toBeNull()
584
+ expect(customer?.id).toBe('cus_123')
585
+ })
586
+
587
+ it('should convert balance from cents', async () => {
588
+ mockFetch.mockResolvedValueOnce(
589
+ mockJsonResponse(stripeMocks.customer('cus_123', { balance: -5000 }))
590
+ )
591
+
592
+ const customer = await provider.getCustomer!('cus_123')
593
+
594
+ expect(customer?.balance).toBe(-50)
595
+ })
596
+ })
597
+
598
+ describe('listCustomers', () => {
599
+ beforeEach(async () => {
600
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
601
+ await provider.initialize({ apiKey: 'sk_test_123' })
602
+ })
603
+
604
+ it('should return paginated customers', async () => {
605
+ const customers = [stripeMocks.customer('cus_1'), stripeMocks.customer('cus_2')]
606
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.listResponse(customers)))
607
+
608
+ const result = await provider.listCustomers!()
609
+
610
+ expect(result.items).toHaveLength(2)
611
+ })
612
+ })
613
+
614
+ // ===========================================================================
615
+ // API Request Formatting Tests
616
+ // ===========================================================================
617
+
618
+ describe('API request formatting', () => {
619
+ beforeEach(async () => {
620
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
621
+ await provider.initialize({ apiKey: 'sk_test_123' })
622
+ })
623
+
624
+ it('should use Bearer token authentication', async () => {
625
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.customer('cus_123')))
626
+
627
+ await provider.getCustomer!('cus_123')
628
+
629
+ const { options } = getLastFetchCall(mockFetch)
630
+ expect(options?.headers).toHaveProperty('Authorization', 'Bearer sk_test_123')
631
+ })
632
+
633
+ it('should use correct base URL', async () => {
634
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(stripeMocks.customer('cus_123')))
635
+
636
+ await provider.getCustomer!('cus_123')
637
+
638
+ const { url } = getLastFetchCall(mockFetch)
639
+ expect(url).toContain('https://api.stripe.com/v1')
640
+ })
641
+ })
642
+
643
+ // ===========================================================================
644
+ // Error Handling Tests
645
+ // ===========================================================================
646
+
647
+ describe('error handling', () => {
648
+ beforeEach(async () => {
649
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
650
+ await provider.initialize({ apiKey: 'sk_test_123' })
651
+ })
652
+
653
+ it('should handle card declined errors', async () => {
654
+ mockFetch.mockResolvedValueOnce(
655
+ mockErrorResponse(
656
+ stripeMocks.error('Your card was declined', 'card_error', 'card_declined'),
657
+ 402
658
+ )
659
+ )
660
+
661
+ await expect(
662
+ provider.createPayment({ amount: 100, currency: 'usd', paymentMethod: 'pm_123' })
663
+ ).rejects.toThrow('Your card was declined')
664
+ })
665
+
666
+ it('should handle rate limit errors', async () => {
667
+ mockFetch.mockResolvedValueOnce(
668
+ mockErrorResponse(stripeMocks.error('Rate limit exceeded', 'rate_limit_error'), 429)
669
+ )
670
+
671
+ await expect(provider.listInvoices()).rejects.toThrow('Rate limit exceeded')
672
+ })
673
+
674
+ it('should handle invalid request errors', async () => {
675
+ mockFetch.mockResolvedValueOnce(
676
+ mockErrorResponse(stripeMocks.error('Invalid currency', 'invalid_request_error'), 400)
677
+ )
678
+
679
+ await expect(
680
+ provider.createInvoice({
681
+ customerId: 'cus_123',
682
+ lineItems: [{ description: 'Item', quantity: 1, unitPrice: 100 }],
683
+ currency: 'invalid',
684
+ })
685
+ ).rejects.toThrow('Invalid currency')
686
+ })
687
+ })
688
+
689
+ // ===========================================================================
690
+ // Dispose Tests
691
+ // ===========================================================================
692
+
693
+ describe('dispose', () => {
694
+ it('should dispose without error', async () => {
695
+ provider = createStripeProvider({ apiKey: 'sk_test_123' })
696
+ await provider.initialize({ apiKey: 'sk_test_123' })
697
+
698
+ await expect(provider.dispose()).resolves.toBeUndefined()
699
+ })
700
+ })
701
+ })