| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- import { describe, expect, it } from "vitest";
- import {
- EnumOptionSpecConfig,
- LimitOptionSpecConfig,
- ModelConfig,
- ModelInputFormatConfig,
- ModelOptionSpecsConfig,
- ModelOutputFormatConfig,
- ModelPropertiesConfig,
- ApiKeyAccessConfig,
- ProviderApiConfig,
- ProviderConfig,
- ProviderConfigMap,
- parseModelConfig,
- parseProviderConfigMap,
- } from "../src/index.js";
- describe("ProviderConfigMap overlay", () => {
- it("Provider enabled 位于外层;Overlay、往返及后续配置写入保留 false", () => {
- const base = parseProviderConfigMap([{ providerId: "p", enabled: true, config: {} }]);
- const off = parseProviderConfigMap([{ providerId: "p", enabled: false, config: {} }]);
- const updated = base
- .overlay(off)
- .overlay(parseProviderConfigMap([{ providerId: "p", providerName: "renamed", config: {} }]));
- expect(updated.getRule("p")?.enabled).toBe(false);
- expect(parseProviderConfigMap(updated.toJSON()).getRule("p")?.enabled).toBe(false);
- expect(() =>
- parseProviderConfigMap([{ providerId: "p", config: { enabled: false } }]),
- ).toThrow();
- });
- it.each([0, -1, 1.5, Number.MAX_SAFE_INTEGER + 1])(
- "完整属性和保存 Schema 一致拒绝非法 contextWindow:%s",
- (contextWindow) => {
- const properties = new ModelPropertiesConfig({
- contextWindow,
- inputFormat: {
- supportsText: true,
- supportsImage: false,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: false,
- },
- outputFormat: { supportsText: true },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: false,
- supportsMidConversationSystem: false,
- });
- expect(() => parseModelConfig({ properties: properties.toJSON() })).toThrow();
- expect(properties.validateComplete()).toContainEqual(
- expect.objectContaining({ path: ["contextWindow"] }),
- );
- },
- );
- it.each([{ values: [" "] }, { values: ["low", "low"] }])(
- "稀疏 Schema 同样拒绝非法档位 $values",
- ({ values }) => {
- expect(() => parseModelConfig({ optionSpecs: { reasoningLevel: { values } } })).toThrow();
- },
- );
- it("完整输出上限拒绝非安全整数", () => {
- const spec = new LimitOptionSpecConfig({
- max: Number.MAX_SAFE_INTEGER + 1,
- map: "{'max_tokens': maxOutputTokens}",
- });
- expect(spec.validateComplete()).toContainEqual(expect.objectContaining({ path: ["max"] }));
- });
- it("保留 ZCodeBuiltin key 的位置,并按 Personal 顺序追加新 key", () => {
- const zcodeBuiltin = new ProviderConfigMap([
- {
- providerId: "A",
- providerName: "ZCodeBuiltin A",
- config: new ProviderConfig({
- logo: { type: "builtin", key: "provider-a" },
- builtinModelIds: ["m1", "m2"],
- }),
- },
- ["B", new ProviderConfig({ api: new ProviderApiConfig({ baseUrl: "https://b.example" }) })],
- ["C", new ProviderConfig({ api: new ProviderApiConfig({ baseUrl: "https://c.example" }) })],
- ]);
- const personal = new ProviderConfigMap([
- ["Y", new ProviderConfig()],
- { providerId: "C", providerName: "Personal C", config: new ProviderConfig({}) },
- ["X", new ProviderConfig()],
- {
- providerId: "A",
- providerName: "Personal A",
- config: new ProviderConfig({
- access: new ApiKeyAccessConfig({
- apiKeyManagementUrl: "https://personal.example/api-keys",
- }),
- personalModelIds: ["m1"],
- }),
- },
- ]);
- const effective = zcodeBuiltin.overlay(personal);
- expect(effective.keys()).toEqual(["A", "B", "C", "Y", "X"]);
- expect(effective.get("A")?.builtinModelIds).toEqual(["m1", "m2"]);
- expect(effective.get("A")?.personalModelIds).toEqual(["m1"]);
- expect(effective.getRule("A")?.providerName).toBe("Personal A");
- expect(effective.get("A")?.logo).toEqual({ type: "builtin", key: "provider-a" });
- expect(
- effective.get("A")?.access?.type === "api-key"
- ? effective.get("A")?.access.apiKeyManagementUrl
- : undefined,
- ).toBe("https://personal.example/api-keys");
- expect(effective.get("C")?.api?.baseUrl).toBe("https://c.example");
- expect(effective.getRule("C")?.providerName).toBe("Personal C");
- expect(zcodeBuiltin.get("A")?.builtinModelIds).toEqual(["m1", "m2"]);
- expect(personal.keys()).toEqual(["Y", "C", "X", "A"]);
- });
- it("按照给定顺序重排已有 key,同时保留未提到的 key", () => {
- const personal = new ProviderConfigMap([
- ["Y", new ProviderConfig()],
- { providerId: "C", providerName: "Personal C", config: new ProviderConfig({}) },
- ["X", new ProviderConfig()],
- ["A", new ProviderConfig({ personalModelIds: ["m1"] })],
- ["unmentioned", new ProviderConfig()],
- ]);
- const reordered = personal.reorder(["A", "B", "C", "X", "Y"]);
- expect(reordered.keys()).toEqual(["A", "C", "X", "Y", "unmentioned"]);
- expect(personal.keys()).toEqual(["Y", "C", "X", "A", "unmentioned"]);
- });
- it("递归覆盖 Config 字段,并整体替换普通字段", () => {
- const base = new ModelConfig({
- properties: new ModelPropertiesConfig({
- requiresMfjsToolSchema: false,
- contextWindow: 200_000,
- inputFormat: new ModelInputFormatConfig({
- supportsText: true,
- supportsImage: false,
- supportsPdf: false,
- }),
- outputFormat: new ModelOutputFormatConfig({ supportsText: true }),
- }),
- optionSpecs: new ModelOptionSpecsConfig({
- reasoningLevel: {
- values: ["low", "high"],
- map: "{'reasoning': {'effort': reasoningLevel}}",
- },
- maxOutputTokens: {
- max: 32_000,
- map: "{'max_output_tokens': maxOutputTokens}",
- },
- }),
- });
- const patch = new ModelConfig({
- properties: new ModelPropertiesConfig({
- requiresMfjsToolSchema: true,
- inputFormat: new ModelInputFormatConfig({ supportsImage: true }),
- }),
- optionSpecs: new ModelOptionSpecsConfig({
- reasoningLevel: { values: ["off", "on"] },
- }),
- });
- const effective = base.overlay(patch);
- expect(effective.properties?.contextWindow).toBe(200_000);
- expect(effective.properties?.inputFormat?.supportsText).toBe(true);
- expect(effective.properties?.inputFormat?.supportsImage).toBe(true);
- expect(effective.properties?.inputFormat?.supportsPdf).toBe(false);
- expect(effective.properties?.outputFormat?.supportsText).toBe(true);
- expect(effective.optionSpecs?.reasoningLevel).toEqual({
- values: ["off", "on"],
- map: "{'reasoning': {'effort': reasoningLevel}}",
- });
- expect(effective.optionSpecs?.maxOutputTokens).toEqual({
- max: 32_000,
- map: "{'max_output_tokens': maxOutputTokens}",
- });
- expect(effective.properties?.requiresMfjsToolSchema).toBe(true);
- });
- it("最终 Option Spec 必须包含可编译且返回 Object 的 map", () => {
- const model = new ModelConfig({
- properties: new ModelPropertiesConfig({
- requiresMfjsToolSchema: false,
- contextWindow: 200_000,
- inputFormat: {
- supportsText: true,
- supportsImage: false,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: false,
- },
- outputFormat: { supportsText: true },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: false,
- supportsMidConversationSystem: false,
- }),
- optionSpecs: new ModelOptionSpecsConfig({
- reasoningLevel: {
- values: ["low", "high"],
- map: "reasoningLevel.field",
- },
- maxOutputTokens: {
- max: 32_000,
- map: "{'max_tokens': maxOutputTokens}",
- },
- }),
- });
- expect(model.validateComplete(["models", "example"])).toContainEqual({
- code: "invalid-option-spec",
- path: ["models", "example", "optionSpecs", "reasoningLevel", "map"],
- message: expect.stringContaining("member access"),
- });
- });
- it("Option Spec 只能引用自己的同名输入变量", () => {
- expect(() =>
- parseModelConfig({
- optionSpecs: {
- reasoningLevel: {
- map: "{'reasoning_effort': maxOutputTokens}",
- },
- },
- }),
- ).toThrow(/maxOutputTokens/);
- expect(() =>
- parseModelConfig({
- optionSpecs: {
- maxOutputTokens: {
- map: "{'max_tokens': reasoningLevel}",
- },
- },
- }),
- ).toThrow(/reasoningLevel/);
- });
- it("使用 supportsJsonSchemaOutput 表达严格 JSON Schema 能力", () => {
- expect(
- parseModelConfig({
- properties: { supportsJsonSchemaOutput: true },
- }).properties?.supportsJsonSchemaOutput,
- ).toBe(true);
- expect(() =>
- parseModelConfig({
- properties: { supportsStructuredOutput: true },
- }),
- ).toThrow();
- });
- it("最终 Model Config 必须包含完整 Reasoning Option Spec", () => {
- const model = new ModelConfig({
- optionSpecs: new ModelOptionSpecsConfig({
- maxOutputTokens: {
- max: 32_000,
- map: "{'max_tokens': maxOutputTokens}",
- },
- }),
- });
- expect(
- model.optionSpecs?.validateComplete(["models", "example", "optionSpecs"]),
- ).toContainEqual(
- expect.objectContaining({
- code: "required-field-missing",
- path: ["models", "example", "optionSpecs", "reasoningLevel"],
- }),
- );
- });
- it("接受 disabled 单档位与空 Request Body Patch 作为无推理基线", () => {
- const optionSpecs = new ModelOptionSpecsConfig({
- reasoningLevel: {
- values: ["disabled"],
- map: "{}",
- },
- maxOutputTokens: {
- max: 32_000,
- map: "{'max_tokens': maxOutputTokens}",
- },
- });
- expect(optionSpecs.validateComplete(["models", "example", "optionSpecs"])).toEqual([]);
- });
- it.each([
- { values: [], message: "reasoningLevel.values 不能为空" },
- { values: ["low", ""], message: "reasoningLevel.values 必须是非空字符串" },
- { values: ["low", "low"], message: "reasoningLevel.values 不能重复" },
- ])("拒绝不完整的 Reasoning 档位:$message", ({ values, message }) => {
- const spec = new EnumOptionSpecConfig({ values, map: "{}" });
- expect(spec.validateComplete(["optionSpecs", "reasoningLevel"])).toContainEqual(
- expect.objectContaining({ code: "invalid-option-spec", message }),
- );
- });
- it("Schema 在写入边界拒绝返回非 Object 的 Option map", () => {
- expect(() =>
- parseModelConfig({
- optionSpecs: {
- reasoningLevel: {
- values: ["low", "high"],
- map: "reasoningLevel",
- },
- },
- }),
- ).toThrow(/object/i);
- });
- it("Schema 拒绝已退役的 Option Spec default", () => {
- expect(() =>
- parseModelConfig({
- optionSpecs: {
- reasoningLevel: {
- values: ["low", "high"],
- default: "high",
- map: "{'reasoning': {'effort': reasoningLevel}}",
- },
- },
- }),
- ).toThrow(/unrecognized|default/i);
- });
- it("用 null 清除字段,并在最终完整性校验中报告必填值缺失", () => {
- const base = new ModelConfig({
- properties: new ModelPropertiesConfig({
- requiresMfjsToolSchema: false,
- contextWindow: 200_000,
- inputFormat: {
- supportsText: true,
- supportsImage: false,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: false,
- },
- outputFormat: { supportsText: true },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: false,
- supportsMidConversationSystem: false,
- }),
- optionSpecs: new ModelOptionSpecsConfig({
- maxOutputTokens: {
- max: 32_000,
- map: "{'max_tokens': maxOutputTokens}",
- },
- }),
- });
- const effective = base.overlay(
- new ModelConfig({
- properties: new ModelPropertiesConfig({
- requiresMfjsToolSchema: null,
- contextWindow: null,
- }),
- }),
- );
- expect(effective.properties?.contextWindow).toBeNull();
- expect(effective.validateComplete(["models", "example"])).toContainEqual({
- code: "required-field-missing",
- path: ["models", "example", "properties", "contextWindow"],
- message: "缺少必填配置 models.example.properties.contextWindow",
- });
- expect(effective.validateComplete(["models", "example"])).toContainEqual({
- code: "required-field-missing",
- path: ["models", "example", "properties", "requiresMfjsToolSchema"],
- message: "缺少必填配置 models.example.properties.requiresMfjsToolSchema",
- });
- });
- it("从普通 Object 解析,并拒绝未知字段", () => {
- const parsed = parseProviderConfigMap([
- {
- providerId: "local",
- config: {
- access: { type: "api-key", apiKey: "test-key" },
- api: {
- type: "anthropic-messages",
- baseUrl: "https://api.example.com",
- },
- builtinModelIds: ["model-a"],
- },
- },
- ]);
- expect(parsed.keys()).toEqual(["local"]);
- expect(parsed.get("local")?.api?.baseUrl).toBe("https://api.example.com");
- expect(() =>
- parseProviderConfigMap([{ providerId: "invalid", config: { unknownField: true } }]),
- ).toThrow();
- });
- });
|