model-data-schema.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { describe, expect, it } from "vitest";
  2. import { parseModelConfig, createRegistryModelConfig } from "../src/index.js";
  3. import { completeModelConfigDataSchema, modelConfigDataSchema } from "@zcode/shared/model-config";
  4. const complete = {
  5. enabled: true,
  6. properties: {
  7. requiresMfjsToolSchema: false,
  8. contextWindow: 131_072,
  9. inputFormat: {
  10. supportsText: true,
  11. supportsImage: false,
  12. supportsVideo: false,
  13. supportsAudio: false,
  14. supportsPdf: false,
  15. },
  16. outputFormat: { supportsText: true },
  17. supportsToolCall: true,
  18. supportsJsonSchemaOutput: true,
  19. supportsNativeWebSearch: false,
  20. supportsMidConversationSystem: true,
  21. },
  22. optionSpecs: {
  23. reasoningLevel: {
  24. values: ["disabled", "high"],
  25. map: "{'reasoning_effort': reasoningLevel}",
  26. },
  27. maxOutputTokens: { max: 32_768, map: "{'max_tokens': maxOutputTokens}" },
  28. },
  29. };
  30. function fieldPaths(value: Record<string, unknown>, prefix: string[] = []): string[][] {
  31. return Object.entries(value).flatMap(([key, child]) => {
  32. const path = [...prefix, key];
  33. return [
  34. path,
  35. ...(child && typeof child === "object" && !Array.isArray(child)
  36. ? fieldPaths(child as Record<string, unknown>, path)
  37. : []),
  38. ];
  39. });
  40. }
  41. describe("Model schema 的完整/稀疏派生合同", () => {
  42. it("MFJS只属于properties,显式false/null不能回退或丢失", () => {
  43. for (const value of [true, false, null]) {
  44. const overlay = parseModelConfig({ properties: { requiresMfjsToolSchema: value } });
  45. const merged = parseModelConfig(complete).overlay(overlay);
  46. expect(merged.toJSON().properties?.requiresMfjsToolSchema).toBe(value);
  47. expect(merged.toJSON()).not.toHaveProperty("requiresMfjsToolSchema");
  48. expect(createRegistryModelConfig(merged).ok).toBe(value !== null);
  49. expect(() => parseModelConfig({ requiresMfjsToolSchema: value })).toThrow();
  50. }
  51. });
  52. it.each([
  53. { key: "reasoningLevel", type: "enum" },
  54. { key: "maxOutputTokens", type: "limit" },
  55. ])("拒绝未发布的 $key.type 字段,不静默兼容", ({ key, type }) => {
  56. expect(() => parseModelConfig({ optionSpecs: { [key]: { type } } })).toThrow();
  57. });
  58. it.each(fieldPaths(complete).map((path) => ({ path, label: path.join(".") })))(
  59. "$label 的缺省/null只在稀疏覆盖合法,不能进入Registry",
  60. ({ path }) => {
  61. for (const replacement of [undefined, null]) {
  62. const value: Record<string, unknown> = structuredClone(complete);
  63. let parent = value;
  64. for (const key of path.slice(0, -1)) parent = parent[key] as Record<string, unknown>;
  65. if (replacement === undefined) delete parent[path.at(-1)!];
  66. else parent[path.at(-1)!] = replacement;
  67. expect(modelConfigDataSchema.safeParse(value).success).toBe(true);
  68. expect(completeModelConfigDataSchema.safeParse(value).success).toBe(false);
  69. const result = createRegistryModelConfig(parseModelConfig(value), ["test-model"]);
  70. expect(result.ok).toBe(false);
  71. if (!result.ok)
  72. expect(result.issues).toContainEqual({
  73. code: "required-field-missing",
  74. path: ["test-model", ...path],
  75. message: `缺少必填配置 test-model.${path.join(".")}`,
  76. });
  77. }
  78. },
  79. );
  80. it("合法完整配置parse/class/JSON往返一致,冻结与overlay行为不变", () => {
  81. const parsed = parseModelConfig(complete);
  82. expect(JSON.parse(JSON.stringify(parsed))).toEqual(complete);
  83. expect(completeModelConfigDataSchema.parse(parsed.toJSON())).toEqual(complete);
  84. expect(createRegistryModelConfig(parsed).ok).toBe(true);
  85. expect(Object.isFrozen(parsed)).toBe(true);
  86. expect(Object.isFrozen(parsed.optionSpecs?.reasoningLevel?.values)).toBe(true);
  87. const cleared = parsed.overlay(
  88. parseModelConfig({ properties: { inputFormat: { supportsImage: null } } }),
  89. );
  90. expect(cleared.properties?.inputFormat?.supportsImage).toBeNull();
  91. expect(cleared.properties?.inputFormat?.supportsText).toBe(true);
  92. expect(parsed.properties?.inputFormat?.supportsImage).toBe(false);
  93. expect(createRegistryModelConfig(cleared).ok).toBe(false);
  94. });
  95. });