mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 10:41:25 +00:00
refactor(test): dedupe pi subscribe text_end cases
This commit is contained in:
@@ -13,7 +13,7 @@ describe("subscribeEmbeddedPiSession", () => {
|
|||||||
{ tag: "antthinking", open: "<antthinking>", close: "</antthinking>" },
|
{ tag: "antthinking", open: "<antthinking>", close: "</antthinking>" },
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
it("does not append when text_end content is a prefix of deltas", () => {
|
function setupTextEndSubscription() {
|
||||||
let handler: ((evt: unknown) => void) | undefined;
|
let handler: ((evt: unknown) => void) | undefined;
|
||||||
const session: StubSession = {
|
const session: StubSession = {
|
||||||
subscribe: (fn) => {
|
subscribe: (fn) => {
|
||||||
@@ -31,103 +31,59 @@ describe("subscribeEmbeddedPiSession", () => {
|
|||||||
blockReplyBreak: "text_end",
|
blockReplyBreak: "text_end",
|
||||||
});
|
});
|
||||||
|
|
||||||
handler?.({
|
const emit = (evt: unknown) => handler?.(evt);
|
||||||
type: "message_update",
|
|
||||||
message: { role: "assistant" },
|
|
||||||
assistantMessageEvent: {
|
|
||||||
type: "text_delta",
|
|
||||||
delta: "Hello world",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
handler?.({
|
const emitDelta = (delta: string) => {
|
||||||
type: "message_update",
|
emit({
|
||||||
message: { role: "assistant" },
|
type: "message_update",
|
||||||
assistantMessageEvent: {
|
message: { role: "assistant" },
|
||||||
type: "text_end",
|
assistantMessageEvent: {
|
||||||
content: "Hello",
|
type: "text_delta",
|
||||||
},
|
delta,
|
||||||
});
|
},
|
||||||
|
});
|
||||||
expect(onBlockReply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(subscription.assistantTexts).toEqual(["Hello world"]);
|
|
||||||
});
|
|
||||||
it("does not append when text_end content is already contained", () => {
|
|
||||||
let handler: ((evt: unknown) => void) | undefined;
|
|
||||||
const session: StubSession = {
|
|
||||||
subscribe: (fn) => {
|
|
||||||
handler = fn;
|
|
||||||
return () => {};
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onBlockReply = vi.fn();
|
const emitTextEnd = (content: string) => {
|
||||||
|
emit({
|
||||||
const subscription = subscribeEmbeddedPiSession({
|
type: "message_update",
|
||||||
session: session as unknown as Parameters<typeof subscribeEmbeddedPiSession>[0]["session"],
|
message: { role: "assistant" },
|
||||||
runId: "run",
|
assistantMessageEvent: {
|
||||||
onBlockReply,
|
type: "text_end",
|
||||||
blockReplyBreak: "text_end",
|
content,
|
||||||
});
|
},
|
||||||
|
});
|
||||||
handler?.({
|
|
||||||
type: "message_update",
|
|
||||||
message: { role: "assistant" },
|
|
||||||
assistantMessageEvent: {
|
|
||||||
type: "text_delta",
|
|
||||||
delta: "Hello world",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
handler?.({
|
|
||||||
type: "message_update",
|
|
||||||
message: { role: "assistant" },
|
|
||||||
assistantMessageEvent: {
|
|
||||||
type: "text_end",
|
|
||||||
content: "world",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(onBlockReply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(subscription.assistantTexts).toEqual(["Hello world"]);
|
|
||||||
});
|
|
||||||
it("appends suffix when text_end content extends deltas", () => {
|
|
||||||
let handler: ((evt: unknown) => void) | undefined;
|
|
||||||
const session: StubSession = {
|
|
||||||
subscribe: (fn) => {
|
|
||||||
handler = fn;
|
|
||||||
return () => {};
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onBlockReply = vi.fn();
|
return { onBlockReply, subscription, emitDelta, emitTextEnd };
|
||||||
|
}
|
||||||
|
|
||||||
const subscription = subscribeEmbeddedPiSession({
|
it.each([
|
||||||
session: session as unknown as Parameters<typeof subscribeEmbeddedPiSession>[0]["session"],
|
{
|
||||||
runId: "run",
|
name: "does not append when text_end content is a prefix of deltas",
|
||||||
onBlockReply,
|
delta: "Hello world",
|
||||||
blockReplyBreak: "text_end",
|
content: "Hello",
|
||||||
});
|
expected: "Hello world",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "does not append when text_end content is already contained",
|
||||||
|
delta: "Hello world",
|
||||||
|
content: "world",
|
||||||
|
expected: "Hello world",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "appends suffix when text_end content extends deltas",
|
||||||
|
delta: "Hello",
|
||||||
|
content: "Hello world",
|
||||||
|
expected: "Hello world",
|
||||||
|
},
|
||||||
|
])("$name", ({ delta, content, expected }) => {
|
||||||
|
const { onBlockReply, subscription, emitDelta, emitTextEnd } = setupTextEndSubscription();
|
||||||
|
|
||||||
handler?.({
|
emitDelta(delta);
|
||||||
type: "message_update",
|
emitTextEnd(content);
|
||||||
message: { role: "assistant" },
|
|
||||||
assistantMessageEvent: {
|
|
||||||
type: "text_delta",
|
|
||||||
delta: "Hello",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
handler?.({
|
|
||||||
type: "message_update",
|
|
||||||
message: { role: "assistant" },
|
|
||||||
assistantMessageEvent: {
|
|
||||||
type: "text_end",
|
|
||||||
content: "Hello world",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(onBlockReply).toHaveBeenCalledTimes(1);
|
expect(onBlockReply).toHaveBeenCalledTimes(1);
|
||||||
expect(subscription.assistantTexts).toEqual(["Hello world"]);
|
expect(subscription.assistantTexts).toEqual([expected]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user