


























@@ -97,15 +97,37 @@ describe("ensureAuthProfileStore", () => {
9797}
98989999function restoreAgentDirEnv(params: {
100+previousStateDir?: string | undefined;
100101previousAgentDir: string | undefined;
101102previousPiAgentDir: string | undefined;
102103}): void {
104+if ("previousStateDir" in params) {
105+restoreEnvValue("OPENCLAW_STATE_DIR", params.previousStateDir);
106+}
103107restoreEnvValue("OPENCLAW_AGENT_DIR", params.previousAgentDir);
104108restoreEnvValue("PI_CODING_AGENT_DIR", params.previousPiAgentDir);
105109}
106110107-function resolveDefaultMainAgentDir(stateDir: string): string {
108-return path.join(stateDir, "agents", "main", "agent");
111+function configureMainAuthTestDirs(root: string): {
112+mainDir: string;
113+agentDir: string;
114+previousStateDir: string | undefined;
115+previousAgentDir: string | undefined;
116+previousPiAgentDir: string | undefined;
117+} {
118+const previousStateDir = process.env.OPENCLAW_STATE_DIR;
119+const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
120+const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
121+const mainDir = path.join(root, "agents", "main", "agent");
122+const agentDir = path.join(root, "agents", "agent-x", "agent");
123+fs.mkdirSync(mainDir, { recursive: true });
124+fs.mkdirSync(agentDir, { recursive: true });
125+126+process.env.OPENCLAW_STATE_DIR = root;
127+process.env.OPENCLAW_AGENT_DIR = mainDir;
128+process.env.PI_CODING_AGENT_DIR = mainDir;
129+clearRuntimeAuthProfileStoreSnapshots();
130+return { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir };
109131}
110132111133function expectApiKeyProfile(
@@ -171,14 +193,9 @@ describe("ensureAuthProfileStore", () => {
171193172194it("merges main auth profiles into agent store and keeps agent overrides", () => {
173195const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-merge-"));
174-const previousStateDir = process.env.OPENCLAW_STATE_DIR;
196+const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
197+configureMainAuthTestDirs(root);
175198try {
176-process.env.OPENCLAW_STATE_DIR = root;
177-const mainDir = resolveDefaultMainAgentDir(root);
178-const agentDir = path.join(root, "agent-x");
179-fs.mkdirSync(mainDir, { recursive: true });
180-fs.mkdirSync(agentDir, { recursive: true });
181-182199const mainStore = {
183200version: AUTH_STORE_VERSION,
184201profiles: {
@@ -228,21 +245,16 @@ describe("ensureAuthProfileStore", () => {
228245key: "agent-key",
229246});
230247} finally {
231-restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
248+restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
232249fs.rmSync(root, { recursive: true, force: true });
233250}
234251});
235252236253it("uses the main agent's newer OAuth profile when an agent still has a stale default profile", () => {
237254const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-"));
238-const previousStateDir = process.env.OPENCLAW_STATE_DIR;
255+const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
256+configureMainAuthTestDirs(root);
239257try {
240-process.env.OPENCLAW_STATE_DIR = root;
241-const mainDir = resolveDefaultMainAgentDir(root);
242-const agentDir = path.join(root, "agent-x");
243-fs.mkdirSync(mainDir, { recursive: true });
244-fs.mkdirSync(agentDir, { recursive: true });
245-246258const freshProfileId = "openai-codex:user@example.com";
247259const staleProfileId = "openai-codex:default";
248260saveAuthProfileStore(
@@ -323,21 +335,16 @@ describe("ensureAuthProfileStore", () => {
323335) as { profiles: Record<string, unknown> };
324336expect(persistedAgentStore.profiles[staleProfileId]).toBeDefined();
325337} finally {
326-restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
338+restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
327339fs.rmSync(root, { recursive: true, force: true });
328340}
329341});
330342331343it("keeps a newer agent replacement credential while repairing stale default references", () => {
332344const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-newer-agent-"));
333-const previousStateDir = process.env.OPENCLAW_STATE_DIR;
345+const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
346+configureMainAuthTestDirs(root);
334347try {
335-process.env.OPENCLAW_STATE_DIR = root;
336-const mainDir = resolveDefaultMainAgentDir(root);
337-const agentDir = path.join(root, "agent-x");
338-fs.mkdirSync(mainDir, { recursive: true });
339-fs.mkdirSync(agentDir, { recursive: true });
340-341348const freshProfileId = "openai-codex:user@example.com";
342349const staleProfileId = "openai-codex:default";
343350saveAuthProfileStore(
@@ -403,21 +410,16 @@ describe("ensureAuthProfileStore", () => {
403410expect(store.order?.["openai-codex"]).toEqual([freshProfileId]);
404411expect(store.lastGood?.["openai-codex"]).toBe(freshProfileId);
405412} finally {
406-restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
413+restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
407414fs.rmSync(root, { recursive: true, force: true });
408415}
409416});
410417411418it("preserves a valid main default OAuth profile while replacing a stale agent override", () => {
412419const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-base-default-"));
413-const previousStateDir = process.env.OPENCLAW_STATE_DIR;
420+const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
421+configureMainAuthTestDirs(root);
414422try {
415-process.env.OPENCLAW_STATE_DIR = root;
416-const mainDir = resolveDefaultMainAgentDir(root);
417-const agentDir = path.join(root, "agent-x");
418-fs.mkdirSync(mainDir, { recursive: true });
419-fs.mkdirSync(agentDir, { recursive: true });
420-421423const freshProfileId = "openai-codex:user@example.com";
422424const defaultProfileId = "openai-codex:default";
423425saveAuthProfileStore(
@@ -489,21 +491,16 @@ describe("ensureAuthProfileStore", () => {
489491lastUsed: 123,
490492});
491493} finally {
492-restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
494+restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
493495fs.rmSync(root, { recursive: true, force: true });
494496}
495497});
496498497499it("keeps a stale default OAuth profile when the main profile belongs to a different identity", () => {
498500const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-mismatch-"));
499-const previousStateDir = process.env.OPENCLAW_STATE_DIR;
501+const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
502+configureMainAuthTestDirs(root);
500503try {
501-process.env.OPENCLAW_STATE_DIR = root;
502-const mainDir = resolveDefaultMainAgentDir(root);
503-const agentDir = path.join(root, "agent-x");
504-fs.mkdirSync(mainDir, { recursive: true });
505-fs.mkdirSync(agentDir, { recursive: true });
506-507504const freshProfileId = "openai-codex:user@example.com";
508505const staleProfileId = "openai-codex:default";
509506saveAuthProfileStore(
@@ -557,21 +554,16 @@ describe("ensureAuthProfileStore", () => {
557554expect(store.order?.["openai-codex"]).toEqual([staleProfileId]);
558555expect(store.lastGood?.["openai-codex"]).toBe(staleProfileId);
559556} finally {
560-restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
557+restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
561558fs.rmSync(root, { recursive: true, force: true });
562559}
563560});
564561565562it("rewrites invalidated per-agent Codex order to the main agent's healthy relogin profile", () => {
566563const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-codex-relogin-"));
567-const previousStateDir = process.env.OPENCLAW_STATE_DIR;
564+const { mainDir, agentDir, previousStateDir, previousAgentDir, previousPiAgentDir } =
565+configureMainAuthTestDirs(root);
568566try {
569-process.env.OPENCLAW_STATE_DIR = root;
570-const mainDir = resolveDefaultMainAgentDir(root);
571-const agentDir = path.join(root, "agent-x");
572-fs.mkdirSync(mainDir, { recursive: true });
573-fs.mkdirSync(agentDir, { recursive: true });
574-575567const now = Date.now();
576568const healthyProfileId = "openai-codex:bunsthedev@gmail.com";
577569const staleProfileId = "openai-codex:val@viewdue.ai";
@@ -642,7 +634,7 @@ describe("ensureAuthProfileStore", () => {
642634expect(store.lastGood?.["openai-codex"]).toBe(healthyProfileId);
643635expect(store.usageStats?.[staleProfileId]).toBeUndefined();
644636} finally {
645-restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
637+restoreAgentDirEnv({ previousStateDir, previousAgentDir, previousPiAgentDir });
646638fs.rmSync(root, { recursive: true, force: true });
647639}
648640});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。