@reactionary/source 0.0.52 → 0.2.16

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/.env-template +19 -0
  2. package/.github/workflows/pull-request.yml +3 -1
  3. package/.github/workflows/release.yml +9 -0
  4. package/.vscode/extensions.json +0 -2
  5. package/LICENSE +21 -0
  6. package/README.md +175 -23
  7. package/core/package.json +6 -3
  8. package/core/src/cache/cache.interface.ts +1 -0
  9. package/core/src/cache/index.ts +4 -0
  10. package/core/src/cache/memory-cache.ts +30 -2
  11. package/core/src/cache/noop-cache.ts +15 -1
  12. package/core/src/cache/redis-cache.ts +20 -0
  13. package/core/src/client/client-builder.ts +71 -54
  14. package/core/src/client/client.ts +9 -47
  15. package/core/src/client/index.ts +2 -0
  16. package/core/src/decorators/index.ts +1 -0
  17. package/core/src/decorators/reactionary.decorator.ts +203 -34
  18. package/core/src/index.ts +6 -19
  19. package/core/src/initialization.ts +1 -18
  20. package/core/src/metrics/metrics.ts +67 -0
  21. package/core/src/providers/analytics.provider.ts +1 -6
  22. package/core/src/providers/base.provider.ts +5 -69
  23. package/core/src/providers/cart.provider.ts +15 -55
  24. package/core/src/providers/category.provider.ts +7 -11
  25. package/core/src/providers/checkout.provider.ts +17 -15
  26. package/core/src/providers/identity.provider.ts +6 -8
  27. package/core/src/providers/index.ts +2 -1
  28. package/core/src/providers/inventory.provider.ts +15 -5
  29. package/core/src/providers/order-search.provider.ts +29 -0
  30. package/core/src/providers/order.provider.ts +47 -15
  31. package/core/src/providers/price.provider.ts +30 -36
  32. package/core/src/providers/product-search.provider.ts +61 -0
  33. package/core/src/providers/product.provider.ts +71 -12
  34. package/core/src/providers/profile.provider.ts +74 -14
  35. package/core/src/providers/store.provider.ts +3 -5
  36. package/core/src/schemas/capabilities.schema.ts +10 -3
  37. package/core/src/schemas/errors/generic.error.ts +9 -0
  38. package/core/src/schemas/errors/index.ts +4 -0
  39. package/core/src/schemas/errors/invalid-input.error.ts +9 -0
  40. package/core/src/schemas/errors/invalid-output.error.ts +9 -0
  41. package/core/src/schemas/errors/not-found.error.ts +9 -0
  42. package/core/src/schemas/index.ts +7 -0
  43. package/core/src/schemas/models/analytics.model.ts +2 -1
  44. package/core/src/schemas/models/base.model.ts +6 -24
  45. package/core/src/schemas/models/cart.model.ts +5 -8
  46. package/core/src/schemas/models/category.model.ts +4 -9
  47. package/core/src/schemas/models/checkout.model.ts +6 -7
  48. package/core/src/schemas/models/cost.model.ts +4 -3
  49. package/core/src/schemas/models/currency.model.ts +2 -1
  50. package/core/src/schemas/models/identifiers.model.ts +106 -62
  51. package/core/src/schemas/models/identity.model.ts +10 -19
  52. package/core/src/schemas/models/index.ts +2 -1
  53. package/core/src/schemas/models/inventory.model.ts +8 -5
  54. package/core/src/schemas/models/order-search.model.ts +28 -0
  55. package/core/src/schemas/models/order.model.ts +20 -26
  56. package/core/src/schemas/models/payment.model.ts +14 -17
  57. package/core/src/schemas/models/price.model.ts +11 -11
  58. package/core/src/schemas/models/product-search.model.ts +42 -0
  59. package/core/src/schemas/models/product.model.ts +64 -22
  60. package/core/src/schemas/models/profile.model.ts +19 -22
  61. package/core/src/schemas/models/shipping-method.model.ts +24 -29
  62. package/core/src/schemas/models/store.model.ts +9 -5
  63. package/core/src/schemas/mutations/analytics.mutation.ts +8 -7
  64. package/core/src/schemas/mutations/base.mutation.ts +2 -1
  65. package/core/src/schemas/mutations/cart.mutation.ts +33 -33
  66. package/core/src/schemas/mutations/checkout.mutation.ts +23 -30
  67. package/core/src/schemas/mutations/identity.mutation.ts +4 -3
  68. package/core/src/schemas/mutations/profile.mutation.ts +38 -3
  69. package/core/src/schemas/queries/base.query.ts +2 -1
  70. package/core/src/schemas/queries/cart.query.ts +3 -3
  71. package/core/src/schemas/queries/category.query.ts +18 -18
  72. package/core/src/schemas/queries/checkout.query.ts +7 -9
  73. package/core/src/schemas/queries/identity.query.ts +2 -1
  74. package/core/src/schemas/queries/index.ts +2 -1
  75. package/core/src/schemas/queries/inventory.query.ts +5 -5
  76. package/core/src/schemas/queries/order-search.query.ts +10 -0
  77. package/core/src/schemas/queries/order.query.ts +3 -2
  78. package/core/src/schemas/queries/price.query.ts +10 -4
  79. package/core/src/schemas/queries/product-search.query.ts +16 -0
  80. package/core/src/schemas/queries/product.query.ts +13 -6
  81. package/core/src/schemas/queries/profile.query.ts +5 -2
  82. package/core/src/schemas/queries/store.query.ts +6 -5
  83. package/core/src/schemas/result.ts +107 -0
  84. package/core/src/schemas/session.schema.ts +4 -4
  85. package/core/src/test/reactionary.decorator.spec.ts +249 -0
  86. package/core/src/zod-utils.ts +19 -0
  87. package/core/tsconfig.json +1 -1
  88. package/core/tsconfig.spec.json +2 -26
  89. package/core/vitest.config.ts +14 -0
  90. package/documentation/1-purpose.md +114 -0
  91. package/documentation/2-getting-started.md +229 -0
  92. package/documentation/3-querying-and-changing-data.md +74 -0
  93. package/documentation/4-product-data.md +107 -0
  94. package/documentation/5-cart-and-checkout.md +211 -0
  95. package/documentation/6-product-search.md +143 -0
  96. package/documentation/7-marketing.md +3 -0
  97. package/eslint.config.mjs +1 -0
  98. package/examples/node/eslint.config.mjs +1 -4
  99. package/examples/node/package.json +10 -3
  100. package/examples/node/project.json +4 -1
  101. package/examples/node/src/basic/basic-node-provider-model-extension.spec.ts +22 -23
  102. package/examples/node/src/basic/basic-node-provider-query-extension.spec.ts +15 -11
  103. package/examples/node/src/basic/basic-node-setup.spec.ts +44 -28
  104. package/examples/node/src/basic/client-creation.spec.ts +53 -0
  105. package/examples/node/src/capabilities/cart.spec.ts +255 -0
  106. package/examples/node/src/capabilities/category.spec.ts +193 -0
  107. package/examples/node/src/capabilities/checkout.spec.ts +341 -0
  108. package/examples/node/src/capabilities/identity.spec.ts +93 -0
  109. package/examples/node/src/capabilities/inventory.spec.ts +66 -0
  110. package/examples/node/src/capabilities/order-search.spec.ts +265 -0
  111. package/examples/node/src/capabilities/order.spec.ts +91 -0
  112. package/examples/node/src/capabilities/price.spec.ts +51 -0
  113. package/examples/node/src/capabilities/product-search.spec.ts +293 -0
  114. package/examples/node/src/capabilities/product.spec.ts +122 -0
  115. package/examples/node/src/capabilities/profile.spec.ts +316 -0
  116. package/examples/node/src/capabilities/store.spec.ts +26 -0
  117. package/examples/node/src/utils.ts +147 -0
  118. package/examples/node/tsconfig.json +9 -12
  119. package/examples/node/tsconfig.lib.json +1 -2
  120. package/examples/node/tsconfig.spec.json +2 -14
  121. package/examples/node/vitest.config.ts +14 -0
  122. package/migrations.json +22 -5
  123. package/nx.json +8 -47
  124. package/package.json +24 -96
  125. package/providers/algolia/README.md +39 -2
  126. package/providers/algolia/package.json +2 -1
  127. package/providers/algolia/src/core/initialize.ts +7 -14
  128. package/providers/algolia/src/index.ts +2 -4
  129. package/providers/algolia/src/providers/index.ts +1 -0
  130. package/providers/algolia/src/providers/product-search.provider.ts +241 -0
  131. package/providers/algolia/src/schema/capabilities.schema.ts +2 -3
  132. package/providers/algolia/src/schema/index.ts +3 -0
  133. package/providers/algolia/src/schema/search.schema.ts +8 -8
  134. package/providers/algolia/tsconfig.json +1 -1
  135. package/providers/algolia/tsconfig.lib.json +1 -1
  136. package/providers/algolia/tsconfig.spec.json +2 -14
  137. package/providers/algolia/vitest.config.ts +14 -0
  138. package/providers/commercetools/README.md +30 -3
  139. package/providers/commercetools/package.json +2 -1
  140. package/providers/commercetools/src/core/client.ts +178 -99
  141. package/providers/commercetools/src/core/initialize.ts +130 -74
  142. package/providers/commercetools/src/core/token-cache.ts +45 -0
  143. package/providers/commercetools/src/index.ts +3 -2
  144. package/providers/commercetools/src/providers/cart.provider.ts +281 -341
  145. package/providers/commercetools/src/providers/category.provider.ts +223 -138
  146. package/providers/commercetools/src/providers/checkout.provider.ts +631 -449
  147. package/providers/commercetools/src/providers/identity.provider.ts +50 -29
  148. package/providers/commercetools/src/providers/index.ts +2 -2
  149. package/providers/commercetools/src/providers/inventory.provider.ts +76 -74
  150. package/providers/commercetools/src/providers/order-search.provider.ts +220 -0
  151. package/providers/commercetools/src/providers/order.provider.ts +96 -61
  152. package/providers/commercetools/src/providers/price.provider.ts +147 -117
  153. package/providers/commercetools/src/providers/product-search.provider.ts +528 -0
  154. package/providers/commercetools/src/providers/product.provider.ts +249 -74
  155. package/providers/commercetools/src/providers/profile.provider.ts +445 -28
  156. package/providers/commercetools/src/providers/store.provider.ts +54 -40
  157. package/providers/commercetools/src/schema/capabilities.schema.ts +3 -1
  158. package/providers/commercetools/src/schema/commercetools.schema.ts +17 -3
  159. package/providers/commercetools/src/schema/configuration.schema.ts +1 -0
  160. package/providers/commercetools/src/schema/session.schema.ts +7 -0
  161. package/providers/commercetools/src/test/caching.spec.ts +82 -0
  162. package/providers/commercetools/src/test/identity.spec.ts +109 -0
  163. package/providers/commercetools/src/test/test-utils.ts +21 -19
  164. package/providers/commercetools/tsconfig.json +1 -1
  165. package/providers/commercetools/tsconfig.lib.json +1 -1
  166. package/providers/commercetools/tsconfig.spec.json +2 -14
  167. package/providers/commercetools/vitest.config.ts +15 -0
  168. package/providers/fake/README.md +20 -4
  169. package/providers/fake/package.json +2 -1
  170. package/providers/fake/src/core/initialize.ts +47 -49
  171. package/providers/fake/src/providers/analytics.provider.ts +5 -7
  172. package/providers/fake/src/providers/cart.provider.ts +163 -92
  173. package/providers/fake/src/providers/category.provider.ts +78 -50
  174. package/providers/fake/src/providers/checkout.provider.ts +254 -0
  175. package/providers/fake/src/providers/identity.provider.ts +57 -65
  176. package/providers/fake/src/providers/index.ts +6 -2
  177. package/providers/fake/src/providers/inventory.provider.ts +40 -36
  178. package/providers/fake/src/providers/order-search.provider.ts +78 -0
  179. package/providers/fake/src/providers/order.provider.ts +106 -0
  180. package/providers/fake/src/providers/price.provider.ts +93 -41
  181. package/providers/fake/src/providers/product-search.provider.ts +206 -0
  182. package/providers/fake/src/providers/product.provider.ts +56 -41
  183. package/providers/fake/src/providers/profile.provider.ts +147 -0
  184. package/providers/fake/src/providers/store.provider.ts +30 -20
  185. package/providers/fake/src/schema/capabilities.schema.ts +5 -1
  186. package/providers/fake/src/test/cart.provider.spec.ts +59 -80
  187. package/providers/fake/src/test/category.provider.spec.ts +145 -87
  188. package/providers/fake/src/test/checkout.provider.spec.ts +222 -0
  189. package/providers/fake/src/test/order-search.provider.spec.ts +50 -0
  190. package/providers/fake/src/test/order.provider.spec.ts +44 -0
  191. package/providers/fake/src/test/price.provider.spec.ts +50 -45
  192. package/providers/fake/src/test/product.provider.spec.ts +15 -7
  193. package/providers/fake/src/test/profile.provider.spec.ts +167 -0
  194. package/providers/fake/tsconfig.json +1 -1
  195. package/providers/fake/tsconfig.lib.json +1 -1
  196. package/providers/fake/tsconfig.spec.json +2 -12
  197. package/providers/fake/vitest.config.ts +14 -0
  198. package/providers/medusa/README.md +30 -0
  199. package/providers/medusa/TESTING.md +98 -0
  200. package/providers/medusa/eslint.config.mjs +19 -0
  201. package/providers/medusa/package.json +22 -0
  202. package/providers/medusa/project.json +34 -0
  203. package/providers/medusa/src/core/client.ts +370 -0
  204. package/providers/medusa/src/core/initialize.ts +78 -0
  205. package/providers/medusa/src/index.ts +13 -0
  206. package/providers/medusa/src/providers/cart.provider.ts +575 -0
  207. package/providers/medusa/src/providers/category.provider.ts +247 -0
  208. package/providers/medusa/src/providers/checkout.provider.ts +636 -0
  209. package/providers/medusa/src/providers/identity.provider.ts +137 -0
  210. package/providers/medusa/src/providers/inventory.provider.ts +173 -0
  211. package/providers/medusa/src/providers/order-search.provider.ts +202 -0
  212. package/providers/medusa/src/providers/order.provider.ts +226 -0
  213. package/providers/medusa/src/providers/price.provider.ts +140 -0
  214. package/providers/medusa/src/providers/product-search.provider.ts +243 -0
  215. package/providers/medusa/src/providers/product.provider.ts +261 -0
  216. package/providers/medusa/src/providers/profile.provider.ts +392 -0
  217. package/providers/medusa/src/schema/capabilities.schema.ts +18 -0
  218. package/providers/medusa/src/schema/configuration.schema.ts +11 -0
  219. package/providers/medusa/src/schema/medusa.schema.ts +31 -0
  220. package/providers/medusa/src/test/cart.provider.spec.ts +240 -0
  221. package/providers/medusa/src/test/category.provider.spec.ts +231 -0
  222. package/providers/medusa/src/test/checkout.spec.ts +349 -0
  223. package/providers/medusa/src/test/identity.provider.spec.ts +122 -0
  224. package/providers/medusa/src/test/inventory.provider.spec.ts +88 -0
  225. package/providers/medusa/src/test/large-cart.provider.spec.ts +103 -0
  226. package/providers/medusa/src/test/price.provider.spec.ts +104 -0
  227. package/providers/medusa/src/test/product.provider.spec.ts +146 -0
  228. package/providers/medusa/src/test/search.provider.spec.ts +203 -0
  229. package/providers/medusa/src/test/test-utils.ts +13 -0
  230. package/providers/medusa/src/utils/medusa-helpers.ts +89 -0
  231. package/providers/medusa/tsconfig.json +21 -0
  232. package/providers/medusa/tsconfig.lib.json +9 -0
  233. package/providers/medusa/tsconfig.spec.json +4 -0
  234. package/providers/medusa/vitest.config.ts +15 -0
  235. package/providers/meilisearch/README.md +48 -0
  236. package/providers/meilisearch/eslint.config.mjs +22 -0
  237. package/providers/meilisearch/package.json +13 -0
  238. package/providers/meilisearch/project.json +34 -0
  239. package/providers/meilisearch/src/core/initialize.ts +21 -0
  240. package/providers/meilisearch/src/index.ts +6 -0
  241. package/providers/meilisearch/src/providers/index.ts +1 -0
  242. package/providers/meilisearch/src/providers/order-search.provider.ts +222 -0
  243. package/providers/meilisearch/src/providers/product-search.provider.ts +251 -0
  244. package/providers/meilisearch/src/schema/capabilities.schema.ts +10 -0
  245. package/providers/meilisearch/src/schema/configuration.schema.ts +11 -0
  246. package/providers/meilisearch/src/schema/index.ts +3 -0
  247. package/providers/meilisearch/src/schema/search.schema.ts +14 -0
  248. package/providers/meilisearch/tsconfig.json +24 -0
  249. package/providers/meilisearch/tsconfig.lib.json +10 -0
  250. package/providers/meilisearch/tsconfig.spec.json +4 -0
  251. package/providers/meilisearch/vitest.config.ts +14 -0
  252. package/providers/posthog/package.json +2 -1
  253. package/providers/posthog/tsconfig.json +1 -1
  254. package/tsconfig.base.json +5 -0
  255. package/vitest.config.ts +10 -0
  256. package/core/src/providers/search.provider.ts +0 -18
  257. package/core/src/schemas/models/search.model.ts +0 -36
  258. package/core/src/schemas/queries/search.query.ts +0 -9
  259. package/examples/next/.swcrc +0 -30
  260. package/examples/next/eslint.config.mjs +0 -21
  261. package/examples/next/index.d.ts +0 -6
  262. package/examples/next/next-env.d.ts +0 -5
  263. package/examples/next/next.config.js +0 -31
  264. package/examples/next/project.json +0 -9
  265. package/examples/next/public/.gitkeep +0 -0
  266. package/examples/next/public/favicon.ico +0 -0
  267. package/examples/next/src/app/global.css +0 -0
  268. package/examples/next/src/app/layout.tsx +0 -18
  269. package/examples/next/src/app/page.module.scss +0 -2
  270. package/examples/next/src/app/page.tsx +0 -47
  271. package/examples/next/src/instrumentation.ts +0 -9
  272. package/examples/next/tsconfig.json +0 -44
  273. package/examples/node/jest.config.ts +0 -10
  274. package/jest.config.ts +0 -6
  275. package/jest.preset.js +0 -3
  276. package/providers/algolia/jest.config.ts +0 -10
  277. package/providers/algolia/src/providers/product.provider.ts +0 -66
  278. package/providers/algolia/src/providers/search.provider.ts +0 -106
  279. package/providers/algolia/src/test/search.provider.spec.ts +0 -91
  280. package/providers/commercetools/jest.config.cjs +0 -10
  281. package/providers/commercetools/src/providers/search.provider.ts +0 -96
  282. package/providers/commercetools/src/test/cart.provider.spec.ts +0 -199
  283. package/providers/commercetools/src/test/category.provider.spec.ts +0 -168
  284. package/providers/commercetools/src/test/checkout.provider.spec.ts +0 -312
  285. package/providers/commercetools/src/test/identity.provider.spec.ts +0 -88
  286. package/providers/commercetools/src/test/inventory.provider.spec.ts +0 -41
  287. package/providers/commercetools/src/test/price.provider.spec.ts +0 -81
  288. package/providers/commercetools/src/test/product.provider.spec.ts +0 -80
  289. package/providers/commercetools/src/test/profile.provider.spec.ts +0 -49
  290. package/providers/commercetools/src/test/search.provider.spec.ts +0 -61
  291. package/providers/commercetools/src/test/store.provider.spec.ts +0 -37
  292. package/providers/fake/jest.config.cjs +0 -10
  293. package/providers/fake/src/providers/search.provider.ts +0 -132
