model-catalog-port.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * ModelCatalogPort 的宿主实现(src/app/model-catalog-port.ts)。
  3. *
  4. * 四条被钉住的事实:活视图重读、`current` 标记、默认档位规则、没有档位的模型。
  5. * 用一个可变的假注册表而不是真 ProviderRegistry:被测的是投影与「什么时候读视图」,
  6. * 而注册表自己的装配有它自己的用例(packages/provider)。
  7. */
  8. import { describe, expect, it } from "vitest";
  9. import type { ModelSelection } from "@zcode/shared/model-selection";
  10. import { createModelCatalogPort } from "../src/app/model-catalog-port.js";
  11. import type { ProviderRegistryModelSource } from "../src/app/provider-registry-model-runtime.js";
  12. interface FakeModel {
  13. modelId: string;
  14. reasoningLevels: string[];
  15. contextWindow?: number;
  16. }
  17. interface FakeProvider {
  18. providerId: string;
  19. /** 注册表真实的三态:取过名、显式清掉(`null`)、从未取过(缺席)。 */
  20. providerName?: string | null;
  21. models: FakeModel[];
  22. }
  23. /**
  24. * 只实现 `getView()`:被测实现刻意不碰注册表的其余成员(它枚举的是整张视图,不做按 id
  25. * 的点查)。其余成员抛错,于是「实现偷偷多用了一个入口」会在用例里立刻暴露。
  26. */
  27. function fakeRegistry(providers: FakeProvider[]): {
  28. registry: ProviderRegistryModelSource;
  29. setProviders: (next: FakeProvider[]) => void;
  30. viewReads: () => number;
  31. } {
  32. let current = providers;
  33. let reads = 0;
  34. const registry = {
  35. getView() {
  36. reads += 1;
  37. return {
  38. revision: reads,
  39. providers: current.map((provider) => ({
  40. providerId: provider.providerId,
  41. ...(provider.providerName === undefined ? {} : { providerName: provider.providerName }),
  42. config: {},
  43. models: provider.models.map((model) => ({
  44. modelId: model.modelId,
  45. config: {
  46. optionSpecs: { reasoningLevel: { values: model.reasoningLevels } },
  47. properties: { contextWindow: model.contextWindow },
  48. },
  49. })),
  50. })),
  51. };
  52. },
  53. getProvider() {
  54. throw new Error("the catalog port must not point-query the registry");
  55. },
  56. getModel() {
  57. throw new Error("the catalog port must not point-query the registry");
  58. },
  59. validateSelection() {
  60. throw new Error("the catalog port must not validate selections");
  61. },
  62. onDidChange() {
  63. throw new Error("the catalog port must not subscribe; it re-reads on every call");
  64. },
  65. } as unknown as ProviderRegistryModelSource;
  66. return {
  67. registry,
  68. setProviders: (next) => {
  69. current = next;
  70. },
  71. viewReads: () => reads,
  72. };
  73. }
  74. const ZHIPU: FakeProvider = {
  75. providerId: "zhipu",
  76. providerName: "Zhipu",
  77. models: [
  78. { modelId: "glm-5.3", reasoningLevels: ["low", "medium", "high"], contextWindow: 200_000 },
  79. { modelId: "glm-5.3-flash", reasoningLevels: [] },
  80. ],
  81. };
  82. describe("createModelCatalogPort", () => {
  83. it("re-reads the live registry view on every call (2026-09-02 stale provider incident)", () => {
  84. // 判别用例:构造之后注册表才变。一份构造期冻结的目录会让第二次列举给出旧答案,
  85. // 而 `subagent_model` 的解析正是据它挑模型。
  86. const fake = fakeRegistry([ZHIPU]);
  87. const port = createModelCatalogPort({ registry: fake.registry, currentSelection: () => undefined });
  88. expect(port.listModels().map((entry) => entry.modelId)).toEqual(["glm-5.3", "glm-5.3-flash"]);
  89. expect(fake.viewReads()).toBe(1);
  90. fake.setProviders([
  91. { providerId: "anthropic", models: [{ modelId: "sonnet", reasoningLevels: [] }] },
  92. ]);
  93. expect(port.listModels().map((entry) => `${entry.providerId}/${entry.modelId}`)).toEqual([
  94. "anthropic/sonnet",
  95. ]);
  96. // 每次调用恰好读一次视图:既不缓存,也不为一次列举重复读。
  97. expect(fake.viewReads()).toBe(2);
  98. });
  99. it("flags exactly the entry that matches the session selection, ignoring reasoning options", () => {
  100. const fake = fakeRegistry([
  101. ZHIPU,
  102. { providerId: "anthropic", models: [{ modelId: "glm-5.3", reasoningLevels: [] }] },
  103. ]);
  104. // 选择带 reasoning 选项,条目按身份两段匹配(options 不是身份的一部分)。
  105. const selection: ModelSelection = {
  106. providerId: "zhipu",
  107. modelId: "glm-5.3",
  108. options: { reasoningLevel: "high" },
  109. };
  110. const port = createModelCatalogPort({
  111. registry: fake.registry,
  112. currentSelection: () => selection,
  113. });
  114. const entries = port.listModels();
  115. expect(entries.filter((entry) => entry.current)).toHaveLength(1);
  116. expect(entries.find((entry) => entry.current)).toMatchObject({
  117. providerId: "zhipu",
  118. modelId: "glm-5.3",
  119. });
  120. // 同名模型挂在另一个 provider 下**不**为真:判据是 provider + model,不是裸 id。
  121. expect(entries.find((entry) => entry.providerId === "anthropic")?.current).toBe(false);
  122. });
  123. it("no session selection at all: every entry is current: false", () => {
  124. const fake = fakeRegistry([ZHIPU]);
  125. const port = createModelCatalogPort({
  126. registry: fake.registry,
  127. currentSelection: () => undefined,
  128. });
  129. expect(port.listModels().every((entry) => entry.current === false)).toBe(true);
  130. });
  131. it("defaults the reasoning level to the last value (same rule as the GUI picker's toModelOption)", () => {
  132. const fake = fakeRegistry([ZHIPU]);
  133. const port = createModelCatalogPort({
  134. registry: fake.registry,
  135. currentSelection: () => undefined,
  136. });
  137. const [levelled] = port.listModels();
  138. expect(levelled).toMatchObject({
  139. providerId: "zhipu",
  140. modelId: "glm-5.3",
  141. providerLabel: "Zhipu",
  142. reasoningLevels: ["low", "medium", "high"],
  143. defaultReasoningLevel: "high",
  144. contextWindow: 200_000,
  145. });
  146. });
  147. it("a model with no reasoning levels gets an empty array and no default", () => {
  148. // 空数组而不是缺席(端口契约),而默认档位整个键不出——一个 `defaultReasoningLevel:
  149. // undefined` 读起来像「有默认但没算出来」。
  150. const fake = fakeRegistry([ZHIPU]);
  151. const port = createModelCatalogPort({
  152. registry: fake.registry,
  153. currentSelection: () => undefined,
  154. });
  155. const flash = port.listModels().find((entry) => entry.modelId === "glm-5.3-flash")!;
  156. expect(flash.reasoningLevels).toEqual([]);
  157. expect("defaultReasoningLevel" in flash).toBe(false);
  158. expect("contextWindow" in flash).toBe(false);
  159. });
  160. it("a provider with no display name leaves providerLabel absent, and no entry is ever disabled", () => {
  161. const fake = fakeRegistry([
  162. { providerId: "anonymous", models: [{ modelId: "m", reasoningLevels: [] }] },
  163. ]);
  164. const port = createModelCatalogPort({
  165. registry: fake.registry,
  166. currentSelection: () => undefined,
  167. });
  168. const [entry] = port.listModels();
  169. expect("providerLabel" in entry!).toBe(false);
  170. // 本宿主没有「配了但不可用」的来源(见实现里的注释):字段恒缺席,绝不兜一个空串。
  171. expect("disabledReason" in entry!).toBe(false);
  172. });
  173. it("null and blank provider names collapse to an absent key, never a printed empty label", () => {
  174. // 注册表的 providerName 是 `string | null | undefined`(config-service.ts 把空串归一成
  175. // null),端口契约只认 `string | undefined`。三种「没名字」必须给出同一个答案——放一个
  176. // null 或空串过去,它会原样印进 ListModels 的那一行。
  177. const fake = fakeRegistry([
  178. {
  179. providerId: "cleared",
  180. providerName: null,
  181. models: [{ modelId: "m", reasoningLevels: [] }],
  182. },
  183. { providerId: "blank", providerName: " ", models: [{ modelId: "m", reasoningLevels: [] }] },
  184. {
  185. providerId: "named",
  186. providerName: " Zhipu ",
  187. models: [{ modelId: "m", reasoningLevels: [] }],
  188. },
  189. ]);
  190. const port = createModelCatalogPort({
  191. registry: fake.registry,
  192. currentSelection: () => undefined,
  193. });
  194. const [cleared, blank, named] = port.listModels();
  195. expect("providerLabel" in cleared!).toBe(false);
  196. expect("providerLabel" in blank!).toBe(false);
  197. // 取过名字的照常在场,且顺手去掉两端空白(读侧拿到的就是要显示的那个词)。
  198. expect(named!.providerLabel).toBe("Zhipu");
  199. });
  200. it("copies the reasoning level array so the caller cannot mutate the registry's view", () => {
  201. const levels = ["low", "high"];
  202. const fake = fakeRegistry([
  203. { providerId: "p", models: [{ modelId: "m", reasoningLevels: levels }] },
  204. ]);
  205. const port = createModelCatalogPort({
  206. registry: fake.registry,
  207. currentSelection: () => undefined,
  208. });
  209. port.listModels()[0]!.reasoningLevels.push("mutated");
  210. expect(levels).toEqual(["low", "high"]);
  211. });
  212. });