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/main740e1af2), 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.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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" };
|
||||
|
||||
@@ -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: [
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 };
|
||||
|
||||
@@ -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 }];
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user