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.
191 lines
6.8 KiB
TypeScript
191 lines
6.8 KiB
TypeScript
// 测试与 --serve 演示共用的假 Magpie 网关:只在本机回环上应答,不发任何真实请求。
|
||
// 请求全部按到达顺序记进 requests,供断言请求体与请求头。
|
||
import { tmpdir } from "node:os";
|
||
import { join } from "node:path";
|
||
|
||
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;
|
||
body?: unknown;
|
||
/** 延迟应答毫秒数,用于模拟超时与外部取消 */
|
||
delayMs?: number;
|
||
/**
|
||
* 模拟 2xx 响应体不完整:truncate=半截 JSON;reset=请求已收到、应答前网关掉线
|
||
* (连接被强制关闭,客户端收到 ECONNRESET;此后假网关不再应答)。
|
||
* reset 不用 ReadableStream 的 controller.error:Bun.serve 会把它当未处理错误打印
|
||
* (error 回调收不到),测试全绿时日志里也出现「error: 连接中断」(#902)。
|
||
*/
|
||
breakBody?: "truncate" | "reset";
|
||
}
|
||
|
||
export interface FakeRequest {
|
||
method: string;
|
||
path: string;
|
||
/** 查询串原样(如 "?kept=1"),用于区分额度缓存读取与完整读取 */
|
||
search: string;
|
||
headers: Record<string, string>;
|
||
/** /v1/chat/completions 的 JSON 请求体;其余请求为 null */
|
||
body: unknown;
|
||
}
|
||
|
||
export interface FakeGatewayOpts {
|
||
models?: unknown[];
|
||
/** 额度完整读取(不带 kept)的应答;kept=1 缺省也用它(缓存里就是最新读数) */
|
||
quotas?: unknown[];
|
||
/** ?kept=1 的独立应答,模拟「缓存为空」触发兜底完整读取 */
|
||
keptQuotas?: unknown[];
|
||
/** /v1/models 的状态码,模拟模型目录接口故障 */
|
||
modelsStatus?: number;
|
||
/** 额度缓存读取(kept=1)的状态码,模拟额度接口故障 */
|
||
quotasStatus?: number;
|
||
/** 完整读取(不带 kept)的状态码;缺省跟随 quotasStatus(额度接口挂了两种读法都挂) */
|
||
fullQuotasStatus?: number;
|
||
/** 单卡刷新(POST /v1/magpie/quotas/refresh)的状态码 */
|
||
refreshStatus?: number;
|
||
/** 完整读取(不带 kept)的延迟毫秒数,用于并发合并测试 */
|
||
fullQuotasDelayMs?: number;
|
||
/** chat 端点的固定应答,或按请求体动态应答 */
|
||
chat?: FakeChatReply | ((body: unknown, req: Request) => FakeChatReply | Promise<FakeChatReply>);
|
||
}
|
||
|
||
export interface FakeGateway {
|
||
base: string;
|
||
requests: FakeRequest[];
|
||
stop: () => void;
|
||
}
|
||
|
||
function reply(status: number, body: unknown): Response {
|
||
return new Response(JSON.stringify(body), {
|
||
status,
|
||
headers: { "content-type": "application/json" },
|
||
});
|
||
}
|
||
|
||
const DEFAULT_CHAT_BODY = {
|
||
id: "chatcmpl-fake",
|
||
object: "chat.completion",
|
||
choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }],
|
||
};
|
||
|
||
export function fakeGateway(opts: FakeGatewayOpts = {}): FakeGateway {
|
||
const requests: FakeRequest[] = [];
|
||
const server = Bun.serve({
|
||
port: 0,
|
||
hostname: "127.0.0.1",
|
||
async fetch(req) {
|
||
const url = new URL(req.url);
|
||
const entry: FakeRequest = {
|
||
method: req.method,
|
||
path: url.pathname,
|
||
search: url.search,
|
||
headers: Object.fromEntries(req.headers.entries()),
|
||
body: null,
|
||
};
|
||
requests.push(entry);
|
||
if (url.pathname === "/v1/models") {
|
||
return reply(opts.modelsStatus ?? 200, { data: opts.models ?? [] });
|
||
}
|
||
if (url.pathname === "/v1/magpie/quotas/refresh" && req.method === "POST") {
|
||
return reply(opts.refreshStatus ?? 200, { data: [] });
|
||
}
|
||
if (url.pathname === "/v1/magpie/quotas") {
|
||
const kept = url.searchParams.get("kept") === "1";
|
||
const status = kept
|
||
? opts.quotasStatus ?? 200
|
||
: opts.fullQuotasStatus ?? opts.quotasStatus ?? 200;
|
||
if (!kept && opts.fullQuotasDelayMs) await Bun.sleep(opts.fullQuotasDelayMs);
|
||
return reply(status, { data: kept ? opts.keptQuotas ?? opts.quotas ?? [] : opts.quotas ?? [] });
|
||
}
|
||
if (url.pathname === "/v1/chat/completions") {
|
||
const raw = await req.text();
|
||
try {
|
||
entry.body = JSON.parse(raw);
|
||
} catch {
|
||
entry.body = raw;
|
||
}
|
||
const conf =
|
||
typeof opts.chat === "function" ? await opts.chat(entry.body, req) : (opts.chat ?? {});
|
||
if (conf.delayMs) await Bun.sleep(conf.delayMs);
|
||
if (conf.breakBody === "reset") {
|
||
queueMicrotask(() => server.stop(true));
|
||
return new Promise<Response>(() => {});
|
||
}
|
||
if (conf.breakBody) {
|
||
const stream = new ReadableStream<Uint8Array>({
|
||
start(controller) {
|
||
controller.enqueue(new TextEncoder().encode('{"id":"chatcmpl-fake"'));
|
||
controller.close();
|
||
},
|
||
});
|
||
return new Response(stream, {
|
||
status: conf.status ?? 200,
|
||
headers: { "content-type": "application/json" },
|
||
});
|
||
}
|
||
return reply(conf.status ?? 200, conf.body ?? DEFAULT_CHAT_BODY);
|
||
}
|
||
return reply(404, { error: { message: `假网关没有 ${url.pathname}` } });
|
||
},
|
||
});
|
||
return {
|
||
base: `http://127.0.0.1:${server.port}`,
|
||
requests,
|
||
stop: () => server.stop(true),
|
||
};
|
||
}
|
||
|
||
// 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: [
|
||
{
|
||
id: "openai/gpt-5.2",
|
||
owned_by: "openai",
|
||
reasoning: true,
|
||
reasoning_levels: ["minimal", "low", "medium", "high"],
|
||
},
|
||
{ id: "openai/gpt-5.2-chat", owned_by: "openai", reasoning: false },
|
||
{
|
||
id: "anthropic/claude-opus-4",
|
||
owned_by: "anthropic",
|
||
reasoning: true,
|
||
reasoning_levels: ["disabled", "low", "high", "max"],
|
||
},
|
||
{
|
||
id: "google/gemini-3-pro",
|
||
owned_by: "google",
|
||
reasoning: true,
|
||
supported_reasoning_levels: [{ effort: "low" }, { effort: "high" }],
|
||
},
|
||
{ id: "deepseek/deepseek-v4", owned_by: "deepseek", reasoning: true, reasoning_levels: ["8192", "2048"] },
|
||
{ id: "group/auto", owned_by: "openai", reasoning: false },
|
||
],
|
||
quotas: [
|
||
{
|
||
provider: "openai",
|
||
user: "acct-yang",
|
||
name: "Yang 主账号",
|
||
kind: "subscription",
|
||
windows: [
|
||
{ name: "5h", used: 2, resetsAt: in2h },
|
||
{ name: "week", used: 31, resetsAt: null },
|
||
],
|
||
},
|
||
{
|
||
provider: "anthropic",
|
||
user: "acct-team",
|
||
name: "Team 席位",
|
||
kind: "subscription",
|
||
windows: [{ name: "5h", used: 0, resetsAt: null }],
|
||
},
|
||
],
|
||
});
|
||
console.log(`fake gateway: ${gw.base}`);
|
||
}
|