fix(test): isolate tests from mock.module cache pollution
mock.module() in index-execute.test.ts permanently poisons Bun's
module cache — mock.restore() does not undo it in Bun 1.3.11.
Use cache-busted dynamic imports (../src/mod.ts?t=${Date.now()}) in
beforeEach for the three affected test files so each test gets a
fresh, unpoisoned module instance.
This commit is contained in:
@@ -4,7 +4,9 @@ import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { registerPerplexityConfigCommand } from "../src/commands/config.js";
|
||||
import { loadConfig, saveConfig } from "../src/config.js";
|
||||
|
||||
let loadConfig: (configPath?: string) => Promise<import("../src/config.js").PerplexityConfig>;
|
||||
let saveConfig: (config: import("../src/config.js").PerplexityConfig, configPath?: string) => Promise<void>;
|
||||
|
||||
let tempDir: string;
|
||||
let configPath: string;
|
||||
@@ -12,6 +14,10 @@ let configPath: string;
|
||||
beforeEach(async () => {
|
||||
tempDir = await mkdtemp(join(tmpdir(), "pi-perplexity-command-test-"));
|
||||
configPath = join(tempDir, "config.json");
|
||||
|
||||
const mod = await import(`../src/config.ts?t=${Date.now()}`);
|
||||
loadConfig = mod.loadConfig;
|
||||
saveConfig = mod.saveConfig;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
|
||||
+24
-1
@@ -3,7 +3,12 @@ import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { loadConfig, saveConfig, resolveSearchDefaults } from "../src/config.js";
|
||||
let loadConfig: (configPath?: string) => Promise<import("../src/config.js").PerplexityConfig>;
|
||||
let saveConfig: (config: import("../src/config.js").PerplexityConfig, configPath?: string) => Promise<void>;
|
||||
let resolveSearchDefaults: (
|
||||
params: { model?: string; incognito?: boolean },
|
||||
config: import("../src/config.js").PerplexityConfig,
|
||||
) => { model: string; incognito: boolean };
|
||||
|
||||
let tempDir: string;
|
||||
let configPath: string;
|
||||
@@ -11,6 +16,11 @@ let configPath: string;
|
||||
beforeEach(async () => {
|
||||
tempDir = await mkdtemp(join(tmpdir(), "pi-perplexity-test-"));
|
||||
configPath = join(tempDir, "config.json");
|
||||
|
||||
const mod = await import(`../src/config.ts?t=${Date.now()}`);
|
||||
loadConfig = mod.loadConfig;
|
||||
saveConfig = mod.saveConfig;
|
||||
resolveSearchDefaults = mod.resolveSearchDefaults;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -119,6 +129,19 @@ describe("resolveSearchDefaults", () => {
|
||||
}
|
||||
});
|
||||
|
||||
test("whitespace-only model env var falls back to config", () => {
|
||||
const originalModel = process.env.PI_PERPLEXITY_MODEL;
|
||||
try {
|
||||
process.env.PI_PERPLEXITY_MODEL = " ";
|
||||
|
||||
const result = resolveSearchDefaults({}, { model: "gpt54" });
|
||||
expect(result.model).toBe("gpt54");
|
||||
} finally {
|
||||
if (originalModel === undefined) delete process.env.PI_PERPLEXITY_MODEL;
|
||||
else process.env.PI_PERPLEXITY_MODEL = originalModel;
|
||||
}
|
||||
});
|
||||
|
||||
test("per-call params override everything", () => {
|
||||
const originalModel = process.env.PI_PERPLEXITY_MODEL;
|
||||
try {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test";
|
||||
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
||||
|
||||
import { searchPerplexity } from "../../src/search/client.js";
|
||||
import { SearchError } from "../../src/search/types.js";
|
||||
|
||||
let searchPerplexity: typeof import("../../src/search/client.js").searchPerplexity;
|
||||
|
||||
function createSseResponse(events: Array<Record<string, unknown>>, status = 200): Response {
|
||||
const streamText = [
|
||||
...events.map((event) => `data: ${JSON.stringify(event)}\n\n`),
|
||||
@@ -18,6 +19,11 @@ function createSseResponse(events: Array<Record<string, unknown>>, status = 200)
|
||||
describe("searchPerplexity", () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
|
||||
beforeEach(async () => {
|
||||
const mod = await import(`../../src/search/client.ts?t=${Date.now()}`);
|
||||
searchPerplexity = mod.searchPerplexity;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user