test: isolate main auth profile fixtures

This commit is contained in:
Peter Steinberger
2026-05-06 00:37:23 +01:00
parent f1636d5e28
commit df4db5a721

View File

@@ -97,15 +97,37 @@ describe("ensureAuthProfileStore", () => {
}
function restoreAgentDirEnv(params: {
previousStateDir?: string | undefined;
previousAgentDir: string | undefined;
previousPiAgentDir: string | undefined;
}): void {
if ("previousStateDir" in params) {
restoreEnvValue("OPENCLAW_STATE_DIR", params.previousStateDir);
}
restoreEnvValue("OPENCLAW_AGENT_DIR", params.previousAgentDir);
restoreEnvValue("PI_CODING_AGENT_DIR", params.previousPiAgentDir);
}
function resolveDefaultMainAgentDir(stateDir: string): string {
return path.join(stateDir, "agents", "main", "agent");
function configureMainAuthTestDirs(root: string): {
mainDir: string;
agentDir: string;
previousStateDir: string | undefined;
previousAgentDir: string | undefined;
previousPiAgentDir: string | undefined;
} {
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
const mainDir = path.join(root, "agents", "main", "agent");
const agentDir = path.join(root, "agents", "agent-x", "agent");
fs.mkdirSync(mainDir, { recursive: true });
fs.mkdirSync(agentDir, { recursive: true });
process.env.OPENCLAW_STATE_DIR = root;
process.env.OPENCLAW_AGENT_DIR = mainDir;
process.env.PI_CODING_AGENT_DIR = mainDir;
clearRuntimeAuthProfileStoreSnapshots();
return { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir };
}
function expectApiKeyProfile(
@@ -171,14 +193,9 @@ describe("ensureAuthProfileStore", () => {
it("merges main auth profiles into agent store and keeps agent overrides", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-merge-"));
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
configureMainAuthTestDirs(root);
try {
process.env.OPENCLAW_STATE_DIR = root;
const mainDir = resolveDefaultMainAgentDir(root);
const agentDir = path.join(root, "agent-x");
fs.mkdirSync(mainDir, { recursive: true });
fs.mkdirSync(agentDir, { recursive: true });
const mainStore = {
version: AUTH_STORE_VERSION,
profiles: {
@@ -228,21 +245,16 @@ describe("ensureAuthProfileStore", () => {
key: "agent-key",
});
} finally {
restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
fs.rmSync(root, { recursive: true, force: true });
}
});
it("uses the main agent's newer OAuth profile when an agent still has a stale default profile", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-"));
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
configureMainAuthTestDirs(root);
try {
process.env.OPENCLAW_STATE_DIR = root;
const mainDir = resolveDefaultMainAgentDir(root);
const agentDir = path.join(root, "agent-x");
fs.mkdirSync(mainDir, { recursive: true });
fs.mkdirSync(agentDir, { recursive: true });
const freshProfileId = "openai-codex:user@example.com";
const staleProfileId = "openai-codex:default";
saveAuthProfileStore(
@@ -323,21 +335,16 @@ describe("ensureAuthProfileStore", () => {
) as { profiles: Record<string, unknown> };
expect(persistedAgentStore.profiles[staleProfileId]).toBeDefined();
} finally {
restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
fs.rmSync(root, { recursive: true, force: true });
}
});
it("keeps a newer agent replacement credential while repairing stale default references", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-newer-agent-"));
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
configureMainAuthTestDirs(root);
try {
process.env.OPENCLAW_STATE_DIR = root;
const mainDir = resolveDefaultMainAgentDir(root);
const agentDir = path.join(root, "agent-x");
fs.mkdirSync(mainDir, { recursive: true });
fs.mkdirSync(agentDir, { recursive: true });
const freshProfileId = "openai-codex:user@example.com";
const staleProfileId = "openai-codex:default";
saveAuthProfileStore(
@@ -403,21 +410,16 @@ describe("ensureAuthProfileStore", () => {
expect(store.order?.["openai-codex"]).toEqual([freshProfileId]);
expect(store.lastGood?.["openai-codex"]).toBe(freshProfileId);
} finally {
restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
fs.rmSync(root, { recursive: true, force: true });
}
});
it("preserves a valid main default OAuth profile while replacing a stale agent override", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-base-default-"));
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
configureMainAuthTestDirs(root);
try {
process.env.OPENCLAW_STATE_DIR = root;
const mainDir = resolveDefaultMainAgentDir(root);
const agentDir = path.join(root, "agent-x");
fs.mkdirSync(mainDir, { recursive: true });
fs.mkdirSync(agentDir, { recursive: true });
const freshProfileId = "openai-codex:user@example.com";
const defaultProfileId = "openai-codex:default";
saveAuthProfileStore(
@@ -489,21 +491,16 @@ describe("ensureAuthProfileStore", () => {
lastUsed: 123,
});
} finally {
restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
fs.rmSync(root, { recursive: true, force: true });
}
});
it("keeps a stale default OAuth profile when the main profile belongs to a different identity", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-mismatch-"));
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
configureMainAuthTestDirs(root);
try {
process.env.OPENCLAW_STATE_DIR = root;
const mainDir = resolveDefaultMainAgentDir(root);
const agentDir = path.join(root, "agent-x");
fs.mkdirSync(mainDir, { recursive: true });
fs.mkdirSync(agentDir, { recursive: true });
const freshProfileId = "openai-codex:user@example.com";
const staleProfileId = "openai-codex:default";
saveAuthProfileStore(
@@ -557,21 +554,16 @@ describe("ensureAuthProfileStore", () => {
expect(store.order?.["openai-codex"]).toEqual([staleProfileId]);
expect(store.lastGood?.["openai-codex"]).toBe(staleProfileId);
} finally {
restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
fs.rmSync(root, { recursive: true, force: true });
}
});
it("rewrites invalidated per-agent Codex order to the main agent's healthy relogin profile", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-codex-relogin-"));
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
configureMainAuthTestDirs(root);
try {
process.env.OPENCLAW_STATE_DIR = root;
const mainDir = resolveDefaultMainAgentDir(root);
const agentDir = path.join(root, "agent-x");
fs.mkdirSync(mainDir, { recursive: true });
fs.mkdirSync(agentDir, { recursive: true });
const now = Date.now();
const healthyProfileId = "openai-codex:bunsthedev@gmail.com";
const staleProfileId = "openai-codex:val@viewdue.ai";
@@ -642,7 +634,7 @@ describe("ensureAuthProfileStore", () => {
expect(store.lastGood?.["openai-codex"]).toBe(healthyProfileId);
expect(store.usageStats?.[staleProfileId]).toBeUndefined();
} finally {
restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
fs.rmSync(root, { recursive: true, force: true });
}
});