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

8.6 KB
Raw Blame
import { describe, expect, it } from "vitest";
import {
  agyGatewayFromSettings,
  agyGatewayIssue,
  agyGatewayToSettings,
  agyModelIds,
  agyModelRowIssue,
  type AgyModelRow,
} from "@/components/providers/forms/AgyGatewayFields";
import { fillAgyModel } from "@/components/providers/forms/modelMetadataFill";

const LEVELS = ["low", "medium", "high", "xhigh", "max"] as const;

const row = (fields: Partial<AgyModelRow>): AgyModelRow => ({
  rowId: "r",
  id: "m",
  ...fields,
});

describe("Antigravity gateway draft", () => {
  it("reads the row's gateway with the default model first", () => {
    const draft = agyGatewayFromSettings({
      env: { GEMINI_API_KEY: "fixture" },
      gateway: {
        wireProtocol: "openai",
        models: ["a", "b", " ", 3],
        defaultModel: "b",
        caCertPath: "/ca.pem",
        customHeaders: { "x-team": "1", bad: 2 },
        routeHint: "kept",
      },
    });
    expect({ ...draft, models: agyModelIds(draft) }).toEqual({
      wireProtocol: "openai",
      models: ["b", "a"],
      headers: { "x-team": "1" },
      caCertPath: "/ca.pem",
      rest: { routeHint: "kept" },
    });
  });

  it("writes back the options and keeps keys the form does not edit", () => {
    expect(
      agyGatewayToSettings({
        wireProtocol: "openai",
        models: [" b", "a ", "b", "", " "].map((id, index) => ({
          rowId: `r${index}`,
          id,
        })),
        headers: { " X-Team ": "1", "draft-header:1": "" },
        caCertPath: " /ca.pem ",
        rest: { routeHint: "kept" },
      }),
    ).toEqual({
      routeHint: "kept",
      wireProtocol: "openai",
      models: ["b", "a"],
      defaultModel: "b",
      customHeaders: { "X-Team": "1" },
      caCertPath: "/ca.pem",
    });
  });

  it("round-trips per-model options and keeps their unknown keys", () => {
    const draft = agyGatewayFromSettings({
      gateway: {
        models: ["a", "b", "c"],
        modelOptions: {
          a: {
            displayName: "Model A",
            maxTokens: 200000,
            maxOutputTokens: 32000,
            supportsThinking: true,
            futureFlag: false,
          },
          c: { supportsThinking: true, supportsImages: true },
          gone: { displayName: "dropped with its model" },
        },
      },
    });
    // supportsThinking 表单不提供开关,和其他不认识的键一样原样保留
    expect(
      draft.models.map(
        ({
          id,
          displayName,
          maxTokens,
          maxOutputTokens,
          supportsImages,
          extra,
        }) => [
          id,
          displayName,
          maxTokens,
          maxOutputTokens,
          supportsImages,
          extra,
        ],
      ),
    ).toEqual([
      [
        "a",
        "Model A",
        "200000",
        "32000",
        undefined,
        { supportsThinking: true, futureFlag: false },
      ],
      ["b", undefined, undefined, undefined, undefined, undefined],
      ["c", undefined, undefined, undefined, true, { supportsThinking: true }],
    ]);
    expect(agyGatewayToSettings(draft)).toEqual({
      models: ["a", "b", "c"],
      defaultModel: "a",
      modelOptions: {
        a: {
          futureFlag: false,
          displayName: "Model A",
          maxTokens: 200000,
          maxOutputTokens: 32000,
          supportsThinking: true,
        },
        c: { supportsThinking: true, supportsImages: true },
      },
    });
  });

  it("round-trips effort variants in level order and drops unknown levels", () => {
    const draft = agyGatewayFromSettings({
      gateway: {
        models: ["m"],
        modelOptions: { m: { effortVariants: ["max", "ultra", "low"] } },
      },
    });
    expect(draft.models[0].effortVariants).toEqual(["low", "max"]);
    expect(draft.models[0].extra).toBeUndefined();
    expect(agyGatewayToSettings(draft)?.modelOptions).toEqual({
      m: { effortVariants: ["low", "max"] },
    });
    draft.models[0].effortVariants = [];
    expect(agyGatewayToSettings(draft)?.modelOptions).toBeUndefined();
  });

  it("keeps the default effort only while it is one of the chosen levels", () => {
    const draft = agyGatewayFromSettings({
      gateway: {
        models: ["m"],
        modelOptions: {
          m: { effortVariants: ["low", "high"], defaultEffort: "high" },
        },
      },
    });
    expect(draft.models[0].defaultEffort).toBe("high");
    expect(agyGatewayToSettings(draft)?.modelOptions).toEqual({
      m: { effortVariants: ["low", "high"], defaultEffort: "high" },
    });
    draft.models[0].effortVariants = ["low"];
    expect(agyGatewayToSettings(draft)?.modelOptions).toEqual({
      m: { effortVariants: ["low"] },
    });
  });

  it("leaves no gateway object when every option is the default", () => {
    const draft = agyGatewayFromSettings({ env: {} });
    expect(draft.wireProtocol).toBe("genai");
    expect(agyGatewayToSettings(draft)).toBeUndefined();
  });
});

