xonecas.io
5875
Os

ollama-space-molt-player / files

ollama-space-molt-player / packages / cui / tests / formatter.test.ts

5.6 KB
1import { describe, expect, test } from "bun:test";
2import {
3	formatDetailLines,
4	formatMultiline,
5	formatPayloadLines,
6	formatThinking,
7	maskSensitiveData,
8	normalizeErrorDetails,
9	splitSentences,
10	truncateLine,
11} from "../src/formatter.ts";
12
13describe("truncateLine", () => {
14	test("returns short lines unchanged", () => {
15		const line = "short line";
16		expect(truncateLine(line)).toBe(line);
17	});
18
19	test("truncates long lines with ellipsis", () => {
20		const longLine = "x".repeat(400);
21		const result = truncateLine(longLine);
22		expect(result.length).toBe(303);
23		expect(result.endsWith("...")).toBe(true);
24	});
25});
26
27describe("formatMultiline", () => {
28	test("returns all lines in verbose mode", () => {
29		const text = Array(10).fill("line").join("\n");
30		const result = formatMultiline(text, true);
31		expect(result.length).toBe(10);
32	});
33
34	test("truncates to 5 lines in normal mode", () => {
35		const text = Array(10).fill("line").join("\n");
36		const result = formatMultiline(text, false);
37
38		expect(result.length).toBe(5);
39		expect(result[1]).toContain("more lines");
40	});
41
42	test("preserves short text", () => {
43		const text = "line1\nline2\nline3";
44		const result = formatMultiline(text, false);
45		expect(result.length).toBe(3);
46	});
47});
48
49describe("splitSentences", () => {
50	test("splits on periods", () => {
51		const result = splitSentences("First. Second. Third.");
52		expect(result).toEqual(["First.", "Second.", "Third."]);
53	});
54
55	test("handles empty string", () => {
56		expect(splitSentences("")).toEqual([]);
57	});
58
59	test("handles text without periods", () => {
60		expect(splitSentences("no periods here")).toEqual(["no periods here"]);
61	});
62});
63
64describe("formatThinking", () => {
65	test("returns full text in verbose mode", () => {
66		const thinking = "First. Second. Third. Fourth. Fifth.";
67		expect(formatThinking(thinking, true)).toBe(thinking);
68	});
69
70	test("truncates long thinking in normal mode", () => {
71		const thinking = "First. Second. Third. Fourth. Fifth.";
72		const result = formatThinking(thinking, false);
73		expect(result).toContain("omitted");
74		expect(result).toContain("First.");
75		expect(result).toContain("Fifth.");
76	});
77
78	test("preserves short thinking", () => {
79		const thinking = "First. Second.";
80		const result = formatThinking(thinking, false);
81		expect(result).toBe("First. Second.");
82	});
83});
84
85describe("maskSensitiveData", () => {
86	test("masks token fields", () => {
87		const data = { token: "secret12345678" };
88		const result = maskSensitiveData(data) as Record<string, unknown>;
89		expect(result.token).toBe("secret12...");
90	});
91
92	test("masks password fields", () => {
93		const data = { password: "secret12345678" };
94		const result = maskSensitiveData(data) as Record<string, unknown>;
95		expect(result.password).toBe("secret12...");
96	});
97
98	test("preserves non-sensitive fields", () => {
99		const data = { username: "player1", credits: 100 };
100		expect(maskSensitiveData(data)).toEqual(data);
101	});
102
103	test("handles nested objects", () => {
104		const data = { user: { password: "secret12345678", name: "test" } };
105		const result = maskSensitiveData(data) as Record<string, Record<string, unknown>>;
106		expect(result.user.password).toBe("secret12...");
107		expect(result.user.name).toBe("test");
108	});
109
110	test("handles arrays", () => {
111		const data = [{ token: "secret12345678" }, { token: "another12345678" }];
112		const result = maskSensitiveData(data) as Array<Record<string, unknown>>;
113		expect(result[0].token).toBe("secret12...");
114		expect(result[1].token).toBe("another1...");
115	});
116});
117
118describe("normalizeErrorDetails", () => {
119	test("extracts message and stack from Error", () => {
120		const error = new Error("test error");
121		const result = normalizeErrorDetails(error) as Record<string, unknown>;
122		expect(result.message).toBe("test error");
123		expect(result.stack).toBeDefined();
124	});
125
126	test("returns non-Error values unchanged", () => {
127		const data = { error: "some error" };
128		expect(normalizeErrorDetails(data)).toBe(data);
129	});
130});
131
132describe("formatPayloadLines", () => {
133	test("formats simple object", () => {
134		const data = { name: "test", value: 42 };
135		const result = formatPayloadLines(data, "", true, { current: 0 });
136		expect(result).toContain("name: test");
137		expect(result).toContain("value: 42");
138	});
139
140	test("truncates in normal mode", () => {
141		const data = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 };
142		const result = formatPayloadLines(data, "", false, { current: 0 });
143		expect(result[result.length - 1]).toBe("...");
144	});
145
146	test("formats nested objects", () => {
147		const data = { outer: { inner: "value" } };
148		const result = formatPayloadLines(data, "", true, { current: 0 });
149		expect(result.some((line) => line.includes("outer:"))).toBe(true);
150		expect(result.some((line) => line.includes("inner: value"))).toBe(true);
151	});
152
153	test("formats arrays", () => {
154		const data = { items: [1, 2, 3] };
155		const result = formatPayloadLines(data, "", true, { current: 0 });
156		expect(result.some((line) => line.includes("[1, 2, 3]"))).toBe(true);
157	});
158
159	test("summarizes object arrays", () => {
160		const data = { items: [{ id: 1 }, { id: 2 }] };
161		const result = formatPayloadLines(data, "", false, { current: 0 });
162		expect(result.some((line) => line.includes("[2 items]"))).toBe(true);
163	});
164});
165
166describe("formatDetailLines", () => {
167	test("returns empty array for null/undefined", () => {
168		expect(formatDetailLines(null, false)).toEqual([]);
169		expect(formatDetailLines(undefined, false)).toEqual([]);
170	});
171
172	test("handles string input", () => {
173		const result = formatDetailLines("test string", false);
174		expect(result).toEqual(["test string"]);
175	});
176
177	test("handles object input", () => {
178		const result = formatDetailLines({ key: "value" }, false);
179		expect(result.some((line) => line.includes("key: value"))).toBe(true);
180	});
181});