test(coding-agent/session): updated session fork tests for helper usage

- Refactored test assertions to use loadEntriesFromFile for session file parsing.
- Verified forked session content using entry type discrimination instead of line-based parsing.
This commit is contained in:
can1357
2026-06-28 07:29:06 +02:00
parent 18f3386aed
commit be01c68efb
@@ -1,7 +1,12 @@
import { describe, expect, it } from "bun:test";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { CURRENT_SESSION_VERSION, type SessionHeader } from "@oh-my-pi/pi-coding-agent/session/session-entries";
import {
CURRENT_SESSION_VERSION,
type SessionHeader,
type SessionMessageEntry,
} from "@oh-my-pi/pi-coding-agent/session/session-entries";
import { loadEntriesFromFile } from "@oh-my-pi/pi-coding-agent/session/session-loader";
import { SessionManager } from "@oh-my-pi/pi-coding-agent/session/session-manager";
import { getTerminalId } from "@oh-my-pi/pi-tui";
import { getAgentDir, getTerminalSessionsDir, removeWithRetries, setAgentDir, TempDir } from "@oh-my-pi/pi-utils";
@@ -65,12 +70,15 @@ describe("SessionManager.forkFrom", () => {
expect(await Bun.file(breadcrumbFile).exists()).toBe(false);
expect(cloneFile).not.toBe(sourceFile);
const lines = (await Bun.file(cloneFile).text()).trim().split("\n");
const cloneHeader = JSON.parse(lines[0] ?? "{}") as SessionHeader;
const cloneMessage = JSON.parse(lines[1] ?? "{}") as JsonlMessageEntry;
expect(cloneHeader.id).not.toBe(sourceHeader.id);
expect(cloneHeader.parentSession).toBe(sourceHeader.id);
expect(cloneHeader.cwd).toBe(cwd);
const cloneEntries = await loadEntriesFromFile(cloneFile);
const cloneHeader = cloneEntries.find((entry): entry is SessionHeader => entry.type === "session");
const cloneMessage = cloneEntries.find(
(entry): entry is SessionMessageEntry => entry.type === "message",
);
expect(cloneHeader?.id).not.toBe(sourceHeader.id);
expect(cloneHeader?.parentSession).toBe(sourceHeader.id);
expect(cloneHeader?.cwd).toBe(cwd);
if (cloneMessage?.message.role !== "user") throw new Error("expected forked user message");
expect(cloneMessage.message.content).toBe("hello");
} finally {
if (previousTermSessionId === undefined) {