Files
openclaw/extensions/codex/src/node-cli-sessions.test.ts
Peter Steinberger 26210c1600 refactor: remove browser and codex dead exports (#105867)
* refactor(browser): collapse Playwright export paths

* refactor(browser): remove dead plugin exports

* refactor(codex): remove dead app-server exports

* refactor(codex): remove remaining dead exports

* test(codex): use canonical private-type owners

* test(browser): isolate proxy startup state

* test(browser): remove stale chrome imports

* refactor(codex): privatize remaining helpers

* chore(deadcode): refresh export baseline after rebase

* refactor(browser): finish canonical helper ownership

* refactor: fix dead-export cleanup gates

* refactor(codex): keep runtime facades LOC-neutral

* chore(ci): refresh TypeScript LOC baseline

* chore(deadcode): refresh ratchets after rebase

* chore(ci): refresh LOC baseline after main advance

* chore(deadcode): align ratchets with latest main
2026-07-12 22:23:11 -07:00

246 lines
8.1 KiB
TypeScript

// Codex tests cover node cli sessions plugin behavior.
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import process from "node:process";
import type { PluginRuntime } from "openclaw/plugin-sdk/plugin-runtime";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
createCodexCliSessionNodeHostCommands,
listCodexCliSessionsOnNode,
} from "./node-cli-sessions.js";
const CODEX_CLI_SESSIONS_LIST_COMMAND = "codex.cli.sessions.list";
let tempDir: string;
let previousCodexHome: string | undefined;
describe("codex cli node sessions", () => {
beforeEach(async () => {
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-cli-sessions-"));
previousCodexHome = process.env.CODEX_HOME;
process.env.CODEX_HOME = tempDir;
});
afterEach(async () => {
if (previousCodexHome === undefined) {
delete process.env.CODEX_HOME;
} else {
process.env.CODEX_HOME = previousCodexHome;
}
await fs.rm(tempDir, { recursive: true, force: true });
});
it("lists recent sessions from Codex history and hydrates cwd from session files", async () => {
const sessionId = "019e2007-1f7e-7eb1-a42b-8c01f4b9b5cd";
await fs.writeFile(
path.join(tempDir, "history.jsonl"),
[
JSON.stringify({ session_id: sessionId, ts: 1778677925, text: "first ask" }),
JSON.stringify({ session_id: sessionId, ts: 1778678322, text: "latest ask" }),
JSON.stringify({ session_id: "older", ts: 1778670000, text: "skip me" }),
].join("\n"),
);
const sessionDir = path.join(tempDir, "sessions", "2026", "05", "13");
await fs.mkdir(sessionDir, { recursive: true });
await fs.writeFile(
path.join(sessionDir, `rollout-2026-05-13T08-29-58-${sessionId}.jsonl`),
`${JSON.stringify({
type: "session_meta",
payload: { id: sessionId, cwd: "/repo" },
})}\n`,
);
const command = createCodexCliSessionNodeHostCommands().find(
(entry) => entry.command === CODEX_CLI_SESSIONS_LIST_COMMAND,
);
const raw = await command?.handle(JSON.stringify({ filter: "latest", limit: 5 }));
const parsed = JSON.parse(raw ?? "{}") as {
sessions?: Array<{
sessionId?: string;
cwd?: string;
lastMessage?: string;
messageCount?: number;
}>;
};
expect(parsed.sessions).toEqual([
{
sessionId,
updatedAt: "2026-05-13T13:18:42.000Z",
lastMessage: "latest ask",
cwd: "/repo",
sessionFile: path.join(sessionDir, `rollout-2026-05-13T08-29-58-${sessionId}.jsonl`),
messageCount: 2,
},
]);
});
it("ignores Date-invalid Codex history timestamps", async () => {
const sessionId = "019e2007-1f7e-7eb1-a42b-8c01f4b9b5cf";
await fs.writeFile(
path.join(tempDir, "history.jsonl"),
JSON.stringify({ session_id: sessionId, ts: 8_700_000_000_000, text: "bad timestamp" }),
);
const command = createCodexCliSessionNodeHostCommands().find(
(entry) => entry.command === CODEX_CLI_SESSIONS_LIST_COMMAND,
);
const raw = await command?.handle(JSON.stringify({ filter: "bad timestamp", limit: 5 }));
const parsed = JSON.parse(raw ?? "{}") as {
sessions?: Array<{
sessionId?: string;
updatedAt?: string;
lastMessage?: string;
messageCount?: number;
}>;
};
expect(parsed.sessions).toEqual([
{
sessionId,
lastMessage: "bad timestamp",
messageCount: 1,
},
]);
});
it("lists sessions from Codex session files when history is absent", async () => {
const sessionId = "019e23d1-f33d-78e3-959e-0f56f30a5249";
const sessionDir = path.join(tempDir, "sessions", "2026", "05", "14");
const sessionFile = path.join(sessionDir, `rollout-2026-05-14T00-10-22-${sessionId}.jsonl`);
await fs.mkdir(sessionDir, { recursive: true });
await fs.writeFile(
sessionFile,
[
JSON.stringify({
timestamp: "2026-05-14T00:10:23.618Z",
type: "session_meta",
payload: { id: sessionId, cwd: "/tmp/codex-work" },
}),
JSON.stringify({
timestamp: "2026-05-14T00:10:23.619Z",
type: "response_item",
payload: {
type: "message",
role: "user",
content: [{ type: "input_text", text: "Reply with exactly: CRABBOX" }],
},
}),
].join("\n"),
);
const command = createCodexCliSessionNodeHostCommands().find(
(entry) => entry.command === CODEX_CLI_SESSIONS_LIST_COMMAND,
);
const raw = await command?.handle(JSON.stringify({ filter: "crabbox", limit: 5 }));
const parsed = JSON.parse(raw ?? "{}") as {
sessions?: Array<{
sessionId?: string;
cwd?: string;
lastMessage?: string;
messageCount?: number;
}>;
};
expect(parsed.sessions).toEqual([
{
sessionId,
updatedAt: "2026-05-14T00:10:23.619Z",
lastMessage: "Reply with exactly: CRABBOX",
cwd: "/tmp/codex-work",
sessionFile,
messageCount: 1,
},
]);
});
it("reports malformed node session payloadJSON with an owned error", async () => {
const runtime = {
nodes: {
list: vi.fn(async () => ({
nodes: [
{
nodeId: "node-1",
connected: true,
commands: [CODEX_CLI_SESSIONS_LIST_COMMAND],
},
],
})),
invoke: vi.fn(async () => ({
ok: true,
payloadJSON: "{not json",
})),
},
} as unknown as PluginRuntime;
await expect(
listCodexCliSessionsOnNode({
runtime,
requestedNode: "node-1",
}),
).rejects.toThrow("Codex CLI node command returned malformed payloadJSON.");
});
it("keeps Codex history session previews on UTF-16 code point boundaries", async () => {
const sessionId = "019e2007-1f7e-7eb1-a42b-8c01f4b9b5ce";
const text = `${"a".repeat(136)}🤖tail`;
await fs.writeFile(
path.join(tempDir, "history.jsonl"),
JSON.stringify({ session_id: sessionId, ts: 1778678322, text }),
);
const command = createCodexCliSessionNodeHostCommands().find(
(entry) => entry.command === CODEX_CLI_SESSIONS_LIST_COMMAND,
);
const raw = await command?.handle(JSON.stringify({ filter: "", limit: 5 }));
const parsed = JSON.parse(raw ?? "{}") as {
sessions?: Array<{ lastMessage?: string }>;
};
expect(parsed.sessions?.[0]?.lastMessage).toBe(`${"a".repeat(136)}...`);
expect(parsed.sessions?.[0]?.lastMessage).not.toContain("\ud83e");
expect(parsed.sessions?.[0]?.lastMessage).not.toContain("\udd16");
});
it("keeps Codex session-file previews on UTF-16 code point boundaries", async () => {
const sessionId = "019e23d1-f33d-78e3-959e-0f56f30a5248";
const sessionDir = path.join(tempDir, "sessions", "2026", "05", "14");
const sessionFile = path.join(sessionDir, `rollout-2026-05-14T00-10-22-${sessionId}.jsonl`);
const text = `${"b".repeat(136)}🤖tail`;
await fs.mkdir(sessionDir, { recursive: true });
await fs.writeFile(
sessionFile,
[
JSON.stringify({
timestamp: "2026-05-14T00:10:23.618Z",
type: "session_meta",
payload: { id: sessionId, cwd: "/tmp/codex-work" },
}),
JSON.stringify({
timestamp: "2026-05-14T00:10:23.619Z",
type: "response_item",
payload: {
type: "message",
role: "user",
content: [{ type: "input_text", text }],
},
}),
].join("\n"),
);
const command = createCodexCliSessionNodeHostCommands().find(
(entry) => entry.command === CODEX_CLI_SESSIONS_LIST_COMMAND,
);
const raw = await command?.handle(JSON.stringify({ filter: "", limit: 5 }));
const parsed = JSON.parse(raw ?? "{}") as {
sessions?: Array<{ lastMessage?: string }>;
};
expect(parsed.sessions?.[0]?.lastMessage).toBe(`${"b".repeat(136)}...`);
expect(parsed.sessions?.[0]?.lastMessage).not.toContain("\ud83e");
expect(parsed.sessions?.[0]?.lastMessage).not.toContain("\udd16");
});
});