Files
oh-my-pi/packages/coding-agent/test/sdk-session-isolation.test.ts
T
maximhar e27ceb2f96 fix: persist MCP discovery tool selections across session lifecycle (#453)
* fix: persist MCP discovery selections across session lifecycle

* fix: restore MCP discovery state on branch switches

* fix: preserve explicit MCP baseline on old branch contexts

* fix(coding-agent): preserve cleared MCP selections on resume

* fix(coding-agent): isolate MCP defaults across session switches

* fix(coding-agent): preserve MCP defaults across outages

---------

Co-authored-by: can1357 <me@can.ac>
2026-03-17 14:49:54 +01:00

99 lines
2.8 KiB
TypeScript

import { afterEach, 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 type { Rule } from "@oh-my-pi/pi-coding-agent/capability/rule";
import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
import { createAgentSession } from "@oh-my-pi/pi-coding-agent/sdk";
import { getSessionsDir, Snowflake } from "@oh-my-pi/pi-utils";
function createTtsrRule(name: string): Rule {
return {
name,
path: `/tmp/${name}.md`,
content: "Avoid forbidden output",
condition: ["forbidden"],
scope: ["text"],
_source: {
provider: "test",
providerName: "test",
path: `/tmp/${name}.md`,
level: "project",
},
};
}
describe("createAgentSession session storage isolation", () => {
const tempDirs: string[] = [];
afterEach(async () => {
for (const tempDir of tempDirs.splice(0)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
it("uses the provided agentDir for the default persistent session root", async () => {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), `pi-sdk-session-isolation-${Snowflake.next()}-`));
tempDirs.push(tempDir);
const cwd = path.join(tempDir, `project-${Snowflake.next()}`);
const agentDir = path.join(tempDir, "agent");
fs.mkdirSync(cwd, { recursive: true });
const { session } = await createAgentSession({
cwd,
agentDir,
settings: Settings.isolated(),
disableExtensionDiscovery: true,
skills: [],
contextFiles: [],
promptTemplates: [],
slashCommands: [],
enableMCP: false,
enableLsp: false,
});
try {
const sessionFile = session.sessionFile;
if (!sessionFile) {
throw new Error("Expected session file path");
}
expect(sessionFile.startsWith(path.join(agentDir, "sessions"))).toBe(true);
expect(sessionFile.startsWith(getSessionsDir())).toBe(false);
} finally {
await session.dispose();
}
});
it("wires the discovered TTSR manager into the created session", async () => {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), `pi-sdk-ttsr-${Snowflake.next()}-`));
tempDirs.push(tempDir);
const cwd = path.join(tempDir, `project-${Snowflake.next()}`);
const agentDir = path.join(tempDir, "agent");
const rule = createTtsrRule("sdk-ttsr-rule");
fs.mkdirSync(cwd, { recursive: true });
const { session } = await createAgentSession({
cwd,
agentDir,
settings: Settings.isolated(),
rules: [rule],
disableExtensionDiscovery: true,
skills: [],
contextFiles: [],
promptTemplates: [],
slashCommands: [],
enableMCP: false,
enableLsp: false,
});
try {
expect(session.ttsrManager).toBeDefined();
expect(session.ttsrManager?.checkDelta("forbidden", { source: "text" }).map(match => match.name)).toEqual([
rule.name,
]);
} finally {
await session.dispose();
}
});
});