From 3a7b1b36b64b4cb81ac14424a48f5183af77749f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 23:59:28 +0000 Subject: [PATCH] perf(test): consolidate shared utility suites --- src/shared/chat-content.test.ts | 26 -------- src/shared/frontmatter.test.ts | 43 ------------- src/shared/node-match.test.ts | 44 ------------- src/shared/shared-misc.test.ts | 111 ++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 113 deletions(-) delete mode 100644 src/shared/chat-content.test.ts delete mode 100644 src/shared/frontmatter.test.ts delete mode 100644 src/shared/node-match.test.ts create mode 100644 src/shared/shared-misc.test.ts diff --git a/src/shared/chat-content.test.ts b/src/shared/chat-content.test.ts deleted file mode 100644 index 7e01037fe11..00000000000 --- a/src/shared/chat-content.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { extractTextFromChatContent } from "./chat-content.js"; - -describe("extractTextFromChatContent", () => { - it("normalizes string content", () => { - expect(extractTextFromChatContent(" hello\nworld ")).toBe("hello world"); - }); - - it("extracts text blocks from array content", () => { - expect( - extractTextFromChatContent([ - { type: "text", text: " hello " }, - { type: "image_url", image_url: "https://example.com" }, - { type: "text", text: "world" }, - ]), - ).toBe("hello world"); - }); - - it("applies sanitizer when provided", () => { - expect( - extractTextFromChatContent("Here [Tool Call: foo (ID: 1)] ok", { - sanitizeText: (text) => text.replace(/\[Tool Call:[^\]]+\]\s*/g, ""), - }), - ).toBe("Here ok"); - }); -}); diff --git a/src/shared/frontmatter.test.ts b/src/shared/frontmatter.test.ts deleted file mode 100644 index 78265a58574..00000000000 --- a/src/shared/frontmatter.test.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { describe, expect, test } from "vitest"; -import { - getFrontmatterString, - normalizeStringList, - parseFrontmatterBool, - resolveOpenClawManifestBlock, -} from "./frontmatter.js"; - -describe("shared/frontmatter", () => { - test("normalizeStringList handles strings and arrays", () => { - expect(normalizeStringList("a, b,,c")).toEqual(["a", "b", "c"]); - expect(normalizeStringList([" a ", "", "b"])).toEqual(["a", "b"]); - expect(normalizeStringList(null)).toEqual([]); - }); - - test("getFrontmatterString extracts strings only", () => { - expect(getFrontmatterString({ a: "b" }, "a")).toBe("b"); - expect(getFrontmatterString({ a: 1 }, "a")).toBeUndefined(); - }); - - test("parseFrontmatterBool respects fallback", () => { - expect(parseFrontmatterBool("true", false)).toBe(true); - expect(parseFrontmatterBool("false", true)).toBe(false); - expect(parseFrontmatterBool(undefined, true)).toBe(true); - }); - - test("resolveOpenClawManifestBlock parses JSON5 metadata and picks openclaw block", () => { - const frontmatter = { - metadata: "{ openclaw: { foo: 1, bar: 'baz' } }", - }; - expect(resolveOpenClawManifestBlock({ frontmatter })).toEqual({ foo: 1, bar: "baz" }); - }); - - test("resolveOpenClawManifestBlock returns undefined for invalid input", () => { - expect(resolveOpenClawManifestBlock({ frontmatter: {} })).toBeUndefined(); - expect( - resolveOpenClawManifestBlock({ frontmatter: { metadata: "not-json5" } }), - ).toBeUndefined(); - expect( - resolveOpenClawManifestBlock({ frontmatter: { metadata: "{ nope: { a: 1 } }" } }), - ).toBeUndefined(); - }); -}); diff --git a/src/shared/node-match.test.ts b/src/shared/node-match.test.ts deleted file mode 100644 index d8d33f1bacb..00000000000 --- a/src/shared/node-match.test.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { resolveNodeIdFromCandidates } from "./node-match.js"; - -describe("resolveNodeIdFromCandidates", () => { - it("matches nodeId", () => { - expect( - resolveNodeIdFromCandidates( - [ - { nodeId: "mac-123", displayName: "Mac Studio", remoteIp: "100.0.0.1" }, - { nodeId: "pi-456", displayName: "Raspberry Pi", remoteIp: "100.0.0.2" }, - ], - "pi-456", - ), - ).toBe("pi-456"); - }); - - it("matches displayName using normalization", () => { - expect( - resolveNodeIdFromCandidates([{ nodeId: "mac-123", displayName: "Mac Studio" }], "mac studio"), - ).toBe("mac-123"); - }); - - it("matches nodeId prefix (>=6 chars)", () => { - expect(resolveNodeIdFromCandidates([{ nodeId: "mac-abcdef" }], "mac-ab")).toBe("mac-abcdef"); - }); - - it("throws unknown node with known list", () => { - expect(() => - resolveNodeIdFromCandidates( - [ - { nodeId: "mac-123", displayName: "Mac Studio", remoteIp: "100.0.0.1" }, - { nodeId: "pi-456" }, - ], - "nope", - ), - ).toThrow(/unknown node: nope.*known: /); - }); - - it("throws ambiguous node with matches list", () => { - expect(() => - resolveNodeIdFromCandidates([{ nodeId: "mac-abcdef" }, { nodeId: "mac-abc999" }], "mac-abc"), - ).toThrow(/ambiguous node: mac-abc.*matches:/); - }); -}); diff --git a/src/shared/shared-misc.test.ts b/src/shared/shared-misc.test.ts new file mode 100644 index 00000000000..298b3ff0a55 --- /dev/null +++ b/src/shared/shared-misc.test.ts @@ -0,0 +1,111 @@ +import { describe, expect, it, test } from "vitest"; +import { extractTextFromChatContent } from "./chat-content.js"; +import { + getFrontmatterString, + normalizeStringList, + parseFrontmatterBool, + resolveOpenClawManifestBlock, +} from "./frontmatter.js"; +import { resolveNodeIdFromCandidates } from "./node-match.js"; + +describe("extractTextFromChatContent", () => { + it("normalizes string content", () => { + expect(extractTextFromChatContent(" hello\nworld ")).toBe("hello world"); + }); + + it("extracts text blocks from array content", () => { + expect( + extractTextFromChatContent([ + { type: "text", text: " hello " }, + { type: "image_url", image_url: "https://example.com" }, + { type: "text", text: "world" }, + ]), + ).toBe("hello world"); + }); + + it("applies sanitizer when provided", () => { + expect( + extractTextFromChatContent("Here [Tool Call: foo (ID: 1)] ok", { + sanitizeText: (text) => text.replace(/\[Tool Call:[^\]]+\]\s*/g, ""), + }), + ).toBe("Here ok"); + }); +}); + +describe("shared/frontmatter", () => { + test("normalizeStringList handles strings and arrays", () => { + expect(normalizeStringList("a, b,,c")).toEqual(["a", "b", "c"]); + expect(normalizeStringList([" a ", "", "b"])).toEqual(["a", "b"]); + expect(normalizeStringList(null)).toEqual([]); + }); + + test("getFrontmatterString extracts strings only", () => { + expect(getFrontmatterString({ a: "b" }, "a")).toBe("b"); + expect(getFrontmatterString({ a: 1 }, "a")).toBeUndefined(); + }); + + test("parseFrontmatterBool respects fallback", () => { + expect(parseFrontmatterBool("true", false)).toBe(true); + expect(parseFrontmatterBool("false", true)).toBe(false); + expect(parseFrontmatterBool(undefined, true)).toBe(true); + }); + + test("resolveOpenClawManifestBlock parses JSON5 metadata and picks openclaw block", () => { + const frontmatter = { + metadata: "{ openclaw: { foo: 1, bar: 'baz' } }", + }; + expect(resolveOpenClawManifestBlock({ frontmatter })).toEqual({ foo: 1, bar: "baz" }); + }); + + test("resolveOpenClawManifestBlock returns undefined for invalid input", () => { + expect(resolveOpenClawManifestBlock({ frontmatter: {} })).toBeUndefined(); + expect( + resolveOpenClawManifestBlock({ frontmatter: { metadata: "not-json5" } }), + ).toBeUndefined(); + expect( + resolveOpenClawManifestBlock({ frontmatter: { metadata: "{ nope: { a: 1 } }" } }), + ).toBeUndefined(); + }); +}); + +describe("resolveNodeIdFromCandidates", () => { + it("matches nodeId", () => { + expect( + resolveNodeIdFromCandidates( + [ + { nodeId: "mac-123", displayName: "Mac Studio", remoteIp: "100.0.0.1" }, + { nodeId: "pi-456", displayName: "Raspberry Pi", remoteIp: "100.0.0.2" }, + ], + "pi-456", + ), + ).toBe("pi-456"); + }); + + it("matches displayName using normalization", () => { + expect( + resolveNodeIdFromCandidates([{ nodeId: "mac-123", displayName: "Mac Studio" }], "mac studio"), + ).toBe("mac-123"); + }); + + it("matches nodeId prefix (>=6 chars)", () => { + expect(resolveNodeIdFromCandidates([{ nodeId: "mac-abcdef" }], "mac-ab")).toBe("mac-abcdef"); + }); + + it("throws unknown node with known list", () => { + expect(() => + resolveNodeIdFromCandidates( + [ + { nodeId: "mac-123", displayName: "Mac Studio", remoteIp: "100.0.0.1" }, + { nodeId: "pi-456" }, + ], + "nope", + ), + ).toThrow(/unknown node: nope.*known: /); + }); + + it("throws ambiguous node with matches list", () => { + expect(() => + resolveNodeIdFromCandidates([{ nodeId: "mac-abcdef" }, { nodeId: "mac-abc999" }], "mac-abc"), + ).toThrow(/ambiguous node: mac-abc.*matches:/); + }); +});