From b4916d414321bff0ffdf0f43af7d14e37b540789 Mon Sep 17 00:00:00 2001 From: "zhou.han" Date: Wed, 11 Nov 2026 20:39:49 +0800 Subject: [PATCH] window-warmup tests: put XDG_CONFIG_HOME and XDG_CACHE_HOME back after each file (#996, #1001) window-warmup's tests no longer leave XDG_CONFIG_HOME and XDG_CACHE_HOME set for every later file of the root `bun test` (#996). What changed (test files only; no runtime code, no version bump, no dist rebuild): - tests/fake-gateway.ts no longer sets the two XDG variables on import. Its `--serve` demo calls the new pinMagpieDirs() first, so the demo still runs with the temporary dirs. - tests/test-env.ts: pinMagpieDirsForTest() records the inherited values, pins both to $TMPDIR/window-warmup-test-magpie-{config,cache}, and returns a restore that puts back each inherited value, or deletes one that wasn't set. - browser, diagnostics, gateway, scheduler and server tests pin at load and restore in afterAll (in a finally after their own cleanup). diagnostics' own override is put back before the file's pin is. - Every test file that imports fake-gateway.ts, or reaches magpieDirs()' defaults through startService/Scheduler, pins its own; magpie-files and store tests pass env explicitly. Run at the reviewed head c13d2844 (its parent is origin/main 740e1af2), on magpie-desktop (WSL Ubuntu 24.04, Bun 1.4.2), HOME=$(mktemp -d): - bun test packages/window-warmup plus a trailing probe file asserting both variables are back: 280 pass, 0 fail with both unset (probe sees null/null), and with both inherited as /tmp/inh-cfg and /tmp/inh-cache (probe sees them exactly). - bun scripts/check.mjs window-warmup: ok. - Root bun test (both XDG unset): 8298 pass, 2 skip, 0 fail, 8317 tests in 584 files. Broken by hand, same probe: - test-env.ts restore made a no-op: probe red (sees the window-warmup temp dirs). - only server.test.ts skipping its restore: probe red. - origin/main's window-warmup tests (740e1af2) with the probe: red, as #996 describes. Not tried: `bun run typecheck` (CI doesn't run it either), and Windows. --- packages/window-warmup/tests/browser.test.ts | 13 ++++++++++--- .../window-warmup/tests/diagnostics.test.ts | 19 +++++++++++++------ packages/window-warmup/tests/fake-gateway.ts | 9 +++++---- packages/window-warmup/tests/gateway.test.ts | 6 +++++- .../window-warmup/tests/scheduler.test.ts | 7 ++++--- packages/window-warmup/tests/server.test.ts | 13 ++++++++++--- packages/window-warmup/tests/test-env.ts | 17 +++++++++++++++++ 7 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 packages/window-warmup/tests/test-env.ts diff --git a/packages/window-warmup/tests/browser.test.ts b/packages/window-warmup/tests/browser.test.ts index d0d0830..f5db8cb 100644 --- a/packages/window-warmup/tests/browser.test.ts +++ b/packages/window-warmup/tests/browser.test.ts @@ -7,6 +7,9 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { startService, type ServiceHandle } from "../src/server"; import { fakeGateway, type FakeGateway } from "./fake-gateway"; +import { pinMagpieDirsForTest } from "./test-env"; + +const restoreMagpieDirs = pinMagpieDirsForTest(); // Playwright 的最小类型面:不依赖它的类型声明,没装也能通过 tsc interface Locator { @@ -97,9 +100,13 @@ beforeAll(async () => { }); afterAll(async () => { - await svc?.stop(); - gw?.stop(); - if (dir) await rm(dir, { recursive: true, force: true }); + try { + await svc?.stop(); + gw?.stop(); + if (dir) await rm(dir, { recursive: true, force: true }); + } finally { + restoreMagpieDirs(); + } }); for (const [name, engine] of ENGINES) { diff --git a/packages/window-warmup/tests/diagnostics.test.ts b/packages/window-warmup/tests/diagnostics.test.ts index bb2a63f..603bfd7 100644 --- a/packages/window-warmup/tests/diagnostics.test.ts +++ b/packages/window-warmup/tests/diagnostics.test.ts @@ -10,6 +10,9 @@ import { Scheduler } from "../src/scheduler"; import { startService, type ServiceHandle } from "../src/server"; import type { Run, Task } from "../src/types"; import { fakeGateway, type FakeGateway } from "./fake-gateway"; +import { pinMagpieDirsForTest } from "./test-env"; + +const restoreMagpieDirs = pinMagpieDirsForTest(); const KEY_RAW = "sk-test-SECRET-9f8e7d6c5b"; const FP = createHash("sha256").update(KEY_RAW, "utf8").digest("hex").slice(0, 10); @@ -74,13 +77,17 @@ beforeAll(async () => { }); afterAll(async () => { - await svc?.stop(); - gw?.stop(); - for (const [k, v] of Object.entries(saved)) { - if (v === undefined) delete process.env[k]; - else process.env[k] = v; + try { + await svc?.stop(); + gw?.stop(); + for (const [k, v] of Object.entries(saved)) { + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } + await rm(root, { recursive: true, force: true }); + } finally { + restoreMagpieDirs(); } - await rm(root, { recursive: true, force: true }); }); const daily = { kind: "daily", time: "06:00", timeZone: "Asia/Shanghai" }; diff --git a/packages/window-warmup/tests/fake-gateway.ts b/packages/window-warmup/tests/fake-gateway.ts index 4770d75..d858e0f 100644 --- a/packages/window-warmup/tests/fake-gateway.ts +++ b/packages/window-warmup/tests/fake-gateway.ts @@ -3,10 +3,10 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; -// 测试进程绝不读真实用户的 Magpie 配置:把目录推导用的 XDG 钉到临时位置 -// (fetchCatalog 与调度器的兜底读取会走 magpieDirs 的默认路径) -process.env.XDG_CONFIG_HOME = join(tmpdir(), "window-warmup-test-magpie-config"); -process.env.XDG_CACHE_HOME = join(tmpdir(), "window-warmup-test-magpie-cache"); +export function pinMagpieDirs() { + process.env.XDG_CONFIG_HOME = join(tmpdir(), "window-warmup-test-magpie-config"); + process.env.XDG_CACHE_HOME = join(tmpdir(), "window-warmup-test-magpie-cache"); +} export interface FakeChatReply { status?: number; @@ -140,6 +140,7 @@ export function fakeGateway(opts: FakeGatewayOpts = {}): FakeGateway { // bun tests/fake-gateway.ts --serve:以示例数据常驻并打印地址,供页面联调与截图(长驻,勿在测试里跑) if (import.meta.main && process.argv.includes("--serve")) { + pinMagpieDirs(); const in2h = new Date(Date.now() + 2 * 3_600_000).toISOString(); const gw = fakeGateway({ models: [ diff --git a/packages/window-warmup/tests/gateway.test.ts b/packages/window-warmup/tests/gateway.test.ts index f63c860..644fe59 100644 --- a/packages/window-warmup/tests/gateway.test.ts +++ b/packages/window-warmup/tests/gateway.test.ts @@ -1,4 +1,4 @@ -import { afterEach, describe, expect, test } from "bun:test"; +import { afterAll, afterEach, describe, expect, test } from "bun:test"; import { createHash } from "node:crypto"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; @@ -19,9 +19,13 @@ import { import type { CachedModel, KeyDirectoryEntry } from "../src/magpie-files"; import type { CatalogAccount, EffortPick, QuotaEntry, QuotaWindow, RawModel, Task } from "../src/types"; import { fakeGateway, type FakeGateway, type FakeGatewayOpts } from "./fake-gateway"; +import { pinMagpieDirsForTest } from "./test-env"; + +const restoreMagpieDirs = pinMagpieDirsForTest(); const started: FakeGateway[] = []; const cleanupDirs: string[] = []; +afterAll(restoreMagpieDirs); function start(opts: FakeGatewayOpts = {}): FakeGateway { const gw = fakeGateway(opts); started.push(gw); diff --git a/packages/window-warmup/tests/scheduler.test.ts b/packages/window-warmup/tests/scheduler.test.ts index a4303a1..6f4bbb8 100644 --- a/packages/window-warmup/tests/scheduler.test.ts +++ b/packages/window-warmup/tests/scheduler.test.ts @@ -1,6 +1,6 @@ // 调度器的失败测试:注入假时钟、假定时器与假网关函数,不发任何真实请求。 // 单个定时器有 10 分钟封顶,跨长时段的断言一律看 snapshot().next(语义到期点)而非定时器 at。 -import { afterEach, expect, test } from "bun:test"; +import { afterAll, afterEach, expect, test } from "bun:test"; import { createHash } from "node:crypto"; import { mkdtemp, readFile, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; @@ -8,15 +8,16 @@ import { join } from "node:path"; import type { CachedModel, KeyDirectoryEntry } from "../src/magpie-files"; import { openStore, type Store, type TaskMarks } from "../src/store"; import { Scheduler, type TaskInput } from "../src/scheduler"; +import { pinMagpieDirsForTest } from "./test-env"; import type { QuotaEntry, RawModel, Run, SendResult, Task } from "../src/types"; // 测试进程绝不读真实用户的 Magpie 配置:把默认目录推导用的 XDG 钉到临时位置 -process.env.XDG_CONFIG_HOME = join(tmpdir(), "window-warmup-test-magpie-config"); -process.env.XDG_CACHE_HOME = join(tmpdir(), "window-warmup-test-magpie-cache"); +const restoreMagpieDirs = pinMagpieDirsForTest(); const HOUR = 3_600_000; const T0 = "2026-10-31T05:00:00+08:00"; // 周六 05:00,上海 const ms = (iso: string) => new Date(iso).getTime(); +afterAll(restoreMagpieDirs); /** 不思考的模型:effort 应为 null,请求不带 reasoning_effort */ const M1: RawModel = { id: "m1", owned_by: "p1", reasoning: false }; diff --git a/packages/window-warmup/tests/server.test.ts b/packages/window-warmup/tests/server.test.ts index 0d299ac..3fba631 100644 --- a/packages/window-warmup/tests/server.test.ts +++ b/packages/window-warmup/tests/server.test.ts @@ -8,14 +8,21 @@ import { join, posix, win32 } from "node:path"; import plugin from "../src/plugin"; import { alreadyServing, insideDir, loadOrCreateToken, startService, TOKEN_FILE, type ServiceHandle } from "../src/server"; import { fakeGateway, type FakeGateway } from "./fake-gateway"; +import { pinMagpieDirsForTest } from "./test-env"; + +const restoreMagpieDirs = pinMagpieDirsForTest(); const services: ServiceHandle[] = []; const gateways: FakeGateway[] = []; const dirs: string[] = []; afterAll(async () => { - while (services.length) await services.pop()!.stop(); - for (const gw of gateways.splice(0)) gw.stop(); - while (dirs.length) await rm(dirs.pop()!, { recursive: true, force: true }); + try { + while (services.length) await services.pop()!.stop(); + for (const gw of gateways.splice(0)) gw.stop(); + while (dirs.length) await rm(dirs.pop()!, { recursive: true, force: true }); + } finally { + restoreMagpieDirs(); + } }); const MODELS = [{ id: "p1/m1", owned_by: "p1", reasoning: false }]; diff --git a/packages/window-warmup/tests/test-env.ts b/packages/window-warmup/tests/test-env.ts new file mode 100644 index 0000000..6aba920 --- /dev/null +++ b/packages/window-warmup/tests/test-env.ts @@ -0,0 +1,17 @@ +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +export function pinMagpieDirsForTest(): () => void { + const inherited = { + XDG_CONFIG_HOME: process.env.XDG_CONFIG_HOME, + XDG_CACHE_HOME: process.env.XDG_CACHE_HOME, + }; + process.env.XDG_CONFIG_HOME = join(tmpdir(), "window-warmup-test-magpie-config"); + process.env.XDG_CACHE_HOME = join(tmpdir(), "window-warmup-test-magpie-cache"); + return () => { + for (const [key, value] of Object.entries(inherited)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + }; +}