1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("when-block-must-have-name", rule, {
7 valid: [
8 // Named `when(name, values, condition)` — no warning possible.
9 {
10 code: `
11 import { createWorkflow, when } from "@medusajs/framework/workflows-sdk"
12 createWorkflow("my-workflow", (input) => {
13 when("my-when", { input }, ({ input }) => !!input.flag).then(() => {
14 return transform(input, (i) => ({ value: i }))
15 })
16 })
17 `,
18 },
19 // `.then()` returns the direct result of a step invocation — `when.ts`
20 // never wraps this, so no name is needed.
21 {
22 code: `
23 import { createWorkflow, when } from "@medusajs/framework/workflows-sdk"
24 createWorkflow("my-workflow", (input) => {
25 when({ input }, ({ input }) => !!input.flag).then(() => {
26 return findOrCreateCustomerStep({ email: input.email })
27 })
28 })
29 `,
30 },
31 // Direct step invocation with a chained `.config(...)` is still step-like.
32 {
33 code: `
34 import { createWorkflow, when } from "@medusajs/framework/workflows-sdk"
35 createWorkflow("my-workflow", (input) => {
36 when({ input }, ({ input }) => !!input.flag).then(() => {
37 return someStep({ id: input.id }).config({ name: "renamed" })
38 })
39 })
40 `,
41 },
42 // `.runAsStep(...)` (a workflow run as a step) is step-like too.
43 {
44 code: `
45 import { createWorkflow, when } from "@medusajs/framework/workflows-sdk"
46 createWorkflow("my-workflow", (input) => {
47 when({ input }, ({ input }) => !!input.flag).then(() => {
48 return someWorkflow.runAsStep({ input })
49 })
50 })
51 `,
52 },
53 // No return at all — `.then()` resolves to `undefined`, never wrapped.
54 {
55 code: `
56 import { createWorkflow, when } from "@medusajs/framework/workflows-sdk"
57 createWorkflow("my-workflow", (input) => {
58 when({ input }, ({ input }) => !!input.flag).then(() => {
59 someStep({ id: input.id })
60 })
61 })
62 `,
63 },
64 // Not the workflows-sdk import — should not flag.
65 {
66 code: `
67 import { createWorkflow, when } from "some-other-lib"
68 createWorkflow("my-workflow", (input) => {
69 when({ input }, ({ input }) => !!input.flag).then(() => {
70 return transform(input, (i) => ({ value: i }))
71 })
72 })
73 `,
74 },
75 // `when(...)` not chained with `.then(...)` — malformed, not this rule's concern.
76 {
77 code: `
78 import { createWorkflow, when } from "@medusajs/framework/workflows-sdk"
79 createWorkflow("my-workflow", (input) => {
80 when({ input }, ({ input }) => !!input.flag)
81 })
82 `,
83 },
84 ],
85 invalid: [
86 // Unnamed `when` whose `.then()` returns a `transform(...)` call
87 {
88 code: `
89 import { createWorkflow, when, transform } from "@medusajs/framework/workflows-sdk"
90 createWorkflow("my-workflow", (input) => {
91 when({ input }, ({ input }) => input.data?.custom_amount === null).then(() => {
92 const action = transform({ input }, ({ input }) => ({ id: input.id }))
93 return transform(action, (a) => [a])
94 })
95 })
96 `,
97 errors: [{ messageId: "whenBlockMissingName" }],
98 },
99 // Unnamed `when` whose `.then()` returns a plain object literal.
100 {
101 code: `
102 import { createWorkflow, when } from "@medusajs/framework/workflows-sdk"
103 createWorkflow("my-workflow", (input) => {
104 when({ input }, ({ input }) => !!input.flag).then(() => {
105 return { active: true }
106 })
107 })
108 `,
109 errors: [{ messageId: "whenBlockMissingName" }],
110 },
111 // Expression-bodied `.then()` callback (implicit return).
112 {
113 code: `
114 import { createWorkflow, when, transform } from "@medusajs/framework/workflows-sdk"
115 createWorkflow("my-workflow", (input) => {
116 when({ input }, ({ input }) => !!input.flag).then(() =>
117 transform(input, (i) => ({ value: i }))
118 )
119 })
120 `,
121 errors: [{ messageId: "whenBlockMissingName" }],
122 },
123 // Aliased `when` import is still tracked.
124 {
125 code: `
126 import { createWorkflow, when as w } from "@medusajs/framework/workflows-sdk"
127 createWorkflow("my-workflow", (input) => {
128 w({ input }, ({ input }) => !!input.flag).then(() => {
129 return { active: true }
130 })
131 })
132 `,
133 errors: [{ messageId: "whenBlockMissingName" }],
134 },
135 ],
136 })