refactor: share cli session history test helpers

This commit is contained in:
Vincent Koc
2026-06-02 00:19:12 +02:00
parent 64697fbe24
commit 285401ced8

View File

@@ -13,6 +13,7 @@ import {
const ORIGINAL_HOME = process.env.HOME;
type ClaudeCliFallbackSeed = NonNullable<ReturnType<typeof readClaudeCliFallbackSeed>>;
type AugmentCliHistoryParams = Parameters<typeof augmentChatHistoryWithCliSessionImports>[0];
function requireFallbackSeed(
seed: ReturnType<typeof readClaudeCliFallbackSeed>,
@@ -41,6 +42,32 @@ function readRecord(value: unknown): Record<string, unknown> {
return value as Record<string, unknown>;
}
function expectCliSessionMarker(message: unknown, sessionId: string): void {
expectFields(readRecord(message)["__openclaw"], { cliSessionId: sessionId });
}
function augmentBoundClaudeHistory(params: {
homeDir: string;
sessionId: string;
provider: AugmentCliHistoryParams["provider"];
localMessages?: AugmentCliHistoryParams["localMessages"];
}) {
return augmentChatHistoryWithCliSessionImports({
entry: {
sessionId: "openclaw-session",
updatedAt: Date.now(),
cliSessionBindings: {
"claude-cli": {
sessionId: params.sessionId,
},
},
},
provider: params.provider,
localMessages: params.localMessages ?? [],
homeDir: params.homeDir,
});
}
function createClaudeHistoryLines(sessionId: string) {
return [
JSON.stringify({
@@ -316,40 +343,24 @@ describe("cli session history", () => {
it("augments chat history when a session has a claude-cli binding", async () => {
await withClaudeProjectsDir(async ({ homeDir, sessionId }) => {
const messages = augmentChatHistoryWithCliSessionImports({
entry: {
sessionId: "openclaw-session",
updatedAt: Date.now(),
cliSessionBindings: {
"claude-cli": {
sessionId,
},
},
},
provider: "claude-cli",
localMessages: [],
const messages = augmentBoundClaudeHistory({
homeDir,
sessionId,
provider: "claude-cli",
});
expect(messages).toHaveLength(3);
expectFields(messages[0], {
role: "user",
});
expectFields(readRecord(messages[0])["__openclaw"], { cliSessionId: sessionId });
expectCliSessionMarker(messages[0], sessionId);
});
});
it("augments anthropic-routed chat history when a Claude CLI binding has local messages", async () => {
await withClaudeProjectsDir(async ({ homeDir, sessionId }) => {
const messages = augmentChatHistoryWithCliSessionImports({
entry: {
sessionId: "openclaw-session",
updatedAt: Date.now(),
cliSessionBindings: {
"claude-cli": {
sessionId,
},
},
},
const messages = augmentBoundClaudeHistory({
homeDir,
sessionId,
provider: "anthropic",
localMessages: [
{
@@ -358,7 +369,6 @@ describe("cli session history", () => {
timestamp: Date.parse("2026-03-26T16:29:57.000Z"),
},
],
homeDir,
});
expect(messages).toHaveLength(4);
@@ -391,19 +401,11 @@ describe("cli session history", () => {
timestamp: Date.parse("2026-03-26T16:29:57.000Z"),
},
];
const messages = augmentChatHistoryWithCliSessionImports({
entry: {
sessionId: "openclaw-session",
updatedAt: Date.now(),
cliSessionBindings: {
"claude-cli": {
sessionId,
},
},
},
const messages = augmentBoundClaudeHistory({
homeDir,
sessionId,
provider: "openai",
localMessages,
homeDir,
});
expect(messages).toBe(localMessages);
@@ -428,7 +430,7 @@ describe("cli session history", () => {
expectFields(messages[1], {
role: "assistant",
});
expectFields(readRecord(messages[1])["__openclaw"], { cliSessionId: sessionId });
expectCliSessionMarker(messages[1], sessionId);
});
});
@@ -448,7 +450,7 @@ describe("cli session history", () => {
expectFields(messages[0], {
role: "user",
});
expectFields(readRecord(messages[0])["__openclaw"], { cliSessionId: sessionId });
expectCliSessionMarker(messages[0], sessionId);
});
});
});