From 0b8227fa92bf99c982be03eb15065c823f4c26e2 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Feb 2026 02:00:43 +0000 Subject: [PATCH] perf(test): trim redundant suites and tighten wait loops --- ...fault-maxchars-explicitly-set-zero.test.ts | 28 +---- src/cron/service.issue-regressions.test.ts | 10 +- src/tui/theme/theme.test.ts | 114 +++++------------- src/web/inbound.media.test.ts | 88 +++++--------- src/web/media.test.ts | 66 +--------- 5 files changed, 65 insertions(+), 241 deletions(-) diff --git a/src/browser/server.skips-default-maxchars-explicitly-set-zero.test.ts b/src/browser/server.skips-default-maxchars-explicitly-set-zero.test.ts index 8b5ca4e580..9d6810ed31 100644 --- a/src/browser/server.skips-default-maxchars-explicitly-set-zero.test.ts +++ b/src/browser/server.skips-default-maxchars-explicitly-set-zero.test.ts @@ -249,52 +249,28 @@ describe("browser control server", () => { await stopBrowserControlServer(); }); - it("covers primary control routes, validation, and profile compatibility", async () => { + it("keeps maxChars unset when snapshot explicitly passes zero", async () => { const { startBrowserControlServerFromConfig } = await import("./server.js"); const started = await startBrowserControlServerFromConfig(); expect(started?.port).toBe(testPort); const base = `http://127.0.0.1:${testPort}`; - const statusBeforeStart = (await realFetch(`${base}/`).then((r) => r.json())) as { - running: boolean; - pid: number | null; - }; - expect(statusBeforeStart.running).toBe(false); - expect(statusBeforeStart.pid).toBe(null); - expect(statusBeforeStart.profile).toBe("openclaw"); - const startedPayload = (await realFetch(`${base}/start`, { method: "POST" }).then((r) => r.json(), )) as { ok: boolean; profile?: string }; expect(startedPayload.ok).toBe(true); expect(startedPayload.profile).toBe("openclaw"); - const statusAfterStart = (await realFetch(`${base}/`).then((r) => r.json())) as { - running: boolean; - pid: number | null; - chosenBrowser: string | null; - }; - expect(statusAfterStart.running).toBe(true); - expect(statusAfterStart.pid).toBe(123); - expect(statusAfterStart.chosenBrowser).toBe("chrome"); - expect(launchCalls.length).toBeGreaterThan(0); - const snapAi = (await realFetch(`${base}/snapshot?format=ai&maxChars=0`).then((r) => r.json(), )) as { ok: boolean; format?: string }; expect(snapAi.ok).toBe(true); expect(snapAi.format).toBe("ai"); + expect(launchCalls.length).toBeGreaterThan(0); const [call] = pwMocks.snapshotAiViaPlaywright.mock.calls.at(-1) ?? []; expect(call).toEqual({ cdpUrl: cdpBaseUrl, targetId: "abcd1234", }); - - const stopped = (await realFetch(`${base}/stop`, { method: "POST" }).then((r) => r.json())) as { - ok: boolean; - profile?: string; - }; - expect(stopped.ok).toBe(true); - expect(stopped.profile).toBe("openclaw"); }); }); diff --git a/src/cron/service.issue-regressions.test.ts b/src/cron/service.issue-regressions.test.ts index 2c369b2f92..6bc9fcd023 100644 --- a/src/cron/service.issue-regressions.test.ts +++ b/src/cron/service.issue-regressions.test.ts @@ -235,7 +235,7 @@ describe("Cron issue regressions", () => { }); await cron.start(); - const runAt = Date.now() + 5; + const runAt = Date.now() + 1; const job = await cron.add({ name: "timer-overlap", enabled: true, @@ -246,8 +246,8 @@ describe("Cron issue regressions", () => { delivery: { mode: "none" }, }); - for (let i = 0; i < 30 && runIsolatedAgentJob.mock.calls.length === 0; i++) { - await delay(5); + for (let i = 0; i < 20 && runIsolatedAgentJob.mock.calls.length === 0; i++) { + await delay(1); } expect(runIsolatedAgentJob).toHaveBeenCalledTimes(1); @@ -256,12 +256,12 @@ describe("Cron issue regressions", () => { expect(runIsolatedAgentJob).toHaveBeenCalledTimes(1); resolveRun?.({ status: "ok", summary: "done" }); - for (let i = 0; i < 30; i++) { + for (let i = 0; i < 20; i++) { const jobs = await cron.list({ includeDisabled: true }); if (jobs.some((j) => j.id === job.id && j.state.lastStatus === "ok")) { break; } - await delay(5); + await delay(1); } cron.stop(); diff --git a/src/tui/theme/theme.test.ts b/src/tui/theme/theme.test.ts index ac3ebd304c..3e9f91b400 100644 --- a/src/tui/theme/theme.test.ts +++ b/src/tui/theme/theme.test.ts @@ -3,109 +3,49 @@ import { markdownTheme } from "./theme.js"; describe("markdownTheme", () => { describe("highlightCode", () => { - it("should return an array of lines for JavaScript code", () => { + it("returns highlighted lines for common language inputs", () => { const code = `const x = 42;`; - const result = markdownTheme.highlightCode!(code, "javascript"); - - expect(result).toBeInstanceOf(Array); - expect(result).toHaveLength(1); - // Result should contain the original code (possibly with ANSI codes) - expect(result[0]).toContain("const"); - expect(result[0]).toContain("42"); - }); - - it("should return correct line count for multi-line code", () => { - const code = `function greet(name: string) { + const js = markdownTheme.highlightCode!(code, "javascript"); + const ts = markdownTheme.highlightCode!( + `function greet(name: string) { return "Hello, " + name; -}`; - const result = markdownTheme.highlightCode!(code, "typescript"); +}`, + "typescript", + ); - expect(result).toHaveLength(3); - expect(result[0]).toContain("function"); - expect(result[1]).toContain("return"); - expect(result[2]).toContain("}"); + expect(js).toBeInstanceOf(Array); + expect(js).toHaveLength(1); + expect(js[0]).toContain("const"); + expect(js[0]).toContain("42"); + expect(ts).toHaveLength(3); + expect(ts[0]).toContain("function"); + expect(ts[1]).toContain("return"); + expect(ts[2]).toContain("}"); }); - it("should handle Python code", () => { - const code = `def hello(): - print("world")`; - const result = markdownTheme.highlightCode!(code, "python"); - - expect(result).toHaveLength(2); - expect(result[0]).toContain("def"); - expect(result[1]).toContain("print"); - }); - - it("should handle unknown languages gracefully", () => { - const code = `const x = 42;`; - const result = markdownTheme.highlightCode!(code, "not-a-real-language"); - - expect(result).toBeInstanceOf(Array); - expect(result).toHaveLength(1); - // Should still return the code content - expect(result[0]).toContain("const"); - }); - - it("should handle code without language specifier", () => { + it("handles unknown and missing language without throwing", () => { const code = `echo "hello"`; - const result = markdownTheme.highlightCode!(code, undefined); - - expect(result).toBeInstanceOf(Array); - expect(result).toHaveLength(1); - expect(result[0]).toContain("echo"); + const unknown = markdownTheme.highlightCode!(code, "not-a-real-language"); + const missing = markdownTheme.highlightCode!(code, undefined); + expect(unknown).toBeInstanceOf(Array); + expect(missing).toBeInstanceOf(Array); + expect(unknown).toHaveLength(1); + expect(missing).toHaveLength(1); + expect(unknown[0]).toContain("echo"); + expect(missing[0]).toContain("echo"); }); - it("should handle empty code", () => { - const result = markdownTheme.highlightCode!("", "javascript"); - - expect(result).toBeInstanceOf(Array); - expect(result).toHaveLength(1); - expect(result[0]).toBe(""); - }); - - it("should handle bash/shell code", () => { - const code = `#!/bin/bash -echo "Hello" -for i in {1..5}; do - echo $i -done`; - const result = markdownTheme.highlightCode!(code, "bash"); - - expect(result).toHaveLength(5); - expect(result[0]).toContain("#!/bin/bash"); - expect(result[1]).toContain("echo"); - }); - - it("should handle JSON", () => { - const code = `{"name": "test", "count": 42, "active": true}`; - const result = markdownTheme.highlightCode!(code, "json"); - - expect(result).toHaveLength(1); - expect(result[0]).toContain("name"); - expect(result[0]).toContain("42"); - }); - - it("should handle code with special characters", () => { - const code = `const regex = /\\d+/g; -const str = "Hello\\nWorld";`; - const result = markdownTheme.highlightCode!(code, "javascript"); - - expect(result).toHaveLength(2); - // Should not throw and should return valid output - expect(result[0].length).toBeGreaterThan(0); - expect(result[1].length).toBeGreaterThan(0); - }); - - it("should preserve code content through highlighting", () => { + it("preserves code content and handles empty input", () => { const code = `const message = "Hello, World!"; console.log(message);`; const result = markdownTheme.highlightCode!(code, "javascript"); + const empty = markdownTheme.highlightCode!("", "javascript"); - // Strip ANSI codes to verify content is preserved const stripAnsi = (str: string) => str.replace(new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g"), ""); expect(stripAnsi(result[0])).toBe(`const message = "Hello, World!";`); expect(stripAnsi(result[1])).toBe("console.log(message);"); + expect(empty).toEqual([""]); }); }); }); diff --git a/src/web/inbound.media.test.ts b/src/web/inbound.media.test.ts index eb23d887b0..382a54a146 100644 --- a/src/web/inbound.media.test.ts +++ b/src/web/inbound.media.test.ts @@ -107,7 +107,7 @@ describe("web inbound media saves with extension", () => { await fs.rm(HOME, { recursive: true, force: true }); }); - it("stores inbound image with jpeg extension", async () => { + it("stores image extension, extracts caption mentions, and keeps document filename", async () => { const onMessage = vi.fn(); const listener = await monitorWebInbox({ verbose: false, onMessage }); const { createWaSocket } = await import("./session.js"); @@ -117,7 +117,7 @@ describe("web inbound media saves with extension", () => { }> )(); - const upsert = { + realSock.ev.emit("messages.upsert", { type: "notify", messages: [ { @@ -126,31 +126,17 @@ describe("web inbound media saves with extension", () => { messageTimestamp: 1_700_000_001, }, ], - }; + }); - realSock.ev.emit("messages.upsert", upsert); - - const msg = await waitForMessage(onMessage); - const mediaPath = msg.mediaPath; + const first = await waitForMessage(onMessage); + const mediaPath = first.mediaPath; expect(mediaPath).toBeDefined(); expect(path.extname(mediaPath as string)).toBe(".jpg"); const stat = await fs.stat(mediaPath as string); expect(stat.size).toBeGreaterThan(0); - await listener.close(); - }); - - it("extracts mentions from media captions", async () => { - const onMessage = vi.fn(); - const listener = await monitorWebInbox({ verbose: false, onMessage }); - const { createWaSocket } = await import("./session.js"); - const realSock = await ( - createWaSocket as unknown as () => Promise<{ - ev: import("node:events").EventEmitter; - }> - )(); - - const upsert = { + onMessage.mockClear(); + realSock.ev.emit("messages.upsert", { type: "notify", messages: [ { @@ -171,13 +157,30 @@ describe("web inbound media saves with extension", () => { messageTimestamp: 1_700_000_002, }, ], - }; + }); - realSock.ev.emit("messages.upsert", upsert); + const second = await waitForMessage(onMessage); + expect(second.chatType).toBe("group"); + expect(second.mentionedJids).toEqual(["999@s.whatsapp.net"]); - const msg = await waitForMessage(onMessage); - expect(msg.chatType).toBe("group"); - expect(msg.mentionedJids).toEqual(["999@s.whatsapp.net"]); + onMessage.mockClear(); + const fileName = "invoice.pdf"; + realSock.ev.emit("messages.upsert", { + type: "notify", + messages: [ + { + key: { id: "doc1", fromMe: false, remoteJid: "333@s.whatsapp.net" }, + message: { documentMessage: { mimetype: "application/pdf", fileName } }, + messageTimestamp: 1_700_000_004, + }, + ], + }); + + const third = await waitForMessage(onMessage); + expect(third.mediaFileName).toBe(fileName); + expect(saveMediaBufferSpy).toHaveBeenCalled(); + const lastCall = saveMediaBufferSpy.mock.calls.at(-1); + expect(lastCall?.[4]).toBe(fileName); await listener.close(); }); @@ -216,37 +219,4 @@ describe("web inbound media saves with extension", () => { await listener.close(); }); - - it("passes document filenames to saveMediaBuffer", async () => { - const onMessage = vi.fn(); - const listener = await monitorWebInbox({ verbose: false, onMessage }); - const { createWaSocket } = await import("./session.js"); - const realSock = await ( - createWaSocket as unknown as () => Promise<{ - ev: import("node:events").EventEmitter; - }> - )(); - - const fileName = "invoice.pdf"; - const upsert = { - type: "notify", - messages: [ - { - key: { id: "doc1", fromMe: false, remoteJid: "333@s.whatsapp.net" }, - message: { documentMessage: { mimetype: "application/pdf", fileName } }, - messageTimestamp: 1_700_000_004, - }, - ], - }; - - realSock.ev.emit("messages.upsert", upsert); - - const msg = await waitForMessage(onMessage); - expect(msg.mediaFileName).toBe(fileName); - expect(saveMediaBufferSpy).toHaveBeenCalled(); - const lastCall = saveMediaBufferSpy.mock.calls.at(-1); - expect(lastCall?.[4]).toBe(fileName); - - await listener.close(); - }); }); diff --git a/src/web/media.test.ts b/src/web/media.test.ts index a1b6422bfc..3305e764fc 100644 --- a/src/web/media.test.ts +++ b/src/web/media.test.ts @@ -40,8 +40,8 @@ beforeAll(async () => { fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-media-test-")); largeJpegBuffer = await sharp({ create: { - width: 1200, - height: 1200, + width: 900, + height: 900, channels: 3, background: "#ff0000", }, @@ -138,24 +138,6 @@ describe("web media loading", () => { expect(result.contentType).toBe("image/jpeg"); }); - it("adds extension to URL fileName when missing", async () => { - const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValueOnce({ - ok: true, - body: true, - arrayBuffer: async () => Buffer.from("%PDF-1.4").buffer, - headers: { get: () => "application/pdf" }, - status: 200, - } as Response); - - const result = await loadWebMedia("https://example.com/download", 1024 * 1024); - - expect(result.kind).toBe("document"); - expect(result.contentType).toBe("application/pdf"); - expect(result.fileName).toBe("download.pdf"); - - fetchMock.mockRestore(); - }); - it("includes URL + status in fetch errors", async () => { const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValueOnce({ ok: false, @@ -217,50 +199,6 @@ describe("web media loading", () => { fetchMock.mockRestore(); }); - it("preserves GIF animation by skipping JPEG optimization", async () => { - // Create a minimal valid GIF (1x1 pixel) - // GIF89a header + minimal image data - const gifBuffer = Buffer.from([ - 0x47, - 0x49, - 0x46, - 0x38, - 0x39, - 0x61, // GIF89a - 0x01, - 0x00, - 0x01, - 0x00, // 1x1 dimensions - 0x00, - 0x00, - 0x00, // no global color table - 0x2c, - 0x00, - 0x00, - 0x00, - 0x00, // image descriptor - 0x01, - 0x00, - 0x01, - 0x00, - 0x00, // 1x1 image - 0x02, - 0x01, - 0x44, - 0x00, - 0x3b, // minimal LZW data + trailer - ]); - - const file = await writeTempFile(gifBuffer, ".gif"); - - const result = await loadWebMedia(file, 1024 * 1024); - - expect(result.kind).toBe("image"); - expect(result.contentType).toBe("image/gif"); - // GIF should NOT be converted to JPEG - expect(result.buffer.slice(0, 3).toString()).toBe("GIF"); - }); - it("preserves GIF from URL without JPEG conversion", async () => { const gifBytes = new Uint8Array([ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,