test(agents): dedupe embedded runner and sessions lifecycle fixtures

This commit is contained in:
Peter Steinberger
2026-02-19 08:47:14 +00:00
parent c9b5def1b8
commit d1cb779f5f
2 changed files with 54 additions and 82 deletions

View File

@@ -148,6 +148,20 @@ function expectSingleCompletionSend(
expect(send?.message).toBe(expected.message); expect(send?.message).toBe(expected.message);
} }
function createDeleteCleanupHooks(setDeletedKey: (key: string | undefined) => void) {
return {
onAgentSubagentSpawn: (params: unknown) => {
const rec = params as { channel?: string; timeout?: number } | undefined;
expect(rec?.channel).toBe("discord");
expect(rec?.timeout).toBe(1);
},
onSessionsDelete: (params: unknown) => {
const rec = params as { key?: string } | undefined;
setDeletedKey(rec?.key);
},
};
}
describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => { describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
beforeEach(() => { beforeEach(() => {
resetSessionsSpawnConfigOverride(); resetSessionsSpawnConfigOverride();
@@ -231,15 +245,9 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
callGatewayMock.mockReset(); callGatewayMock.mockReset();
let deletedKey: string | undefined; let deletedKey: string | undefined;
const ctx = setupSessionsSpawnGatewayMock({ const ctx = setupSessionsSpawnGatewayMock({
onAgentSubagentSpawn: (params) => { ...createDeleteCleanupHooks((key) => {
const rec = params as { channel?: string; timeout?: number } | undefined; deletedKey = key;
expect(rec?.channel).toBe("discord"); }),
expect(rec?.timeout).toBe(1);
},
onSessionsDelete: (params) => {
const rec = params as { key?: string } | undefined;
deletedKey = rec?.key;
},
}); });
const tool = await getSessionsSpawnTool({ const tool = await getSessionsSpawnTool({
@@ -315,15 +323,9 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
let deletedKey: string | undefined; let deletedKey: string | undefined;
const ctx = setupSessionsSpawnGatewayMock({ const ctx = setupSessionsSpawnGatewayMock({
includeChatHistory: true, includeChatHistory: true,
onAgentSubagentSpawn: (params) => { ...createDeleteCleanupHooks((key) => {
const rec = params as { channel?: string; timeout?: number } | undefined; deletedKey = key;
expect(rec?.channel).toBe("discord"); }),
expect(rec?.timeout).toBe(1);
},
onSessionsDelete: (params) => {
const rec = params as { key?: string } | undefined;
deletedKey = rec?.key;
},
agentWaitResult: { status: "ok", startedAt: 3000, endedAt: 4000 }, agentWaitResult: { status: "ok", startedAt: 3000, endedAt: 4000 },
}); });

View File

@@ -64,6 +64,30 @@ describe("resolveExtraParams", () => {
}); });
describe("applyExtraParamsToAgent", () => { describe("applyExtraParamsToAgent", () => {
function createOptionsCaptureAgent() {
const calls: Array<SimpleStreamOptions | undefined> = [];
const baseStreamFn: StreamFn = (_model, _context, options) => {
calls.push(options);
return {} as ReturnType<StreamFn>;
};
return {
calls,
agent: { streamFn: baseStreamFn },
};
}
function buildAnthropicModelConfig(modelKey: string, params: Record<string, unknown>) {
return {
agents: {
defaults: {
models: {
[modelKey]: { params },
},
},
},
};
}
function runStoreMutationCase(params: { function runStoreMutationCase(params: {
applyProvider: string; applyProvider: string;
applyModelId: string; applyModelId: string;
@@ -86,12 +110,7 @@ describe("applyExtraParamsToAgent", () => {
} }
it("adds OpenRouter attribution headers to stream options", () => { it("adds OpenRouter attribution headers to stream options", () => {
const calls: Array<SimpleStreamOptions | undefined> = []; const { calls, agent } = createOptionsCaptureAgent();
const baseStreamFn: StreamFn = (_model, _context, options) => {
calls.push(options);
return {} as ReturnType<StreamFn>;
};
const agent = { streamFn: baseStreamFn };
applyExtraParamsToAgent(agent, undefined, "openrouter", "openrouter/auto"); applyExtraParamsToAgent(agent, undefined, "openrouter", "openrouter/auto");
@@ -113,25 +132,8 @@ describe("applyExtraParamsToAgent", () => {
}); });
it("adds Anthropic 1M beta header when context1m is enabled for Opus/Sonnet", () => { it("adds Anthropic 1M beta header when context1m is enabled for Opus/Sonnet", () => {
const calls: Array<SimpleStreamOptions | undefined> = []; const { calls, agent } = createOptionsCaptureAgent();
const baseStreamFn: StreamFn = (_model, _context, options) => { const cfg = buildAnthropicModelConfig("anthropic/claude-opus-4-6", { context1m: true });
calls.push(options);
return {} as ReturnType<StreamFn>;
};
const agent = { streamFn: baseStreamFn };
const cfg = {
agents: {
defaults: {
models: {
"anthropic/claude-opus-4-6": {
params: {
context1m: true,
},
},
},
},
},
};
applyExtraParamsToAgent(agent, cfg, "anthropic", "claude-opus-4-6"); applyExtraParamsToAgent(agent, cfg, "anthropic", "claude-opus-4-6");
@@ -152,26 +154,11 @@ describe("applyExtraParamsToAgent", () => {
}); });
it("merges existing anthropic-beta headers with configured betas", () => { it("merges existing anthropic-beta headers with configured betas", () => {
const calls: Array<SimpleStreamOptions | undefined> = []; const { calls, agent } = createOptionsCaptureAgent();
const baseStreamFn: StreamFn = (_model, _context, options) => { const cfg = buildAnthropicModelConfig("anthropic/claude-sonnet-4-5", {
calls.push(options); context1m: true,
return {} as ReturnType<StreamFn>; anthropicBeta: ["files-api-2025-04-14"],
}; });
const agent = { streamFn: baseStreamFn };
const cfg = {
agents: {
defaults: {
models: {
"anthropic/claude-sonnet-4-5": {
params: {
context1m: true,
anthropicBeta: ["files-api-2025-04-14"],
},
},
},
},
},
};
applyExtraParamsToAgent(agent, cfg, "anthropic", "claude-sonnet-4-5"); applyExtraParamsToAgent(agent, cfg, "anthropic", "claude-sonnet-4-5");
@@ -193,25 +180,8 @@ describe("applyExtraParamsToAgent", () => {
}); });
it("ignores context1m for non-Opus/Sonnet Anthropic models", () => { it("ignores context1m for non-Opus/Sonnet Anthropic models", () => {
const calls: Array<SimpleStreamOptions | undefined> = []; const { calls, agent } = createOptionsCaptureAgent();
const baseStreamFn: StreamFn = (_model, _context, options) => { const cfg = buildAnthropicModelConfig("anthropic/claude-haiku-3-5", { context1m: true });
calls.push(options);
return {} as ReturnType<StreamFn>;
};
const agent = { streamFn: baseStreamFn };
const cfg = {
agents: {
defaults: {
models: {
"anthropic/claude-haiku-3-5": {
params: {
context1m: true,
},
},
},
},
},
};
applyExtraParamsToAgent(agent, cfg, "anthropic", "claude-haiku-3-5"); applyExtraParamsToAgent(agent, cfg, "anthropic", "claude-haiku-3-5");