|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +import { |
| 4 | + jsonRespond, |
| 5 | + MCP_JSON_FORMAT_VERSION, |
| 6 | + readMcpServerVersion, |
| 7 | + readPackageVersion, |
| 8 | + spreadDefined, |
| 9 | + spreadWhen, |
| 10 | + truncateLines, |
| 11 | + truncateText, |
| 12 | +} from "./json.js"; |
| 13 | + |
| 14 | +describe("MCP_JSON_FORMAT_VERSION", () => { |
| 15 | + test("is '1'", () => { |
| 16 | + expect(MCP_JSON_FORMAT_VERSION).toBe("1"); |
| 17 | + }); |
| 18 | +}); |
| 19 | + |
| 20 | +describe("readPackageVersion", () => { |
| 21 | + test("returns a semver string from package.json", () => { |
| 22 | + const v = readPackageVersion(); |
| 23 | + expect(v).toMatch(/^\d+\.\d+\.\d+/); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe("readMcpServerVersion", () => { |
| 28 | + test("returns major.minor.patch format", () => { |
| 29 | + const v = readMcpServerVersion(); |
| 30 | + expect(v).toMatch(/^\d+\.\d+\.\d+$/); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe("jsonRespond", () => { |
| 35 | + test("returns minified JSON", () => { |
| 36 | + const result = jsonRespond({ a: 1, b: "two" }); |
| 37 | + expect(result).toBe('{"a":1,"b":"two"}'); |
| 38 | + }); |
| 39 | + |
| 40 | + test("handles nested objects", () => { |
| 41 | + const result = jsonRespond({ x: { y: [1, 2] } }); |
| 42 | + expect(result).toBe('{"x":{"y":[1,2]}}'); |
| 43 | + }); |
| 44 | + |
| 45 | + test("handles empty object", () => { |
| 46 | + expect(jsonRespond({})).toBe("{}"); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe("spreadWhen", () => { |
| 51 | + test("returns fields when condition is true", () => { |
| 52 | + const result = { base: 1, ...spreadWhen(true, { extra: 2 }) }; |
| 53 | + expect(result).toEqual({ base: 1, extra: 2 }); |
| 54 | + }); |
| 55 | + |
| 56 | + test("returns empty object when condition is false", () => { |
| 57 | + const result = { base: 1, ...spreadWhen(false, { extra: 2 }) }; |
| 58 | + expect(result).toEqual({ base: 1 }); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("spreadDefined", () => { |
| 63 | + test("spreads key when value is defined", () => { |
| 64 | + const result = { a: 1, ...spreadDefined("b", 42) }; |
| 65 | + expect(result).toEqual({ a: 1, b: 42 }); |
| 66 | + }); |
| 67 | + |
| 68 | + test("spreads nothing when value is undefined", () => { |
| 69 | + const result = { a: 1, ...spreadDefined("b", undefined) }; |
| 70 | + expect(result).toEqual({ a: 1 }); |
| 71 | + }); |
| 72 | + |
| 73 | + test("spreads falsy values that are not undefined", () => { |
| 74 | + expect({ ...spreadDefined("x", 0) }).toEqual({ x: 0 }); |
| 75 | + expect({ ...spreadDefined("x", "") }).toEqual({ x: "" }); |
| 76 | + expect({ ...spreadDefined("x", false) }).toEqual({ x: false }); |
| 77 | + expect({ ...spreadDefined("x", null) }).toEqual({ x: null }); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe("truncateLines", () => { |
| 82 | + test("returns text unchanged when within limit", () => { |
| 83 | + const text = "line1\nline2\nline3"; |
| 84 | + expect(truncateLines(text, 5)).toBe(text); |
| 85 | + }); |
| 86 | + |
| 87 | + test("returns text unchanged at exact limit", () => { |
| 88 | + const text = "a\nb\nc"; |
| 89 | + expect(truncateLines(text, 3)).toBe(text); |
| 90 | + }); |
| 91 | + |
| 92 | + test("truncates and appends notice when over limit", () => { |
| 93 | + const text = "1\n2\n3\n4\n5"; |
| 94 | + const result = truncateLines(text, 2); |
| 95 | + expect(result).toBe("1\n2\n... [3 lines truncated]"); |
| 96 | + }); |
| 97 | + |
| 98 | + test("single line over limit keeps first line", () => { |
| 99 | + // 1 line, limit 1 — no truncation |
| 100 | + expect(truncateLines("only", 1)).toBe("only"); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe("truncateText", () => { |
| 105 | + test("returns text unchanged when within limit", () => { |
| 106 | + expect(truncateText("short", 100)).toBe("short"); |
| 107 | + }); |
| 108 | + |
| 109 | + test("returns text unchanged at exact limit", () => { |
| 110 | + expect(truncateText("12345", 5)).toBe("12345"); |
| 111 | + }); |
| 112 | + |
| 113 | + test("truncates and appends notice when over limit", () => { |
| 114 | + const result = truncateText("abcdef", 3); |
| 115 | + expect(result).toBe("abc\n... [truncated]"); |
| 116 | + }); |
| 117 | +}); |
0 commit comments