mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-05 13:00:22 +00:00
test: dedupe plugin hook runner suites
This commit is contained in:
@@ -67,6 +67,47 @@ describe("compaction hook wiring", () => {
|
||||
};
|
||||
}
|
||||
|
||||
function getBeforeCompactionCall() {
|
||||
const beforeCalls = hookMocks.runner.runBeforeCompaction.mock.calls as unknown as Array<
|
||||
[unknown, unknown]
|
||||
>;
|
||||
return {
|
||||
event: beforeCalls[0]?.[0] as
|
||||
| { messageCount?: number; messages?: unknown[]; sessionFile?: string }
|
||||
| undefined,
|
||||
hookCtx: beforeCalls[0]?.[1] as { sessionKey?: string } | undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function getAfterCompactionCall() {
|
||||
const afterCalls = hookMocks.runner.runAfterCompaction.mock.calls as unknown as Array<
|
||||
[unknown, unknown]
|
||||
>;
|
||||
return {
|
||||
event: afterCalls[0]?.[0] as
|
||||
| { messageCount?: number; compactedCount?: number; sessionFile?: string }
|
||||
| undefined,
|
||||
hookCtx: afterCalls[0]?.[1] as { sessionKey?: string } | undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function runCompactionEnd(
|
||||
ctx: ReturnType<typeof createCompactionEndCtx> | Record<string, unknown>,
|
||||
event: {
|
||||
willRetry: boolean;
|
||||
result?: { summary: string };
|
||||
aborted?: boolean;
|
||||
},
|
||||
) {
|
||||
handleAutoCompactionEnd(
|
||||
ctx as never,
|
||||
{
|
||||
type: "auto_compaction_end",
|
||||
...event,
|
||||
} as never,
|
||||
);
|
||||
}
|
||||
|
||||
it("calls runBeforeCompaction in handleAutoCompactionStart", () => {
|
||||
hookMocks.runner.hasHooks.mockReturnValue(true);
|
||||
|
||||
@@ -86,17 +127,10 @@ describe("compaction hook wiring", () => {
|
||||
handleAutoCompactionStart(ctx as never);
|
||||
|
||||
expect(hookMocks.runner.runBeforeCompaction).toHaveBeenCalledTimes(1);
|
||||
|
||||
const beforeCalls = hookMocks.runner.runBeforeCompaction.mock.calls as unknown as Array<
|
||||
[unknown, unknown]
|
||||
>;
|
||||
const event = beforeCalls[0]?.[0] as
|
||||
| { messageCount?: number; messages?: unknown[]; sessionFile?: string }
|
||||
| undefined;
|
||||
const { event, hookCtx } = getBeforeCompactionCall();
|
||||
expect(event?.messageCount).toBe(3);
|
||||
expect(event?.messages).toEqual([1, 2, 3]);
|
||||
expect(event?.sessionFile).toBe("/tmp/test.jsonl");
|
||||
const hookCtx = beforeCalls[0]?.[1] as { sessionKey?: string } | undefined;
|
||||
expect(hookCtx?.sessionKey).toBe("agent:main:web-abc123");
|
||||
expect(ctx.ensureCompactionPromise).toHaveBeenCalledTimes(1);
|
||||
expect(hookMocks.emitAgentEvent).toHaveBeenCalledWith({
|
||||
@@ -121,27 +155,13 @@ describe("compaction hook wiring", () => {
|
||||
compactionCount: 1,
|
||||
});
|
||||
|
||||
handleAutoCompactionEnd(
|
||||
ctx as never,
|
||||
{
|
||||
type: "auto_compaction_end",
|
||||
willRetry: false,
|
||||
result: { summary: "compacted" },
|
||||
} as never,
|
||||
);
|
||||
runCompactionEnd(ctx, { willRetry: false, result: { summary: "compacted" } });
|
||||
|
||||
expect(hookMocks.runner.runAfterCompaction).toHaveBeenCalledTimes(1);
|
||||
|
||||
const afterCalls = hookMocks.runner.runAfterCompaction.mock.calls as unknown as Array<
|
||||
[unknown, unknown]
|
||||
>;
|
||||
const event = afterCalls[0]?.[0] as
|
||||
| { messageCount?: number; compactedCount?: number; sessionFile?: string }
|
||||
| undefined;
|
||||
const { event, hookCtx } = getAfterCompactionCall();
|
||||
expect(event?.messageCount).toBe(2);
|
||||
expect(event?.compactedCount).toBe(1);
|
||||
expect(event?.sessionFile).toBe("/tmp/session.jsonl");
|
||||
const hookCtx = afterCalls[0]?.[1] as { sessionKey?: string } | undefined;
|
||||
expect(hookCtx?.sessionKey).toBe("agent:main:web-xyz");
|
||||
expect(ctx.incrementCompactionCount).toHaveBeenCalledTimes(1);
|
||||
expect(ctx.maybeResolveCompactionWait).toHaveBeenCalledTimes(1);
|
||||
@@ -161,14 +181,7 @@ describe("compaction hook wiring", () => {
|
||||
withRetryHooks: true,
|
||||
});
|
||||
|
||||
handleAutoCompactionEnd(
|
||||
ctx as never,
|
||||
{
|
||||
type: "auto_compaction_end",
|
||||
willRetry: true,
|
||||
result: { summary: "compacted" },
|
||||
} as never,
|
||||
);
|
||||
runCompactionEnd(ctx, { willRetry: true, result: { summary: "compacted" } });
|
||||
|
||||
expect(hookMocks.runner.runAfterCompaction).not.toHaveBeenCalled();
|
||||
// Counter is incremented even with willRetry — compaction succeeded (#38905)
|
||||
@@ -183,51 +196,16 @@ describe("compaction hook wiring", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("does not increment counter when compaction was aborted", () => {
|
||||
const ctx = createCompactionEndCtx({ runId: "r3b" });
|
||||
|
||||
handleAutoCompactionEnd(
|
||||
ctx as never,
|
||||
{
|
||||
type: "auto_compaction_end",
|
||||
willRetry: false,
|
||||
result: undefined,
|
||||
aborted: true,
|
||||
} as never,
|
||||
);
|
||||
|
||||
expect(ctx.incrementCompactionCount).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not increment counter when compaction has result but was aborted", () => {
|
||||
const ctx = createCompactionEndCtx({ runId: "r3b2" });
|
||||
|
||||
handleAutoCompactionEnd(
|
||||
ctx as never,
|
||||
{
|
||||
type: "auto_compaction_end",
|
||||
willRetry: false,
|
||||
result: { summary: "compacted" },
|
||||
aborted: true,
|
||||
} as never,
|
||||
);
|
||||
|
||||
expect(ctx.incrementCompactionCount).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not increment counter when result is undefined", () => {
|
||||
it.each([
|
||||
["does not increment counter when compaction was aborted", { willRetry: false, aborted: true }],
|
||||
[
|
||||
"does not increment counter when compaction has result but was aborted",
|
||||
{ willRetry: false, result: { summary: "compacted" }, aborted: true },
|
||||
],
|
||||
["does not increment counter when result is undefined", { willRetry: false }],
|
||||
] as const)("%s", (_name, event) => {
|
||||
const ctx = createCompactionEndCtx({ runId: "r3c" });
|
||||
|
||||
handleAutoCompactionEnd(
|
||||
ctx as never,
|
||||
{
|
||||
type: "auto_compaction_end",
|
||||
willRetry: false,
|
||||
result: undefined,
|
||||
aborted: false,
|
||||
} as never,
|
||||
);
|
||||
|
||||
runCompactionEnd(ctx, event);
|
||||
expect(ctx.incrementCompactionCount).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -255,14 +233,7 @@ describe("compaction hook wiring", () => {
|
||||
incrementCompactionCount: vi.fn(),
|
||||
};
|
||||
|
||||
handleAutoCompactionEnd(
|
||||
ctx as never,
|
||||
{
|
||||
type: "auto_compaction_end",
|
||||
willRetry: false,
|
||||
result: { summary: "compacted" },
|
||||
} as never,
|
||||
);
|
||||
runCompactionEnd(ctx, { willRetry: false, result: { summary: "compacted" } });
|
||||
|
||||
const assistantOne = messages[1] as { usage?: unknown };
|
||||
const assistantTwo = messages[2] as { usage?: unknown };
|
||||
@@ -288,13 +259,7 @@ describe("compaction hook wiring", () => {
|
||||
getCompactionCount: () => 0,
|
||||
};
|
||||
|
||||
handleAutoCompactionEnd(
|
||||
ctx as never,
|
||||
{
|
||||
type: "auto_compaction_end",
|
||||
willRetry: true,
|
||||
} as never,
|
||||
);
|
||||
runCompactionEnd(ctx, { willRetry: true });
|
||||
|
||||
const assistant = messages[0] as { usage?: unknown };
|
||||
expect(assistant.usage).toEqual({ totalTokens: 184_297, input: 130_000, output: 2_000 });
|
||||
|
||||
Reference in New Issue
Block a user