refactor(test): dedupe block streaming runner setup

This commit is contained in:
Peter Steinberger
2026-02-14 20:23:26 +00:00
parent 2b5ad475ad
commit 05e2957edc

View File

@@ -38,27 +38,21 @@ vi.mock("./queue.js", async () => {
import { runReplyAgent } from "./agent-runner.js"; import { runReplyAgent } from "./agent-runner.js";
describe("runReplyAgent block streaming", () => { describe("runReplyAgent block streaming", () => {
it("coalesces duplicate text_end block replies", async () => { function createBaseContext() {
const onBlockReply = vi.fn(); return {
runEmbeddedPiAgentMock.mockImplementationOnce(async (params) => { typing: createMockTypingController(),
const block = params.onBlockReply as ((payload: { text?: string }) => void) | undefined; sessionCtx: {
block?.({ text: "Hello" }); Provider: "discord",
block?.({ text: "Hello" }); OriginatingTo: "channel:C1",
return { AccountId: "primary",
payloads: [{ text: "Final message" }], MessageSid: "msg",
meta: {}, } as unknown as TemplateContext,
}; resolvedQueue: { mode: "interrupt" } as unknown as QueueSettings,
}); };
}
const typing = createMockTypingController(); function createBaseFollowupRun() {
const sessionCtx = { return {
Provider: "discord",
OriginatingTo: "channel:C1",
AccountId: "primary",
MessageSid: "msg",
} as unknown as TemplateContext;
const resolvedQueue = { mode: "interrupt" } as unknown as QueueSettings;
const followupRun = {
prompt: "hello", prompt: "hello",
summaryLine: "hello", summaryLine: "hello",
enqueuedAt: Date.now(), enqueuedAt: Date.now(),
@@ -94,17 +88,20 @@ describe("runReplyAgent block streaming", () => {
blockReplyBreak: "text_end", blockReplyBreak: "text_end",
}, },
} as unknown as FollowupRun; } as unknown as FollowupRun;
}
const result = await runReplyAgent({ function createBaseRunArgs(params: { onBlockReply: unknown; blockReplyTimeoutMs?: number }) {
const { typing, sessionCtx, resolvedQueue } = createBaseContext();
return {
commandBody: "hello", commandBody: "hello",
followupRun, followupRun: createBaseFollowupRun(),
queueKey: "main", queueKey: "main",
resolvedQueue, resolvedQueue,
shouldSteer: false, shouldSteer: false,
shouldFollowup: false, shouldFollowup: false,
isActive: false, isActive: false,
isStreaming: false, isStreaming: false,
opts: { onBlockReply }, opts: { onBlockReply: params.onBlockReply, blockReplyTimeoutMs: params.blockReplyTimeoutMs },
typing, typing,
sessionCtx, sessionCtx,
defaultModel: "anthropic/claude-opus-4-5", defaultModel: "anthropic/claude-opus-4-5",
@@ -119,8 +116,23 @@ describe("runReplyAgent block streaming", () => {
resolvedBlockStreamingBreak: "text_end", resolvedBlockStreamingBreak: "text_end",
shouldInjectGroupIntro: false, shouldInjectGroupIntro: false,
typingMode: "instant", typingMode: "instant",
};
}
it("coalesces duplicate text_end block replies", async () => {
const onBlockReply = vi.fn();
runEmbeddedPiAgentMock.mockImplementationOnce(async (params) => {
const block = params.onBlockReply as ((payload: { text?: string }) => void) | undefined;
block?.({ text: "Hello" });
block?.({ text: "Hello" });
return {
payloads: [{ text: "Final message" }],
meta: {},
};
}); });
const result = await runReplyAgent(createBaseRunArgs({ onBlockReply }));
expect(onBlockReply).toHaveBeenCalledTimes(1); expect(onBlockReply).toHaveBeenCalledTimes(1);
expect(onBlockReply.mock.calls[0][0].text).toBe("Hello"); expect(onBlockReply.mock.calls[0][0].text).toBe("Hello");
expect(result).toBeUndefined(); expect(result).toBeUndefined();
@@ -152,76 +164,9 @@ describe("runReplyAgent block streaming", () => {
}; };
}); });
const typing = createMockTypingController(); const resultPromise = runReplyAgent(
const sessionCtx = { createBaseRunArgs({ onBlockReply, blockReplyTimeoutMs: 1 }),
Provider: "discord", );
OriginatingTo: "channel:C1",
AccountId: "primary",
MessageSid: "msg",
} as unknown as TemplateContext;
const resolvedQueue = { mode: "interrupt" } as unknown as QueueSettings;
const followupRun = {
prompt: "hello",
summaryLine: "hello",
enqueuedAt: Date.now(),
run: {
sessionId: "session",
sessionKey: "main",
messageProvider: "discord",
sessionFile: "/tmp/session.jsonl",
workspaceDir: "/tmp",
config: {
agents: {
defaults: {
blockStreamingCoalesce: {
minChars: 1,
maxChars: 200,
idleMs: 0,
},
},
},
},
skillsSnapshot: {},
provider: "anthropic",
model: "claude",
thinkLevel: "low",
verboseLevel: "off",
elevatedLevel: "off",
bashElevated: {
enabled: false,
allowed: false,
defaultLevel: "off",
},
timeoutMs: 1_000,
blockReplyBreak: "text_end",
},
} as unknown as FollowupRun;
const resultPromise = runReplyAgent({
commandBody: "hello",
followupRun,
queueKey: "main",
resolvedQueue,
shouldSteer: false,
shouldFollowup: false,
isActive: false,
isStreaming: false,
opts: { onBlockReply, blockReplyTimeoutMs: 1 },
typing,
sessionCtx,
defaultModel: "anthropic/claude-opus-4-5",
resolvedVerboseLevel: "off",
isNewSession: false,
blockStreamingEnabled: true,
blockReplyChunking: {
minChars: 1,
maxChars: 200,
breakPreference: "paragraph",
},
resolvedBlockStreamingBreak: "text_end",
shouldInjectGroupIntro: false,
typingMode: "instant",
});
await vi.advanceTimersByTimeAsync(5); await vi.advanceTimersByTimeAsync(5);
const result = await resultPromise; const result = await resultPromise;