1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("prices-in-major-units", rule, {
7 valid: [
8 // Decimal numeric literal — fine.
9 {
10 code: `const item = { unit_price: 19.99, currency_code: "usd" }`,
11 },
12 // Small integer (< 1000) without other red flags — fine.
13 {
14 code: `const item = { amount: 50, currency_code: "usd" }`,
15 },
16 // Identifier value not matching cents pattern — fine.
17 {
18 code: `const item = { amount: priceInMajor }`,
19 },
20 // Multiplied by something other than 100/1000 — fine.
21 {
22 code: `const item = { amount: base * 2 }`,
23 },
24 // Key not in the price list — fine.
25 {
26 code: `const cfg = { score: value * 100 }`,
27 },
28 // Sibling currency_code is zero-decimal (JPY) — large integer is plausible.
29 {
30 code: `const item = { amount: 5000, currency_code: "jpy" }`,
31 },
32 // Sibling currency_code is zero-decimal (KRW), uppercase — also accepted.
33 {
34 code: `const item = { amount: 50000, currency_code: "KRW" }`,
35 },
36 // Computed key — out of scope.
37 {
38 code: `const item = { [dynamicKey]: value * 100 }`,
39 },
40 // Decimal raw token (1000.0) — not flagged as large integer.
41 {
42 code: `const item = { amount: 1000.0 }`,
43 },
44 // Non-price variable name — fine.
45 {
46 code: `const count = items * 100`,
47 },
48 // Variable matches price key but value is a decimal — fine.
49 {
50 code: `const total = 19.99`,
51 },
52 // Assignment to non-price variable — fine.
53 {
54 code: `let x; x = amount * 100`,
55 },
56 ],
57 invalid: [
58 // `* 100` shape.
59 {
60 code: `const item = { amount: price * 100 }`,
61 errors: [{ messageId: "multipliedByHundred", data: { key: "amount" } }],
62 },
63 // `* 1000` shape.
64 {
65 code: `const item = { unit_price: base * 1000 }`,
66 errors: [
67 { messageId: "multipliedByHundred", data: { key: "unit_price" } },
68 ],
69 },
70 // Factor on the left.
71 {
72 code: `const item = { total: 100 * base }`,
73 errors: [{ messageId: "multipliedByHundred", data: { key: "total" } }],
74 },
75 // Identifier named `cents`.
76 {
77 code: `const item = { amount: cents }`,
78 errors: [
79 {
80 messageId: "centsIdentifier",
81 data: { key: "amount", name: "cents" },
82 },
83 ],
84 },
85 // Identifier with `_cents` suffix.
86 {
87 code: `const item = { price: amount_cents }`,
88 errors: [
89 {
90 messageId: "centsIdentifier",
91 data: { key: "price", name: "amount_cents" },
92 },
93 ],
94 },
95 // Identifier named `priceInCents`.
96 {
97 code: `const item = { unit_price: priceInCents }`,
98 errors: [
99 {
100 messageId: "centsIdentifier",
101 data: { key: "unit_price", name: "priceInCents" },
102 },
103 ],
104 },
105 // Identifier named `amountInCents`.
106 {
107 code: `const item = { subtotal: amountInCents }`,
108 errors: [
109 {
110 messageId: "centsIdentifier",
111 data: { key: "subtotal", name: "amountInCents" },
112 },
113 ],
114 },
115 // Member expression ending in InCents.
116 {
117 code: `const item = { amount: req.body.priceInCents }`,
118 errors: [
119 {
120 messageId: "centsIdentifier",
121 data: { key: "amount", name: "priceInCents" },
122 },
123 ],
124 },
125 // Large integer (>= 1000) with no sibling currency_code.
126 {
127 code: `const item = { amount: 2500 }`,
128 errors: [
129 {
130 messageId: "largeIntegerLikelyCents",
131 data: { key: "amount", value: "2500" },
132 },
133 ],
134 },
135 // Large integer with non-zero-decimal sibling currency (USD).
136 {
137 code: `const item = { amount: 2500, currency_code: "usd" }`,
138 errors: [
139 {
140 messageId: "largeIntegerLikelyCents",
141 data: { key: "amount", value: "2500" },
142 },
143 ],
144 },
145 // Variable declarator: `const total = amount * 100`.
146 {
147 code: `const total = amount * 100`,
148 errors: [{ messageId: "multipliedByHundred", data: { key: "total" } }],
149 },
150 // Variable declarator with `*_cents` initializer.
151 {
152 code: `const subtotal = amount_cents`,
153 errors: [
154 {
155 messageId: "centsIdentifier",
156 data: { key: "subtotal", name: "amount_cents" },
157 },
158 ],
159 },
160 // Variable declarator with large integer literal.
161 {
162 code: `const unit_price = 2500`,
163 errors: [
164 {
165 messageId: "largeIntegerLikelyCents",
166 data: { key: "unit_price", value: "2500" },
167 },
168 ],
169 },
170 // Assignment expression: `total = amount * 100`.
171 {
172 code: `let total; total = amount * 100`,
173 errors: [{ messageId: "multipliedByHundred", data: { key: "total" } }],
174 },
175 // Assignment to a member expression with a price-like name.
176 {
177 code: `payload.price = priceInCents`,
178 errors: [
179 {
180 messageId: "centsIdentifier",
181 data: { key: "price", name: "priceInCents" },
182 },
183 ],
184 },
185 // All applicable price keys flagged in a single object.
186 {
187 code: `const order = {
188 subtotal: subtotal_cents,
189 tax_total: tax * 100,
190 shipping_total: 5000,
191 currency_code: "usd",
192 }`,
193 errors: [
194 {
195 messageId: "centsIdentifier",
196 data: { key: "subtotal", name: "subtotal_cents" },
197 },
198 { messageId: "multipliedByHundred", data: { key: "tax_total" } },
199 {
200 messageId: "largeIntegerLikelyCents",
201 data: { key: "shipping_total", value: "5000" },
202 },
203 ],
204 },
205 ],
206 })