Skip to content

Commit b841bd3

Browse files
committed
Fix test-mock leakage breaking codex-session and eslint unused import
mock.module for codex/session.js and xai/session.js in oauth-scope-check.test.ts, and for oauth-scope-check.js in provider-setup-submit.test.ts, replaced those modules for the whole bun test process without restoring them, breaking tests/unit/codex-session.test.ts which imports the real module directly. Capture the real module before mocking and restore it in afterAll. Also drops an unused beforeEach import that eslint flagged.
1 parent 19d109f commit b841bd3

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

src/auth/oauth-scope-check.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
1+
import { afterAll, afterEach, describe, expect, mock, test } from "bun:test";
22

33
// getValidCodexToken/getValidXaiToken hit the real home-level auth store and
44
// refresh endpoints; stub the session layer so this test only exercises the
5-
// scope probe's own HTTP call and status classification.
5+
// scope probe's own HTTP call and status classification. Other suites
6+
// (tests/unit/codex-session.test.ts) import the real modules directly, so the
7+
// mocks must be torn down after this file's tests run rather than leaking
8+
// into the rest of the bun test process.
9+
const realCodexSession = { ...(await import("./codex/session.js")) };
10+
const realXaiSession = { ...(await import("./xai/session.js")) };
11+
612
mock.module("./codex/session.js", () => ({
13+
...realCodexSession,
714
getValidCodexToken: async () => ({ access: "codex-token", accountId: "acct-1" }),
815
}));
916
mock.module("./xai/session.js", () => ({
17+
...realXaiSession,
1018
getValidXaiToken: async () => ({ access: "xai-token" }),
11-
xaiUserIdFromAccessToken: () => undefined,
1219
}));
1320

21+
afterAll(() => {
22+
mock.module("./codex/session.js", () => realCodexSession);
23+
mock.module("./xai/session.js", () => realXaiSession);
24+
});
25+
1426
const { checkOAuthProviderScope } = await import("./oauth-scope-check.js");
1527

1628
const originalFetch = global.fetch;

src/tui/provider-setup-submit.test.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, test, expect, afterEach, mock } from "bun:test";
1+
import { describe, test, expect, afterEach, afterAll, mock } from "bun:test";
22
import { mkdtemp, rm } from "node:fs/promises";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
@@ -7,16 +7,23 @@ import type { OAuthScopeCheckResult } from "../auth/oauth-scope-check.js";
77

88
// The oauth branch probes real provider scope over the network; stub the
99
// check so these tests exercise buildProviderSubmitHandler's own branching
10-
// (ok / insufficient-scope / unavailable) without a live call.
10+
// (ok / insufficient-scope / unavailable) without a live call. Restored after
11+
// this file's tests run so a mock never leaks into another suite that
12+
// imports provider-setup-submit.js and expects the real probe.
13+
const realOAuthScopeCheck = { ...(await import("../auth/oauth-scope-check.js")) };
1114
let scopeCheckResult: OAuthScopeCheckResult = { status: "ok" };
1215
mock.module("../auth/oauth-scope-check.js", () => ({
16+
...realOAuthScopeCheck,
1317
checkOAuthProviderScope: async () => scopeCheckResult,
1418
}));
1519

20+
afterAll(() => {
21+
mock.module("../auth/oauth-scope-check.js", () => realOAuthScopeCheck);
22+
});
23+
1624
const { buildProviderSubmitHandler } = await import("./provider-setup-submit.js");
17-
const { loadLocalSettings, loadSettings, localSettingsPath } = await import(
18-
"../config/settings.js"
19-
);
25+
const { loadLocalSettings, loadSettings, localSettingsPath } =
26+
await import("../config/settings.js");
2027
import type { ProviderFormValues, SubmitPhase } from "./provider-setup.js";
2128

2229
const noopSetPhase = (_phase: SubmitPhase): void => {};
@@ -226,7 +233,10 @@ describe("buildProviderSubmitHandler", () => {
226233
oauthProfile: "work",
227234
},
228235
noopSetPhase,
229-
{ skipValidation: false, oauth: { kind: "codex", providerName: "codex/work", profile: "work" } },
236+
{
237+
skipValidation: false,
238+
oauth: { kind: "codex", providerName: "codex/work", profile: "work" },
239+
},
230240
);
231241

232242
const local = await loadLocalSettings(localPath);
@@ -286,7 +296,10 @@ describe("buildProviderSubmitHandler", () => {
286296
oauthProfile: "work",
287297
},
288298
noopSetPhase,
289-
{ skipValidation: false, oauth: { kind: "codex", providerName: "codex/work", profile: "work" } },
299+
{
300+
skipValidation: false,
301+
oauth: { kind: "codex", providerName: "codex/work", profile: "work" },
302+
},
290303
);
291304

292305
const local = await loadLocalSettings(localPath);
@@ -310,7 +323,10 @@ describe("buildProviderSubmitHandler", () => {
310323
oauthProfile: "work",
311324
},
312325
noopSetPhase,
313-
{ skipValidation: true, oauth: { kind: "codex", providerName: "codex/work", profile: "work" } },
326+
{
327+
skipValidation: true,
328+
oauth: { kind: "codex", providerName: "codex/work", profile: "work" },
329+
},
314330
);
315331

316332
const local = await loadLocalSettings(localPath);

0 commit comments

Comments
 (0)