composerPlanGroups.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import type { ProviderSettingsView } from "@zcode/services";
  2. import { describe, expect, it } from "vitest";
  3. import { buildComposerPlanGroups, composerConnectionKey } from "@/lib/composerPlanGroups.js";
  4. import { BUILTIN_MODEL_PROVIDER_IDS as IDS } from "@zcode/shared";
  5. const personal = {
  6. key: `registry-provider:${IDS.bigmodelIndividualCodingPlan}`,
  7. label: "BigModel",
  8. labelBadge: "Individual",
  9. items: [{ key: "m", value: "m", name: "GLM-5.3" }],
  10. };
  11. const team = {
  12. ...personal,
  13. key: `registry-provider:${IDS.bigmodelTeamCodingPlan}`,
  14. labelBadge: "Team",
  15. };
  16. const api = { key: "api", label: "DeepSeek", labelBadge: "API", items: [] };
  17. const product = {
  18. family: "bigmodel" as const,
  19. productId: "p",
  20. subscribed: true,
  21. teamProjects: [
  22. { organizationId: "o", projectId: "j", organizationName: "Team One" },
  23. { organizationId: "o2", projectId: "j2", organizationName: "Team Two" },
  24. ],
  25. };
  26. describe("Composer concrete team provider rows", () => {
  27. it.each(["unavailable", "unknown", "available", undefined] as const)(
  28. "按 Provider 权威状态过滤菜单:%s",
  29. (availability) => {
  30. const settings = {
  31. providers: [
  32. { providerId: IDS.bigmodelTeamCodingPlan, accountState: { availability } },
  33. { providerId: IDS.bigmodelIndividualCodingPlan, accountState: { availability } },
  34. ],
  35. } as unknown as ProviderSettingsView;
  36. const result = buildComposerPlanGroups([personal, team, api], [product], settings);
  37. expect(result.map((g) => g.key)).toEqual(
  38. availability === "unavailable"
  39. ? [expect.any(String), expect.any(String), api.key]
  40. : [personal.key, expect.any(String), expect.any(String), api.key],
  41. );
  42. },
  43. );
  44. it.each([true, false])(
  45. "当前团队不可用只隐藏该身份,其他团队仍可选(目录已到达:%s)",
  46. (hasCatalog) => {
  47. const selection = {
  48. kind: "team-coding-plan" as const,
  49. productId: "p",
  50. organizationId: "o",
  51. projectId: "j",
  52. };
  53. const settings = {
  54. providers: [
  55. {
  56. providerId: IDS.bigmodelTeamCodingPlan,
  57. enabled: true,
  58. effectiveConfig: {
  59. access: { type: "zhipu-account", accountType: "bigmodel", mode: "team-coding-plan" },
  60. api: { type: "anthropic-messages" },
  61. },
  62. accountState: { availability: "unavailable" },
  63. },
  64. ],
  65. } as unknown as ProviderSettingsView;
  66. const groups = buildComposerPlanGroups(
  67. hasCatalog ? [personal, team, api] : [personal, api],
  68. [product],
  69. settings,
  70. "glm",
  71. {},
  72. { bigmodel: selection },
  73. );
  74. expect(groups.some((g) => g.key === composerConnectionKey("bigmodel", selection))).toBe(
  75. false,
  76. );
  77. const other = groups.find((g) => g.labelBadge === "Team Two");
  78. expect(other?.connection?.selection).toEqual({
  79. kind: "team-coding-plan",
  80. productId: "p",
  81. organizationId: "o2",
  82. projectId: "j2",
  83. });
  84. expect(other?.connection?.disabled).not.toBe(true);
  85. },
  86. );
  87. it.each([
  88. ["个人", "团队"],
  89. ["Individual", "Team"],
  90. ])("补充的非激活个人套餐复用当前语言 %s", (personalLabel, teamLabel) => {
  91. const settings = {
  92. providers: [
  93. {
  94. providerId: IDS.bigmodelIndividualCodingPlan,
  95. enabled: true,
  96. effectiveConfig: {
  97. access: {
  98. type: "zhipu-account",
  99. accountType: "bigmodel",
  100. mode: "individual-coding-plan",
  101. entitled: true,
  102. },
  103. api: { type: "anthropic-messages" },
  104. },
  105. accountState: { availability: "available", entitled: true },
  106. },
  107. ],
  108. } as unknown as ProviderSettingsView;
  109. const result = buildComposerPlanGroups([team, api], [product], settings, "glm", {
  110. codingPlanBadgeLabel: personalLabel,
  111. teamPlanBadgeLabel: teamLabel,
  112. });
  113. expect(result[0]?.labelBadge).toBe(personalLabel);
  114. expect(result[1]?.labelBadge).toBe("Team One");
  115. });
  116. it("每个完整已购团队直接成行,保留品牌与真实标签,API 不归并", () => {
  117. const groups = buildComposerPlanGroups([personal, team, api], [product]);
  118. expect(groups.map((g) => [g.label, g.labelBadge])).toEqual([
  119. ["BigModel", "Individual"],
  120. ["BigModel", "Team One"],
  121. ["BigModel", "Team Two"],
  122. ["DeepSeek", "API"],
  123. ]);
  124. expect(groups[1]?.connection?.selection).toEqual({
  125. kind: "team-coding-plan",
  126. productId: "p",
  127. organizationId: "o",
  128. projectId: "j",
  129. });
  130. expect(groups[3]).toBe(api);
  131. });
  132. it("相同名称不同身份保留,重复身份去重,不展示未购买或残缺团队", () => {
  133. const same = {
  134. ...product,
  135. teamProjects: product.teamProjects.map((p) => ({ ...p, organizationName: "Same" })),
  136. };
  137. const groups = buildComposerPlanGroups(
  138. [personal, team, api],
  139. [
  140. same,
  141. same,
  142. { ...product, subscribed: false },
  143. {
  144. ...product,
  145. productId: "bad",
  146. teamProjects: [{ organizationId: "o", projectId: "", organizationName: "Bad" }],
  147. },
  148. ],
  149. );
  150. expect(groups.filter((g) => g.connection?.selection?.kind === "team-coding-plan")).toHaveLength(
  151. 2,
  152. );
  153. expect(groups[1]?.key).not.toBe(groups[2]?.key);
  154. });
  155. it("当前团队目录暂未到达时保留原团队行,不伪装为个人", () => {
  156. expect(buildComposerPlanGroups([personal, team, api], []).map((g) => g.key)).toEqual([
  157. personal.key,
  158. team.key,
  159. api.key,
  160. ]);
  161. });
  162. it("未选团队额度未知时仍可由已购目录发现,Agent 格式不兼容时隐藏", () => {
  163. const settings = {
  164. providers: [
  165. {
  166. providerId: IDS.bigmodelTeamCodingPlan,
  167. enabled: true,
  168. effectiveConfig: {
  169. access: { type: "zhipu-account", accountType: "bigmodel", mode: "team-coding-plan" },
  170. api: { type: "anthropic-messages" },
  171. },
  172. accountState: { availability: "unknown", entitled: false },
  173. },
  174. ],
  175. } as unknown as ProviderSettingsView;
  176. expect(
  177. buildComposerPlanGroups([personal, api], [product], settings).filter(
  178. (g) => g.connection?.selection?.kind === "team-coding-plan",
  179. ),
  180. ).toHaveLength(2);
  181. expect(
  182. buildComposerPlanGroups([personal, api], [product], settings, "codex").filter(
  183. (g) => g.connection?.selection?.kind === "team-coding-plan",
  184. ),
  185. ).toHaveLength(0);
  186. });
  187. it("隐藏明确不可用团队,不回退为泛化 Team 行;unknown 和可用项目保留", () => {
  188. const unavailable = {
  189. ...product,
  190. teamProjects: product.teamProjects.map((p) => ({ ...p, apiKeyStatus: "unavailable" })),
  191. };
  192. expect(buildComposerPlanGroups([personal, team, api], [unavailable]).map((g) => g.key)).toEqual(
  193. [personal.key, api.key],
  194. );
  195. const mixed = {
  196. ...product,
  197. apiKeyStatus: "unavailable",
  198. teamProjects: [
  199. { ...product.teamProjects[0]!, apiKeyStatus: "unknown" },
  200. { ...product.teamProjects[1]!, apiKeyStatus: "available" },
  201. ],
  202. };
  203. expect(
  204. buildComposerPlanGroups([personal, team, api], [mixed]).filter(
  205. (g) => g.connection?.selection?.kind === "team-coding-plan",
  206. ),
  207. ).toHaveLength(2);
  208. expect(
  209. buildComposerPlanGroups(
  210. [personal, team, api],
  211. [{ ...product, apiKeyStatus: "unavailable" }],
  212. ).map((g) => g.key),
  213. ).toEqual([personal.key, api.key]);
  214. });
  215. });