Files
oh-my-pi/packages/coding-agent/test/session-manager/title-source-persistence.test.ts
T
can1357 d24d11a274 fix: resolved AI/OAuth helper duplication via shared modules
- Standardized missing-file read errors and now return `File not found: <path>` for absent edit targets.
- Centralized AI provider, usage, and OAuth helpers into shared modules to remove duplicated logic.
- Migrated OAuth/API-key login flows to shared factory helpers and removed inline prompt/token-exchange code.
- Reused shared tools and formatter utilities for discovery, stream tails, LSP batching, and source formatting.
- Consolidated repeated test helpers and fixtures into shared modules, replacing inline helper duplicates.
2026-04-23 21:02:14 +02:00

80 lines
2.6 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import {
loadEntriesFromFile,
type SessionHeader,
SessionManager,
} from "@oh-my-pi/pi-coding-agent/session/session-manager";
import { getConfigRootDir, setAgentDir } from "@oh-my-pi/pi-utils";
import { makeAssistantMessage } from "./helpers";
function getHeader(entries: unknown[]): SessionHeader | undefined {
return entries.find(
(entry): entry is SessionHeader =>
typeof entry === "object" && entry !== null && "type" in entry && entry.type === "session",
);
}
describe("session title source persistence", () => {
let testAgentDir: string;
let cwd: string;
const originalAgentDir = process.env.PI_CODING_AGENT_DIR;
const fallbackAgentDir = path.join(getConfigRootDir(), "agent");
beforeEach(() => {
testAgentDir = fs.mkdtempSync(path.join(os.tmpdir(), "omp-title-source-"));
cwd = path.join(testAgentDir, "cwd");
fs.mkdirSync(cwd, { recursive: true });
setAgentDir(testAgentDir);
});
afterEach(() => {
if (originalAgentDir) {
setAgentDir(originalAgentDir);
} else {
setAgentDir(fallbackAgentDir);
delete process.env.PI_CODING_AGENT_DIR;
}
fs.rmSync(testAgentDir, { recursive: true, force: true });
});
it("persists auto title source across reopen", async () => {
const session = SessionManager.create(cwd);
session.appendMessage({ role: "user", content: "hello", timestamp: 1 });
await session.setSessionName("Auto title", "auto");
session.appendMessage(makeAssistantMessage());
await session.flush();
const sessionFile = session.getSessionFile();
expect(sessionFile).toBeDefined();
const entries = await loadEntriesFromFile(sessionFile!);
expect(getHeader(entries)?.titleSource).toBe("auto");
const reopened = await SessionManager.open(sessionFile!);
expect(reopened.getSessionName()).toBe("Auto title");
expect(reopened.titleSource).toBe("auto");
});
it("persists user title source across reopen", async () => {
const session = SessionManager.create(cwd);
session.appendMessage({ role: "user", content: "hello", timestamp: 1 });
await session.setSessionName("Manual title", "user");
session.appendMessage(makeAssistantMessage());
await session.flush();
const sessionFile = session.getSessionFile();
expect(sessionFile).toBeDefined();
const entries = await loadEntriesFromFile(sessionFile!);
expect(getHeader(entries)?.titleSource).toBe("user");
const reopened = await SessionManager.open(sessionFile!);
expect(reopened.getSessionName()).toBe("Manual title");
expect(reopened.titleSource).toBe("user");
});
});