1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("no-reserved-default-properties-in-model", rule, {
7 valid: [
8 // Schema with no reserved properties.
9 {
10 code: `
11 import { model } from "@medusajs/framework/utils"
12 export const Brand = model.define("brand", {
13 id: model.id().primaryKey(),
14 name: model.text(),
15 })
16 `,
17 },
18 // Empty schema.
19 {
20 code: `
21 import { model } from "@medusajs/framework/utils"
22 export const Brand = model.define("brand", {})
23 `,
24 },
25 // \`model\` from another package — not flagged.
26 {
27 code: `
28 import { model } from "some-other-lib"
29 export const Brand = model.define("brand", {
30 created_at: model.dateTime(),
31 })
32 `,
33 },
34 // Non-define method — not flagged.
35 {
36 code: `
37 import { model } from "@medusajs/framework/utils"
38 const col = model.text()
39 `,
40 },
41 // Non-object second argument — skipped.
42 {
43 code: `
44 import { model } from "@medusajs/framework/utils"
45 const schema = { created_at: model.dateTime() }
46 export const Brand = model.define("brand", schema)
47 `,
48 },
49 // Computed key matching reserved name — not flagged (out of scope).
50 {
51 code: `
52 import { model } from "@medusajs/framework/utils"
53 const K = "created_at"
54 export const Brand = model.define("brand", {
55 id: model.id().primaryKey(),
56 [K]: model.dateTime(),
57 })
58 `,
59 },
60 // Property named "id" is NOT reserved — users must define it themselves.
61 {
62 code: `
63 import { model } from "@medusajs/framework/utils"
64 export const Brand = model.define("brand", {
65 id: model.id().primaryKey(),
66 })
67 `,
68 },
69 ],
70 invalid: [
71 // Single reserved property — autofix removes it.
72 {
73 code: `
74 import { model } from "@medusajs/framework/utils"
75 export const Brand = model.define("brand", {
76 id: model.id().primaryKey(),
77 created_at: model.dateTime(),
78 })
79 `,
80 errors: [
81 { messageId: "reservedDefaultProperty", data: { name: "created_at" } },
82 ],
83 output: `
84 import { model } from "@medusajs/framework/utils"
85 export const Brand = model.define("brand", {
86 id: model.id().primaryKey(),
87 })
88 `,
89 },
90 // updated_at — autofix.
91 {
92 code: `
93 import { model } from "@medusajs/framework/utils"
94 export const Brand = model.define("brand", {
95 id: model.id().primaryKey(),
96 updated_at: model.dateTime(),
97 })
98 `,
99 errors: [
100 { messageId: "reservedDefaultProperty", data: { name: "updated_at" } },
101 ],
102 output: `
103 import { model } from "@medusajs/framework/utils"
104 export const Brand = model.define("brand", {
105 id: model.id().primaryKey(),
106 })
107 `,
108 },
109 // deleted_at — autofix.
110 {
111 code: `
112 import { model } from "@medusajs/framework/utils"
113 export const Brand = model.define("brand", {
114 id: model.id().primaryKey(),
115 deleted_at: model.dateTime(),
116 })
117 `,
118 errors: [
119 { messageId: "reservedDefaultProperty", data: { name: "deleted_at" } },
120 ],
121 output: `
122 import { model } from "@medusajs/framework/utils"
123 export const Brand = model.define("brand", {
124 id: model.id().primaryKey(),
125 })
126 `,
127 },
128 // All three reserved properties — each flagged. (Multi-pass autofix
129 // converges to a clean schema; one-pass output not asserted because
130 // ESLint skips adjacent-range fixes.)
131 {
132 code: `
133 import { model } from "@medusajs/framework/utils"
134 export const Brand = model.define("brand", {
135 id: model.id().primaryKey(),
136 created_at: model.dateTime(),
137 updated_at: model.dateTime(),
138 deleted_at: model.dateTime(),
139 })
140 `,
141 errors: [
142 { messageId: "reservedDefaultProperty", data: { name: "created_at" } },
143 { messageId: "reservedDefaultProperty", data: { name: "updated_at" } },
144 { messageId: "reservedDefaultProperty", data: { name: "deleted_at" } },
145 ],
146 output: [
147 `
148 import { model } from "@medusajs/framework/utils"
149 export const Brand = model.define("brand", {
150 id: model.id().primaryKey(),
151 updated_at: model.dateTime(),
152 })
153 `,
154 `
155 import { model } from "@medusajs/framework/utils"
156 export const Brand = model.define("brand", {
157 id: model.id().primaryKey(),
158 })
159 `,
160 ],
161 },
162 // String-literal key — flagged.
163 {
164 code: `
165 import { model } from "@medusajs/framework/utils"
166 export const Brand = model.define("brand", {
167 id: model.id().primaryKey(),
168 "created_at": model.dateTime(),
169 })
170 `,
171 errors: [
172 { messageId: "reservedDefaultProperty", data: { name: "created_at" } },
173 ],
174 output: `
175 import { model } from "@medusajs/framework/utils"
176 export const Brand = model.define("brand", {
177 id: model.id().primaryKey(),
178 })
179 `,
180 },
181 // Aliased model import binding.
182 {
183 code: `
184 import { model as m } from "@medusajs/framework/utils"
185 export const Brand = m.define("brand", {
186 id: m.id().primaryKey(),
187 created_at: m.dateTime(),
188 })
189 `,
190 errors: [
191 { messageId: "reservedDefaultProperty", data: { name: "created_at" } },
192 ],
193 output: `
194 import { model as m } from "@medusajs/framework/utils"
195 export const Brand = m.define("brand", {
196 id: m.id().primaryKey(),
197 })
198 `,
199 },
200 // No trailing comma on reserved property — comma after prev property is preserved.
201 {
202 code: `
203 import { model } from "@medusajs/framework/utils"
204 export const Brand = model.define("brand", {
205 id: model.id().primaryKey(),
206 created_at: model.dateTime()
207 })
208 `,
209 errors: [
210 { messageId: "reservedDefaultProperty", data: { name: "created_at" } },
211 ],
212 output: `
213 import { model } from "@medusajs/framework/utils"
214 export const Brand = model.define("brand", {
215 id: model.id().primaryKey(),
216 })
217 `,
218 },
219 // Sole property — just remove it.
220 {
221 code: `
222 import { model } from "@medusajs/framework/utils"
223 export const Brand = model.define("brand", { created_at: model.dateTime() })
224 `,
225 errors: [
226 { messageId: "reservedDefaultProperty", data: { name: "created_at" } },
227 ],
228 output: `
229 import { model } from "@medusajs/framework/utils"
230 export const Brand = model.define("brand", {})
231 `,
232 },
233 ],
234 })