mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-18 08:37:28 +00:00
refactor(test): reuse sanitize session history fixtures
This commit is contained in:
@@ -2,6 +2,11 @@ import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type { SessionManager } from "@mariozechner/pi-coding-agent";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import * as helpers from "./pi-embedded-helpers.js";
|
||||
import {
|
||||
makeInMemorySessionManager,
|
||||
makeModelSnapshotEntry,
|
||||
makeReasoningAssistantMessages,
|
||||
} from "./pi-embedded-runner.sanitize-session-history.test-harness.js";
|
||||
|
||||
type SanitizeSessionHistory =
|
||||
typeof import("./pi-embedded-runner/google.js").sanitizeSessionHistory;
|
||||
@@ -70,36 +75,15 @@ describe("sanitizeSessionHistory e2e smoke", () => {
|
||||
});
|
||||
|
||||
it("downgrades openai reasoning blocks when the model snapshot changed", async () => {
|
||||
const sessionEntries: Array<{ type: string; customType: string; data: unknown }> = [
|
||||
{
|
||||
type: "custom",
|
||||
customType: "model-snapshot",
|
||||
data: {
|
||||
timestamp: Date.now(),
|
||||
provider: "anthropic",
|
||||
modelApi: "anthropic-messages",
|
||||
modelId: "claude-3-7",
|
||||
},
|
||||
},
|
||||
];
|
||||
const sessionManager = {
|
||||
getEntries: vi.fn(() => sessionEntries),
|
||||
appendCustomEntry: vi.fn((customType: string, data: unknown) => {
|
||||
sessionEntries.push({ type: "custom", customType, data });
|
||||
const sessionEntries = [
|
||||
makeModelSnapshotEntry({
|
||||
provider: "anthropic",
|
||||
modelApi: "anthropic-messages",
|
||||
modelId: "claude-3-7",
|
||||
}),
|
||||
} as unknown as SessionManager;
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "thinking",
|
||||
thinking: "reasoning",
|
||||
thinkingSignature: { id: "rs_test", type: "reasoning" },
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
const sessionManager = makeInMemorySessionManager(sessionEntries);
|
||||
const messages = makeReasoningAssistantMessages({ thinkingSignature: "object" });
|
||||
|
||||
const result = await sanitizeSessionHistory({
|
||||
messages,
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type { SessionManager } from "@mariozechner/pi-coding-agent";
|
||||
import { vi } from "vitest";
|
||||
|
||||
export type SessionEntry = { type: string; customType: string; data: unknown };
|
||||
|
||||
export function makeModelSnapshotEntry(data: {
|
||||
timestamp?: number;
|
||||
provider: string;
|
||||
modelApi: string;
|
||||
modelId: string;
|
||||
}): SessionEntry {
|
||||
return {
|
||||
type: "custom",
|
||||
customType: "model-snapshot",
|
||||
data: {
|
||||
timestamp: data.timestamp ?? Date.now(),
|
||||
provider: data.provider,
|
||||
modelApi: data.modelApi,
|
||||
modelId: data.modelId,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function makeInMemorySessionManager(entries: SessionEntry[]): SessionManager {
|
||||
return {
|
||||
getEntries: vi.fn(() => entries),
|
||||
appendCustomEntry: vi.fn((customType: string, data: unknown) => {
|
||||
entries.push({ type: "custom", customType, data });
|
||||
}),
|
||||
} as unknown as SessionManager;
|
||||
}
|
||||
|
||||
export function makeReasoningAssistantMessages(opts?: {
|
||||
thinkingSignature?: "object" | "json";
|
||||
}): AgentMessage[] {
|
||||
const thinkingSignature: unknown =
|
||||
opts?.thinkingSignature === "json"
|
||||
? JSON.stringify({ id: "rs_test", type: "reasoning" })
|
||||
: { id: "rs_test", type: "reasoning" };
|
||||
|
||||
// Intentional: we want to build message payloads that can carry non-string
|
||||
// signatures, but core typing currently expects a string.
|
||||
const messages = [
|
||||
{
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "thinking",
|
||||
thinking: "reasoning",
|
||||
thinkingSignature,
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
return messages as unknown as AgentMessage[];
|
||||
}
|
||||
@@ -2,6 +2,11 @@ import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type { SessionManager } from "@mariozechner/pi-coding-agent";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import * as helpers from "./pi-embedded-helpers.js";
|
||||
import {
|
||||
makeInMemorySessionManager,
|
||||
makeModelSnapshotEntry,
|
||||
makeReasoningAssistantMessages,
|
||||
} from "./pi-embedded-runner.sanitize-session-history.test-harness.js";
|
||||
|
||||
type SanitizeSessionHistory =
|
||||
typeof import("./pi-embedded-runner/google.js").sanitizeSessionHistory;
|
||||
@@ -216,36 +221,15 @@ describe("sanitizeSessionHistory", () => {
|
||||
});
|
||||
|
||||
it("does not downgrade openai reasoning when the model has not changed", async () => {
|
||||
const sessionEntries: Array<{ type: string; customType: string; data: unknown }> = [
|
||||
{
|
||||
type: "custom",
|
||||
customType: "model-snapshot",
|
||||
data: {
|
||||
timestamp: Date.now(),
|
||||
provider: "openai",
|
||||
modelApi: "openai-responses",
|
||||
modelId: "gpt-5.2-codex",
|
||||
},
|
||||
},
|
||||
];
|
||||
const sessionManager = {
|
||||
getEntries: vi.fn(() => sessionEntries),
|
||||
appendCustomEntry: vi.fn((customType: string, data: unknown) => {
|
||||
sessionEntries.push({ type: "custom", customType, data });
|
||||
const sessionEntries = [
|
||||
makeModelSnapshotEntry({
|
||||
provider: "openai",
|
||||
modelApi: "openai-responses",
|
||||
modelId: "gpt-5.2-codex",
|
||||
}),
|
||||
} as unknown as SessionManager;
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "thinking",
|
||||
thinking: "reasoning",
|
||||
thinkingSignature: JSON.stringify({ id: "rs_test", type: "reasoning" }),
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
const sessionManager = makeInMemorySessionManager(sessionEntries);
|
||||
const messages = makeReasoningAssistantMessages({ thinkingSignature: "json" });
|
||||
|
||||
const result = await sanitizeSessionHistory({
|
||||
messages,
|
||||
@@ -260,36 +244,15 @@ describe("sanitizeSessionHistory", () => {
|
||||
});
|
||||
|
||||
it("downgrades openai reasoning only when the model changes", async () => {
|
||||
const sessionEntries: Array<{ type: string; customType: string; data: unknown }> = [
|
||||
{
|
||||
type: "custom",
|
||||
customType: "model-snapshot",
|
||||
data: {
|
||||
timestamp: Date.now(),
|
||||
provider: "anthropic",
|
||||
modelApi: "anthropic-messages",
|
||||
modelId: "claude-3-7",
|
||||
},
|
||||
},
|
||||
];
|
||||
const sessionManager = {
|
||||
getEntries: vi.fn(() => sessionEntries),
|
||||
appendCustomEntry: vi.fn((customType: string, data: unknown) => {
|
||||
sessionEntries.push({ type: "custom", customType, data });
|
||||
const sessionEntries = [
|
||||
makeModelSnapshotEntry({
|
||||
provider: "anthropic",
|
||||
modelApi: "anthropic-messages",
|
||||
modelId: "claude-3-7",
|
||||
}),
|
||||
} as unknown as SessionManager;
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "thinking",
|
||||
thinking: "reasoning",
|
||||
thinkingSignature: { id: "rs_test", type: "reasoning" },
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
const sessionManager = makeInMemorySessionManager(sessionEntries);
|
||||
const messages = makeReasoningAssistantMessages({ thinkingSignature: "object" });
|
||||
|
||||
const result = await sanitizeSessionHistory({
|
||||
messages,
|
||||
|
||||
Reference in New Issue
Block a user