test: restore directory profile state after cache tests

This commit is contained in:
can1357
2026-06-27 01:23:20 +02:00
parent 9dea09e238
commit a241cb0999
4 changed files with 55 additions and 16 deletions
@@ -13,7 +13,7 @@ import { Markit } from "@oh-my-pi/pi-coding-agent/markit";
import type { ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
import { ReadTool } from "@oh-my-pi/pi-coding-agent/tools/read";
import * as markit from "@oh-my-pi/pi-coding-agent/utils/markit";
import { __resetProfileSnapshotForTests, refreshDirsFromEnv, Snowflake, setAgentDir } from "@oh-my-pi/pi-utils";
import { __resetDirsFromEnvForTests, Snowflake, setAgentDir } from "@oh-my-pi/pi-utils";
function restoreEnv(key: string, value: string | undefined): void {
if (value === undefined) {
@@ -143,8 +143,7 @@ describe("read PDF with a line-range selector", () => {
restoreEnv("PI_CODING_AGENT_DIR", originalPiCodingAgentDir);
restoreEnv("OMP_PROFILE", originalOmpProfile);
restoreEnv("PI_PROFILE", originalPiProfile);
__resetProfileSnapshotForTests();
refreshDirsFromEnv();
__resetDirsFromEnvForTests();
}
});
});
@@ -13,13 +13,7 @@ import * as path from "node:path";
import { Markit } from "@oh-my-pi/pi-coding-agent/markit";
import { convertBufferWithMarkit, convertFileWithMarkit } from "@oh-my-pi/pi-coding-agent/utils/markit";
import { pruneMarkitConversionCache } from "@oh-my-pi/pi-coding-agent/utils/markit-cache";
import {
__resetProfileSnapshotForTests,
getAgentDir,
refreshDirsFromEnv,
Snowflake,
setAgentDir,
} from "@oh-my-pi/pi-utils";
import { __resetDirsFromEnvForTests, getAgentDir, Snowflake, setAgentDir } from "@oh-my-pi/pi-utils";
function restoreEnv(key: string, value: string | undefined): void {
if (value === undefined) {
@@ -52,8 +46,7 @@ describe("document conversion cache", () => {
restoreEnv("OMP_PROFILE", originalOmpProfile);
restoreEnv("PI_PROFILE", originalPiProfile);
restoreEnv("XDG_CACHE_HOME", originalXdgCacheHome);
__resetProfileSnapshotForTests();
refreshDirsFromEnv();
__resetDirsFromEnvForTests();
await fs.rm(testDir, { recursive: true, force: true });
});
+12
View File
@@ -431,6 +431,18 @@ export function __resetProfileSnapshotForTests(): void {
);
}
/**
* Test-only: rebuild profile + directory state from the current process env.
* Production code keeps the module-load profile stable; tests that mutate
* `setAgentDir`/`setProfile` need an exact restore point after they put env vars
* back.
*/
export function __resetDirsFromEnvForTests(): void {
activeProfile = readProfileFromEnvSafe();
__resetProfileSnapshotForTests();
refreshDirsFromEnv();
}
/** Activate a named profile. Passing undefined or "default" returns to the default profile. */
export function setProfile(profile: string | undefined): void {
const next = normalizeProfileName(profile);
+39 -4
View File
@@ -3,10 +3,11 @@ import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import {
__resetProfileSnapshotForTests,
__resetDirsFromEnvForTests,
getActiveProfile,
getConfigDirName,
getDocumentConversionCacheDir,
refreshDirsFromEnv,
getProfileRootDir,
setAgentDir,
} from "@oh-my-pi/pi-utils/dirs";
import { Snowflake } from "@oh-my-pi/pi-utils/snowflake";
@@ -40,8 +41,7 @@ describe("document conversion cache directory", () => {
restoreEnv("OMP_PROFILE", originalOmpProfile);
restoreEnv("PI_PROFILE", originalPiProfile);
restoreEnv("XDG_CACHE_HOME", originalXdgCacheHome);
__resetProfileSnapshotForTests();
refreshDirsFromEnv();
__resetDirsFromEnvForTests();
await fs.rm(tempRoot, { recursive: true, force: true });
});
@@ -67,3 +67,38 @@ describe("document conversion cache directory", () => {
expect(getDocumentConversionCacheDir()).toBe(path.join(customAgentDir, "cache", "document-conversions"));
});
});
describe("test directory state cleanup", () => {
it("restores the active profile from the current env after setAgentDir mutations", () => {
const originalPiCodingAgentDir = process.env.PI_CODING_AGENT_DIR;
const originalOmpProfile = process.env.OMP_PROFILE;
const originalPiProfile = process.env.PI_PROFILE;
const originalXdgCacheHome = process.env.XDG_CACHE_HOME;
try {
process.env.OMP_PROFILE = "cache-profile";
delete process.env.PI_PROFILE;
delete process.env.PI_CODING_AGENT_DIR;
delete process.env.XDG_CACHE_HOME;
__resetDirsFromEnvForTests();
setAgentDir(path.join(os.tmpdir(), "pi-utils-document-cache", Snowflake.next(), "agent"));
expect(getActiveProfile()).toBeUndefined();
process.env.OMP_PROFILE = "cache-profile";
delete process.env.PI_PROFILE;
delete process.env.PI_CODING_AGENT_DIR;
__resetDirsFromEnvForTests();
expect(getActiveProfile()).toBe("cache-profile");
expect(getDocumentConversionCacheDir()).toBe(
path.join(getProfileRootDir("cache-profile"), "agent", "cache", "document-conversions"),
);
} finally {
restoreEnv("PI_CODING_AGENT_DIR", originalPiCodingAgentDir);
restoreEnv("OMP_PROFILE", originalOmpProfile);
restoreEnv("PI_PROFILE", originalPiProfile);
restoreEnv("XDG_CACHE_HOME", originalXdgCacheHome);
__resetDirsFromEnvForTests();
}
});
});