thoughtLevelOptions.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodeConfigOption } from "@zcode/shared";
  3. import {
  4. getNextThoughtLevelValue,
  5. getThoughtLevelLabel,
  6. isNoThoughtLevel,
  7. } from "@/chat-input-toolbar/thoughtLevelOptions.js";
  8. import zhCN from "@/i18n/locales/zh-CN.js";
  9. import enUS from "@/i18n/locales/en-US.js";
  10. const option: ZCodeConfigOption = {
  11. id: "thought_level",
  12. category: "thought_level",
  13. type: "select",
  14. currentValue: "",
  15. };
  16. const expectedLabels = [
  17. [["disabled", "false", "no", "none", "nothink", "no-think", "no_think", "off"], "关闭", "Off"],
  18. [["enable", "enabled", "on", "true"], "开启", "On"],
  19. [["minimal"], "极低", "Minimal"],
  20. [["low"], "低", "Low"],
  21. [["medium"], "中", "Medium"],
  22. [["high"], "高", "High"],
  23. [["xhigh", "extra-high", "extra_high"], "极高", "Extra high"],
  24. [["max"], "最高", "Max"],
  25. [["ultra"], "极致", "Ultra"],
  26. ] as const;
  27. describe.each([
  28. ["zh-CN", zhCN, 1],
  29. ["en-US", enUS, 2],
  30. ] as const)("思考档位 %s", (_locale, messages, column) => {
  31. const intl = { formatMessage: ({ id }: { id: string }) => messages[id] ?? id };
  32. for (const row of expectedLabels) {
  33. it.each(row[0])("%s 使用约定文案且不改原始值", (value) => {
  34. const entry = Object.freeze({ value: ` ${value.toUpperCase()} `, name: "原始名称" });
  35. expect(getThoughtLevelLabel(intl, "glm", option, entry)).toBe(row[column]);
  36. expect(entry.value).toBe(` ${value.toUpperCase()} `);
  37. });
  38. }
  39. it.each([
  40. "light",
  41. "shallow",
  42. "balanced",
  43. "default",
  44. "normal",
  45. "standard",
  46. "deep",
  47. "maximum",
  48. "very-high",
  49. "very_high",
  50. "custom",
  51. "constructor",
  52. "__proto__",
  53. ])("%s 保留来源名称", (value) => {
  54. expect(getThoughtLevelLabel(intl, "glm", option, { value, name: `来源 ${value}` })).toBe(
  55. `来源 ${value}`,
  56. );
  57. });
  58. });
  59. describe("思考档位配置顺序", () => {
  60. // 自定义顺序刻意不符合旧名称 rank,证明循环不再暗中重排。
  61. const values = [
  62. "custom",
  63. "high",
  64. "minimal",
  65. "off",
  66. "low",
  67. "max",
  68. "ultra",
  69. "extra-high",
  70. "extra_high",
  71. ];
  72. const options = values.map((value) => ({ value, name: value }));
  73. it.each(values)("从 %s 按配置顺序循环", (currentValue) => {
  74. expect(getNextThoughtLevelValue({ ...option, currentValue, options })).toBe(
  75. values[(values.indexOf(currentValue) + 1) % values.length],
  76. );
  77. expect(options.map((entry) => entry.value)).toEqual(values);
  78. });
  79. it.each(["", "missing"])("%s 从配置首项开始", (currentValue) => {
  80. expect(getNextThoughtLevelValue({ ...option, currentValue, options })).toBe("custom");
  81. });
  82. it("零档和单档不循环", () => {
  83. expect(getNextThoughtLevelValue({ ...option, options: [] })).toBeNull();
  84. expect(getNextThoughtLevelValue({ ...option, options: options.slice(0, 1) })).toBeNull();
  85. });
  86. it("仅关闭类 value 是零强度,不从来源名称猜测", () => {
  87. expect(isNoThoughtLevel({ value: " NOTHINK ", name: "自定义名称" })).toBe(true);
  88. expect(isNoThoughtLevel({ value: "custom", name: "off" })).toBe(false);
  89. expect(isNoThoughtLevel({ value: "minimal", name: "none" })).toBe(false);
  90. });
  91. });