Skip to content

Commit 60e7425

Browse files
committed
feat: add comprehensive tests for loom functionality
- Introduced a new test file `loom.test.ts` to validate the behavior of the `createLoom` function. - Added tests for input processing, schema validation, option validation, version comparison helpers, and handling of empty input arrays. - Ensured that the loom instance correctly processes inputs and adheres to defined schemas.
1 parent cc5fede commit 60e7425

File tree

2 files changed

+134
-5
lines changed

2 files changed

+134
-5
lines changed

src/loom.test.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { expect, it } from "vitest";
2+
import { z } from "zod";
3+
import { createLoom } from "./loom";
4+
5+
const testInputSchema = z.object({
6+
name: z.string(),
7+
value: z.number(),
8+
});
9+
10+
const testOptionsSchema = z.object({
11+
version: z.string(),
12+
prefix: z.string(),
13+
});
14+
15+
it("should create a loom instance that processes input correctly", () => {
16+
const loom = createLoom({
17+
inputSchema: testInputSchema,
18+
optionsSchema: testOptionsSchema,
19+
template: (ctx, item) => `${ctx.options.prefix} ${item.name}: ${item.value}`,
20+
});
21+
22+
const result = loom({
23+
version: "1.0.0",
24+
prefix: "Test",
25+
input: [
26+
{ name: "item1", value: 42 },
27+
{ name: "item2", value: 123 },
28+
],
29+
});
30+
31+
expect(result).toBe("Test item1: 42\nTest item2: 123");
32+
});
33+
34+
it("should skip items when predicate returns false", () => {
35+
const loom = createLoom({
36+
inputSchema: testInputSchema,
37+
optionsSchema: testOptionsSchema,
38+
template: (ctx, item) => `${item.name}: ${item.value}`,
39+
predicate: (ctx, item) => item.value > 50,
40+
});
41+
42+
const result = loom({
43+
version: "1.0.0",
44+
prefix: "Test",
45+
input: [
46+
{ name: "item1", value: 42 },
47+
{ name: "item2", value: 123 },
48+
{ name: "item3", value: 30 },
49+
],
50+
});
51+
52+
expect(result).toBe("item2: 123");
53+
});
54+
55+
it("should validate input against schema", () => {
56+
const loom = createLoom({
57+
inputSchema: testInputSchema,
58+
optionsSchema: testOptionsSchema,
59+
template: (ctx, item) => `${item.name}: ${item.value}`,
60+
});
61+
62+
expect(() =>
63+
loom({
64+
version: "1.0.0",
65+
prefix: "Test",
66+
input: [
67+
// @ts-expect-error wrong type
68+
{ name: "item1", value: "" },
69+
],
70+
}),
71+
).toThrow();
72+
});
73+
74+
it("should validate options against schema", () => {
75+
const loom = createLoom({
76+
inputSchema: testInputSchema,
77+
optionsSchema: testOptionsSchema,
78+
template: (ctx, item) => `${item.name}: ${item.value}`,
79+
});
80+
81+
expect(() =>
82+
loom({
83+
// missing required 'version' field
84+
prefix: "Test",
85+
input: [{ name: "item1", value: 42 }],
86+
} as any),
87+
).toThrow();
88+
});
89+
90+
it("should provide correct version comparison helpers", () => {
91+
const loom = createLoom({
92+
inputSchema: testInputSchema,
93+
optionsSchema: testOptionsSchema,
94+
template: (ctx, item) => {
95+
const versionChecks = [
96+
ctx.isVersionLessThan("1.1.0"),
97+
ctx.isVersionGreaterThan("0.9.0"),
98+
ctx.isVersionEqual("1.0.0"),
99+
ctx.isVersionGreaterThanOrEqual("1.0.0"),
100+
ctx.isVersionLessThanOrEqual("1.0.0"),
101+
];
102+
return `${item.name}: ${versionChecks.join(", ")}`;
103+
},
104+
});
105+
106+
const result = loom({
107+
version: "1.0.0",
108+
prefix: "Test",
109+
input: [{ name: "test", value: 42 }],
110+
});
111+
112+
expect(result).toBe("test: true, true, true, true, true");
113+
});
114+
115+
it("should handle empty input array", () => {
116+
const loom = createLoom({
117+
inputSchema: testInputSchema,
118+
optionsSchema: testOptionsSchema,
119+
template: (ctx, item) => `${item.name}: ${item.value}`,
120+
});
121+
122+
const result = loom({
123+
version: "1.0.0",
124+
prefix: "Test",
125+
input: [],
126+
});
127+
128+
expect(result).toBe("");
129+
});

src/loom.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ function buildLoomContext<TOptionsSchema extends z.ZodType>(options: TOptionsSch
3636
return {
3737
options,
3838
isVersionLessThan: (version) => {
39-
return compare(version, options.version, "<");
39+
return compare(options.version, version, "<");
4040
},
4141
isVersionGreaterThan: (version) => {
42-
return compare(version, options.version, ">");
42+
return compare(options.version, version, ">");
4343
},
4444
isVersionEqual: (version) => {
45-
return compare(version, options.version, "=");
45+
return compare(options.version, version, "=");
4646
},
4747
isVersionGreaterThanOrEqual: (version) => {
48-
return compare(version, options.version, ">=");
48+
return compare(options.version, version, ">=");
4949
},
5050
isVersionLessThanOrEqual: (version) => {
51-
return compare(version, options.version, "<=");
51+
return compare(options.version, version, "<=");
5252
},
5353
};
5454
}

0 commit comments

Comments
 (0)