digital-tools 2.1.3 → 2.4.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 (294) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +19 -0
  3. package/README.md +2 -0
  4. package/dist/client.d.ts +109 -0
  5. package/dist/client.d.ts.map +1 -0
  6. package/dist/client.js +69 -0
  7. package/dist/client.js.map +1 -0
  8. package/dist/define.d.ts +2 -2
  9. package/dist/define.d.ts.map +1 -1
  10. package/dist/define.js +21 -11
  11. package/dist/define.js.map +1 -1
  12. package/dist/function-ref.d.ts +229 -0
  13. package/dist/function-ref.d.ts.map +1 -0
  14. package/dist/function-ref.js +28 -0
  15. package/dist/function-ref.js.map +1 -0
  16. package/dist/function-sugar.d.ts +57 -0
  17. package/dist/function-sugar.d.ts.map +1 -0
  18. package/dist/function-sugar.js +79 -0
  19. package/dist/function-sugar.js.map +1 -0
  20. package/dist/index.d.ts +10 -3
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +24 -4
  23. package/dist/index.js.map +1 -1
  24. package/dist/providers/analytics/mixpanel.d.ts.map +1 -1
  25. package/dist/providers/analytics/mixpanel.js +21 -18
  26. package/dist/providers/analytics/mixpanel.js.map +1 -1
  27. package/dist/providers/calendar/cal-com.d.ts.map +1 -1
  28. package/dist/providers/calendar/cal-com.js +10 -10
  29. package/dist/providers/calendar/cal-com.js.map +1 -1
  30. package/dist/providers/calendar/google-calendar.d.ts.map +1 -1
  31. package/dist/providers/calendar/google-calendar.js +4 -4
  32. package/dist/providers/calendar/google-calendar.js.map +1 -1
  33. package/dist/providers/crm/hubspot.d.ts.map +1 -1
  34. package/dist/providers/crm/hubspot.js +107 -85
  35. package/dist/providers/crm/hubspot.js.map +1 -1
  36. package/dist/providers/development/github.d.ts.map +1 -1
  37. package/dist/providers/development/github.js +40 -43
  38. package/dist/providers/development/github.js.map +1 -1
  39. package/dist/providers/ecommerce/shopify.d.ts.map +1 -1
  40. package/dist/providers/ecommerce/shopify.js +79 -62
  41. package/dist/providers/ecommerce/shopify.js.map +1 -1
  42. package/dist/providers/email/resend.d.ts.map +1 -1
  43. package/dist/providers/email/resend.js +20 -16
  44. package/dist/providers/email/resend.js.map +1 -1
  45. package/dist/providers/email/sendgrid.d.ts.map +1 -1
  46. package/dist/providers/email/sendgrid.js +12 -9
  47. package/dist/providers/email/sendgrid.js.map +1 -1
  48. package/dist/providers/finance/stripe.d.ts.map +1 -1
  49. package/dist/providers/finance/stripe.js +44 -42
  50. package/dist/providers/finance/stripe.js.map +1 -1
  51. package/dist/providers/forms/typeform.d.ts.map +1 -1
  52. package/dist/providers/forms/typeform.js +68 -58
  53. package/dist/providers/forms/typeform.js.map +1 -1
  54. package/dist/providers/knowledge/notion.d.ts.map +1 -1
  55. package/dist/providers/knowledge/notion.js +75 -41
  56. package/dist/providers/knowledge/notion.js.map +1 -1
  57. package/dist/providers/marketing/mailchimp.d.ts.map +1 -1
  58. package/dist/providers/marketing/mailchimp.js +74 -61
  59. package/dist/providers/marketing/mailchimp.js.map +1 -1
  60. package/dist/providers/media/cloudinary.d.ts.map +1 -1
  61. package/dist/providers/media/cloudinary.js +30 -28
  62. package/dist/providers/media/cloudinary.js.map +1 -1
  63. package/dist/providers/messaging/slack.d.ts.map +1 -1
  64. package/dist/providers/messaging/slack.js +75 -58
  65. package/dist/providers/messaging/slack.js.map +1 -1
  66. package/dist/providers/messaging/twilio-sms.d.ts.map +1 -1
  67. package/dist/providers/messaging/twilio-sms.js +33 -15
  68. package/dist/providers/messaging/twilio-sms.js.map +1 -1
  69. package/dist/providers/project-management/linear.d.ts.map +1 -1
  70. package/dist/providers/project-management/linear.js +31 -27
  71. package/dist/providers/project-management/linear.js.map +1 -1
  72. package/dist/providers/spreadsheet/google-sheets.d.ts.map +1 -1
  73. package/dist/providers/spreadsheet/google-sheets.js +21 -18
  74. package/dist/providers/spreadsheet/google-sheets.js.map +1 -1
  75. package/dist/providers/spreadsheet/xlsx.d.ts.map +1 -1
  76. package/dist/providers/spreadsheet/xlsx.js +4 -4
  77. package/dist/providers/spreadsheet/xlsx.js.map +1 -1
  78. package/dist/providers/storage/index.js +1 -0
  79. package/dist/providers/storage/index.js.map +1 -1
  80. package/dist/providers/storage/s3.d.ts.map +1 -1
  81. package/dist/providers/storage/s3.js +36 -27
  82. package/dist/providers/storage/s3.js.map +1 -1
  83. package/dist/providers/support/zendesk.d.ts.map +1 -1
  84. package/dist/providers/support/zendesk.js +24 -25
  85. package/dist/providers/support/zendesk.js.map +1 -1
  86. package/dist/providers/tasks/todoist.d.ts.map +1 -1
  87. package/dist/providers/tasks/todoist.js +18 -18
  88. package/dist/providers/tasks/todoist.js.map +1 -1
  89. package/dist/providers/video-conferencing/google-meet.d.ts.map +1 -1
  90. package/dist/providers/video-conferencing/google-meet.js +11 -11
  91. package/dist/providers/video-conferencing/google-meet.js.map +1 -1
  92. package/dist/providers/video-conferencing/jitsi.js +14 -14
  93. package/dist/providers/video-conferencing/jitsi.js.map +1 -1
  94. package/dist/providers/video-conferencing/teams.d.ts.map +1 -1
  95. package/dist/providers/video-conferencing/teams.js +9 -7
  96. package/dist/providers/video-conferencing/teams.js.map +1 -1
  97. package/dist/providers/video-conferencing/zoom.d.ts.map +1 -1
  98. package/dist/providers/video-conferencing/zoom.js +26 -24
  99. package/dist/providers/video-conferencing/zoom.js.map +1 -1
  100. package/dist/tools/data.d.ts.map +1 -1
  101. package/dist/tools/data.js +5 -12
  102. package/dist/tools/data.js.map +1 -1
  103. package/dist/tools/index.d.ts +1 -0
  104. package/dist/tools/index.d.ts.map +1 -1
  105. package/dist/tools/index.js +1 -0
  106. package/dist/tools/index.js.map +1 -1
  107. package/dist/tools/system.d.ts +289 -0
  108. package/dist/tools/system.d.ts.map +1 -0
  109. package/dist/tools/system.js +752 -0
  110. package/dist/tools/system.js.map +1 -0
  111. package/dist/tools/web.d.ts.map +1 -1
  112. package/dist/tools/web.js +22 -10
  113. package/dist/tools/web.js.map +1 -1
  114. package/dist/track-record.d.ts +101 -0
  115. package/dist/track-record.d.ts.map +1 -0
  116. package/dist/track-record.js +17 -0
  117. package/dist/track-record.js.map +1 -0
  118. package/dist/types.d.ts +210 -9
  119. package/dist/types.d.ts.map +1 -1
  120. package/dist/verb-registration.d.ts +122 -0
  121. package/dist/verb-registration.d.ts.map +1 -0
  122. package/dist/verb-registration.js +176 -0
  123. package/dist/verb-registration.js.map +1 -0
  124. package/dist/worker.d.ts +93 -0
  125. package/dist/worker.d.ts.map +1 -0
  126. package/dist/worker.js +315 -0
  127. package/dist/worker.js.map +1 -0
  128. package/dist/wrap.d.ts +89 -0
  129. package/dist/wrap.d.ts.map +1 -0
  130. package/dist/wrap.js +225 -0
  131. package/dist/wrap.js.map +1 -0
  132. package/package.json +31 -14
  133. package/src/client.ts +136 -0
  134. package/src/define.ts +30 -24
  135. package/src/function-ref.ts +264 -0
  136. package/src/function-sugar.ts +134 -0
  137. package/src/index.ts +132 -10
  138. package/src/providers/analytics/mixpanel.ts +19 -18
  139. package/src/providers/calendar/cal-com.ts +29 -18
  140. package/src/providers/calendar/google-calendar.ts +20 -14
  141. package/src/providers/crm/hubspot.ts +225 -99
  142. package/src/providers/development/github.ts +206 -135
  143. package/src/providers/ecommerce/shopify.ts +250 -89
  144. package/src/providers/email/resend.ts +101 -28
  145. package/src/providers/email/sendgrid.ts +12 -9
  146. package/src/providers/finance/stripe.ts +128 -49
  147. package/src/providers/forms/typeform.ts +74 -58
  148. package/src/providers/knowledge/notion.ts +340 -88
  149. package/src/providers/marketing/mailchimp.ts +86 -70
  150. package/src/providers/media/cloudinary.ts +99 -41
  151. package/src/providers/messaging/slack.ts +283 -85
  152. package/src/providers/messaging/twilio-sms.ts +35 -15
  153. package/src/providers/project-management/linear.ts +143 -55
  154. package/src/providers/spreadsheet/google-sheets.ts +222 -56
  155. package/src/providers/spreadsheet/xlsx.ts +47 -16
  156. package/src/providers/storage/s3.ts +119 -47
  157. package/src/providers/support/zendesk.ts +196 -46
  158. package/src/providers/tasks/todoist.ts +20 -26
  159. package/src/providers/video-conferencing/google-meet.ts +17 -20
  160. package/src/providers/video-conferencing/jitsi.ts +14 -14
  161. package/src/providers/video-conferencing/teams.ts +14 -13
  162. package/src/providers/video-conferencing/zoom.ts +54 -49
  163. package/src/tools/data.ts +6 -16
  164. package/src/tools/index.ts +1 -0
  165. package/src/tools/system.ts +887 -0
  166. package/src/tools/web.ts +22 -10
  167. package/src/track-record.ts +106 -0
  168. package/src/types.ts +241 -13
  169. package/src/verb-registration.ts +197 -0
  170. package/src/worker.ts +370 -0
  171. package/src/wrap.ts +260 -0
  172. package/test/client.test.ts +146 -0
  173. package/test/communication-tools-extended.test.ts +734 -0
  174. package/test/data-tools-extended.test.ts +743 -0
  175. package/test/define-extended.test.ts +819 -0
  176. package/test/define.test.ts +150 -41
  177. package/test/entities.test.ts +623 -0
  178. package/test/extended-entities.test.ts +1228 -0
  179. package/test/provider-implementations.test.ts +725 -0
  180. package/test/provider-registry-extended.test.ts +583 -0
  181. package/test/providers/google-sheets.test.ts +851 -0
  182. package/test/providers/helpers.ts +554 -0
  183. package/test/providers/hubspot.test.ts +576 -0
  184. package/test/providers/slack.test.ts +932 -0
  185. package/test/providers/stripe.test.ts +701 -0
  186. package/test/providers.test.ts +578 -0
  187. package/test/system-tools-extended.test.ts +632 -0
  188. package/test/system.test.ts +673 -0
  189. package/test/tools.test.ts +15 -11
  190. package/test/types.test.ts +402 -0
  191. package/test/verb-registration.test.ts +395 -0
  192. package/test/web-tools.test.ts +553 -0
  193. package/test/worker-extended.test.ts +699 -0
  194. package/test/worker.test.ts +576 -0
  195. package/test/wrap.test.ts +366 -0
  196. package/tsconfig.json +3 -13
  197. package/vitest.config.ts +37 -0
  198. package/wrangler.jsonc +9 -0
  199. package/LICENSE +0 -21
  200. package/dist/providers/voice/vapi.d.ts +0 -27
  201. package/dist/providers/voice/vapi.d.ts.map +0 -1
  202. package/dist/providers/voice/vapi.js +0 -440
  203. package/dist/providers/voice/vapi.js.map +0 -1
  204. package/src/define.js +0 -259
  205. package/src/entities/advertising.js +0 -999
  206. package/src/entities/ai.js +0 -756
  207. package/src/entities/analytics.js +0 -1588
  208. package/src/entities/automation.js +0 -601
  209. package/src/entities/communication.js +0 -1150
  210. package/src/entities/crm.js +0 -1386
  211. package/src/entities/design.js +0 -546
  212. package/src/entities/development.js +0 -2212
  213. package/src/entities/document.js +0 -874
  214. package/src/entities/ecommerce.js +0 -1429
  215. package/src/entities/experiment.js +0 -1039
  216. package/src/entities/finance.js +0 -3478
  217. package/src/entities/forms.js +0 -1892
  218. package/src/entities/hr.js +0 -661
  219. package/src/entities/identity.js +0 -997
  220. package/src/entities/index.js +0 -282
  221. package/src/entities/infrastructure.js +0 -1153
  222. package/src/entities/knowledge.js +0 -1438
  223. package/src/entities/marketing.js +0 -1610
  224. package/src/entities/media.js +0 -1634
  225. package/src/entities/notification.js +0 -1199
  226. package/src/entities/presentation.js +0 -1274
  227. package/src/entities/productivity.js +0 -1317
  228. package/src/entities/project-management.js +0 -1136
  229. package/src/entities/recruiting.js +0 -736
  230. package/src/entities/shipping.js +0 -509
  231. package/src/entities/signature.js +0 -1102
  232. package/src/entities/site.js +0 -222
  233. package/src/entities/spreadsheet.js +0 -1341
  234. package/src/entities/storage.js +0 -1198
  235. package/src/entities/support.js +0 -1166
  236. package/src/entities/video-conferencing.js +0 -1750
  237. package/src/entities/video.js +0 -950
  238. package/src/entities.js +0 -1663
  239. package/src/index.js +0 -74
  240. package/src/providers/analytics/index.js +0 -17
  241. package/src/providers/analytics/mixpanel.js +0 -255
  242. package/src/providers/calendar/cal-com.js +0 -303
  243. package/src/providers/calendar/google-calendar.js +0 -335
  244. package/src/providers/calendar/index.js +0 -20
  245. package/src/providers/crm/hubspot.js +0 -566
  246. package/src/providers/crm/index.js +0 -17
  247. package/src/providers/development/github.js +0 -472
  248. package/src/providers/development/index.js +0 -17
  249. package/src/providers/ecommerce/index.js +0 -17
  250. package/src/providers/ecommerce/shopify.js +0 -378
  251. package/src/providers/email/index.js +0 -20
  252. package/src/providers/email/resend.js +0 -258
  253. package/src/providers/email/sendgrid.js +0 -161
  254. package/src/providers/finance/index.js +0 -17
  255. package/src/providers/finance/stripe.js +0 -549
  256. package/src/providers/forms/index.js +0 -17
  257. package/src/providers/forms/typeform.js +0 -500
  258. package/src/providers/index.js +0 -123
  259. package/src/providers/knowledge/index.js +0 -17
  260. package/src/providers/knowledge/notion.js +0 -389
  261. package/src/providers/marketing/index.js +0 -17
  262. package/src/providers/marketing/mailchimp.js +0 -443
  263. package/src/providers/media/cloudinary.js +0 -318
  264. package/src/providers/media/index.js +0 -17
  265. package/src/providers/messaging/index.js +0 -20
  266. package/src/providers/messaging/slack.js +0 -393
  267. package/src/providers/messaging/twilio-sms.js +0 -249
  268. package/src/providers/project-management/index.js +0 -17
  269. package/src/providers/project-management/linear.js +0 -575
  270. package/src/providers/registry.js +0 -86
  271. package/src/providers/spreadsheet/google-sheets.js +0 -375
  272. package/src/providers/spreadsheet/index.js +0 -20
  273. package/src/providers/spreadsheet/xlsx.js +0 -423
  274. package/src/providers/storage/index.js +0 -24
  275. package/src/providers/storage/s3.js +0 -419
  276. package/src/providers/support/index.js +0 -17
  277. package/src/providers/support/zendesk.js +0 -373
  278. package/src/providers/tasks/index.js +0 -17
  279. package/src/providers/tasks/todoist.js +0 -286
  280. package/src/providers/types.js +0 -9
  281. package/src/providers/video-conferencing/google-meet.js +0 -286
  282. package/src/providers/video-conferencing/index.js +0 -31
  283. package/src/providers/video-conferencing/jitsi.js +0 -254
  284. package/src/providers/video-conferencing/teams.js +0 -270
  285. package/src/providers/video-conferencing/zoom.js +0 -332
  286. package/src/registry.js +0 -128
  287. package/src/tools/communication.js +0 -184
  288. package/src/tools/data.js +0 -205
  289. package/src/tools/index.js +0 -11
  290. package/src/tools/web.js +0 -137
  291. package/src/types.js +0 -10
  292. package/test/define.test.js +0 -306
  293. package/test/registry.test.js +0 -357
  294. package/test/tools.test.js +0 -363
