v4SessionQuotaBannerState.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { BUILTIN_MODEL_PROVIDER_IDS, type UsageEntitlementSnapshot } from "@zcode/shared";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. buildSessionQuotaBannerDismissKey,
  5. buildSessionQuotaBannerState,
  6. normalizeProviderLimitedBannerMessage,
  7. resolveQuotaBannerUpgradeProviderId,
  8. shouldOfferQuotaBannerUpgrade,
  9. } from "@/v4/sessionQuotaBannerState.js";
  10. import { resolveMcpUnavailableNotice } from "@/v4/mcpUnavailableBannerNotice.js";
  11. import type { ConversationRow } from "@zcode/shared/zcode-protocol-v4";
  12. function entitlement(params: {
  13. dailyRemaining: number;
  14. modelRemaining: number;
  15. }): UsageEntitlementSnapshot {
  16. return {
  17. generatedAt: 1,
  18. authenticated: true,
  19. provider: {
  20. id: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  21. name: "Z.AI Start Plan",
  22. },
  23. remaining: {
  24. count: params.dailyRemaining,
  25. isShow: true,
  26. percentage: params.dailyRemaining / 500,
  27. },
  28. subscription: null,
  29. quota: {
  30. level: "Start",
  31. limits: [
  32. {
  33. type: "glm-5.2",
  34. bucketId: "bucket-glm-52",
  35. periodStart: 0,
  36. periodEnd: 1000,
  37. number: 100,
  38. remaining: params.modelRemaining,
  39. percentage: params.modelRemaining / 100,
  40. usageDetails: [{ modelCode: "glm-5.2", usage: 100 - params.modelRemaining }],
  41. },
  42. ],
  43. },
  44. };
  45. }
  46. describe("V4 session quota banner state", () => {
  47. it("只保留 10% 和耗尽提醒,且不阻断提交", () => {
  48. const cases = [
  49. [50, null, false],
  50. [48, null, false],
  51. [47, null, false],
  52. [20, null, false],
  53. [10.1, null, false],
  54. [10, "model-very-low", true],
  55. [9, "model-very-low", true],
  56. [0, "daily-exhausted", false],
  57. ] as const;
  58. for (const [remaining, kind, dismissible] of cases) {
  59. const state = buildSessionQuotaBannerState({
  60. activeProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  61. snapshot: entitlement({
  62. dailyRemaining: 450,
  63. modelRemaining: remaining,
  64. }),
  65. modelId: "glm-5.2",
  66. });
  67. expect(state.kind).toBe(kind);
  68. expect(state.dismissible).toBe(dismissible);
  69. expect(state.blocksSubmit).toBe(false);
  70. }
  71. });
  72. it("服务端并发和额度错误优先于 entitlement,dismiss key 隔离错误实例", () => {
  73. const state = buildSessionQuotaBannerState({
  74. activeProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  75. snapshot: entitlement({ dailyRemaining: 450, modelRemaining: 50 }),
  76. modelId: "glm-5.2",
  77. serverConcurrentLimited: true,
  78. serverConcurrentLimitBusinessCode: "3010",
  79. serverConcurrentLimitReason: "retry-exhausted-busy",
  80. });
  81. expect(state.kind).toBe("concurrent-limit");
  82. expect(state.priority).toBe(60);
  83. expect(buildSessionQuotaBannerDismissKey(state, "error-a")).not.toBe(
  84. buildSessionQuotaBannerDismissKey(state, "error-b"),
  85. );
  86. });
  87. it("官方 MCP 不可用时按 code 分流,且不阻断输入", () => {
  88. const quotaState = buildSessionQuotaBannerState({
  89. activeProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  90. snapshot: null,
  91. modelId: "glm-5.2",
  92. mcpUnavailableNotice: {
  93. code: "quota_exceeded",
  94. rowId: 12,
  95. serverName: "zcode-official",
  96. toolName: "search_image",
  97. },
  98. });
  99. expect(quotaState.kind).toBe("mcp-quota-exhausted");
  100. expect(quotaState.mcpServerName).toBe("zcode-official");
  101. expect(quotaState.dismissible).toBe(true);
  102. expect(quotaState.blocksSubmit).toBe(false);
  103. const planState = buildSessionQuotaBannerState({
  104. activeProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  105. snapshot: null,
  106. modelId: "glm-5.2",
  107. mcpUnavailableNotice: {
  108. code: "coding_plan_required",
  109. rowId: 12,
  110. serverName: "zcode-official",
  111. toolName: "search_image",
  112. },
  113. });
  114. expect(planState.kind).toBe("mcp-plan-required");
  115. });
  116. it("今日额度用完不给升级入口,权益缺失才给", () => {
  117. // 额度用完只能等自然日重置,升级按钮会让用户以为花钱就能立刻继续。
  118. expect(shouldOfferQuotaBannerUpgrade("mcp-quota-exhausted")).toBe(false);
  119. expect(shouldOfferQuotaBannerUpgrade("mcp-plan-required")).toBe(true);
  120. // 既有 kind 的升级入口不受影响。
  121. expect(shouldOfferQuotaBannerUpgrade("daily-exhausted")).toBe(true);
  122. expect(shouldOfferQuotaBannerUpgrade("provider-limited")).toBe(true);
  123. expect(shouldOfferQuotaBannerUpgrade(null)).toBe(true);
  124. });
  125. it("模型额度问题优先于 MCP 提示", () => {
  126. // 模型额度直接影响对话本身,不能被 MCP 提示挡住。
  127. const state = buildSessionQuotaBannerState({
  128. activeProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  129. snapshot: entitlement({ dailyRemaining: 0, modelRemaining: 0 }),
  130. modelId: "glm-5.2",
  131. serverQuotaExhausted: true,
  132. mcpUnavailableNotice: {
  133. code: "quota_exceeded",
  134. rowId: 12,
  135. serverName: "zcode-official",
  136. toolName: "search_image",
  137. },
  138. });
  139. expect(state.kind).toBe("daily-exhausted");
  140. expect(state.priority).toBeGreaterThan(8);
  141. });
  142. it("MCP 提示的 dismiss key 跟随 server 与具体调用", () => {
  143. const build = (rowId: number, serverName: string) =>
  144. buildSessionQuotaBannerDismissKey(
  145. buildSessionQuotaBannerState({
  146. activeProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  147. snapshot: null,
  148. modelId: "glm-5.2",
  149. mcpUnavailableNotice: {
  150. code: "quota_exceeded",
  151. rowId,
  152. serverName,
  153. toolName: "search_image",
  154. },
  155. }),
  156. );
  157. // 关闭一次后同一次调用不再弹;新的失败调用或另一个 server 会重新弹。
  158. expect(build(12, "zcode-official")).toBe(build(12, "zcode-official"));
  159. expect(build(12, "zcode-official")).not.toBe(build(13, "zcode-official"));
  160. expect(build(12, "zcode-official")).not.toBe(build(12, "zcode-video-edit"));
  161. });
  162. it("从 rows 里取最新一条带标识的官方 MCP 调用", () => {
  163. const toolRow = (rowId: number, unavailable?: { code: "quota_exceeded" }): ConversationRow =>
  164. ({
  165. rowId,
  166. turnId: "t1",
  167. kind: "toolCall",
  168. toolCallId: `call-${rowId}`,
  169. toolName: "mcp__zcode-official__search_image",
  170. status: "success",
  171. inputText: "{}",
  172. display: {
  173. kind: "mcp_tool",
  174. serverName: "zcode-official",
  175. toolName: "search_image",
  176. ...(unavailable ? { unavailable } : {}),
  177. },
  178. }) as unknown as ConversationRow;
  179. expect(resolveMcpUnavailableNotice(undefined)).toBeNull();
  180. expect(resolveMcpUnavailableNotice([toolRow(1)])).toBeNull();
  181. expect(
  182. resolveMcpUnavailableNotice([
  183. toolRow(1, { code: "quota_exceeded" }),
  184. toolRow(2),
  185. toolRow(3, { code: "quota_exceeded" }),
  186. ]),
  187. ).toEqual({
  188. code: "quota_exceeded",
  189. rowId: 3,
  190. serverName: "zcode-official",
  191. toolName: "search_image",
  192. });
  193. });
  194. it("同一 MCP 工具的新成功调用覆盖旧失败,其他工具失败不受影响", () => {
  195. const toolRow = (
  196. rowId: number,
  197. serverName: string,
  198. toolName: string,
  199. unavailable?: { code: "quota_exceeded" },
  200. ): ConversationRow =>
  201. ({
  202. rowId,
  203. turnId: "t1",
  204. kind: "toolCall",
  205. toolCallId: `call-${rowId}`,
  206. toolName: `mcp__${serverName}__${toolName}`,
  207. status: "success",
  208. inputText: "{}",
  209. display: {
  210. kind: "mcp_tool",
  211. serverName,
  212. toolName,
  213. ...(unavailable ? { unavailable } : {}),
  214. },
  215. }) as unknown as ConversationRow;
  216. expect(
  217. resolveMcpUnavailableNotice([
  218. toolRow(1, "official", "search_image", { code: "quota_exceeded" }),
  219. toolRow(2, "official", "search_image"),
  220. ]),
  221. ).toBeNull();
  222. expect(
  223. resolveMcpUnavailableNotice([
  224. toolRow(1, "official", "search_image", { code: "quota_exceeded" }),
  225. toolRow(2, "official", "speech_synthesize"),
  226. ]),
  227. ).toMatchObject({ rowId: 1, toolName: "search_image" });
  228. });
  229. it("清洗 GLM bracket 文案并保留当前 Provider 身份", () => {
  230. expect(
  231. normalizeProviderLimitedBannerMessage("[1113][余额不足或无可用资源包,请充值。][request-id]"),
  232. ).toBe("余额不足或无可用资源包,请充值。");
  233. expect(
  234. resolveQuotaBannerUpgradeProviderId(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan),
  235. ).toBe(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan);
  236. });
  237. });