mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 22:48:27 +00:00
test(agents): dedupe subscribe reasoning tag fixtures
This commit is contained in:
@@ -7,6 +7,13 @@ type SubscribeEmbeddedPiSessionParams = Parameters<SubscribeEmbeddedPiSession>[0
|
|||||||
type PiSession = Parameters<SubscribeEmbeddedPiSession>[0]["session"];
|
type PiSession = Parameters<SubscribeEmbeddedPiSession>[0]["session"];
|
||||||
type OnBlockReply = NonNullable<SubscribeEmbeddedPiSessionParams["onBlockReply"]>;
|
type OnBlockReply = NonNullable<SubscribeEmbeddedPiSessionParams["onBlockReply"]>;
|
||||||
|
|
||||||
|
export const THINKING_TAG_CASES = [
|
||||||
|
{ tag: "think", open: "<think>", close: "</think>" },
|
||||||
|
{ tag: "thinking", open: "<thinking>", close: "</thinking>" },
|
||||||
|
{ tag: "thought", open: "<thought>", close: "</thought>" },
|
||||||
|
{ tag: "antthinking", open: "<antthinking>", close: "</antthinking>" },
|
||||||
|
] as const;
|
||||||
|
|
||||||
export function createStubSessionHarness(): {
|
export function createStubSessionHarness(): {
|
||||||
session: PiSession;
|
session: PiSession;
|
||||||
emit: (evt: unknown) => void;
|
emit: (evt: unknown) => void;
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { createStubSessionHarness } from "./pi-embedded-subscribe.e2e-harness.js";
|
import {
|
||||||
|
THINKING_TAG_CASES,
|
||||||
|
createStubSessionHarness,
|
||||||
|
} from "./pi-embedded-subscribe.e2e-harness.js";
|
||||||
import { subscribeEmbeddedPiSession } from "./pi-embedded-subscribe.js";
|
import { subscribeEmbeddedPiSession } from "./pi-embedded-subscribe.js";
|
||||||
|
|
||||||
describe("subscribeEmbeddedPiSession", () => {
|
describe("subscribeEmbeddedPiSession", () => {
|
||||||
const THINKING_TAG_CASES = [
|
|
||||||
{ tag: "think", open: "<think>", close: "</think>" },
|
|
||||||
{ tag: "thinking", open: "<thinking>", close: "</thinking>" },
|
|
||||||
{ tag: "thought", open: "<thought>", close: "</thought>" },
|
|
||||||
{ tag: "antthinking", open: "<antthinking>", close: "</antthinking>" },
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
function createReasoningBlockReplyHarness() {
|
function createReasoningBlockReplyHarness() {
|
||||||
const { session, emit } = createStubSessionHarness();
|
const { session, emit } = createStubSessionHarness();
|
||||||
const onBlockReply = vi.fn();
|
const onBlockReply = vi.fn();
|
||||||
@@ -26,6 +22,12 @@ describe("subscribeEmbeddedPiSession", () => {
|
|||||||
return { emit, onBlockReply };
|
return { emit, onBlockReply };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function expectReasoningAndAnswerCalls(onBlockReply: ReturnType<typeof vi.fn>) {
|
||||||
|
expect(onBlockReply).toHaveBeenCalledTimes(2);
|
||||||
|
expect(onBlockReply.mock.calls[0][0].text).toBe("Reasoning:\n_Because it helps_");
|
||||||
|
expect(onBlockReply.mock.calls[1][0].text).toBe("Final answer");
|
||||||
|
}
|
||||||
|
|
||||||
it("emits reasoning as a separate message when enabled", () => {
|
it("emits reasoning as a separate message when enabled", () => {
|
||||||
const { emit, onBlockReply } = createReasoningBlockReplyHarness();
|
const { emit, onBlockReply } = createReasoningBlockReplyHarness();
|
||||||
|
|
||||||
@@ -39,9 +41,7 @@ describe("subscribeEmbeddedPiSession", () => {
|
|||||||
|
|
||||||
emit({ type: "message_end", message: assistantMessage });
|
emit({ type: "message_end", message: assistantMessage });
|
||||||
|
|
||||||
expect(onBlockReply).toHaveBeenCalledTimes(2);
|
expectReasoningAndAnswerCalls(onBlockReply);
|
||||||
expect(onBlockReply.mock.calls[0][0].text).toBe("Reasoning:\n_Because it helps_");
|
|
||||||
expect(onBlockReply.mock.calls[1][0].text).toBe("Final answer");
|
|
||||||
});
|
});
|
||||||
it.each(THINKING_TAG_CASES)(
|
it.each(THINKING_TAG_CASES)(
|
||||||
"promotes <%s> tags to thinking blocks at write-time",
|
"promotes <%s> tags to thinking blocks at write-time",
|
||||||
@@ -60,9 +60,7 @@ describe("subscribeEmbeddedPiSession", () => {
|
|||||||
|
|
||||||
emit({ type: "message_end", message: assistantMessage });
|
emit({ type: "message_end", message: assistantMessage });
|
||||||
|
|
||||||
expect(onBlockReply).toHaveBeenCalledTimes(2);
|
expectReasoningAndAnswerCalls(onBlockReply);
|
||||||
expect(onBlockReply.mock.calls[0][0].text).toBe("Reasoning:\n_Because it helps_");
|
|
||||||
expect(onBlockReply.mock.calls[1][0].text).toBe("Final answer");
|
|
||||||
|
|
||||||
expect(assistantMessage.content).toEqual([
|
expect(assistantMessage.content).toEqual([
|
||||||
{ type: "thinking", thinking: "Because it helps" },
|
{ type: "thinking", thinking: "Because it helps" },
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import {
|
import {
|
||||||
|
THINKING_TAG_CASES,
|
||||||
createStubSessionHarness,
|
createStubSessionHarness,
|
||||||
emitMessageStartAndEndForAssistantText,
|
emitMessageStartAndEndForAssistantText,
|
||||||
expectSingleAgentEventText,
|
expectSingleAgentEventText,
|
||||||
@@ -13,13 +14,6 @@ type StubSession = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
describe("subscribeEmbeddedPiSession", () => {
|
describe("subscribeEmbeddedPiSession", () => {
|
||||||
const THINKING_TAG_CASES = [
|
|
||||||
{ tag: "think", open: "<think>", close: "</think>" },
|
|
||||||
{ tag: "thinking", open: "<thinking>", close: "</thinking>" },
|
|
||||||
{ tag: "thought", open: "<thought>", close: "</thought>" },
|
|
||||||
{ tag: "antthinking", open: "<antthinking>", close: "</antthinking>" },
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
function createAgentEventHarness(options?: { runId?: string; sessionKey?: string }) {
|
function createAgentEventHarness(options?: { runId?: string; sessionKey?: string }) {
|
||||||
const { session, emit } = createStubSessionHarness();
|
const { session, emit } = createStubSessionHarness();
|
||||||
const onAgentEvent = vi.fn();
|
const onAgentEvent = vi.fn();
|
||||||
|
|||||||
Reference in New Issue
Block a user