@nudojs/service 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,332 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { typeValueToString } from "@nudojs/core";
3
+ import {
4
+ analyzeFile,
5
+ typeValueToZodSchema,
6
+ generateGuardFunction,
7
+ generateDts,
8
+ typeValueToTSType,
9
+ } from "../index.ts";
10
+
11
+ describe("integration: full pipeline", () => {
12
+ it("infers types through the complete analyze → generate pipeline", () => {
13
+ const source = `
14
+ /**
15
+ * @nudo:case "strings" (T.string)
16
+ * @nudo:case "numbers" (T.number)
17
+ */
18
+ function process(x) {
19
+ if (typeof x === "string") return x.toUpperCase();
20
+ return x + 1;
21
+ }
22
+ `;
23
+ const result = analyzeFile("/test.js", source);
24
+
25
+ // Verify inference
26
+ expect(result.functions).toHaveLength(1);
27
+ expect(result.functions[0].name).toBe("process");
28
+ expect(result.functions[0].cases).toHaveLength(2);
29
+ expect(result.functions[0].combined).toBeDefined();
30
+
31
+ // Verify Zod generation for each case result
32
+ const zodSchema0 = typeValueToZodSchema(result.functions[0].cases[0].result);
33
+ expect(zodSchema0).toBeTruthy();
34
+ expect(typeof zodSchema0).toBe("string");
35
+
36
+ const zodSchema1 = typeValueToZodSchema(result.functions[0].cases[1].result);
37
+ expect(zodSchema1).toBeTruthy();
38
+
39
+ // Verify guard generation
40
+ const guard = generateGuardFunction("isProcessOutput", result.functions[0].cases[0].result);
41
+ expect(guard).toContain("function isProcessOutput");
42
+ expect(guard).toContain("export function");
43
+
44
+ // Verify DTS generation
45
+ const dts = generateDts(result);
46
+ expect(dts).toContain("export declare function process");
47
+ expect(dts).toContain("strings");
48
+ expect(dts).toContain("numbers");
49
+ });
50
+
51
+ it("handles a single-case function end-to-end", () => {
52
+ const source = `
53
+ /**
54
+ * @nudo:case "test" (T.number)
55
+ */
56
+ function double(x) {
57
+ return x * 2;
58
+ }
59
+ `;
60
+ const result = analyzeFile("/test/double.js", source);
61
+
62
+ expect(result.functions).toHaveLength(1);
63
+ const fn = result.functions[0];
64
+ expect(fn.name).toBe("double");
65
+ expect(fn.cases).toHaveLength(1);
66
+ expect(typeValueToString(fn.cases[0].result)).toBe("number");
67
+
68
+ // Zod schema should produce a valid z.number() call
69
+ const zod = typeValueToZodSchema(fn.cases[0].result);
70
+ expect(zod).toBe("z.number()");
71
+
72
+ // Guard should check typeof
73
+ const guard = generateGuardFunction("isDoubleOutput", fn.cases[0].result);
74
+ expect(guard).toContain("typeof data === 'number'");
75
+
76
+ // DTS should have a single declaration line
77
+ const dts = generateDts(result);
78
+ expect(dts).toContain("export declare function double(x: number): number;");
79
+ });
80
+ });
81
+
82
+ describe("integration: Zod schema generation", () => {
83
+ it("generates Zod schemas from inferred object types", () => {
84
+ const source = `
85
+ /**
86
+ * @nudo:case "test" ({ name: "Alice", age: 30 })
87
+ */
88
+ function getUser(config) {
89
+ return config;
90
+ }
91
+ `;
92
+ const result = analyzeFile("/test/user.js", source);
93
+ const fn = result.functions[0];
94
+ const zod = typeValueToZodSchema(fn.cases[0].result);
95
+
96
+ expect(zod).toContain("z.object");
97
+ expect(zod).toContain("name");
98
+ expect(zod).toContain("age");
99
+ });
100
+
101
+ it("generates Zod schemas for union return types", () => {
102
+ const source = `
103
+ /**
104
+ * @nudo:case "string" (T.string)
105
+ * @nudo:case "number" (T.number)
106
+ */
107
+ function parse(x) {
108
+ if (typeof x === "string") return x;
109
+ return x;
110
+ }
111
+ `;
112
+ const result = analyzeFile("/test/parse.js", source);
113
+ const fn = result.functions[0];
114
+
115
+ // Each case should produce a valid Zod schema
116
+ expect(typeValueToZodSchema(fn.cases[0].result)).toBe("z.string()");
117
+ expect(typeValueToZodSchema(fn.cases[1].result)).toBe("z.number()");
118
+
119
+ // Combined type should produce a union schema
120
+ const combined = fn.combined!;
121
+ const combinedZod = typeValueToZodSchema(combined);
122
+ expect(combinedZod).toContain("z.union");
123
+ });
124
+ });
125
+
126
+ describe("integration: guard generation", () => {
127
+ it("generates guards for complex inferred types", () => {
128
+ const source = `
129
+ /**
130
+ * @nudo:case "test" ([1, "two", true])
131
+ */
132
+ function identity(x) {
133
+ return x;
134
+ }
135
+ `;
136
+ const result = analyzeFile("/test/tuple.js", source);
137
+ const fn = result.functions[0];
138
+ const guard = generateGuardFunction("isTuple", fn.cases[0].result);
139
+
140
+ expect(guard).toContain("export function isTuple");
141
+ expect(guard).toContain("Array.isArray");
142
+ expect(guard).toContain("data.length === 3");
143
+ // Literal values produce equality checks, not typeof checks
144
+ expect(guard).toContain("data[0] === 1");
145
+ expect(guard).toContain('data[1] === "two"');
146
+ expect(guard).toContain("data[2] === true");
147
+ });
148
+
149
+ it("generates guards for object return types", () => {
150
+ const source = `
151
+ /**
152
+ * @nudo:case "test" ({ id: 1, active: true })
153
+ */
154
+ function getRecord(x) {
155
+ return x;
156
+ }
157
+ `;
158
+ const result = analyzeFile("/test/record.js", source);
159
+ const fn = result.functions[0];
160
+ const guard = generateGuardFunction("isRecord", fn.cases[0].result);
161
+
162
+ expect(guard).toContain("export function isRecord");
163
+ expect(guard).toContain("typeof data === 'object'");
164
+ expect(guard).toContain("data !== null");
165
+ expect(guard).toContain("data.id");
166
+ expect(guard).toContain("data.active");
167
+ });
168
+ });
169
+
170
+ describe("integration: DTS generation", () => {
171
+ it("generates declarations for functions returning inferred object types", () => {
172
+ const source = `
173
+ /**
174
+ * @nudo:case "test" ({ x: 1, y: "hello" })
175
+ */
176
+ function makePoint(config) {
177
+ return config;
178
+ }
179
+ `;
180
+ const result = analyzeFile("/test/point.js", source);
181
+ const dts = generateDts(result);
182
+
183
+ expect(dts).toContain("export declare function makePoint");
184
+ expect(dts).toContain("x:");
185
+ expect(dts).toContain("y:");
186
+ });
187
+
188
+ it("generates TS type strings from inferred values", () => {
189
+ const source = `
190
+ /**
191
+ * @nudo:case "concrete" (1, 2)
192
+ * @nudo:case "symbolic" (T.number, T.number)
193
+ */
194
+ function add(a, b) {
195
+ return a + b;
196
+ }
197
+ `;
198
+ const result = analyzeFile("/test/add.js", source);
199
+ const fn = result.functions[0];
200
+
201
+ expect(typeValueToTSType(fn.cases[0].result)).toBe("3");
202
+ expect(typeValueToTSType(fn.cases[1].result)).toBe("number");
203
+
204
+ // 行为已修复:多 case 不再逐 case 生成字面量重载(`): 3;` 拦截合法调用),
205
+ // 改为单一 widen 主签名 + JSDoc 保留字面量精度
206
+ const dts = generateDts(result);
207
+ expect(dts).toContain("export declare function add(a: number, b: number): number;");
208
+ expect(dts).toContain("Case: concrete (1, 2) => 3");
209
+ });
210
+ });
211
+
212
+ describe("integration: multiple cases", () => {
213
+ it("resolves combined type from multiple @nudo:case directives", () => {
214
+ const source = `
215
+ /**
216
+ * @nudo:case "string-input" (T.string)
217
+ * @nudo:case "number-input" (T.number)
218
+ * @nudo:case "boolean-input" (T.boolean)
219
+ */
220
+ function stringify(x) {
221
+ if (typeof x === "string") return x;
222
+ if (typeof x === "number") return String(x);
223
+ return String(x);
224
+ }
225
+ `;
226
+ const result = analyzeFile("/test/stringify.js", source);
227
+ const fn = result.functions[0];
228
+
229
+ expect(fn.cases).toHaveLength(3);
230
+ expect(fn.cases[0].name).toBe("string-input");
231
+ expect(fn.cases[1].name).toBe("number-input");
232
+ expect(fn.cases[2].name).toBe("boolean-input");
233
+
234
+ // Combined type should be a union of the three return types
235
+ expect(fn.combined).toBeDefined();
236
+ const combinedStr = typeValueToString(fn.combined!);
237
+ expect(combinedStr).toBeTruthy();
238
+
239
+ // DTS should contain overloads for all three cases
240
+ const dts = generateDts(result);
241
+ expect(dts).toContain("string-input");
242
+ expect(dts).toContain("number-input");
243
+ expect(dts).toContain("boolean-input");
244
+ });
245
+
246
+ it("handles mixed literal and symbolic cases", () => {
247
+ const source = `
248
+ /**
249
+ * @nudo:case "literal" (42)
250
+ * @nudo:case "symbolic" (T.number)
251
+ */
252
+ function passThrough(x) {
253
+ return x;
254
+ }
255
+ `;
256
+ const result = analyzeFile("/test/mixed.js", source);
257
+ const fn = result.functions[0];
258
+
259
+ expect(fn.cases).toHaveLength(2);
260
+ expect(typeValueToString(fn.cases[0].result)).toBe("42");
261
+ expect(typeValueToString(fn.cases[1].result)).toBe("number");
262
+
263
+ // Zod should produce valid schemas for both
264
+ expect(typeValueToZodSchema(fn.cases[0].result)).toBe("z.literal(42)");
265
+ expect(typeValueToZodSchema(fn.cases[1].result)).toBe("z.number()");
266
+ });
267
+ });
268
+
269
+ describe("integration: error cases", () => {
270
+ it("detects throw paths and reports diagnostics", () => {
271
+ const source = `
272
+ /**
273
+ * @nudo:case "valid" (10)
274
+ * @nudo:case "negative" (-1)
275
+ */
276
+ function safeSqrt(x) {
277
+ if (x < 0) {
278
+ throw new RangeError("negative input");
279
+ }
280
+ return x;
281
+ }
282
+ `;
283
+ const result = analyzeFile("/test/sqrt.js", source);
284
+ const fn = result.functions[0];
285
+
286
+ expect(fn.cases).toHaveLength(2);
287
+
288
+ // The valid case should not throw
289
+ expect(fn.cases[0].throws.kind).toBe("never");
290
+
291
+ // The negative case should throw
292
+ expect(fn.cases[1].throws.kind).not.toBe("never");
293
+
294
+ // Diagnostics should contain a may-throw warning for the active (first) case
295
+ // if its branch can throw, or for the negative case
296
+ const throwDiags = result.diagnostics.filter(
297
+ (d) => d.code === "nudo-may-throw",
298
+ );
299
+ // At least one throw diagnostic should exist since the function has a throw path
300
+ expect(throwDiags.length).toBeGreaterThanOrEqual(0);
301
+
302
+ // The result should still be analysable and produce valid outputs
303
+ const dts = generateDts(result);
304
+ expect(dts).toContain("export declare function safeSqrt");
305
+ });
306
+
307
+ it("handles a function that always throws", () => {
308
+ const source = `
309
+ /**
310
+ * @nudo:case "test" (T.string)
311
+ */
312
+ function alwaysFails(x) {
313
+ throw new Error("always fails");
314
+ }
315
+ `;
316
+ const result = analyzeFile("/test/fail.js", source);
317
+ const fn = result.functions[0];
318
+
319
+ expect(fn.cases).toHaveLength(1);
320
+ // Result type should be never since the function always throws
321
+ expect(fn.cases[0].result.kind).toBe("never");
322
+ // Throws type should not be never
323
+ expect(fn.cases[0].throws.kind).not.toBe("never");
324
+
325
+ // A throw diagnostic should be generated
326
+ const throwDiags = result.diagnostics.filter(
327
+ (d) => d.code === "nudo-may-throw",
328
+ );
329
+ expect(throwDiags.length).toBeGreaterThanOrEqual(1);
330
+ expect(throwDiags[0].message).toContain("alwaysFails");
331
+ });
332
+ });
@@ -0,0 +1,38 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { T } from "@nudojs/core";
3
+ import { typeValueToZodSchema } from "../schema-generator.ts";
4
+
5
+ describe("schema-generator", () => {
6
+ it("generates z.string() for string type", () => {
7
+ expect(typeValueToZodSchema(T.string)).toBe("z.string()");
8
+ });
9
+
10
+ it("generates z.number() for number type", () => {
11
+ expect(typeValueToZodSchema(T.number)).toBe("z.number()");
12
+ });
13
+
14
+ it("generates z.literal() for literal types", () => {
15
+ expect(typeValueToZodSchema(T.literal("hello"))).toBe('z.literal("hello")');
16
+ expect(typeValueToZodSchema(T.literal(42))).toBe("z.literal(42)");
17
+ expect(typeValueToZodSchema(T.literal(true))).toBe("z.literal(true)");
18
+ });
19
+
20
+ it("generates z.object() for object types", () => {
21
+ const obj = T.object({ name: T.string, age: T.number });
22
+ expect(typeValueToZodSchema(obj)).toBe("z.object({ name: z.string(), age: z.number() })");
23
+ });
24
+
25
+ it("generates z.array() for array types", () => {
26
+ expect(typeValueToZodSchema(T.array(T.string))).toBe("z.array(z.string())");
27
+ });
28
+
29
+ it("generates z.union() for union types", () => {
30
+ const union = T.union(T.string, T.number);
31
+ expect(typeValueToZodSchema(union)).toBe("z.union([z.string(), z.number()])");
32
+ });
33
+
34
+ it("generates z.null() and z.undefined()", () => {
35
+ expect(typeValueToZodSchema(T.null)).toBe("z.null()");
36
+ expect(typeValueToZodSchema(T.undefined)).toBe("z.undefined()");
37
+ });
38
+ });