providerModelSmartDraft.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { describe, expect, it } from "vitest";
  2. import { smartDraftModel } from "./providerModelSmartDraftFixture.js";
  3. import {
  4. createProviderModelDraftValues,
  5. resolveProviderModelDraftCommit,
  6. } from "@/settings/model-provider-section/ProviderModelMetadata.js";
  7. import {
  8. projectModelDraft,
  9. updateModelDraft,
  10. modelDraftOverrides,
  11. } from "@/settings/model-provider-section/ProviderModelDraftState.js";
  12. describe("Todo90/92 common model draft", () => {
  13. it.each([true, false])("隐藏的 MFJS=%s 不参与编辑,已有覆盖在智能模式保持", (value) => {
  14. const model = smartDraftModel();
  15. model.personalConfig.properties = { requiresMfjsToolSchema: value };
  16. const draft = updateModelDraft(
  17. createProviderModelDraftValues(model),
  18. { contextWindowValue: "123456" },
  19. model,
  20. );
  21. const result = resolveProviderModelDraftCommit({ currentModel: model, draft });
  22. expect(result.status).toBe("commit");
  23. if (result.status !== "commit") throw new Error("expected commit");
  24. expect(result.model.personalConfig).not.toHaveProperty("requiresMfjsToolSchema");
  25. expect(result.model.personalConfig.properties?.requiresMfjsToolSchema).toBe(value);
  26. const reopened = createProviderModelDraftValues(result.model);
  27. expect(reopened).not.toHaveProperty("requiresMfjsToolSchemaValue");
  28. expect(reopened.overriddenFieldsValue).not.toContain("requiresMfjsToolSchemaValue");
  29. });
  30. it("智能配置未改MFJS不物化false,已有null保持清除意图", () => {
  31. for (const properties of [undefined, { requiresMfjsToolSchema: null }]) {
  32. for (const effectiveValue of [undefined, null, false]) {
  33. const model = smartDraftModel();
  34. model.personalConfig = { enabled: false, properties };
  35. model.config.properties!.requiresMfjsToolSchema = effectiveValue;
  36. const result = resolveProviderModelDraftCommit({
  37. currentModel: model,
  38. draft: createProviderModelDraftValues(model),
  39. });
  40. expect(result.status).toBe("commit");
  41. if (result.status !== "commit") throw new Error("expected commit");
  42. expect(result.model.personalConfig.properties?.requiresMfjsToolSchema).toBe(
  43. properties?.requiresMfjsToolSchema,
  44. );
  45. expect(result.model.personalConfig.enabled).toBe(false);
  46. }
  47. }
  48. });
  49. it("preserves explicit equal-value intent across ID changes, projects only untouched values", () => {
  50. const model = smartDraftModel();
  51. let draft = createProviderModelDraftValues(model);
  52. draft = updateModelDraft(
  53. draft,
  54. { contextWindowValue: "100000", maxOutputTokensValue: "32000" },
  55. model,
  56. );
  57. draft = updateModelDraft(
  58. draft,
  59. { inputFormatValue: { ...draft.inputFormatValue, supportsPdf: true } },
  60. model,
  61. );
  62. const config = structuredClone(model.config);
  63. config.properties!.contextWindow = 200000;
  64. config.properties!.inputFormat!.supportsImage = false;
  65. config.optionSpecs!.reasoningLevel!.values = ["off", "max"];
  66. const next = { ...model, modelId: "b", config, inheritedConfig: config };
  67. draft = projectModelDraft({ ...draft, idValue: "b" }, next);
  68. expect(draft.contextWindowValue).toBe("100000");
  69. expect(draft.inputFormatValue.supportsImage).toBe(false);
  70. expect(draft.inputFormatValue.supportsPdf).toBe(true);
  71. expect(draft.reasoningLevelValuesValue).toEqual(["off", "max"]);
  72. const result = resolveProviderModelDraftCommit({
  73. currentModel: model,
  74. draft: { ...draft, idValue: "a" },
  75. });
  76. expect(result.status).toBe("commit");
  77. if (result.status === "commit") {
  78. expect(result.model.personalConfig.properties?.contextWindow).toBe(100000);
  79. expect(result.model.personalConfig.optionSpecs?.maxOutputTokens?.max).toBe(32000);
  80. expect(result.model.personalConfig.properties?.inputFormat?.supportsPdf).toBe(true);
  81. }
  82. });
  83. it("freezes editable facts when switching off; rejects each blank required input", () => {
  84. const model = smartDraftModel();
  85. const draft = updateModelDraft(
  86. createProviderModelDraftValues(model),
  87. { useRecommendedConfigValue: false },
  88. model,
  89. );
  90. expect(draft.contextWindowValue).toBe("100000");
  91. expect(draft.inputFormatValue.supportsAudio).toBe(true);
  92. expect(modelDraftOverrides(draft).size).toBe(0);
  93. for (const field of [
  94. "contextWindowValue",
  95. "maxOutputTokensValue",
  96. "reasoningLevelMapValue",
  97. ] as const) {
  98. expect(
  99. resolveProviderModelDraftCommit({ currentModel: model, draft: { ...draft, [field]: "" } })
  100. .status,
  101. ).toBe("invalid");
  102. }
  103. const result = resolveProviderModelDraftCommit({ currentModel: model, draft });
  104. expect(result.status).toBe("commit");
  105. if (result.status === "commit") {
  106. expect(result.model.personalConfig.enabled).toBe(false);
  107. expect(result.model.personalConfig.properties).not.toHaveProperty("requiresMfjsToolSchema");
  108. expect(result.model.personalConfig.optionSpecs?.reasoningLevel?.map).toBe("{}");
  109. }
  110. });
  111. it("clears old overrides on re-enable, preserves subsequent edits and enabled", () => {
  112. const model = smartDraftModel();
  113. model.personalConfig = {
  114. enabled: false,
  115. optionSpecs: { maxOutputTokens: { map: "{'old_limit': maxOutputTokens}" } },
  116. };
  117. let draft = updateModelDraft(
  118. createProviderModelDraftValues(model),
  119. { useRecommendedConfigValue: false },
  120. model,
  121. );
  122. draft = updateModelDraft(draft, { contextWindowValue: "444444" }, model);
  123. draft = updateModelDraft(draft, { useRecommendedConfigValue: true }, model);
  124. expect(draft.contextWindowValue).toBe("");
  125. draft = updateModelDraft(draft, { maxOutputTokensValue: "12345" }, model);
  126. draft = projectModelDraft(draft, model);
  127. expect(draft).not.toHaveProperty("maxOutputTokensMapValue");
  128. const result = resolveProviderModelDraftCommit({ currentModel: model, draft });
  129. expect(result.status).toBe("commit");
  130. if (result.status === "commit") {
  131. expect(result.model.personalConfig.properties?.contextWindow).toBeUndefined();
  132. expect(result.model.personalConfig.optionSpecs?.maxOutputTokens?.max).toBe(12345);
  133. expect(result.model.personalConfig.enabled).toBe(false);
  134. expect(result.model.personalConfig.optionSpecs?.maxOutputTokens?.map).toBe(
  135. "{'old_limit': maxOutputTokens}",
  136. );
  137. }
  138. });
  139. it("keeps independent override feedback while another field is invalid", () => {
  140. const model = smartDraftModel();
  141. let draft = updateModelDraft(
  142. createProviderModelDraftValues(model),
  143. { reasoningLevelValuesValue: ["low", "max"] },
  144. model,
  145. );
  146. draft = updateModelDraft(
  147. draft,
  148. { contextWindowValue: "not a number", reasoningLevelMapValue: "bad syntax (" },
  149. model,
  150. );
  151. const flags = modelDraftOverrides(draft);
  152. expect(flags.has("reasoningLevelValuesValue")).toBe(true);
  153. expect(flags.has("reasoningLevelMapValue")).toBe(true);
  154. expect(flags.has("contextWindowValue")).toBe(true);
  155. expect(flags.has("supportsJsonSchemaOutputValue")).toBe(false);
  156. });
  157. });