Skip to content

Commit 1497b3c

Browse files
committed
gradient tests
1 parent 2a388b9 commit 1497b3c

File tree

1 file changed

+233
-0
lines changed

1 file changed

+233
-0
lines changed

src/test/gradient.test.ts

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
import { expect, test } from "vitest";
2+
import fs from "fs";
3+
import { round2, getSelectedGradient, deriveGradientStrength } from "../utils/gradient";
4+
import { GradientOption } from "../types";
5+
6+
test("round2 rounds numbers to 2 decimal places", () => {
7+
expect(round2(1.234)).toBe(1.23);
8+
expect(round2(1.235)).toBe(1.24);
9+
expect(round2(1.999)).toBe(2.0);
10+
expect(round2(0.001)).toBe(0.0);
11+
expect(round2(5)).toBe(5.0);
12+
});
13+
14+
test("getSelectedGradient returns default when no options provided", () => {
15+
const getCurrentValue = () => undefined;
16+
const result = getSelectedGradient([], "default_value", getCurrentValue);
17+
18+
expect(result.currentGradient).toBe("default_value");
19+
expect(result.selectedOption.value).toBe("default_value");
20+
expect(result.selectedOption.display_name).toBe("default_value");
21+
});
22+
23+
test("getSelectedGradient selects first valid option when current value not found", () => {
24+
const gradientOptions: GradientOption[] = [
25+
{
26+
display_name: "Gradient A",
27+
value: "gradient_a",
28+
path: "objects.test.gradient",
29+
packing_mode: "gradient",
30+
},
31+
{
32+
display_name: "Gradient B",
33+
value: "gradient_b",
34+
path: "objects.test.gradient",
35+
packing_mode: "gradient",
36+
},
37+
];
38+
const getCurrentValue = () => undefined;
39+
const result = getSelectedGradient(gradientOptions, "default", getCurrentValue);
40+
41+
expect(result.currentGradient).toBe("gradient_a");
42+
expect(result.selectedOption.value).toBe("gradient_a");
43+
expect(result.selectedOption.display_name).toBe("Gradient A");
44+
});
45+
46+
test("getSelectedGradient returns matching option when found in store", () => {
47+
const gradientOptions: GradientOption[] = [
48+
{
49+
display_name: "Gradient A",
50+
value: "gradient_a",
51+
path: "objects.test.gradient",
52+
packing_mode: "gradient",
53+
},
54+
{
55+
display_name: "Gradient B",
56+
value: "gradient_b",
57+
path: "objects.test.gradient",
58+
packing_mode: "gradient",
59+
},
60+
];
61+
const getCurrentValue = (path: string) => {
62+
if (path === "objects.test.gradient") return "gradient_b";
63+
return undefined;
64+
};
65+
const result = getSelectedGradient(gradientOptions, "default", getCurrentValue);
66+
67+
expect(result.currentGradient).toBe("gradient_b");
68+
expect(result.selectedOption.value).toBe("gradient_b");
69+
expect(result.selectedOption.display_name).toBe("Gradient B");
70+
});
71+
72+
test("getSelectedGradient filters by packing mode", () => {
73+
const gradientOptions: GradientOption[] = [
74+
{
75+
display_name: "Random",
76+
value: "none",
77+
path: "objects.test.gradient",
78+
packing_mode: "random",
79+
packing_mode_path: "objects.test.packing_mode",
80+
},
81+
{
82+
display_name: "Gradient A",
83+
value: "gradient_a",
84+
path: "objects.test.gradient",
85+
packing_mode: "gradient",
86+
packing_mode_path: "objects.test.packing_mode",
87+
},
88+
];
89+
const getCurrentValue = (path: string) => {
90+
if (path === "objects.test.packing_mode") return "random";
91+
return undefined;
92+
};
93+
const result = getSelectedGradient(gradientOptions, "default", getCurrentValue);
94+
95+
expect(result.currentGradient).toBe("none");
96+
expect(result.selectedOption.display_name).toBe("Random");
97+
});
98+
99+
test("deriveGradientStrength returns undefined when no strength path", () => {
100+
const option: GradientOption = {
101+
display_name: "Test Gradient",
102+
value: "test",
103+
path: "test.path",
104+
};
105+
const getCurrentValue = () => undefined;
106+
const result = deriveGradientStrength(option, getCurrentValue);
107+
108+
expect(result).toBeUndefined();
109+
});
110+
111+
test("deriveGradientStrength returns undefined when option is undefined", () => {
112+
const getCurrentValue = () => undefined;
113+
const result = deriveGradientStrength(undefined, getCurrentValue);
114+
115+
expect(result).toBeUndefined();
116+
});
117+
118+
test("deriveGradientStrength calculates strength with default values", () => {
119+
const option: GradientOption = {
120+
display_name: "Test Gradient",
121+
value: "test",
122+
path: "test.path",
123+
strength_path: "gradients.test.decay",
124+
strength_min: 0,
125+
strength_max: 10,
126+
strength_default: 5,
127+
};
128+
const getCurrentValue = () => undefined;
129+
const result = deriveGradientStrength(option, getCurrentValue);
130+
131+
expect(result).toBeDefined();
132+
expect(result?.displayName).toBe("Decay Length");
133+
expect(result?.path).toBe("gradients.test.decay");
134+
expect(result?.uiValue).toBe(5.0);
135+
expect(result?.min).toBe(0);
136+
expect(result?.max).toBe(10);
137+
});
138+
139+
test("deriveGradientStrength uses value from store", () => {
140+
const option: GradientOption = {
141+
display_name: "Test Gradient",
142+
value: "test",
143+
path: "test.path",
144+
strength_path: "gradients.test.decay",
145+
strength_min: 0,
146+
strength_max: 10,
147+
strength_default: 5,
148+
};
149+
const getCurrentValue = (path: string) => {
150+
if (path === "gradients.test.decay") return 7.5;
151+
return undefined;
152+
};
153+
const result = deriveGradientStrength(option, getCurrentValue);
154+
155+
expect(result?.uiValue).toBe(7.5);
156+
});
157+
158+
test("deriveGradientStrength clamps values to min/max", () => {
159+
const option: GradientOption = {
160+
display_name: "Test Gradient",
161+
value: "test",
162+
path: "test.path",
163+
strength_path: "gradients.test.decay",
164+
strength_min: 0,
165+
strength_max: 10,
166+
};
167+
const getCurrentValue = (path: string) => {
168+
if (path === "gradients.test.decay") return 15;
169+
return undefined;
170+
};
171+
const result = deriveGradientStrength(option, getCurrentValue);
172+
173+
expect(result?.uiValue).toBe(10.0);
174+
});
175+
176+
test("deriveGradientStrength uses custom description when provided", () => {
177+
const option: GradientOption = {
178+
display_name: "Test Gradient",
179+
value: "test",
180+
path: "test.path",
181+
strength_path: "gradients.test.decay",
182+
strength_description: "Custom description text",
183+
};
184+
const getCurrentValue = () => undefined;
185+
const result = deriveGradientStrength(option, getCurrentValue);
186+
187+
expect(result?.description).toBe("Custom description text");
188+
});
189+
190+
test("gradients recipe has correct structure", () => {
191+
const recipe = JSON.parse(fs.readFileSync("src/test/test-files/gradients.json", "utf8"));
192+
193+
expect(recipe.name).toBe("gradients");
194+
expect(recipe.version).toBe("default");
195+
expect(recipe.format_version).toBe("2.0");
196+
expect(recipe.bounding_box).toEqual([
197+
[-100, -100, 0],
198+
[100, 100, 1]
199+
]);
200+
});
201+
202+
test("gradients recipe contains radial gradient with correct settings", () => {
203+
const recipe = JSON.parse(fs.readFileSync("src/test/test-files/gradients.json", "utf8"));
204+
205+
expect(recipe.gradients?.radial_gradient).toBeDefined();
206+
expect(recipe.gradients?.radial_gradient.mode).toBe("radial");
207+
expect(recipe.gradients?.radial_gradient.weight_mode).toBe("cube");
208+
expect(recipe.gradients?.radial_gradient.pick_mode).toBe("rnd");
209+
expect(recipe.gradients?.radial_gradient.description).toBe("Radial gradient from the center");
210+
expect(recipe.gradients?.radial_gradient.mode_settings.radius).toBe(100);
211+
expect(recipe.gradients?.radial_gradient.mode_settings.center).toEqual([0, 0, 0]);
212+
});
213+
214+
test("gradients recipe contains vector gradient with correct settings", () => {
215+
const recipe = JSON.parse(fs.readFileSync("src/test/test-files/gradients.json", "utf8"));
216+
217+
expect(recipe.gradients?.vector_gradient).toBeDefined();
218+
expect(recipe.gradients?.vector_gradient.mode).toBe("vector");
219+
expect(recipe.gradients?.vector_gradient.weight_mode).toBe("cube");
220+
expect(recipe.gradients?.vector_gradient.pick_mode).toBe("rnd");
221+
expect(recipe.gradients?.vector_gradient.description).toBe("Gradient away from the plane formed by center and vector");
222+
expect(recipe.gradients?.vector_gradient.mode_settings.direction).toEqual([1, 1, 0]);
223+
});
224+
225+
test("gradients recipe object uses gradient packing mode", () => {
226+
const recipe = JSON.parse(fs.readFileSync("src/test/test-files/gradients.json", "utf8"));
227+
228+
expect(recipe.objects?.sphere).toBeDefined();
229+
expect(recipe.objects?.sphere.gradient).toBe("vector_gradient");
230+
expect(recipe.objects?.sphere.type).toBe("single_sphere");
231+
expect(recipe.objects?.sphere.radius).toBe(5);
232+
expect(recipe.objects?.base?.packing_mode).toBe("gradient");
233+
});

0 commit comments

Comments
 (0)