From 4ec0fcf1b677ec9d183bf0d6ccebe913143e6ba2 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 18:53:49 +0000 Subject: [PATCH] refactor: share plugin test fixtures --- src/plugins/source-display.test.ts | 79 ++++++++-------------- src/plugins/wired-hooks-compaction.test.ts | 77 ++++++++++----------- 2 files changed, 63 insertions(+), 93 deletions(-) diff --git a/src/plugins/source-display.test.ts b/src/plugins/source-display.test.ts index 3c85cca88b7..6d1b3da7719 100644 --- a/src/plugins/source-display.test.ts +++ b/src/plugins/source-display.test.ts @@ -3,83 +3,60 @@ import { describe, expect, it } from "vitest"; import { withEnv } from "../test-utils/env.js"; import { formatPluginSourceForTable, resolvePluginSourceRoots } from "./source-display.js"; +function createPluginSourceRoots() { + const stockRoot = path.resolve( + path.sep, + "opt", + "homebrew", + "lib", + "node_modules", + "openclaw", + "extensions", + ); + const globalRoot = path.resolve(path.sep, "Users", "x", ".openclaw", "extensions"); + const workspaceRoot = path.resolve(path.sep, "Users", "x", "ws", ".openclaw", "extensions"); + return { + stock: stockRoot, + global: globalRoot, + workspace: workspaceRoot, + }; +} + describe("formatPluginSourceForTable", () => { it("shortens bundled plugin sources under the stock root", () => { - const stockRoot = path.resolve( - path.sep, - "opt", - "homebrew", - "lib", - "node_modules", - "openclaw", - "extensions", - ); - const globalRoot = path.resolve(path.sep, "Users", "x", ".openclaw", "extensions"); - const workspaceRoot = path.resolve(path.sep, "Users", "x", "ws", ".openclaw", "extensions"); + const roots = createPluginSourceRoots(); const out = formatPluginSourceForTable( { origin: "bundled", - source: path.join(stockRoot, "bluebubbles", "index.ts"), - }, - { - stock: stockRoot, - global: globalRoot, - workspace: workspaceRoot, + source: path.join(roots.stock, "bluebubbles", "index.ts"), }, + roots, ); expect(out.value).toBe("stock:bluebubbles/index.ts"); expect(out.rootKey).toBe("stock"); }); it("shortens workspace plugin sources under the workspace root", () => { - const stockRoot = path.resolve( - path.sep, - "opt", - "homebrew", - "lib", - "node_modules", - "openclaw", - "extensions", - ); - const globalRoot = path.resolve(path.sep, "Users", "x", ".openclaw", "extensions"); - const workspaceRoot = path.resolve(path.sep, "Users", "x", "ws", ".openclaw", "extensions"); + const roots = createPluginSourceRoots(); const out = formatPluginSourceForTable( { origin: "workspace", - source: path.join(workspaceRoot, "matrix", "index.ts"), - }, - { - stock: stockRoot, - global: globalRoot, - workspace: workspaceRoot, + source: path.join(roots.workspace, "matrix", "index.ts"), }, + roots, ); expect(out.value).toBe("workspace:matrix/index.ts"); expect(out.rootKey).toBe("workspace"); }); it("shortens global plugin sources under the global root", () => { - const stockRoot = path.resolve( - path.sep, - "opt", - "homebrew", - "lib", - "node_modules", - "openclaw", - "extensions", - ); - const globalRoot = path.resolve(path.sep, "Users", "x", ".openclaw", "extensions"); - const workspaceRoot = path.resolve(path.sep, "Users", "x", "ws", ".openclaw", "extensions"); + const roots = createPluginSourceRoots(); const out = formatPluginSourceForTable( { origin: "global", - source: path.join(globalRoot, "zalo", "index.js"), - }, - { - stock: stockRoot, - global: globalRoot, - workspace: workspaceRoot, + source: path.join(roots.global, "zalo", "index.js"), }, + roots, ); expect(out.value).toBe("global:zalo/index.js"); expect(out.rootKey).toBe("global"); diff --git a/src/plugins/wired-hooks-compaction.test.ts b/src/plugins/wired-hooks-compaction.test.ts index 5081922ec1d..1e3f0021e29 100644 --- a/src/plugins/wired-hooks-compaction.test.ts +++ b/src/plugins/wired-hooks-compaction.test.ts @@ -40,6 +40,28 @@ describe("compaction hook wiring", () => { vi.mocked(emitAgentEvent).mockClear(); }); + function createCompactionEndCtx(params: { + runId: string; + messages?: unknown[]; + compactionCount?: number; + withRetryHooks?: boolean; + }) { + return { + params: { runId: params.runId, session: { messages: params.messages ?? [] } }, + state: { compactionInFlight: true }, + log: { debug: vi.fn(), warn: vi.fn() }, + maybeResolveCompactionWait: vi.fn(), + incrementCompactionCount: vi.fn(), + getCompactionCount: () => params.compactionCount ?? 0, + ...(params.withRetryHooks + ? { + noteCompactionRetry: vi.fn(), + resetForCompactionRetry: vi.fn(), + } + : {}), + }; + } + it("calls runBeforeCompaction in handleAutoCompactionStart", () => { hookMocks.runner.hasHooks.mockReturnValue(true); @@ -86,14 +108,11 @@ describe("compaction hook wiring", () => { it("calls runAfterCompaction when willRetry is false", () => { hookMocks.runner.hasHooks.mockReturnValue(true); - const ctx = { - params: { runId: "r2", session: { messages: [1, 2] } }, - state: { compactionInFlight: true }, - log: { debug: vi.fn(), warn: vi.fn() }, - maybeResolveCompactionWait: vi.fn(), - incrementCompactionCount: vi.fn(), - getCompactionCount: () => 1, - }; + const ctx = createCompactionEndCtx({ + runId: "r2", + messages: [1, 2], + compactionCount: 1, + }); handleAutoCompactionEnd( ctx as never, @@ -126,16 +145,11 @@ describe("compaction hook wiring", () => { it("does not call runAfterCompaction when willRetry is true but still increments counter", () => { hookMocks.runner.hasHooks.mockReturnValue(true); - const ctx = { - params: { runId: "r3", session: { messages: [] } }, - state: { compactionInFlight: true }, - log: { debug: vi.fn(), warn: vi.fn() }, - noteCompactionRetry: vi.fn(), - resetForCompactionRetry: vi.fn(), - maybeResolveCompactionWait: vi.fn(), - incrementCompactionCount: vi.fn(), - getCompactionCount: () => 1, - }; + const ctx = createCompactionEndCtx({ + runId: "r3", + compactionCount: 1, + withRetryHooks: true, + }); handleAutoCompactionEnd( ctx as never, @@ -160,14 +174,7 @@ describe("compaction hook wiring", () => { }); it("does not increment counter when compaction was aborted", () => { - const ctx = { - params: { runId: "r3b", session: { messages: [] } }, - state: { compactionInFlight: true }, - log: { debug: vi.fn(), warn: vi.fn() }, - maybeResolveCompactionWait: vi.fn(), - incrementCompactionCount: vi.fn(), - getCompactionCount: () => 0, - }; + const ctx = createCompactionEndCtx({ runId: "r3b" }); handleAutoCompactionEnd( ctx as never, @@ -183,14 +190,7 @@ describe("compaction hook wiring", () => { }); it("does not increment counter when compaction has result but was aborted", () => { - const ctx = { - params: { runId: "r3b2", session: { messages: [] } }, - state: { compactionInFlight: true }, - log: { debug: vi.fn(), warn: vi.fn() }, - maybeResolveCompactionWait: vi.fn(), - incrementCompactionCount: vi.fn(), - getCompactionCount: () => 0, - }; + const ctx = createCompactionEndCtx({ runId: "r3b2" }); handleAutoCompactionEnd( ctx as never, @@ -206,14 +206,7 @@ describe("compaction hook wiring", () => { }); it("does not increment counter when result is undefined", () => { - const ctx = { - params: { runId: "r3c", session: { messages: [] } }, - state: { compactionInFlight: true }, - log: { debug: vi.fn(), warn: vi.fn() }, - maybeResolveCompactionWait: vi.fn(), - incrementCompactionCount: vi.fn(), - getCompactionCount: () => 0, - }; + const ctx = createCompactionEndCtx({ runId: "r3c" }); handleAutoCompactionEnd( ctx as never,