1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("no-duplicate-step-id-in-workflow", rule, {
7 valid: [
8 // Single step invocation — no duplicate possible.
9 {
10 code: `
11 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
12 createWorkflow("hello", () => {
13 const { data } = useQueryGraphStep({ entity: "product", fields: ["id"] })
14 return data
15 })
16 `,
17 },
18 // Two different step factories — different IDs by default.
19 {
20 code: `
21 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
22 createWorkflow("hello", () => {
23 const a = fetchProductsStep({})
24 const b = fetchCustomersStep({})
25 })
26 `,
27 },
28 // Same factory invoked twice but second renamed via .config({name}).
29 {
30 code: `
31 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
32 createWorkflow("hello", () => {
33 const { data: products } = useQueryGraphStep({ entity: "product", fields: ["id"] })
34 const { data: customers } = useQueryGraphStep({ entity: "customer", fields: ["id"] }).config({ name: "fetch-customers" })
35 })
36 `,
37 },
38 // Both calls renamed with distinct .config({name}) values.
39 {
40 code: `
41 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
42 createWorkflow("hello", () => {
43 useQueryGraphStep({}).config({ name: "a" })
44 useQueryGraphStep({}).config({ name: "b" })
45 })
46 `,
47 },
48 // Same factory called twice but inside separate workflows — no conflict.
49 {
50 code: `
51 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
52 createWorkflow("one", () => {
53 useQueryGraphStep({})
54 })
55 createWorkflow("two", () => {
56 useQueryGraphStep({})
57 })
58 `,
59 },
60 // Step invoked inside a nested createStep callback — not the constructor body.
61 {
62 code: `
63 import { createWorkflow, createStep } from "@medusajs/framework/workflows-sdk"
64 createWorkflow("hello", () => {
65 const s = createStep("s", () => {
66 useQueryGraphStep({})
67 useQueryGraphStep({})
68 return 1
69 })
70 })
71 `,
72 },
73 // Outside any workflow constructor.
74 {
75 code: `
76 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
77 useQueryGraphStep({})
78 useQueryGraphStep({})
79 createWorkflow("hello", () => {})
80 `,
81 },
82 // Not the workflows-sdk import — should not flag.
83 {
84 code: `
85 import { createWorkflow } from "some-other-lib"
86 createWorkflow("hello", () => {
87 useQueryGraphStep({})
88 useQueryGraphStep({})
89 })
90 `,
91 },
92 // SDK helpers (transform/when/parallelize) are not step invocations.
93 {
94 code: `
95 import { createWorkflow, transform, when, parallelize } from "@medusajs/framework/workflows-sdk"
96 createWorkflow("hello", (input) => {
97 const a = transform({ input }, (d) => d.input)
98 const b = transform({ input }, (d) => d.input)
99 when({ input }, (d) => true).then(() => {})
100 when({ input }, (d) => true).then(() => {})
101 parallelize(a, b)
102 parallelize(a, b)
103 })
104 `,
105 },
106 // Both calls renamed via `.config({ name })` with dynamic (non-literal)
107 // values. The developer explicitly set distinct names we can't compare
108 // statically, so neither is flagged.
109 {
110 code: `
111 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
112 createWorkflow("hello", () => {
113 useQueryGraphStep({}).config({ name: serverStepId })
114 useQueryGraphStep({}).config({ name: workerStepId })
115 })
116 `,
117 },
118 // Even the same variable on both calls isn't flagged — once a name is set
119 // explicitly, we don't second-guess it statically.
120 {
121 code: `
122 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
123 createWorkflow("hello", () => {
124 useQueryGraphStep({}).config({ name: dynamicName })
125 useQueryGraphStep({}).config({ name: dynamicName })
126 })
127 `,
128 },
129 // Mirrors the reported false positive: parallelize with two
130 // `.config({ name })` calls using variable names.
131 {
132 code: `
133 import { createWorkflow, parallelize } from "@medusajs/framework/workflows-sdk"
134 createWorkflow("hello", () => {
135 parallelize(
136 waitForResourceStep({}).config({ name: waitForServerStepId, async: true }),
137 waitForResourceStep({}).config({ name: waitForWorkerStepId, async: true })
138 )
139 })
140 `,
141 },
142 ],
143 invalid: [
144 // Two bare calls to the same step factory.
145 {
146 code: `
147 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
148 createWorkflow("hello", () => {
149 const { data: products } = useQueryGraphStep({ entity: "product", fields: ["id"] })
150 const { data: customers } = useQueryGraphStep({ entity: "customer", fields: ["id"] })
151 })
152 `,
153 errors: [
154 { messageId: "duplicateStepId", data: { key: "use-query-graph" } },
155 ],
156 output: `
157 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
158 createWorkflow("hello", () => {
159 const { data: products } = useQueryGraphStep({ entity: "product", fields: ["id"] })
160 const { data: customers } = useQueryGraphStep({ entity: "customer", fields: ["id"] }).config({ name: "use-query-graph-2" })
161 })
162 `,
163 },
164 // Three duplicates → two reports (first untouched).
165 {
166 code: `
167 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
168 createWorkflow("hello", () => {
169 useQueryGraphStep({})
170 useQueryGraphStep({})
171 useQueryGraphStep({})
172 })
173 `,
174 errors: [
175 { messageId: "duplicateStepId", data: { key: "use-query-graph" } },
176 { messageId: "duplicateStepId", data: { key: "use-query-graph" } },
177 ],
178 output: `
179 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
180 createWorkflow("hello", () => {
181 useQueryGraphStep({})
182 useQueryGraphStep({}).config({ name: "use-query-graph-2" })
183 useQueryGraphStep({}).config({ name: "use-query-graph-3" })
184 })
185 `,
186 },
187 // Two .config({name}) calls with the same name → duplicate, no autofix.
188 {
189 code: `
190 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
191 createWorkflow("hello", () => {
192 useQueryGraphStep({}).config({ name: "fetch" })
193 useQueryGraphStep({}).config({ name: "fetch" })
194 })
195 `,
196 errors: [{ messageId: "duplicateStepId", data: { key: "fetch" } }],
197 },
198 // A bare call and a .config-renamed call collide with a third bare call.
199 {
200 code: `
201 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
202 createWorkflow("hello", () => {
203 fetchStep({})
204 fetchStep({}).config({ name: "other" })
205 fetchStep({})
206 })
207 `,
208 errors: [{ messageId: "duplicateStepId", data: { key: "fetch" } }],
209 output: `
210 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
211 createWorkflow("hello", () => {
212 fetchStep({})
213 fetchStep({}).config({ name: "other" })
214 fetchStep({}).config({ name: "fetch-2" })
215 })
216 `,
217 },
218 // Aliased createWorkflow binding.
219 {
220 code: `
221 import { createWorkflow as cw } from "@medusajs/framework/workflows-sdk"
222 cw("hello", () => {
223 useQueryGraphStep({})
224 useQueryGraphStep({})
225 })
226 `,
227 errors: [
228 { messageId: "duplicateStepId", data: { key: "use-query-graph" } },
229 ],
230 output: `
231 import { createWorkflow as cw } from "@medusajs/framework/workflows-sdk"
232 cw("hello", () => {
233 useQueryGraphStep({})
234 useQueryGraphStep({}).config({ name: "use-query-graph-2" })
235 })
236 `,
237 },
238 // Function-expression constructor with duplicates.
239 {
240 code: `
241 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
242 createWorkflow("hello", function () {
243 fetchProductsStep({})
244 fetchProductsStep({})
245 })
246 `,
247 errors: [
248 { messageId: "duplicateStepId", data: { key: "fetch-products" } },
249 ],
250 output: `
251 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
252 createWorkflow("hello", function () {
253 fetchProductsStep({})
254 fetchProductsStep({}).config({ name: "fetch-products-2" })
255 })
256 `,
257 },
258 // Independent workflows: each tracked on its own. Only the second workflow has duplicates.
259 {
260 code: `
261 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
262 createWorkflow("one", () => {
263 useQueryGraphStep({})
264 })
265 createWorkflow("two", () => {
266 fetchStep({})
267 fetchStep({})
268 })
269 `,
270 errors: [{ messageId: "duplicateStepId", data: { key: "fetch" } }],
271 output: `
272 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
273 createWorkflow("one", () => {
274 useQueryGraphStep({})
275 })
276 createWorkflow("two", () => {
277 fetchStep({})
278 fetchStep({}).config({ name: "fetch-2" })
279 })
280 `,
281 },
282 // A dynamic-named call doesn't shield the remaining bare duplicates: the
283 // two bare calls still collide, while the dynamic one is left alone.
284 {
285 code: `
286 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
287 createWorkflow("hello", () => {
288 fetchStep({})
289 fetchStep({}).config({ name: dynamicName })
290 fetchStep({})
291 })
292 `,
293 errors: [{ messageId: "duplicateStepId", data: { key: "fetch" } }],
294 output: `
295 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
296 createWorkflow("hello", () => {
297 fetchStep({})
298 fetchStep({}).config({ name: dynamicName })
299 fetchStep({}).config({ name: "fetch-2" })
300 })
301 `,
302 },
303 ],
304 })