describe("Antigravity model limits", () => {
  it("flags combinations agy rejects", () => {
    // agy 认得的模型族自带上下文,其余必须填,空行不报
    expect(agyModelRowIssue(row({ id: "gpt-5.5" }))).toBeNull();
    expect(agyModelRowIssue(row({ id: "anthropic/Claude-Sonnet" }))).toBeNull();
    expect(agyModelRowIssue(row({ id: "gemini-3.5-flash" }))).toBeNull();
    expect(agyModelRowIssue(row({ id: " " }))).toBeNull();
    expect(agyModelRowIssue(row({ id: "deepseek-flash" }))).toBe(
      "contextRequired",
    );
    expect(agyModelRowIssue(row({ maxTokens: "30000" }))).toBeNull();
    expect(
      agyModelRowIssue(row({ maxTokens: "64000", maxOutputTokens: "32000" })),
    ).toBeNull();
    expect(agyModelRowIssue(row({ maxOutputTokens: "32000" }))).toBe(
      "outputNeedsContext",
    );
    expect(
      agyModelRowIssue(row({ maxTokens: "48000", maxOutputTokens: "32000" })),
    ).toBe("contextTooSmall");
    expect(agyModelRowIssue(row({ maxTokens: "12k" }))).toBe("limitInvalid");
    expect(agyModelRowIssue(row({ maxTokens: "0" }))).toBe("limitInvalid");
  });

  it("reports the first listed model with a problem", () => {
    expect(
      agyGatewayIssue({
        ...agyGatewayFromSettings(undefined),
        models: [
          row({ rowId: "1", id: " ", maxOutputTokens: "1" }),
          row({ rowId: "2", id: "gpt-ok" }),
          row({ rowId: "3", id: "bad", maxOutputTokens: "8000" }),
        ],
      }),
    ).toEqual({ model: "bad", issue: "outputNeedsContext" });
  });

  it("fills limits from metadata and caps output at half the context", () => {
    expect(
      fillAgyModel(
        row({}),
        {
          contextWindow: 128000,
          maxOutputTokens: 128000,
          reasoning: true,
        },
        LEVELS,
      ),
    ).toEqual({
      rowId: "r",
      id: "m",
      maxTokens: "128000",
      maxOutputTokens: "64000",
    });
    // 用户填过的不动
    expect(
      fillAgyModel(
        row({ maxTokens: "400000", maxOutputTokens: "1000" }),
        {
          contextWindow: 200000,
          maxOutputTokens: 64000,
        },
        LEVELS,
      ),
    ).toMatchObject({ maxTokens: "400000", maxOutputTokens: "1000" });
  });

  it("fills thinking levels agy knows, in canonical order", () => {
    // Gemini 的 minimal agy 不认,丢掉
    expect(
      fillAgyModel(
        row({}),
        {
          reasoningEfforts: ["high", "minimal", "low", "medium"],
        },
        LEVELS,
      ).effortVariants,
    ).toEqual(["low", "medium", "high"]);
    // genai 用不到的 max 照存,后端发布时跳过
    expect(
      fillAgyModel(
        row({}),
        { reasoningEfforts: ["low", "high", "max"] },
        LEVELS,
      ).effortVariants,
    ).toEqual(["low", "high", "max"]);
    // 用户选过的不动;一个都对不上不写
    expect(
      fillAgyModel(
        row({ effortVariants: ["high"] }),
        {
          reasoningEfforts: ["low", "medium", "high"],
        },
        LEVELS,
      ).effortVariants,
    ).toEqual(["high"]);
    expect(
      fillAgyModel(row({}), { reasoningEfforts: ["none"] }, LEVELS),
    ).not.toHaveProperty("effortVariants");
  });

  it("turns image input on only when the metadata lists images (#10219)", () => {
    expect(
      fillAgyModel(row({}), { inputModalities: ["text", "image"] }, LEVELS)
        .supportsImages,
    ).toBe(true);
    expect(
      fillAgyModel(row({}), { inputModalities: ["text"] }, LEVELS),
    ).not.toHaveProperty("supportsImages");
    // 只往「支持」补,不替用户关掉
    expect(
      fillAgyModel(
        row({ supportsImages: true }),
        { inputModalities: ["text"] },
        LEVELS,
      ).supportsImages,
    ).toBe(true);
  });
});