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,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");

View File

@ -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,