@nudojs/service 0.2.1 → 0.3.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.
@@ -1,247 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { typeValueToString } from "@nudojs/core";
3
- import { analyzeFile } from "../analyzer.ts";
4
-
5
- describe("fnSig impl: concrete args produce precise values", () => {
6
- it("Math.floor with literal returns exact result", () => {
7
- const source = `
8
- /// @nudo:env es
9
-
10
- /**
11
- * @nudo:case "concrete" (3.7)
12
- * @nudo:case "symbolic" (T.number)
13
- */
14
- function floorIt(x) {
15
- return Math.floor(x);
16
- }
17
- `;
18
- const result = analyzeFile("/test/math.js", source);
19
- const fn = result.functions[0];
20
- expect(fn.cases[0].name).toBe("concrete");
21
- expect(typeValueToString(fn.cases[0].result)).toBe("3");
22
- expect(fn.cases[1].name).toBe("symbolic");
23
- expect(typeValueToString(fn.cases[1].result)).toBe("number");
24
- });
25
-
26
- it("Math.max with literals returns exact result", () => {
27
- const source = `
28
- /// @nudo:env es
29
-
30
- /**
31
- * @nudo:case "test" (3, 7)
32
- */
33
- function maxOf(a, b) {
34
- return Math.max(a, b);
35
- }
36
- `;
37
- const result = analyzeFile("/test/math.js", source);
38
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe("7");
39
- });
40
-
41
- it("JSON.stringify with literal returns exact string", () => {
42
- const source = `
43
- /// @nudo:env es
44
-
45
- /**
46
- * @nudo:case "test" (42)
47
- */
48
- function toJson(x) {
49
- return JSON.stringify(x);
50
- }
51
- `;
52
- const result = analyzeFile("/test/json.js", source);
53
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe('"42"');
54
- });
55
-
56
- it("encodeURIComponent with literal returns exact string", () => {
57
- const source = `
58
- /// @nudo:env es
59
-
60
- /**
61
- * @nudo:case "test" ("hello world")
62
- */
63
- function encode(s) {
64
- return encodeURIComponent(s);
65
- }
66
- `;
67
- const result = analyzeFile("/test/encode.js", source);
68
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe('"hello%20world"');
69
- });
70
-
71
- it("Number.isInteger with literal returns exact boolean", () => {
72
- const source = `
73
- /// @nudo:env es
74
-
75
- /**
76
- * @nudo:case "int" (5)
77
- * @nudo:case "float" (5.5)
78
- */
79
- function checkInt(x) {
80
- return Number.isInteger(x);
81
- }
82
- `;
83
- const result = analyzeFile("/test/num.js", source);
84
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe("true");
85
- expect(typeValueToString(result.functions[0].cases[1].result)).toBe("false");
86
- });
87
-
88
- it("parseInt with literal returns exact number", () => {
89
- const source = `
90
- /// @nudo:env es
91
-
92
- /**
93
- * @nudo:case "test" ("42")
94
- */
95
- function parseIt(s) {
96
- return parseInt(s);
97
- }
98
- `;
99
- const result = analyzeFile("/test/parse.js", source);
100
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe("42");
101
- });
102
-
103
- it("Promise.resolve preserves argument type", () => {
104
- const source = `
105
- /// @nudo:env es
106
-
107
- /**
108
- * @nudo:case "test" (42)
109
- */
110
- async function wrap(x) {
111
- return await Promise.resolve(x);
112
- }
113
- `;
114
- const result = analyzeFile("/test/promise.js", source);
115
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe("Promise<42>");
116
- });
117
- });
118
-
119
- describe("fnSig impl: URL constructor with concrete args", () => {
120
- it("URL with literal string returns precise properties", () => {
121
- const source = `
122
- /// @nudo:env web
123
-
124
- /**
125
- * @nudo:case "test" ("https://example.com/path?q=hello#hash")
126
- */
127
- function parseUrl(raw) {
128
- const url = new URL(raw);
129
- return {
130
- host: url.hostname,
131
- path: url.pathname,
132
- query: url.search,
133
- hash: url.hash,
134
- };
135
- }
136
- `;
137
- const result = analyzeFile("/test/url.js", source);
138
- const res = typeValueToString(result.functions[0].cases[0].result);
139
- expect(res).toContain('"example.com"');
140
- expect(res).toContain('"/path"');
141
- expect(res).toContain('"?q=hello"');
142
- expect(res).toContain('"#hash"');
143
- });
144
-
145
- it("URL with symbolic string falls back to generic type", () => {
146
- const source = `
147
- /// @nudo:env web
148
-
149
- /**
150
- * @nudo:case "test" (T.string)
151
- */
152
- function parseUrl(raw) {
153
- const url = new URL(raw);
154
- return url.hostname;
155
- }
156
- `;
157
- const result = analyzeFile("/test/url.js", source);
158
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe("string");
159
- });
160
- });
161
-
162
- describe("fnSig impl: Node.js path with concrete args", () => {
163
- it("path.join with literals returns exact path", () => {
164
- const source = `
165
- /// @nudo:env node
166
-
167
- import { join } from "node:path";
168
-
169
- /**
170
- * @nudo:case "test" ("src", "utils", "index.js")
171
- */
172
- function buildPath(a, b, c) {
173
- return join(a, b, c);
174
- }
175
- `;
176
- const result = analyzeFile("/test/path.js", source);
177
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe('"src/utils/index.js"');
178
- });
179
-
180
- it("path.extname with literal returns exact extension", () => {
181
- const source = `
182
- /// @nudo:env node
183
-
184
- import { extname } from "node:path";
185
-
186
- /**
187
- * @nudo:case "test" ("file.ts")
188
- */
189
- function getExt(f) {
190
- return extname(f);
191
- }
192
- `;
193
- const result = analyzeFile("/test/path.js", source);
194
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe('".ts"');
195
- });
196
-
197
- it("path.basename with literal returns exact name", () => {
198
- const source = `
199
- /// @nudo:env node
200
-
201
- import { basename } from "node:path";
202
-
203
- /**
204
- * @nudo:case "test" ("/home/user/file.txt")
205
- */
206
- function getName(p) {
207
- return basename(p);
208
- }
209
- `;
210
- const result = analyzeFile("/test/path.js", source);
211
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe('"file.txt"');
212
- });
213
-
214
- it("path.dirname with literal returns exact dir", () => {
215
- const source = `
216
- /// @nudo:env node
217
-
218
- import { dirname } from "node:path";
219
-
220
- /**
221
- * @nudo:case "test" ("/home/user/file.txt")
222
- */
223
- function getDir(p) {
224
- return dirname(p);
225
- }
226
- `;
227
- const result = analyzeFile("/test/path.js", source);
228
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe('"/home/user"');
229
- });
230
-
231
- it("path.join with symbolic args falls back to string", () => {
232
- const source = `
233
- /// @nudo:env node
234
-
235
- import { join } from "node:path";
236
-
237
- /**
238
- * @nudo:case "test" (T.string, T.string)
239
- */
240
- function buildPath(a, b) {
241
- return join(a, b);
242
- }
243
- `;
244
- const result = analyzeFile("/test/path.js", source);
245
- expect(typeValueToString(result.functions[0].cases[0].result)).toBe("string");
246
- });
247
- });
@@ -1,29 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { T } from "@nudojs/core";
3
- import { generateGuardFunction } from "../guard-generator.ts";
4
-
5
- describe("guard-generator", () => {
6
- it("generates guard for string type", () => {
7
- const guard = generateGuardFunction("isString", T.string);
8
- expect(guard).toContain("typeof data === 'string'");
9
- });
10
-
11
- it("generates guard for object type", () => {
12
- const obj = T.object({ name: T.string, age: T.number });
13
- const guard = generateGuardFunction("isUser", obj);
14
- expect(guard).toContain("typeof data === 'object'");
15
- expect(guard).toContain("data.name");
16
- expect(guard).toContain("data.age");
17
- });
18
-
19
- it("generates guard for union type", () => {
20
- const union = T.union(T.string, T.number);
21
- const guard = generateGuardFunction("isStringOrNumber", union);
22
- expect(guard).toContain("||");
23
- });
24
-
25
- it("generates guard for array type", () => {
26
- const guard = generateGuardFunction("isStringArray", T.array(T.string));
27
- expect(guard).toContain("Array.isArray(data)");
28
- });
29
- });
@@ -1,332 +0,0 @@
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
- });
@@ -1,38 +0,0 @@
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
- });