-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.test.ts
More file actions
69 lines (57 loc) · 2.08 KB
/
json.test.ts
File metadata and controls
69 lines (57 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Unit tests for src/server/json.ts.
*/
import { describe, expect, test } from "bun:test";
import { jsonRespond, readMcpServerVersion, spreadDefined, spreadWhen } from "./json.js";
describe("readMcpServerVersion", () => {
test("returns a valid major.minor.patch string", () => {
const v = readMcpServerVersion();
expect(v).toMatch(/^\d+\.\d+\.\d+$/);
});
test("result satisfies the FastMCP version type constraint", () => {
const v = readMcpServerVersion();
const parts = v.split(".").map(Number);
expect(parts).toHaveLength(3);
for (const p of parts) {
expect(Number.isInteger(p)).toBe(true);
expect(p).toBeGreaterThanOrEqual(0);
}
});
});
describe("jsonRespond", () => {
test("serialises a simple object", () => {
expect(jsonRespond({ ok: true })).toBe('{"ok":true}');
});
test("serialises nested objects", () => {
const result = jsonRespond({ error: "not_found", path: "/a/b" });
expect(JSON.parse(result)).toEqual({ error: "not_found", path: "/a/b" });
});
});
describe("spreadWhen", () => {
test("returns the fields when cond is true", () => {
const result = spreadWhen(true, { foo: "bar" });
expect(result).toEqual({ foo: "bar" });
});
test("returns an empty object when cond is false", () => {
const result = spreadWhen(false, { foo: "bar" });
expect(result).toEqual({});
});
test("spread into an object literal works as expected", () => {
const out = { ...spreadWhen(true, { a: 1 }), ...spreadWhen(false, { b: 2 }) };
expect(out).toEqual({ a: 1 });
});
});
describe("spreadDefined", () => {
test("spreads the key when value is defined", () => {
const result = spreadDefined("count", 42);
expect(result).toEqual({ count: 42 });
});
test("returns empty object when value is undefined", () => {
const result = spreadDefined("count", undefined);
expect(result).toEqual({});
});
test("false and 0 are treated as defined (not undefined)", () => {
expect(spreadDefined("flag", false)).toEqual({ flag: false });
expect(spreadDefined("num", 0)).toEqual({ num: 0 });
});
});