Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

5.0 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { APP_PROFILE_SCOPE } from "@/components/profiles/scope";
import { WARN_BELOW_PERCENT } from "@/components/quota/quotaRules";
import { TIER_I18N_KEYS } from "@/components/SubscriptionQuotaFooter";
import { DISPLAY_CURRENCIES } from "@/components/usage/format";
import type { AppId } from "@/lib/api/types";
import { isValidUserAgentHeader } from "@/lib/userAgent";
import { orderAppIds } from "@/config/appConfig";
import type { Provider } from "@/types";
import {
  CUSTOM_ICON_PREFIX,
  MAX_CUSTOM_ICON_LENGTH,
  OUTPUT_SIZE,
} from "@/utils/customIcon";
import { providerNeedsRouting } from "@/utils/providerCapabilities";
import { detectBalanceProvider } from "@/config/balanceProviders";
import { detectCodingPlanProvider } from "@/config/codingPlanProviders";
import { COPILOT_REASONING_EFFORTS } from "@/config/copilotConfig";
import {
  HERMES_PROVIDER_SOURCE_CUSTOM_LIST,
  HERMES_PROVIDER_SOURCE_DICT,
  HERMES_PROVIDER_SOURCE_FIELD,
} from "@/config/hermesProviderMeta";
import {
  CODEX_ANTHROPIC_WIRE_API_VALUES,
  CODEX_CHAT_WIRE_API_VALUES,
  CODEX_RESERVED_MODEL_PROVIDER_IDS,
  isCodexAnthropicWireApi,
  isCodexChatWireApi,
} from "@/utils/codexConfigUtils";
import backend from "./backendConstants.json";

// backendConstants.json is generated by the backend test
// backend_constants_match_shared_fixture (src-tauri/src/frontend_parity_tests.rs).
// Each frontend copy below must match it.
describe("frontend copies of backend constants", () => {
  it("offers the Copilot reasoning efforts the backend accepts", () => {
    expect([...COPILOT_REASONING_EFFORTS]).toEqual(
      backend.copilotReasoningEfforts,
    );
  });

  it("reserves the same Codex model provider IDs", () => {
    expect([...CODEX_RESERVED_MODEL_PROVIDER_IDS]).toEqual(
      backend.codexReservedModelProviderIds,
    );
  });

  it("reads the same Codex wire_api aliases as the backend", () => {
    expect([...CODEX_CHAT_WIRE_API_VALUES]).toEqual(backend.codexWireApis.chat);
    expect([...CODEX_ANTHROPIC_WIRE_API_VALUES]).toEqual(
      backend.codexWireApis.anthropic,
    );
    for (const value of backend.codexWireApis.chat) {
      expect(isCodexChatWireApi(` ${value.toUpperCase()} `)).toBe(true);
      expect(isCodexAnthropicWireApi(value)).toBe(false);
    }
    for (const value of backend.codexWireApis.anthropic) {
      expect(isCodexAnthropicWireApi(` ${value.toUpperCase()} `)).toBe(true);
      expect(isCodexChatWireApi(value)).toBe(false);
    }
  });

  it("reads the Hermes provider source marker the backend writes", () => {
    expect({
      field: HERMES_PROVIDER_SOURCE_FIELD,
      customList: HERMES_PROVIDER_SOURCE_CUSTOM_LIST,
      dict: HERMES_PROVIDER_SOURCE_DICT,
    }).toEqual(backend.hermesProviderSource);
  });

  it.each(Object.entries(backend.codingPlanProviderByUrl))(
    "detects the same coding plan for %s",
    (url, id) => {
      expect(detectCodingPlanProvider(url)).toBe(id);
    },
  );

  it.each(Object.entries(backend.balanceProviderByUrl))(
    "detects the same balance provider for %s",
    (url, id) => {
      expect(detectBalanceProvider(url)?.id ?? null).toBe(id);
    },
  );

  it("warns about quota at the same percentage as the tray", () => {
    expect(WARN_BELOW_PERCENT).toBe(backend.quotaWarnBelowPercent);
  });

  it("displays both Kimi monthly tiers emitted by the backend", () => {
    expect(
      Object.keys(TIER_I18N_KEYS).filter((name) =>
        name.startsWith("kimi_monthly_"),
      ),
    ).toEqual(backend.kimiMonthlyTiers);
  });

  it("offers USD plus exactly the currencies the backend fetches rates for", () => {
    // Compared as sets: the menu order is a UI choice, not the backend's
    // fetch order.
    expect([...DISPLAY_CURRENCIES].sort()).toEqual(
      ["USD", ...backend.exchangeRateCurrencies].sort(),
    );
  });

  it("produces custom icons the backend accepts", () => {
    expect(CUSTOM_ICON_PREFIX).toBe(backend.customIcon.prefix);
    expect(MAX_CUSTOM_ICON_LENGTH).toBe(backend.customIcon.maxLength);
    expect(OUTPUT_SIZE).toBeLessThanOrEqual(backend.customIcon.maxDimension);
  });

  it("groups the same apps into profile scopes", () => {
    for (const [app, scope] of Object.entries(backend.profileScopeByApp)) {
      expect(APP_PROFILE_SCOPE[app as AppId] ?? null, app).toBe(scope);
    }
  });

  it.each(Object.entries(backend.userAgentValid))(
    "judges User-Agent %j the same way",
    (value, valid) => {
      expect(isValidUserAgentHeader(value)).toBe(valid);
    },
  );

  it.each(backend.providerNeedsRouting)(
    "agrees whether the $name card needs routing",
    ({ provider, needsRouting }) => {
      for (const [app, needs] of Object.entries(needsRouting)) {
        expect(
          providerNeedsRouting(app as AppId, provider as unknown as Provider),
          app,
        ).toBe(needs);
      }
    },
  );

  it.each(backend.appOrder)(
    "resolves saved app order $saved the same way",
    ({ saved, ordered }) => {
      expect(orderAppIds(saved)).toEqual(ordered);
    },
  );
});