legacy-cli-output-token-migration.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { describe, expect, it } from "vitest";
  2. import { importLegacyCliPersonalProviderConfig } from "../src/app/legacy-cli-personal-provider-config-importer.js";
  3. function importModels(models: Record<string, unknown>) {
  4. return importLegacyCliPersonalProviderConfig({
  5. input: { provider: { custom: { kind: "anthropic", models } } },
  6. });
  7. }
  8. describe("CLI 首次导入输出上限", () => {
  9. it.each([
  10. { model: { limit: { output: 64_000 } }, expected: 64_000 },
  11. { model: { options: { max_tokens: 48_000 } }, expected: 48_000 },
  12. { model: { maxOutputTokens: 16_000 }, expected: 16_000 },
  13. {
  14. model: {
  15. limit: { output: 64_000 },
  16. options: { max_tokens: 48_000 },
  17. maxOutputTokens: 16_000,
  18. },
  19. expected: 64_000,
  20. },
  21. { model: { options: { max_tokens: 48_000 }, maxOutputTokens: 16_000 }, expected: 48_000 },
  22. { model: { limit: { output: 1.5 }, maxOutputTokens: 16_000 }, expected: 16_000 },
  23. ])("将 $model 迁为稀疏输出规则", ({ model, expected }) => {
  24. const before = structuredClone(model);
  25. const imported = importModels({ evaluation: model });
  26. expect(imported.models.getExact("custom", "evaluation")?.toJSON()).toEqual({
  27. optionSpecs: { maxOutputTokens: { max: expected } },
  28. });
  29. expect(model).toEqual(before);
  30. });
  31. it.each([undefined, null, 0, -1, 1.5, "24000", Number.NaN, Number.POSITIVE_INFINITY])(
  32. "非法 options.max_tokens %s 不进入配置,仍可使用合法顶层值",
  33. (maxTokens) => {
  34. const imported = importModels({
  35. missing: { options: { max_tokens: maxTokens } },
  36. fallback: { options: { max_tokens: maxTokens }, maxOutputTokens: 16_000 },
  37. });
  38. expect(imported.models.getExact("custom", "missing")).toBeUndefined();
  39. expect(
  40. imported.models.getExact("custom", "fallback")?.optionSpecs?.maxOutputTokens?.max,
  41. ).toBe(16_000);
  42. },
  43. );
  44. it.each([0, -1, "24000", Number.NaN, Number.POSITIVE_INFINITY])(
  45. "保持 parser 对非法显式字段 %s 的拒绝边界",
  46. (value) => {
  47. expect(() => importModels({ model: { maxOutputTokens: value } })).toThrow();
  48. expect(() => importModels({ model: { limit: { output: value } } })).toThrow();
  49. },
  50. );
  51. it("输出缺省不写默认值,context 单独保留,小数不成为输出规则", () => {
  52. const imported = importModels({
  53. context: { contextWindow: 128_000 },
  54. empty: {},
  55. fractional: { maxOutputTokens: 1.5 },
  56. });
  57. expect(imported.models.toPersonalJSON().providerModelRules).toEqual([
  58. {
  59. providerId: "custom",
  60. modelId: "context",
  61. config: { properties: { contextWindow: 128_000 } },
  62. },
  63. ]);
  64. });
  65. it("删除与重复成员不复活,使用 trim 后的真实 model id", () => {
  66. const imported = importModels({
  67. removed: { id: " shared ", deleted: true, maxOutputTokens: 1_000 },
  68. first: { id: " shared ", maxOutputTokens: 24_000 },
  69. duplicate: { id: "shared", maxOutputTokens: 48_000 },
  70. blank: { id: " ", maxOutputTokens: 8_000 },
  71. });
  72. expect(imported.providers.get("custom")?.personalModelIds).toEqual(["shared"]);
  73. expect(imported.models.toPersonalJSON().providerModelRules).toEqual([
  74. {
  75. providerId: "custom",
  76. modelId: "shared",
  77. config: { optionSpecs: { maxOutputTokens: { max: 24_000 } } },
  78. },
  79. ]);
  80. });
  81. });