test: speed up google and twitch suites

This commit is contained in:
Peter Steinberger
2026-03-24 16:25:25 +00:00
parent 332d2ebfe8
commit 27b92f8335
2 changed files with 21 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
import { join, parse } from "node:path";
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { describe, expect, it, vi, beforeAll, beforeEach, afterEach } from "vitest";
vi.mock("../../src/infra/wsl.js", () => ({
isWSL2Sync: () => false,
@@ -38,13 +38,11 @@ describe("extractGeminiCliCredentials", () => {
`;
let originalPath: string | undefined;
async function loadCredentialsModule() {
return await import("./oauth.credentials.js");
}
let extractGeminiCliCredentials: typeof import("./oauth.credentials.js").extractGeminiCliCredentials;
let clearCredentialsCache: typeof import("./oauth.credentials.js").clearCredentialsCache;
let setOAuthCredentialsFsForTest: typeof import("./oauth.credentials.js").setOAuthCredentialsFsForTest;
async function installMockFs() {
const { setOAuthCredentialsFsForTest } = await loadCredentialsModule();
setOAuthCredentialsFsForTest({
existsSync: (...args) => mockExistsSync(...args),
readFileSync: (...args) => mockReadFileSync(...args),
@@ -156,6 +154,11 @@ describe("extractGeminiCliCredentials", () => {
});
}
beforeAll(async () => {
({ extractGeminiCliCredentials, clearCredentialsCache, setOAuthCredentialsFsForTest } =
await import("./oauth.credentials.js"));
});
beforeEach(async () => {
vi.clearAllMocks();
originalPath = process.env.PATH;
@@ -164,7 +167,6 @@ describe("extractGeminiCliCredentials", () => {
afterEach(async () => {
process.env.PATH = originalPath;
const { setOAuthCredentialsFsForTest } = await loadCredentialsModule();
setOAuthCredentialsFsForTest();
});
@@ -172,7 +174,6 @@ describe("extractGeminiCliCredentials", () => {
process.env.PATH = "/nonexistent";
mockExistsSync.mockReturnValue(false);
const { extractGeminiCliCredentials, clearCredentialsCache } = await loadCredentialsModule();
clearCredentialsCache();
expect(extractGeminiCliCredentials()).toBeNull();
});
@@ -180,7 +181,6 @@ describe("extractGeminiCliCredentials", () => {
it("extracts credentials from oauth2.js in known path", async () => {
installGeminiLayout({ oauth2Exists: true, oauth2Content: FAKE_OAUTH2_CONTENT });
const { extractGeminiCliCredentials, clearCredentialsCache } = await loadCredentialsModule();
clearCredentialsCache();
const result = extractGeminiCliCredentials();
@@ -190,7 +190,6 @@ describe("extractGeminiCliCredentials", () => {
it("extracts credentials when PATH entry is an npm global shim", async () => {
installNpmShimLayout({ oauth2Exists: true, oauth2Content: FAKE_OAUTH2_CONTENT });
const { extractGeminiCliCredentials, clearCredentialsCache } = await loadCredentialsModule();
clearCredentialsCache();
const result = extractGeminiCliCredentials();
@@ -200,7 +199,6 @@ describe("extractGeminiCliCredentials", () => {
it("returns null when oauth2.js cannot be found", async () => {
installGeminiLayout({ oauth2Exists: false, readdir: [] });
const { extractGeminiCliCredentials, clearCredentialsCache } = await loadCredentialsModule();
clearCredentialsCache();
expect(extractGeminiCliCredentials()).toBeNull();
});
@@ -208,7 +206,6 @@ describe("extractGeminiCliCredentials", () => {
it("returns null when oauth2.js lacks credentials", async () => {
installGeminiLayout({ oauth2Exists: true, oauth2Content: "// no credentials here" });
const { extractGeminiCliCredentials, clearCredentialsCache } = await loadCredentialsModule();
clearCredentialsCache();
expect(extractGeminiCliCredentials()).toBeNull();
});
@@ -216,7 +213,6 @@ describe("extractGeminiCliCredentials", () => {
it("caches credentials after first extraction", async () => {
installGeminiLayout({ oauth2Exists: true, oauth2Content: FAKE_OAUTH2_CONTENT });
const { extractGeminiCliCredentials, clearCredentialsCache } = await loadCredentialsModule();
clearCredentialsCache();
// First call

View File

@@ -9,7 +9,8 @@
* - Error handling and edge cases
*/
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { resolveTwitchToken } from "./token.js";
import { TwitchClientManager } from "./twitch-client.js";
import type { ChannelLogSink, TwitchAccountConfig, TwitchChatMessage } from "./types.js";
@@ -83,6 +84,7 @@ vi.mock("./token.js", () => ({
describe("TwitchClientManager", () => {
let manager: TwitchClientManager;
let mockLogger: ChannelLogSink;
let resolveTwitchTokenMock: ReturnType<typeof vi.mocked<typeof resolveTwitchToken>>;
const testAccount: TwitchAccountConfig = {
username: "testbot",
@@ -100,7 +102,11 @@ describe("TwitchClientManager", () => {
enabled: true,
};
beforeEach(async () => {
beforeAll(() => {
resolveTwitchTokenMock = vi.mocked(resolveTwitchToken);
});
beforeEach(() => {
// Clear all mocks first
vi.clearAllMocks();
@@ -108,8 +114,7 @@ describe("TwitchClientManager", () => {
messageHandlers.length = 0;
// Re-set up the default token mock implementation after clearing
const { resolveTwitchToken } = await import("./token.js");
vi.mocked(resolveTwitchToken).mockReturnValue({
resolveTwitchTokenMock.mockReturnValue({
token: "oauth:mock-token-from-tests",
source: "config" as const,
});
@@ -176,8 +181,7 @@ describe("TwitchClientManager", () => {
};
// Override the mock to return a specific token for this test
const { resolveTwitchToken } = await import("./token.js");
vi.mocked(resolveTwitchToken).mockReturnValue({
resolveTwitchTokenMock.mockReturnValue({
token: "oauth:actualtoken123",
source: "config" as const,
});
@@ -189,8 +193,7 @@ describe("TwitchClientManager", () => {
it("should use token directly when no oauth: prefix", async () => {
// Override the mock to return a token without oauth: prefix
const { resolveTwitchToken } = await import("./token.js");
vi.mocked(resolveTwitchToken).mockReturnValue({
resolveTwitchTokenMock.mockReturnValue({
token: "oauth:mock-token-from-tests",
source: "config" as const,
});
@@ -221,8 +224,7 @@ describe("TwitchClientManager", () => {
it("should throw error when token is missing", async () => {
// Override the mock to return empty token
const { resolveTwitchToken } = await import("./token.js");
vi.mocked(resolveTwitchToken).mockReturnValue({
resolveTwitchTokenMock.mockReturnValue({
token: "",
source: "none" as const,
});