Files
cc-switch/tests/lib/backgroundTaskNotice.test.ts
Jason d7b12c677d fix(network): say which request failed and why
#9911: a firewall prompt showed only 172.67.220.122 (models.dev, the
pricing sync), the user denied it, and the firewall then blocked every
connection CC Switch made, including a provider on the local network.
CC Switch itself said nothing about the background request, and the
connectivity check only said "error sending request", so the user was
left guessing what the app was secretly contacting.

- Startup background requests now report their failure once per launch:
  the update check names dl.ccswitch.io / github.com, the models.dev
  sync says it fetches prices for usage costs. Connection-level errors
  read as "couldn't reach the server" (raw text stays in the log). The
  update notice offers "Don't show again"; the pricing notice can turn
  auto sync off directly.
- The connectivity check classifies the cause from the error's source
  chain by io::ErrorKind (blocked by a firewall, nothing listening on
  the port, timeout, DNS, TLS) and shows a matching hint. The message
  keeps the root cause and drops the URL. A timeout is now retried by
  kind too, since the localized OS text may not contain "timeout".
2026-11-25 01:39:10 -08:00

88 lines
2.8 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const warning = vi.fn();
const success = vi.fn();
const getModelsDevSyncConfig = vi.fn();
const saveModelsDevSyncConfig = vi.fn();
vi.mock("@/lib/toast", () => ({
toast: { warning, success, error: vi.fn() },
}));
vi.mock("@/lib/api/usage", () => ({
usageApi: { getModelsDevSyncConfig, saveModelsDevSyncConfig },
}));
async function load() {
vi.resetModules();
return import("@/lib/backgroundTaskNotice");
}
type Options = { action: { onClick: () => void }; description: string };
const lastOptions = () => warning.mock.calls.at(-1)?.[1] as Options;
describe("notifyBackgroundTaskFailed", () => {
beforeEach(() => {
warning.mockReset();
success.mockReset();
getModelsDevSyncConfig.mockReset();
saveModelsDevSyncConfig.mockReset();
localStorage.clear();
});
it("names the failed task and shows each task at most once per launch", async () => {
const { notifyBackgroundTaskFailed } = await load();
notifyBackgroundTaskFailed("updateCheck", new Error("offline"));
notifyBackgroundTaskFailed("updateCheck", new Error("offline"));
notifyBackgroundTaskFailed(
"modelsDevSync",
new Error(
"error sending request for url (https://dl.ccswitch.io/latest.json)",
),
);
const { default: i18n } = await import("@/i18n");
expect(warning.mock.calls.map((call) => call[0])).toEqual([
i18n.t("backgroundTask.updateCheckFailed.title"),
i18n.t("backgroundTask.modelsDevSyncFailed.title"),
]);
// 连接层失败的原文换成一句话
expect(lastOptions().description).toContain(
i18n.t("backgroundTask.networkError"),
);
expect(lastOptions().description).not.toContain("offline");
});
it("stays quiet in later launches after the user mutes the update check", async () => {
const first = await load();
first.notifyBackgroundTaskFailed("updateCheck", "offline");
lastOptions().action.onClick();
const next = await load();
next.notifyBackgroundTaskFailed("updateCheck", "offline");
expect(warning).toHaveBeenCalledTimes(1);
});
it("turns off models.dev auto sync from the notice", async () => {
const config = {
autoSyncEnabled: true,
includeCommonModels: true,
selectedModelKeys: ["a"],
excludedCommonModelKeys: [],
lastSyncAt: null,
lastSyncError: null,
};
getModelsDevSyncConfig.mockResolvedValue({ config, configPath: "/x" });
saveModelsDevSyncConfig.mockResolvedValue(undefined);
const { notifyBackgroundTaskFailed } = await load();
notifyBackgroundTaskFailed("modelsDevSync", "offline");
lastOptions().action.onClick();
await vi.waitFor(() => expect(success).toHaveBeenCalled());
expect(saveModelsDevSyncConfig).toHaveBeenCalledWith({
...config,
autoSyncEnabled: false,
});
});
});