refactor: share plugin test fixtures

This commit is contained in:
Peter Steinberger 2026-03-13 18:53:49 +00:00
parent 5f34391f75
commit 4ec0fcf1b6
2 changed files with 63 additions and 93 deletions

View File

@ -3,8 +3,7 @@ import { describe, expect, it } from "vitest";
import { withEnv } from "../test-utils/env.js"; import { withEnv } from "../test-utils/env.js";
import { formatPluginSourceForTable, resolvePluginSourceRoots } from "./source-display.js"; import { formatPluginSourceForTable, resolvePluginSourceRoots } from "./source-display.js";
describe("formatPluginSourceForTable", () => { function createPluginSourceRoots() {
it("shortens bundled plugin sources under the stock root", () => {
const stockRoot = path.resolve( const stockRoot = path.resolve(
path.sep, path.sep,
"opt", "opt",
@ -16,70 +15,48 @@ describe("formatPluginSourceForTable", () => {
); );
const globalRoot = path.resolve(path.sep, "Users", "x", ".openclaw", "extensions"); const globalRoot = path.resolve(path.sep, "Users", "x", ".openclaw", "extensions");
const workspaceRoot = path.resolve(path.sep, "Users", "x", "ws", ".openclaw", "extensions"); const workspaceRoot = path.resolve(path.sep, "Users", "x", "ws", ".openclaw", "extensions");
const out = formatPluginSourceForTable( return {
{
origin: "bundled",
source: path.join(stockRoot, "bluebubbles", "index.ts"),
},
{
stock: stockRoot, stock: stockRoot,
global: globalRoot, global: globalRoot,
workspace: workspaceRoot, workspace: workspaceRoot,
};
}
describe("formatPluginSourceForTable", () => {
it("shortens bundled plugin sources under the stock root", () => {
const roots = createPluginSourceRoots();
const out = formatPluginSourceForTable(
{
origin: "bundled",
source: path.join(roots.stock, "bluebubbles", "index.ts"),
}, },
roots,
); );
expect(out.value).toBe("stock:bluebubbles/index.ts"); expect(out.value).toBe("stock:bluebubbles/index.ts");
expect(out.rootKey).toBe("stock"); expect(out.rootKey).toBe("stock");
}); });
it("shortens workspace plugin sources under the workspace root", () => { it("shortens workspace plugin sources under the workspace root", () => {
const stockRoot = path.resolve( const roots = createPluginSourceRoots();
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 out = formatPluginSourceForTable( const out = formatPluginSourceForTable(
{ {
origin: "workspace", origin: "workspace",
source: path.join(workspaceRoot, "matrix", "index.ts"), source: path.join(roots.workspace, "matrix", "index.ts"),
},
{
stock: stockRoot,
global: globalRoot,
workspace: workspaceRoot,
}, },
roots,
); );
expect(out.value).toBe("workspace:matrix/index.ts"); expect(out.value).toBe("workspace:matrix/index.ts");
expect(out.rootKey).toBe("workspace"); expect(out.rootKey).toBe("workspace");
}); });
it("shortens global plugin sources under the global root", () => { it("shortens global plugin sources under the global root", () => {
const stockRoot = path.resolve( const roots = createPluginSourceRoots();
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 out = formatPluginSourceForTable( const out = formatPluginSourceForTable(
{ {
origin: "global", origin: "global",
source: path.join(globalRoot, "zalo", "index.js"), source: path.join(roots.global, "zalo", "index.js"),
},
{
stock: stockRoot,
global: globalRoot,
workspace: workspaceRoot,
}, },
roots,
); );
expect(out.value).toBe("global:zalo/index.js"); expect(out.value).toBe("global:zalo/index.js");
expect(out.rootKey).toBe("global"); expect(out.rootKey).toBe("global");

View File

@ -40,6 +40,28 @@ describe("compaction hook wiring", () => {
vi.mocked(emitAgentEvent).mockClear(); 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", () => { it("calls runBeforeCompaction in handleAutoCompactionStart", () => {
hookMocks.runner.hasHooks.mockReturnValue(true); hookMocks.runner.hasHooks.mockReturnValue(true);
@ -86,14 +108,11 @@ describe("compaction hook wiring", () => {
it("calls runAfterCompaction when willRetry is false", () => { it("calls runAfterCompaction when willRetry is false", () => {
hookMocks.runner.hasHooks.mockReturnValue(true); hookMocks.runner.hasHooks.mockReturnValue(true);
const ctx = { const ctx = createCompactionEndCtx({
params: { runId: "r2", session: { messages: [1, 2] } }, runId: "r2",
state: { compactionInFlight: true }, messages: [1, 2],
log: { debug: vi.fn(), warn: vi.fn() }, compactionCount: 1,
maybeResolveCompactionWait: vi.fn(), });
incrementCompactionCount: vi.fn(),
getCompactionCount: () => 1,
};
handleAutoCompactionEnd( handleAutoCompactionEnd(
ctx as never, ctx as never,
@ -126,16 +145,11 @@ describe("compaction hook wiring", () => {
it("does not call runAfterCompaction when willRetry is true but still increments counter", () => { it("does not call runAfterCompaction when willRetry is true but still increments counter", () => {
hookMocks.runner.hasHooks.mockReturnValue(true); hookMocks.runner.hasHooks.mockReturnValue(true);
const ctx = { const ctx = createCompactionEndCtx({
params: { runId: "r3", session: { messages: [] } }, runId: "r3",
state: { compactionInFlight: true }, compactionCount: 1,
log: { debug: vi.fn(), warn: vi.fn() }, withRetryHooks: true,
noteCompactionRetry: vi.fn(), });
resetForCompactionRetry: vi.fn(),
maybeResolveCompactionWait: vi.fn(),
incrementCompactionCount: vi.fn(),
getCompactionCount: () => 1,
};
handleAutoCompactionEnd( handleAutoCompactionEnd(
ctx as never, ctx as never,
@ -160,14 +174,7 @@ describe("compaction hook wiring", () => {
}); });
it("does not increment counter when compaction was aborted", () => { it("does not increment counter when compaction was aborted", () => {
const ctx = { const ctx = createCompactionEndCtx({ runId: "r3b" });
params: { runId: "r3b", session: { messages: [] } },
state: { compactionInFlight: true },
log: { debug: vi.fn(), warn: vi.fn() },
maybeResolveCompactionWait: vi.fn(),
incrementCompactionCount: vi.fn(),
getCompactionCount: () => 0,
};
handleAutoCompactionEnd( handleAutoCompactionEnd(
ctx as never, ctx as never,
@ -183,14 +190,7 @@ describe("compaction hook wiring", () => {
}); });
it("does not increment counter when compaction has result but was aborted", () => { it("does not increment counter when compaction has result but was aborted", () => {
const ctx = { const ctx = createCompactionEndCtx({ runId: "r3b2" });
params: { runId: "r3b2", session: { messages: [] } },
state: { compactionInFlight: true },
log: { debug: vi.fn(), warn: vi.fn() },
maybeResolveCompactionWait: vi.fn(),
incrementCompactionCount: vi.fn(),
getCompactionCount: () => 0,
};
handleAutoCompactionEnd( handleAutoCompactionEnd(
ctx as never, ctx as never,
@ -206,14 +206,7 @@ describe("compaction hook wiring", () => {
}); });
it("does not increment counter when result is undefined", () => { it("does not increment counter when result is undefined", () => {
const ctx = { const ctx = createCompactionEndCtx({ runId: "r3c" });
params: { runId: "r3c", session: { messages: [] } },
state: { compactionInFlight: true },
log: { debug: vi.fn(), warn: vi.fn() },
maybeResolveCompactionWait: vi.fn(),
incrementCompactionCount: vi.fn(),
getCompactionCount: () => 0,
};
handleAutoCompactionEnd( handleAutoCompactionEnd(
ctx as never, ctx as never,