1 import {
2 updateOrderTaxLinesWorkflow,
3 updateTaxLinesWorkflow,
4 upsertTaxLinesWorkflow,
5 } from "@medusajs/core-flows"
6 import { StepResponse } from "@medusajs/framework/workflows-sdk"
7 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
8 import {
9 ICartModuleService,
10 IOrderModuleService,
11 ITaxModuleService,
12 } from "@medusajs/types"
13 import { Modules } from "@medusajs/utils"
14
15 jest.setTimeout(100000)
16
17 const env = {}
18
19 medusaIntegrationTestRunner({
20 env,
21 testSuite: ({ getContainer }) => {
22 describe("setTaxLineContext hook - additional tax context passthrough", () => {
23 let appContainer
24 let cartModuleService: ICartModuleService
25 let orderModuleService: IOrderModuleService
26 let taxModuleService: ITaxModuleService
27
28 // Toggled per test so the hook can be registered once globally and
29 // opt into different behaviour per case without re-registering.
30 let setTaxLineContextHook:
31 | ((input: any) => StepResponse<Record<string, unknown> | undefined>)
32 | undefined
33
34 beforeAll(async () => {
35 appContainer = getContainer()
36 cartModuleService = appContainer.resolve(Modules.CART)
37 orderModuleService = appContainer.resolve(Modules.ORDER)
38 taxModuleService = appContainer.resolve(Modules.TAX)
39
40 const hook = (input: any) => {
41 if (setTaxLineContextHook) {
42 return setTaxLineContextHook(input)
43 }
44 }
45
46 updateTaxLinesWorkflow.hooks.setTaxLineContext(hook, () => {})
47 upsertTaxLinesWorkflow.hooks.setTaxLineContext(hook, () => {})
48 updateOrderTaxLinesWorkflow.hooks.setTaxLineContext(hook, () => {})
49 })
50
51 afterEach(() => {
52 setTaxLineContextHook = undefined
53 jest.restoreAllMocks()
54 })
55
56 /**
57 * Spies on the tax module service's `getTaxLines` (the same singleton the
58 * workflow step resolves) so we can inspect the `TaxCalculationContext`
59 * that ultimately reaches the tax provider. The implementation is mocked
60 * to return no tax lines so the test does not depend on a configured tax
61 * region.
62 */
63 const spyOnGetTaxLines = () => {
64 return jest.spyOn(taxModuleService, "getTaxLines").mockResolvedValue([])
65 }
66
67 describe("updateTaxLinesWorkflow (cart)", () => {
68 it("forwards the hook result as additional_context to the tax provider", async () => {
69 const cart = await cartModuleService.createCarts({
70 currency_code: "usd",
71 shipping_address: { country_code: "us" },
72 items: [{ quantity: 1, unit_price: 5000, title: "Test item" }],
73 })
74
75 const additionalContext = {
76 tax_exempt: true,
77 certificate_id: "cert_123",
78 }
79
80 setTaxLineContextHook = () => new StepResponse(additionalContext)
81
82 const getTaxLinesSpy = spyOnGetTaxLines()
83
84 await updateTaxLinesWorkflow(appContainer).run({
85 input: { cart_id: cart.id, force_tax_calculation: true },
86 throwOnError: true,
87 })
88
89 expect(getTaxLinesSpy).toHaveBeenCalled()
90 const [, calculationContext] = getTaxLinesSpy.mock.calls[0]
91 expect(calculationContext.additional_context).toEqual(
92 additionalContext
93 )
94 })
95
96 it("exposes the cart, items and shipping_methods to the hook", async () => {
97 const cart = await cartModuleService.createCarts({
98 currency_code: "usd",
99 shipping_address: { country_code: "us" },
100 items: [{ quantity: 1, unit_price: 5000, title: "Test item" }],
101 })
102
103 let receivedInput: any
104 setTaxLineContextHook = (input) => {
105 receivedInput = input
106 return new StepResponse(undefined)
107 }
108
109 spyOnGetTaxLines()
110
111 await updateTaxLinesWorkflow(appContainer).run({
112 input: { cart_id: cart.id, force_tax_calculation: true },
113 throwOnError: true,
114 })
115
116 expect(receivedInput).toEqual(
117 expect.objectContaining({
118 cart: expect.objectContaining({ id: cart.id }),
119 items: expect.any(Array),
120 })
121 )
122 expect(receivedInput).toHaveProperty("shipping_methods")
123 })
124 })
125
126 describe("upsertTaxLinesWorkflow (cart)", () => {
127 it("forwards the hook result as additional_context to the tax provider", async () => {
128 const cart = await cartModuleService.createCarts({
129 currency_code: "usd",
130 shipping_address: { country_code: "us" },
131 items: [{ quantity: 1, unit_price: 5000, title: "Test item" }],
132 })
133
134 const cartWithItems = await cartModuleService.retrieveCart(cart.id, {
135 relations: ["items"],
136 })
137
138 const additionalContext = { vat_validated: true }
139
140 setTaxLineContextHook = () => new StepResponse(additionalContext)
141
142 const getTaxLinesSpy = spyOnGetTaxLines()
143
144 await upsertTaxLinesWorkflow(appContainer).run({
145 input: {
146 cart_id: cart.id,
147 items: cartWithItems.items as any,
148 shipping_methods: [],
149 force_tax_calculation: true,
150 },
151 throwOnError: true,
152 })
153
154 expect(getTaxLinesSpy).toHaveBeenCalled()
155 const [, calculationContext] = getTaxLinesSpy.mock.calls[0]
156 expect(calculationContext.additional_context).toEqual(
157 additionalContext
158 )
159 })
160 })
161
162 describe("updateOrderTaxLinesWorkflow (order)", () => {
163 it("forwards the hook result as additional_context to the tax provider", async () => {
164 const order = await orderModuleService.createOrders({
165 currency_code: "usd",
166 email: "test@medusajs.com",
167 shipping_address: {
168 first_name: "Test",
169 last_name: "Test",
170 address_1: "Test",
171 city: "Test",
172 country_code: "us",
173 postal_code: "12345",
174 },
175 items: [{ quantity: 1, unit_price: 5000, title: "Test item" }],
176 })
177
178 const additionalContext = { nexus_state: "ca" }
179
180 setTaxLineContextHook = () => new StepResponse(additionalContext)
181
182 const getTaxLinesSpy = spyOnGetTaxLines()
183
184 await updateOrderTaxLinesWorkflow(appContainer).run({
185 input: { order_id: order.id, force_tax_calculation: true },
186 throwOnError: true,
187 })
188
189 expect(getTaxLinesSpy).toHaveBeenCalled()
190 const [, calculationContext] = getTaxLinesSpy.mock.calls[0]
191 expect(calculationContext.additional_context).toEqual(
192 additionalContext
193 )
194 })
195
196 it("forwards shipping method names to the tax provider for full-order calculations", async () => {
197 const order = await orderModuleService.createOrders({
198 currency_code: "usd",
199 email: "test@medusajs.com",
200 shipping_address: {
201 first_name: "Test",
202 last_name: "Test",
203 address_1: "Test",
204 city: "Test",
205 country_code: "us",
206 postal_code: "12345",
207 },
208 items: [{ quantity: 1, unit_price: 5000, title: "Test item" }],
209 shipping_methods: [
210 {
211 name: "Standard Shipping",
212 amount: 1000,
213 data: {},
214 },
215 ],
216 })
217
218 const getTaxLinesSpy = spyOnGetTaxLines()
219
220 await updateOrderTaxLinesWorkflow(appContainer).run({
221 input: { order_id: order.id, force_tax_calculation: true },
222 throwOnError: true,
223 })
224
225 expect(getTaxLinesSpy).toHaveBeenCalled()
226 const [, calculationContext] = getTaxLinesSpy.mock.calls[0]
227 expect(calculationContext.shipping_methods).toEqual([
228 expect.objectContaining({
229 name: "Standard Shipping",
230 }),
231 ])
232 })
233
234 it("forwards shipping method names to the tax provider for partial shipping calculations", async () => {
235 const order = await orderModuleService.createOrders({
236 currency_code: "usd",
237 email: "test@medusajs.com",
238 shipping_address: {
239 first_name: "Test",
240 last_name: "Test",
241 address_1: "Test",
242 city: "Test",
243 country_code: "us",
244 postal_code: "12345",
245 },
246 items: [{ quantity: 1, unit_price: 5000, title: "Test item" }],
247 shipping_methods: [
248 {
249 name: "Express Shipping",
250 amount: 1500,
251 data: {},
252 },
253 ],
254 })
255
256 const shippingMethod = order.shipping_methods![0]
257 const getTaxLinesSpy = spyOnGetTaxLines()
258
259 await updateOrderTaxLinesWorkflow(appContainer).run({
260 input: {
261 order_id: order.id,
262 shipping_method_ids: [shippingMethod.id],
263 force_tax_calculation: true,
264 },
265 throwOnError: true,
266 })
267
268 expect(getTaxLinesSpy).toHaveBeenCalled()
269 const [, calculationContext] = getTaxLinesSpy.mock.calls[0]
270 expect(calculationContext.shipping_methods).toEqual([
271 expect.objectContaining({
272 name: "Express Shipping",
273 }),
274 ])
275 })
276
277 it("exposes the order, items and shipping_methods to the hook", async () => {
278 const order = await orderModuleService.createOrders({
279 currency_code: "usd",
280 email: "test@medusajs.com",
281 shipping_address: {
282 first_name: "Test",
283 last_name: "Test",
284 address_1: "Test",
285 city: "Test",
286 country_code: "us",
287 postal_code: "12345",
288 },
289 items: [{ quantity: 1, unit_price: 5000, title: "Test item" }],
290 })
291
292 let receivedInput: any
293 setTaxLineContextHook = (input) => {
294 receivedInput = input
295 return new StepResponse(undefined)
296 }
297
298 spyOnGetTaxLines()
299
300 await updateOrderTaxLinesWorkflow(appContainer).run({
301 input: { order_id: order.id, force_tax_calculation: true },
302 throwOnError: true,
303 })
304
305 // For a full-order calculation the hook receives the `order`
306 // (with its nested items and shipping methods). The separate
307 // `items`/`shipping_methods` inputs are only populated for partial
308 // calculations driven by `item_ids`/`shipping_method_ids`.
309 expect(receivedInput).toEqual(
310 expect.objectContaining({
311 order: expect.objectContaining({
312 id: order.id,
313 items: expect.any(Array),
314 }),
315 })
316 )
317 })
318 })
319 })
320 },
321 })