1 import { createRuleTester } from "../../../test-utils"
2 import * as path from "path"
3 import { rule } from "../rule"
4
5 const ruleTester = createRuleTester({
6 languageOptions: {
7 parserOptions: {
8 projectService: {
9 allowDefaultProject: ["*.ts*"],
10 },
11 tsconfigRootDir: path.join(__dirname, "..", "..", "..", ".."),
12 },
13 },
14 })
15
16 ruleTester.run("no-workflow-call-without-container", rule, {
17 valid: [
18 // Canonical use: invoke with container, chain .run.
19 {
20 code: `
21 import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
22 const myWorkflow = createWorkflow("my", () => new WorkflowResponse({}))
23 async function handler(container: any) {
24 await myWorkflow(container).run({ input: {} })
25 }
26 `,
27 },
28 // Invoking with container but not chaining .run — no longer flagged (container-only rule).
29 {
30 code: `
31 import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
32 const myWorkflow = createWorkflow("my", () => new WorkflowResponse({}))
33 async function handler(container: any) {
34 const exec = myWorkflow(container)
35 }
36 `,
37 },
38 // ReturnWorkflow-typed value invoked properly.
39 {
40 code: `
41 import type { ReturnWorkflow } from "@medusajs/framework/workflows-sdk"
42 declare const myWorkflow: ReturnWorkflow<any, any, []>
43 async function handler(container: any) {
44 await myWorkflow(container).run({ input: {} })
45 }
46 `,
47 },
48 // Identifier whose type isn't a workflow — ignored.
49 {
50 code: `
51 function regular(): number { return 1 }
52 regular()
53 `,
54 },
55 // The createWorkflow call itself is not a workflow invocation.
56 {
57 code: `
58 import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
59 const myWorkflow = createWorkflow("my", () => new WorkflowResponse({}))
60 `,
61 },
62 // Look-alike object — not a workflow type.
63 {
64 code: `
65 const myWorkflow = { run: () => {} }
66 myWorkflow.run()
67 `,
68 },
69 ],
70 invalid: [
71 // Workflow invoked with no container.
72 {
73 code: `
74 import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
75 const myWorkflow = createWorkflow("my", () => new WorkflowResponse({}))
76 async function go() {
77 await myWorkflow().run({ input: {} })
78 }
79 `,
80 errors: [{ messageId: "missingContainer", data: { name: "myWorkflow" } }],
81 },
82 // Calling .run directly on the workflow value bypasses container resolution.
83 {
84 code: `
85 import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
86 const helloWorldWorkflow = createWorkflow("hello", () => new WorkflowResponse({}))
87 async function go() {
88 await helloWorldWorkflow.run({ input: {} })
89 }
90 `,
91 errors: [
92 { messageId: "missingContainer", data: { name: "helloWorldWorkflow" } },
93 ],
94 },
95 // ReturnWorkflow-typed: no container.
96 {
97 code: `
98 import type { ReturnWorkflow } from "@medusajs/framework/workflows-sdk"
99 declare const myWorkflow: ReturnWorkflow<any, any, []>
100 async function go() {
101 await myWorkflow().run({ input: {} })
102 }
103 `,
104 errors: [{ messageId: "missingContainer", data: { name: "myWorkflow" } }],
105 },
106 // ReturnWorkflow-typed: .run accessed directly.
107 {
108 code: `
109 import type { ReturnWorkflow } from "@medusajs/framework/workflows-sdk"
110 declare const myWorkflow: ReturnWorkflow<any, any, []>
111 async function go() {
112 await myWorkflow.run({ input: {} })
113 }
114 `,
115 errors: [{ messageId: "missingContainer", data: { name: "myWorkflow" } }],
116 },
117 // Member-expression callee: namespace-style access, called with no container.
118 {
119 code: `
120 import type { ReturnWorkflow } from "@medusajs/framework/workflows-sdk"
121 declare const flows: { createCustomerWorkflow: ReturnWorkflow<any, any, []> }
122 async function go() {
123 await flows.createCustomerWorkflow().run({ input: {} })
124 }
125 `,
126 errors: [
127 {
128 messageId: "missingContainer",
129 data: { name: "createCustomerWorkflow" },
130 },
131 ],
132 },
133 // Member-expression .run on member-accessed workflow value.
134 {
135 code: `
136 import type { ReturnWorkflow } from "@medusajs/framework/workflows-sdk"
137 declare const flows: { createCustomerWorkflow: ReturnWorkflow<any, any, []> }
138 async function go() {
139 await flows.createCustomerWorkflow.run({ input: {} })
140 }
141 `,
142 errors: [
143 {
144 messageId: "missingContainer",
145 data: { name: "createCustomerWorkflow" },
146 },
147 ],
148 },
149 ],
150 })