@@ -0,0 +1,341 @@
1
+ import 'dotenv/config';
2
+ import type { Cart, Checkout, PaymentInstruction } from '@reactionary/core';
3
+ import {
4
+ PaymentInstructionSchema,
5
+ ShippingInstructionSchema,
6
+ } from '@reactionary/core';
7
+ import { describe, expect, it, beforeEach, assert } from 'vitest';
8
+ import { createClient, PrimaryProvider } from '../utils.js';
9
+
10
+ const testData = {
11
+ skuWithoutTiers: '0766623301831',
12
+ skuWithTiers: '0766623360203',
13
+ };
14
+
15
+ describe.each([PrimaryProvider.COMMERCETOOLS])(
16
+ 'Checkout Capability - %s',
17
+ (provider) => {
18
+ let client: ReturnType<typeof createClient>;
19
+
20
+ beforeEach(() => {
21
+ client = createClient(provider);
22
+ });
23
+
24
+ describe('anonymous sessions', () => {
25
+ let cart: Cart;
26
+
27
+ beforeEach(async () => {
28
+ const c = await client.cart.add({
29
+ variant: {
30
+ sku: testData.skuWithoutTiers,
31
+ },
32
+ quantity: 1,
33
+ });
34
+
35
+ if (!c.success) {
36
+ assert.fail();
37
+ }
38
+
39
+ cart = c.value;
40
+ });
41
+
42
+ it('can create a checkout session from a cart', async () => {
43
+ // we have either an anonymous user, or an authenticated user.
44
+ // if it is anonymous, we assume you will have collected some basic info by now ?
45
+
46
+ const checkout = await client.checkout.initiateCheckoutForCart({
47
+ cart: cart,
48
+ billingAddress: {
49
+ countryCode: 'US',
50
+ firstName: 'John',
51
+ lastName: 'Doe',
52
+ streetAddress: '123 Main St',
53
+ streetNumber: '1A',
54
+ postalCode: '12345',
55
+ city: 'Anytown',
56
+ region: '',
57
+ },
58
+ notificationEmail: 'sample@example.com',
59
+ notificationPhone: '+4512345678',
60
+ });
61
+
62
+ if (!checkout.success) {
63
+ assert.fail();
64
+ }
65
+
66
+ expect(checkout.value.identifier.key).toBeDefined();
67
+ expect(checkout.value.originalCartReference.key).toBe(
68
+ cart.identifier.key
69
+ );
70
+ expect(checkout.value.billingAddress?.firstName).toBe('John');
71
+ expect(checkout.value.items.length).toBe(1);
72
+ expect(checkout.value.items[0].variant.sku).toBe(
73
+ testData.skuWithoutTiers
74
+ );
75
+ });
76
+
77
+ describe('checkout actions', () => {
78
+ let checkout: Checkout;
79
+ beforeEach(async () => {
80
+ const cc = await client.checkout.initiateCheckoutForCart({
81
+ cart: cart,
82
+ billingAddress: {
83
+ countryCode: 'US',
84
+ firstName: 'John',
85
+ lastName: 'Doe',
86
+ streetAddress: '123 Main St',
87
+ streetNumber: '1A',
88
+ postalCode: '12345',
89
+ city: 'Anytown',
90
+ region: '',
91
+ },
92
+ notificationEmail: 'sample@example.com',
93
+ notificationPhone: '+4512345678',
94
+ });
95
+
96
+ if (!cc.success) {
97
+ console.log(cc);
98
+
99
+ assert.fail();
100
+ }
101
+
102
+ checkout = cc.value;
103
+ });
104
+
105
+ it('can list payment methods', async () => {
106
+ const paymentMethods =
107
+ await client.checkout.getAvailablePaymentMethods({
108
+ checkout: checkout.identifier,
109
+ });
110
+
111
+ if (!paymentMethods.success) {
112
+ assert.fail();
113
+ }
114
+
115
+ expect(paymentMethods.value.length).toBeGreaterThan(0);
116
+ expect(
117
+ paymentMethods.value.find((x) => x.identifier.method === 'stripe')
118
+ ).toBeDefined();
119
+ });
120
+
121
+ it('can list shipping methods', async () => {
122
+ const shippingMethods =
123
+ await client.checkout.getAvailableShippingMethods({
124
+ checkout: checkout.identifier,
125
+ });
126
+
127
+ if (!shippingMethods.success) {
128
+ assert.fail();
129
+ }
130
+
131
+ expect(shippingMethods.value.length).toBeGreaterThan(0);
132
+ expect(
133
+ shippingMethods.value.find(
134
+ (x) => x.identifier.key === 'us-delivery'
135
+ )
136
+ ).toBeDefined();
137
+ });
138
+
139
+ it('can add a payment instruction', async () => {
140
+ const paymentMethods =
141
+ await client.checkout.getAvailablePaymentMethods({
142
+ checkout: checkout.identifier,
143
+ });
144
+
145
+ if (!paymentMethods.success) {
146
+ assert.fail();
147
+ }
148
+
149
+ const pm = paymentMethods.value.find(
150
+ (x) => x.identifier.method === 'stripe'
151
+ );
152
+ expect(pm).toBeDefined();
153
+
154
+ const checkoutWithPi = await client.checkout.addPaymentInstruction({
155
+ checkout: checkout.identifier,
156
+ paymentInstruction: PaymentInstructionSchema.parse({
157
+ paymentMethod: pm!.identifier,
158
+ amount: checkout.price.grandTotal,
159
+ protocolData: [{ key: 'test-key', value: 'test-value' }],
160
+ identifier: {
161
+ key: 'pm1',
162
+ },
163
+ status: 'pending',
164
+ } satisfies PaymentInstruction),
165
+ });
166
+
167
+ if (!checkoutWithPi.success) {
168
+ assert.fail();
169
+ }
170
+
171
+ expect(checkoutWithPi.value.paymentInstructions.length).toBe(1);
172
+ expect(
173
+ checkoutWithPi.value.paymentInstructions[0].paymentMethod.method
174
+ ).toBe('stripe');
175
+ expect(
176
+ checkoutWithPi.value.paymentInstructions[0].protocolData.find(
177
+ (x) => x.key === 'stripe_clientSecret'
178
+ )?.value
179
+ ).toBeDefined();
180
+ });
181
+
182
+ it.skip('can cancel an in-progress payment', async () => {
183
+ const paymentMethods =
184
+ await client.checkout.getAvailablePaymentMethods({
185
+ checkout: checkout.identifier,
186
+ });
187
+
188
+ if (!paymentMethods.success) {
189
+ assert.fail();
190
+ }
191
+
192
+ const pm = paymentMethods.value.find(
193
+ (x) => x.identifier.method === 'stripe'
194
+ );
195
+ expect(pm).toBeDefined();
196
+
197
+ const checkoutWithPi = await client.checkout.addPaymentInstruction({
198
+ checkout: checkout.identifier,
199
+ paymentInstruction: {
200
+ paymentMethod: pm!.identifier,
201
+ amount: checkout.price.grandTotal,
202
+ protocolData: [{ key: 'test-key', value: 'test-value' }],
203
+ },
204
+ });
205
+
206
+ if (!checkoutWithPi.success) {
207
+ assert.fail();
208
+ }
209
+
210
+ expect(checkoutWithPi.value.paymentInstructions.length).toBe(1);
211
+
212
+ const checkoutAfterCancel =
213
+ await client.checkout.removePaymentInstruction({
214
+ checkout: checkout.identifier,
215
+ paymentInstruction:
216
+ checkoutWithPi.value.paymentInstructions[0].identifier,
217
+ });
218
+
219
+ if (!checkoutAfterCancel.success) {
220
+ assert.fail();
221
+ }
222
+
223
+ expect(checkoutAfterCancel.value.paymentInstructions.length).toBe(0);
224
+ });
225
+
226
+ it('can set shipping address', async () => {
227
+ const checkoutWithShipping = await client.checkout.setShippingAddress(
228
+ {
229
+ checkout: checkout.identifier,
230
+ shippingAddress: {
231
+ countryCode: 'US',
232
+ firstName: 'Jane',
233
+ lastName: 'Doe',
234
+ streetAddress: '456 Other St',
235
+ streetNumber: '2B',
236
+ postalCode: '54321',
237
+ city: 'Othertown',
238
+ region: '',
239
+ },
240
+ }
241
+ );
242
+
243
+ if (!checkoutWithShipping.success) {
244
+ assert.fail();
245
+ }
246
+
247
+ expect(checkoutWithShipping.value.shippingAddress).toBeDefined();
248
+ expect(checkoutWithShipping.value.shippingAddress?.firstName).toBe('Jane');
249
+ });
250
+
251
+ it('can set shipping instructions', async () => {
252
+ const shippingMethods =
253
+ await client.checkout.getAvailableShippingMethods({
254
+ checkout: checkout.identifier,
255
+ });
256
+
257
+ if (!shippingMethods.success) {
258
+ assert.fail();
259
+ }
260
+
261
+ const sm = shippingMethods.value.find(
262
+ (x) => x.identifier.key === 'us-delivery'
263
+ );
264
+ expect(sm).toBeDefined();
265
+
266
+ const shippingInstruction = ShippingInstructionSchema.parse({
267
+ shippingMethod: sm?.identifier || { key: '' },
268
+ amount: checkout.price.totalShipping,
269
+ instructions: 'Leave at front door if not home',
270
+ consentForUnattendedDelivery: true,
271
+ pickupPoint: '4190asx141', // this would be a real pickup point ID in a real scenario
272
+ });
273
+
274
+ const checkoutWithShipping =
275
+ await client.checkout.setShippingInstruction({
276
+ checkout: checkout.identifier,
277
+ shippingInstruction,
278
+ });
279
+
280
+ if (!checkoutWithShipping.success) {
281
+ assert.fail();
282
+ }
283
+
284
+ expect(checkout.price.totalShipping.value).toBe(0);
285
+ expect(
286
+ checkoutWithShipping.value.price.totalShipping.value
287
+ ).toBeGreaterThan(0);
288
+ expect(checkoutWithShipping.value.shippingInstruction).toBeDefined();
289
+ expect(
290
+ checkoutWithShipping.value.shippingInstruction?.shippingMethod.key
291
+ ).toBe('us-delivery');
292
+ expect(checkoutWithShipping.value.shippingInstruction?.instructions).toBe(
293
+ 'Leave at front door if not home'
294
+ );
295
+ expect(checkoutWithShipping.value.shippingInstruction?.pickupPoint).toBe(
296
+ '4190asx141'
297
+ );
298
+ expect(
299
+ checkoutWithShipping.value.shippingInstruction
300
+ ?.consentForUnattendedDelivery
301
+ ).toBe(true);
302
+ });
303
+
304
+ it.skip('wont report it finalizable until everything is paid/authorized', async () => {
305
+ expect(checkout.readyForFinalization).toBe(false);
306
+ const r = await client.checkout.getAvailablePaymentMethods({
307
+ checkout: checkout.identifier,
308
+ });
309
+
310
+ if (!r.success) {
311
+ assert.fail();
312
+ }
313
+
314
+ const pm = r.value.find((x) => x.identifier.method === 'stripe');
315
+
316
+ const checkoutWithPi = await client.checkout.addPaymentInstruction({
317
+ checkout: checkout.identifier,
318
+ paymentInstruction: {
319
+ paymentMethod: pm!.identifier,
320
+ amount: checkout.price.grandTotal,
321
+ protocolData: [{ key: 'test-key', value: 'test-value' }],
322
+ },
323
+ });
324
+
325
+ if (!checkoutWithPi.success) {
326
+ assert.fail();
327
+ }
328
+
329
+ // do something to simulate payment authorization ?
330
+ const checkoutReady = await client.checkout.getById({
331
+ identifier: checkoutWithPi.value.identifier,
332
+ });
333
+ if (!checkoutReady.success) {
334
+ expect.fail('checkout not found');
335
+ }
336
+ expect(checkoutReady.value.readyForFinalization).toBe(true);
337
+ });
338
+ });
339
+ });
340
+ }
341
+ );
@@ -0,0 +1,93 @@
1
+ import 'dotenv/config';
2
+ import { describe, expect, it, beforeEach, assert } from 'vitest';
3
+ import { createClient, PrimaryProvider } from '../utils.js';
4
+
5
+ describe.each([PrimaryProvider.COMMERCETOOLS])('Identity Capability - %s', (provider) => {
6
+ let client: ReturnType<typeof createClient>;
7
+
8
+ beforeEach(() => {
9
+ client = createClient(provider);
10
+ });
11
+
12
+ it('should default to an anonymous identity if no operations have been performed', async () => {
13
+ const identity = await client.identity.getSelf({});
14
+
15
+ if (!identity.success) {
16
+ assert.fail();
17
+ }
18
+
19
+ expect(identity.value.type).toBe('Anonymous');
20
+ });
21
+
22
+ it('should automatically upgrade to guest the moment an operation is performed', async () => {
23
+ const updatedCart = await client.cart.add(
24
+ {
25
+ quantity: 1,
26
+ variant: {
27
+ sku: '0766623301831'
28
+ },
29
+ }
30
+ );
31
+
32
+ const identity = await client.identity.getSelf({});
33
+
34
+ if (!identity.success) {
35
+ assert.fail();
36
+ }
37
+
38
+ expect(identity.value.type).toBe('Guest');
39
+ });
40
+
41
+ it('should be able to register a new customer', async () => {
42
+ const time = new Date().getTime();
43
+ const identity = await client.identity.register(
44
+ {
45
+ username: `test-user+${time}@example.com`,
46
+ password: 'love2test',
47
+ }
48
+ );
49
+
50
+ if (!identity.success) {
51
+ assert.fail();
52
+ }
53
+
54
+ expect(identity.value.type).toBe('Registered');
55
+
56
+ const refreshedIdentity = await client.identity.getSelf({});
57
+
58
+ if (!refreshedIdentity.success) {
59
+ assert.fail();
60
+ }
61
+ expect(refreshedIdentity.value.type).toBe('Registered');
62
+ });
63
+
64
+ it('should be able to log out from a Registered identity', async () => {
65
+ const time = new Date().getTime();
66
+ const identity = await client.identity.register(
67
+ {
68
+ username: `test-user+${time}@example.com`,
69
+ password: 'love2test',
70
+ }
71
+ );
72
+
73
+ if (!identity.success) {
74
+ assert.fail();
75
+ }
76
+
77
+ expect(identity.value.type).toBe('Registered');
78
+
79
+ const loggedOutIdentity = await client.identity.logout({});
80
+
81
+ if (!loggedOutIdentity.success) {
82
+ assert.fail();
83
+ }
84
+ expect(loggedOutIdentity.value.type).toBe('Anonymous');
85
+
86
+ const refreshedIdentity = await client.identity.getSelf({});
87
+
88
+ if (!refreshedIdentity.success) {
89
+ assert.fail();
90
+ }
91
+ expect(refreshedIdentity.value.type).toBe('Anonymous');
92
+ });
93
+ });
@@ -0,0 +1,66 @@
1
+ import 'dotenv/config';
2
+ import { describe, expect, it, beforeEach, assert } from 'vitest';
3
+ import { createClient, PrimaryProvider } from '../utils.js';
4
+
5
+ describe.each([PrimaryProvider.COMMERCETOOLS])('Inventory Capability', (provider) => {
6
+ let client: ReturnType<typeof createClient>;
7
+
8
+ beforeEach(() => {
9
+ client = createClient(provider);
10
+ });
11
+
12
+ it('should return NotFound for unknown SKU', async () => {
13
+ const inventory = await client.inventory.getBySKU({
14
+ variant: {
15
+ sku: 'GMCT-01x',
16
+ },
17
+ fulfilmentCenter: {
18
+ key: 'solteqPhysicalStore',
19
+ },
20
+ });
21
+
22
+ if (inventory.success) {
23
+ assert.fail();
24
+ }
25
+
26
+ expect(inventory.error.type).toBe('NotFound');
27
+ });
28
+
29
+ it('should return NotFound for unknown ffmcenter', async () => {
30
+ const inventory = await client.inventory.getBySKU({
31
+ variant: {
32
+ sku: 'GMCT-01',
33
+ },
34
+ fulfilmentCenter: {
35
+ key: 'unknown-ffmcenter',
36
+ },
37
+ });
38
+
39
+ if (inventory.success) {
40
+ assert.fail();
41
+ }
42
+
43
+ expect(inventory.error.type).toBe('NotFound');
44
+ });
45
+
46
+ it('should be able to fetch inventory for a given SKU and Fulfillment Center', async () => {
47
+ const inventory = await client.inventory.getBySKU({
48
+ variant: {
49
+ sku: 'GMCT-01',
50
+ },
51
+ fulfilmentCenter: {
52
+ key: 'solteqPhysicalStore',
53
+ },
54
+ });
55
+
56
+ if (!inventory.success) {
57
+ assert.fail();
58
+ }
59
+
60
+ expect(inventory.value.identifier.variant.sku).toBe('GMCT-01');
61
+ expect(inventory.value.identifier.fulfillmentCenter.key).toBe(
62
+ 'solteqPhysicalStore'
63
+ );
64
+ expect(inventory.value.quantity).toBe(42);
65
+ });
66
+ });