@@ -0,0 +1,576 @@
1
+ /**
2
+ * HubSpot CRM Provider Tests
3
+ *
4
+ * Tests for the HubSpot CRM provider implementation covering:
5
+ * - Provider initialization with access tokens
6
+ * - API request formatting and headers
7
+ * - Response parsing for contacts, deals, and activities
8
+ * - Error handling and edge cases
9
+ */
10
+
11
+ import { describe, it, expect, beforeEach, afterEach, vi, type MockInstance } from 'vitest'
12
+ import { createHubSpotProvider, hubspotInfo } from '../../src/providers/crm/hubspot.js'
13
+ import type { CRMProvider } from '../../src/providers/types.js'
14
+ import {
15
+ setupMockFetch,
16
+ resetMockFetch,
17
+ mockJsonResponse,
18
+ mockErrorResponse,
19
+ mockNetworkError,
20
+ getLastFetchCall,
21
+ getFetchCall,
22
+ parseFetchJsonBody,
23
+ hubspotMocks,
24
+ createTestContact,
25
+ } from './helpers.js'
26
+
27
+ describe('HubSpot CRM Provider', () => {
28
+ let mockFetch: MockInstance
29
+ let provider: CRMProvider
30
+
31
+ beforeEach(() => {
32
+ mockFetch = setupMockFetch()
33
+ })
34
+
35
+ afterEach(() => {
36
+ resetMockFetch(mockFetch)
37
+ })
38
+
39
+ // ===========================================================================
40
+ // Provider Initialization Tests
41
+ // ===========================================================================
42
+
43
+ describe('initialization', () => {
44
+ it('should have correct provider info', () => {
45
+ const provider = createHubSpotProvider({})
46
+ expect(provider.info).toBe(hubspotInfo)
47
+ expect(provider.info.id).toBe('crm.hubspot')
48
+ expect(provider.info.name).toBe('HubSpot')
49
+ expect(provider.info.category).toBe('crm')
50
+ })
51
+
52
+ it('should require access token for initialization', async () => {
53
+ provider = createHubSpotProvider({})
54
+ await expect(provider.initialize({})).rejects.toThrow('HubSpot access token is required')
55
+ })
56
+
57
+ it('should initialize successfully with valid access token', async () => {
58
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
59
+ await expect(provider.initialize({ accessToken: 'test-token' })).resolves.toBeUndefined()
60
+ })
61
+
62
+ it('should use default API URL when baseUrl not provided', async () => {
63
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
64
+ await provider.initialize({ accessToken: 'test-token' })
65
+
66
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.contact('123')))
67
+ await provider.getContact('123')
68
+
69
+ const { url } = getLastFetchCall(mockFetch)
70
+ expect(url).toContain('https://api.hubapi.com/crm/v3')
71
+ })
72
+
73
+ it('should use custom baseUrl when provided', async () => {
74
+ provider = createHubSpotProvider({
75
+ accessToken: 'test-token',
76
+ baseUrl: 'https://custom-api.hubspot.com/v3',
77
+ })
78
+ await provider.initialize({
79
+ accessToken: 'test-token',
80
+ baseUrl: 'https://custom-api.hubspot.com/v3',
81
+ })
82
+
83
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.contact('123')))
84
+ await provider.getContact('123')
85
+
86
+ const { url } = getLastFetchCall(mockFetch)
87
+ expect(url).toContain('https://custom-api.hubspot.com/v3')
88
+ })
89
+ })
90
+
91
+ // ===========================================================================
92
+ // Health Check Tests
93
+ // ===========================================================================
94
+
95
+ describe('healthCheck', () => {
96
+ beforeEach(async () => {
97
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
98
+ await provider.initialize({ accessToken: 'test-token' })
99
+ })
100
+
101
+ it('should return healthy status on successful API call', async () => {
102
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.listResponse([])))
103
+
104
+ const health = await provider.healthCheck()
105
+
106
+ expect(health.healthy).toBe(true)
107
+ expect(health.message).toBe('Connected')
108
+ expect(health.latencyMs).toBeGreaterThanOrEqual(0)
109
+ expect(health.checkedAt).toBeInstanceOf(Date)
110
+ })
111
+
112
+ it('should return unhealthy status on API error', async () => {
113
+ mockFetch.mockResolvedValueOnce(mockErrorResponse({ message: 'Unauthorized' }, 401))
114
+
115
+ const health = await provider.healthCheck()
116
+
117
+ expect(health.healthy).toBe(false)
118
+ expect(health.message).toBe('HTTP 401')
119
+ })
120
+
121
+ it('should return unhealthy status on network error', async () => {
122
+ mockFetch.mockRejectedValueOnce(mockNetworkError('Connection refused'))
123
+
124
+ const health = await provider.healthCheck()
125
+
126
+ expect(health.healthy).toBe(false)
127
+ expect(health.message).toBe('Connection refused')
128
+ })
129
+
130
+ it('should include Authorization header in health check request', async () => {
131
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.listResponse([])))
132
+
133
+ await provider.healthCheck()
134
+
135
+ const { options } = getLastFetchCall(mockFetch)
136
+ expect(options?.headers).toHaveProperty('Authorization', 'Bearer test-token')
137
+ })
138
+ })
139
+
140
+ // ===========================================================================
141
+ // Contact Operations Tests
142
+ // ===========================================================================
143
+
144
+ describe('createContact', () => {
145
+ beforeEach(async () => {
146
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
147
+ await provider.initialize({ accessToken: 'test-token' })
148
+ })
149
+
150
+ it('should create contact with required fields', async () => {
151
+ const mockContact = hubspotMocks.contact('123')
152
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(mockContact))
153
+
154
+ const result = await provider.createContact({
155
+ firstName: 'John',
156
+ lastName: 'Doe',
157
+ })
158
+
159
+ expect(result.id).toBe('123')
160
+ expect(result.firstName).toBe('John')
161
+ expect(result.lastName).toBe('Doe')
162
+ })
163
+
164
+ it('should format request body correctly', async () => {
165
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.contact('123')))
166
+
167
+ await provider.createContact({
168
+ firstName: 'John',
169
+ lastName: 'Doe',
170
+ email: 'john@example.com',
171
+ phone: '+1234567890',
172
+ company: 'Acme',
173
+ title: 'Developer',
174
+ })
175
+
176
+ const body = parseFetchJsonBody(mockFetch) as { properties: Record<string, string> }
177
+ expect(body.properties.firstname).toBe('John')
178
+ expect(body.properties.lastname).toBe('Doe')
179
+ expect(body.properties.email).toBe('john@example.com')
180
+ expect(body.properties.phone).toBe('+1234567890')
181
+ expect(body.properties.company).toBe('Acme')
182
+ expect(body.properties.jobtitle).toBe('Developer')
183
+ })
184
+
185
+ it('should include custom fields in request', async () => {
186
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.contact('123')))
187
+
188
+ await provider.createContact({
189
+ firstName: 'John',
190
+ lastName: 'Doe',
191
+ customFields: {
192
+ custom_field_1: 'value1',
193
+ custom_field_2: 123,
194
+ },
195
+ })
196
+
197
+ const body = parseFetchJsonBody(mockFetch) as { properties: Record<string, string | number> }
198
+ expect(body.properties.custom_field_1).toBe('value1')
199
+ // The provider converts numbers to strings
200
+ expect(String(body.properties.custom_field_2)).toBe('123')
201
+ })
202
+
203
+ it('should use POST method and correct endpoint', async () => {
204
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.contact('123')))
205
+
206
+ await provider.createContact({ firstName: 'John', lastName: 'Doe' })
207
+
208
+ const { url, options } = getLastFetchCall(mockFetch)
209
+ expect(url).toContain('/objects/contacts')
210
+ expect(options?.method).toBe('POST')
211
+ })
212
+
213
+ it('should throw error on API failure', async () => {
214
+ mockFetch.mockResolvedValueOnce(mockErrorResponse({ message: 'Validation error' }, 400))
215
+
216
+ await expect(provider.createContact({ firstName: 'John', lastName: 'Doe' })).rejects.toThrow(
217
+ 'Failed to create contact: Validation error'
218
+ )
219
+ })
220
+ })
221
+
222
+ describe('getContact', () => {
223
+ beforeEach(async () => {
224
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
225
+ await provider.initialize({ accessToken: 'test-token' })
226
+ })
227
+
228
+ it('should retrieve contact by ID', async () => {
229
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.contact('123')))
230
+
231
+ const contact = await provider.getContact('123')
232
+
233
+ expect(contact).not.toBeNull()
234
+ expect(contact?.id).toBe('123')
235
+ expect(contact?.firstName).toBe('John')
236
+ expect(contact?.lastName).toBe('Doe')
237
+ })
238
+
239
+ it('should return null for non-existent contact', async () => {
240
+ mockFetch.mockResolvedValueOnce(mockErrorResponse({ message: 'Not found' }, 404))
241
+
242
+ const contact = await provider.getContact('nonexistent')
243
+
244
+ expect(contact).toBeNull()
245
+ })
246
+
247
+ it('should parse dates correctly', async () => {
248
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.contact('123')))
249
+
250
+ const contact = await provider.getContact('123')
251
+
252
+ expect(contact?.createdAt).toBeInstanceOf(Date)
253
+ expect(contact?.updatedAt).toBeInstanceOf(Date)
254
+ })
255
+ })
256
+
257
+ describe('updateContact', () => {
258
+ beforeEach(async () => {
259
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
260
+ await provider.initialize({ accessToken: 'test-token' })
261
+ })
262
+
263
+ it('should update contact with partial data', async () => {
264
+ const updatedContact = hubspotMocks.contact('123', { email: 'newemail@example.com' })
265
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(updatedContact))
266
+
267
+ const result = await provider.updateContact('123', { email: 'newemail@example.com' })
268
+
269
+ expect(result.id).toBe('123')
270
+ const body = parseFetchJsonBody(mockFetch) as { properties: Record<string, string> }
271
+ expect(body.properties.email).toBe('newemail@example.com')
272
+ })
273
+
274
+ it('should use PATCH method', async () => {
275
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.contact('123')))
276
+
277
+ await provider.updateContact('123', { firstName: 'Jane' })
278
+
279
+ const { options } = getLastFetchCall(mockFetch)
280
+ expect(options?.method).toBe('PATCH')
281
+ })
282
+ })
283
+
284
+ describe('listContacts', () => {
285
+ beforeEach(async () => {
286
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
287
+ await provider.initialize({ accessToken: 'test-token' })
288
+ })
289
+
290
+ it('should return paginated contacts', async () => {
291
+ const contacts = [hubspotMocks.contact('1'), hubspotMocks.contact('2')]
292
+ mockFetch.mockResolvedValueOnce(
293
+ mockJsonResponse(hubspotMocks.listResponse(contacts, true, 'cursor123'))
294
+ )
295
+
296
+ const result = await provider.listContacts({ limit: 10 })
297
+
298
+ expect(result.items).toHaveLength(2)
299
+ expect(result.hasMore).toBe(true)
300
+ expect(result.nextCursor).toBe('cursor123')
301
+ })
302
+
303
+ it('should apply pagination options', async () => {
304
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.listResponse([])))
305
+
306
+ await provider.listContacts({ limit: 50, cursor: 'page2' })
307
+
308
+ const { url } = getLastFetchCall(mockFetch)
309
+ expect(url).toContain('limit=50')
310
+ expect(url).toContain('after=page2')
311
+ })
312
+ })
313
+
314
+ describe('searchContacts', () => {
315
+ beforeEach(async () => {
316
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
317
+ await provider.initialize({ accessToken: 'test-token' })
318
+ })
319
+
320
+ it('should search contacts by query', async () => {
321
+ const contacts = [hubspotMocks.contact('1')]
322
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.listResponse(contacts)))
323
+
324
+ const result = await provider.searchContacts!('john')
325
+
326
+ expect(result).toHaveLength(1)
327
+ const body = parseFetchJsonBody(mockFetch) as { filterGroups: unknown[] }
328
+ expect(body.filterGroups).toBeDefined()
329
+ })
330
+ })
331
+
332
+ // ===========================================================================
333
+ // Deal Operations Tests
334
+ // ===========================================================================
335
+
336
+ describe('createDeal', () => {
337
+ beforeEach(async () => {
338
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
339
+ await provider.initialize({ accessToken: 'test-token' })
340
+ })
341
+
342
+ it('should create deal with required fields', async () => {
343
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.deal('deal-123')))
344
+
345
+ const result = await provider.createDeal({
346
+ name: 'New Deal',
347
+ stage: 'negotiation',
348
+ })
349
+
350
+ expect(result.id).toBe('deal-123')
351
+ expect(result.name).toBe('New Deal')
352
+ expect(result.stage).toBe('negotiation')
353
+ })
354
+
355
+ it('should format deal properties correctly', async () => {
356
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.deal('deal-123')))
357
+
358
+ await provider.createDeal({
359
+ name: 'Big Deal',
360
+ stage: 'qualified',
361
+ value: 50000,
362
+ currency: 'USD',
363
+ probability: 0.75,
364
+ closeDate: new Date('2024-12-31'),
365
+ })
366
+
367
+ const body = parseFetchJsonBody(mockFetch) as { properties: Record<string, string> }
368
+ expect(body.properties.dealname).toBe('Big Deal')
369
+ expect(body.properties.dealstage).toBe('qualified')
370
+ expect(body.properties.amount).toBe('50000')
371
+ expect(body.properties.deal_currency_code).toBe('USD')
372
+ expect(body.properties.hs_deal_stage_probability).toBe('0.75')
373
+ })
374
+
375
+ it('should associate deal with contact when contactId provided', async () => {
376
+ mockFetch
377
+ .mockResolvedValueOnce(mockJsonResponse(hubspotMocks.deal('deal-123')))
378
+ .mockResolvedValueOnce(mockJsonResponse({})) // association call
379
+
380
+ await provider.createDeal({
381
+ name: 'Deal',
382
+ stage: 'negotiation',
383
+ contactId: 'contact-456',
384
+ })
385
+
386
+ expect(mockFetch).toHaveBeenCalledTimes(2)
387
+ const { url } = getFetchCall(mockFetch, 1)
388
+ expect(url).toContain('associations/contacts/contact-456')
389
+ })
390
+ })
391
+
392
+ describe('getDeal', () => {
393
+ beforeEach(async () => {
394
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
395
+ await provider.initialize({ accessToken: 'test-token' })
396
+ })
397
+
398
+ it('should retrieve deal by ID', async () => {
399
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.deal('deal-123')))
400
+
401
+ const deal = await provider.getDeal('deal-123')
402
+
403
+ expect(deal).not.toBeNull()
404
+ expect(deal?.id).toBe('deal-123')
405
+ expect(deal?.value).toBe(10000)
406
+ })
407
+
408
+ it('should return null for non-existent deal', async () => {
409
+ mockFetch.mockResolvedValueOnce(mockErrorResponse({ message: 'Not found' }, 404))
410
+
411
+ const deal = await provider.getDeal('nonexistent')
412
+
413
+ expect(deal).toBeNull()
414
+ })
415
+ })
416
+
417
+ describe('listDeals', () => {
418
+ beforeEach(async () => {
419
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
420
+ await provider.initialize({ accessToken: 'test-token' })
421
+ })
422
+
423
+ it('should return paginated deals', async () => {
424
+ const deals = [hubspotMocks.deal('1'), hubspotMocks.deal('2')]
425
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.listResponse(deals)))
426
+
427
+ const result = await provider.listDeals()
428
+
429
+ expect(result.items).toHaveLength(2)
430
+ })
431
+ })
432
+
433
+ // ===========================================================================
434
+ // Activity Operations Tests
435
+ // ===========================================================================
436
+
437
+ describe('logActivity', () => {
438
+ beforeEach(async () => {
439
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
440
+ await provider.initialize({ accessToken: 'test-token' })
441
+ })
442
+
443
+ it('should log activity for contact', async () => {
444
+ mockFetch
445
+ .mockResolvedValueOnce(mockJsonResponse(hubspotMocks.engagement('eng-123', 'note')))
446
+ .mockResolvedValueOnce(mockJsonResponse({})) // association call
447
+
448
+ const result = await provider.logActivity!('contact-456', {
449
+ type: 'note',
450
+ subject: 'Follow up',
451
+ body: 'Called about proposal',
452
+ })
453
+
454
+ expect(result.id).toBe('eng-123')
455
+ expect(result.type).toBe('note')
456
+ expect(result.contactId).toBe('contact-456')
457
+ })
458
+
459
+ it('should map activity types correctly', async () => {
460
+ mockFetch
461
+ .mockResolvedValueOnce(mockJsonResponse(hubspotMocks.engagement('eng-123', 'email')))
462
+ .mockResolvedValueOnce(mockJsonResponse({}))
463
+
464
+ await provider.logActivity!('contact-456', {
465
+ type: 'email',
466
+ subject: 'Email Subject',
467
+ body: 'Email body content',
468
+ })
469
+
470
+ // Get the first call (engagement creation)
471
+ const { options } = getFetchCall(mockFetch, 0)
472
+ const body = JSON.parse(options?.body as string) as { properties: Record<string, string> }
473
+ expect(body.properties.hs_engagement_type).toBe('email')
474
+ expect(body.properties.hs_email_text).toBe('Email body content')
475
+ })
476
+ })
477
+
478
+ describe('listActivities', () => {
479
+ beforeEach(async () => {
480
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
481
+ await provider.initialize({ accessToken: 'test-token' })
482
+ })
483
+
484
+ it('should return empty array when no activities', async () => {
485
+ mockFetch.mockResolvedValueOnce(mockJsonResponse({ results: [] }))
486
+
487
+ const activities = await provider.listActivities!('contact-123')
488
+
489
+ expect(activities).toEqual([])
490
+ })
491
+
492
+ it('should fetch and return activities for contact', async () => {
493
+ mockFetch
494
+ .mockResolvedValueOnce(mockJsonResponse({ results: [{ id: 'eng-1' }, { id: 'eng-2' }] }))
495
+ .mockResolvedValueOnce(mockJsonResponse(hubspotMocks.engagement('eng-1', 'note')))
496
+ .mockResolvedValueOnce(mockJsonResponse(hubspotMocks.engagement('eng-2', 'call')))
497
+
498
+ const activities = await provider.listActivities!('contact-123')
499
+
500
+ expect(activities).toHaveLength(2)
501
+ })
502
+ })
503
+
504
+ // ===========================================================================
505
+ // API Request Formatting Tests
506
+ // ===========================================================================
507
+
508
+ describe('API request formatting', () => {
509
+ beforeEach(async () => {
510
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
511
+ await provider.initialize({ accessToken: 'test-token' })
512
+ })
513
+
514
+ it('should include correct Content-Type header', async () => {
515
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.contact('123')))
516
+
517
+ await provider.createContact({ firstName: 'John', lastName: 'Doe' })
518
+
519
+ const { options } = getLastFetchCall(mockFetch)
520
+ expect(options?.headers).toHaveProperty('Content-Type', 'application/json')
521
+ })
522
+
523
+ it('should include Bearer token in Authorization header', async () => {
524
+ mockFetch.mockResolvedValueOnce(mockJsonResponse(hubspotMocks.contact('123')))
525
+
526
+ await provider.getContact('123')
527
+
528
+ const { options } = getLastFetchCall(mockFetch)
529
+ expect(options?.headers).toHaveProperty('Authorization', 'Bearer test-token')
530
+ })
531
+ })
532
+
533
+ // ===========================================================================
534
+ // Error Handling Tests
535
+ // ===========================================================================
536
+
537
+ describe('error handling', () => {
538
+ beforeEach(async () => {
539
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
540
+ await provider.initialize({ accessToken: 'test-token' })
541
+ })
542
+
543
+ it('should handle rate limit errors', async () => {
544
+ mockFetch.mockResolvedValueOnce(mockErrorResponse({ message: 'Rate limit exceeded' }, 429))
545
+
546
+ await expect(provider.createContact({ firstName: 'John', lastName: 'Doe' })).rejects.toThrow(
547
+ 'Failed to create contact'
548
+ )
549
+ })
550
+
551
+ it('should handle authentication errors', async () => {
552
+ mockFetch.mockResolvedValueOnce(mockErrorResponse({ message: 'Invalid API key' }, 401))
553
+
554
+ await expect(provider.getContact('123')).rejects.toThrow('Failed to get contact')
555
+ })
556
+
557
+ it('should handle network errors gracefully', async () => {
558
+ mockFetch.mockRejectedValueOnce(new Error('ECONNREFUSED'))
559
+
560
+ await expect(provider.listContacts()).rejects.toThrow('Failed to list contacts')
561
+ })
562
+ })
563
+
564
+ // ===========================================================================
565
+ // Dispose Tests
566
+ // ===========================================================================
567
+
568
+ describe('dispose', () => {
569
+ it('should dispose without error', async () => {
570
+ provider = createHubSpotProvider({ accessToken: 'test-token' })
571
+ await provider.initialize({ accessToken: 'test-token' })
572
+
573
+ await expect(provider.dispose()).resolves.toBeUndefined()
574
+ })
575
+ })
576
+ })