import { afterEach, describe, expect, it, vi } from "vitest"; describe("桌面后端引导", () => { afterEach(() => { vi.unstubAllGlobals(); vi.resetModules(); }); it("使用 sidecar 注入的动态地址和令牌,不回退到 8788", async () => { const fetcher = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ service: "opsxdb", version: "0.1.0" }), }); vi.stubGlobal("window", { __OPSXDB_API__: "http://127.0.0.1:49123", __OPSXDB_TOKEN__: "local-test-token", location: { protocol: "tauri:", hostname: "tauri.localhost", port: "" }, }); vi.stubGlobal("fetch", fetcher); vi.resetModules(); const { health } = await import("./api"); await health(); expect(fetcher).toHaveBeenCalledWith("http://127.0.0.1:49123/api/health", { headers: { Authorization: "Bearer local-test-token" }, }); }); it("其他项目占用目标地址时拒绝将其当作 OpsxDB", async () => { vi.stubGlobal("window", { __OPSXDB_API__: "http://127.0.0.1:8788", location: { protocol: "tauri:", hostname: "tauri.localhost", port: "" }, }); vi.stubGlobal("fetch", vi.fn().mockResolvedValue({ ok: true, json: async () => ({ service: "opsxapi" }), })); vi.resetModules(); const { health } = await import("./api"); await expect(health()).rejects.toThrow("不是 OpsxDB 后端"); }); });