v4ConversationRowViewTimelineMarker.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // @vitest-environment jsdom
  2. import { act, createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { render } from "@testing-library/react";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared";
  7. import type { ModelSelectionView } from "@zcode/services";
  8. import type { TimelineMarkerRow } from "@zcode/shared/zcode-protocol-v4";
  9. import { DEFAULT_CODE_PREVIEW_SETTINGS } from "@/lib/codePreviewSettings.js";
  10. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  11. import { ConversationRowView } from "@/v4/ConversationRowView.js";
  12. import type { ConversationRowRenderContext } from "@/v4/conversationRowContext.js";
  13. const modelSelectionMock = vi.hoisted(() => ({ view: null as ModelSelectionView | null }));
  14. const context: ConversationRowRenderContext = {
  15. codePreviewSettings: DEFAULT_CODE_PREVIEW_SETTINGS,
  16. sessionId: "session-1",
  17. theme: "system",
  18. workspacePath: "/workspace",
  19. };
  20. function currentContext(): ConversationRowRenderContext {
  21. return { ...context, modelSelectionView: modelSelectionMock.view };
  22. }
  23. function renderMarker(row: TimelineMarkerRow, locale: "en-US" | "zh-CN" = "zh-CN"): string {
  24. return renderToStaticMarkup(
  25. createElement(
  26. ZCodeIntlProvider,
  27. { initialLocale: locale },
  28. createElement(ConversationRowView, { context: currentContext(), row }),
  29. ),
  30. );
  31. }
  32. afterEach(() => {
  33. modelSelectionMock.view = null;
  34. });
  35. function replaceProviders(providers: readonly { providerId: string; label: string }[]): void {
  36. modelSelectionMock.view = {
  37. revision: 1,
  38. providers: providers.map((provider) => ({
  39. providerId: provider.providerId,
  40. providerName: provider.label,
  41. config: {},
  42. models: [],
  43. })),
  44. } satisfies ModelSelectionView;
  45. }
  46. describe("ConversationRowView timeline marker contract", () => {
  47. it("exposes compact type, status, and origin without using localized text as authority", () => {
  48. const html = renderMarker({
  49. rowId: 12,
  50. turnId: "turn-1",
  51. createdAt: 1_700_000_000_000,
  52. createdAtSeq: 1,
  53. kind: "timelineMarker",
  54. sourceCommandId: "command-compact-1",
  55. marker: {
  56. type: "compact",
  57. status: "success",
  58. origin: "manual",
  59. operationId: "compact-1",
  60. },
  61. });
  62. expect(html).toContain('data-row-kind="timelineMarker"');
  63. expect(html).toContain('data-marker-type="compact"');
  64. expect(html).toContain('data-status="success"');
  65. expect(html).toContain('data-origin="manual"');
  66. expect(html).toContain('data-source-command-id="command-compact-1"');
  67. });
  68. it("不同供应商的同名模型使用供应商名称展示完整切换身份", () => {
  69. replaceProviders([
  70. { providerId: "primary-provider", label: "Primary Coding Plan" },
  71. { providerId: "alternate-provider", label: "Alternate API" },
  72. ]);
  73. const html = renderMarker({
  74. rowId: 13,
  75. turnId: "turn-2",
  76. createdAt: 1_700_000_001_000,
  77. createdAtSeq: 2,
  78. kind: "timelineMarker",
  79. marker: {
  80. type: "modelChange",
  81. fromProvider: "primary-provider",
  82. fromModel: "glm-5.2",
  83. toProvider: "alternate-provider",
  84. toModel: "glm-5.2",
  85. toThought: "high",
  86. },
  87. });
  88. expect(html).toContain("模型已切换 Primary Coding Plan/glm-5.2 → Alternate API/glm-5.2");
  89. expect(html).toContain("lucide-arrow-right-left");
  90. });
  91. it("source 缺失的初始模型事实渲染为正在使用实际模型", () => {
  92. replaceProviders([{ providerId: "child-provider", label: "Child Provider" }]);
  93. const row: TimelineMarkerRow = {
  94. rowId: 17,
  95. turnId: "turn-child-1",
  96. createdAt: 1_700_000_005_000,
  97. createdAtSeq: 6,
  98. kind: "timelineMarker",
  99. marker: {
  100. type: "modelChange",
  101. toProvider: "child-provider",
  102. toModel: "child-model",
  103. toThought: "high",
  104. },
  105. };
  106. const html = renderMarker(row);
  107. const englishHtml = renderMarker(row, "en-US");
  108. expect(html).toContain("正在使用 Child Provider/child-model");
  109. expect(html).not.toContain("正在使用 Child Provider/child-model 模型");
  110. expect(html).not.toContain("模型已切换");
  111. expect(html).not.toContain("lucide-arrow-right-left");
  112. expect(englishHtml).toContain("Using Child Provider/child-model");
  113. expect(englishHtml).not.toContain("Using Child Provider/child-model model");
  114. });
  115. it("账号套餐模型显示套餐类型,自定义供应商保留名称", () => {
  116. replaceProviders([
  117. {
  118. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  119. label: "Z.ai - Coding Plan",
  120. },
  121. {
  122. providerId: "custom:provider-b",
  123. label: "Provider B",
  124. },
  125. ]);
  126. const html = renderMarker({
  127. rowId: 16,
  128. turnId: "turn-5",
  129. createdAt: 1_700_000_004_000,
  130. createdAtSeq: 5,
  131. kind: "timelineMarker",
  132. marker: {
  133. type: "modelChange",
  134. fromProvider: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  135. fromModel: "GLM-5.2",
  136. toProvider: "custom:provider-b",
  137. toModel: "model-b-4.5",
  138. toThought: "high",
  139. },
  140. });
  141. expect(html).toContain("模型已切换 GLM-5.2(个人套餐) → Provider B/model-b-4.5");
  142. });
  143. it.each([
  144. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan, "个人套餐", "Individual Plan"],
  145. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan, "体验套餐", "Start Plan"],
  146. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan, "团队套餐", "Team Plan"],
  147. [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan, "个人套餐", "Individual Plan"],
  148. [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan, "体验套餐", "Start Plan"],
  149. [BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan, "团队套餐", "Team Plan"],
  150. ])("目录缺失时仍按记录的 %s 展示套餐,支持中英文和首次使用", (providerId, zh, en) => {
  151. const row: TimelineMarkerRow = {
  152. rowId: 18,
  153. turnId: "turn-plan",
  154. createdAt: 1_700_000_006_000,
  155. createdAtSeq: 7,
  156. kind: "timelineMarker",
  157. marker: {
  158. type: "modelChange",
  159. fromProvider: providerId,
  160. fromModel: "GLM-5.3",
  161. toProvider: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  162. toModel: "GLM-5.3",
  163. },
  164. };
  165. expect(renderMarker(row)).toContain(`模型已切换 GLM-5.3(${zh}) → GLM-5.3(体验套餐)`);
  166. expect(renderMarker(row, "en-US")).toContain(`GLM-5.3(${en}) → GLM-5.3(Start Plan)`);
  167. row.marker = { type: "modelChange", toProvider: providerId, toModel: "GLM-5.3" };
  168. expect(renderMarker(row)).toContain(`正在使用 GLM-5.3(${zh})`);
  169. });
  170. it("供应商配置缺失时回落 provider ID 以保留同名模型身份", () => {
  171. const html = renderMarker({
  172. rowId: 14,
  173. turnId: "turn-3",
  174. createdAt: 1_700_000_002_000,
  175. createdAtSeq: 3,
  176. kind: "timelineMarker",
  177. marker: {
  178. type: "modelChange",
  179. fromProvider: "removed-provider",
  180. fromModel: "glm-5.2",
  181. toProvider: "current-provider",
  182. toModel: "glm-5.2",
  183. toThought: "high",
  184. },
  185. });
  186. expect(html).toContain("模型已切换 removed-provider/glm-5.2 → current-provider/glm-5.2");
  187. });
  188. it("供应商目录稍后水合时原地刷新已渲染 marker 名称", () => {
  189. const row: TimelineMarkerRow = {
  190. rowId: 15,
  191. turnId: "turn-4",
  192. createdAt: 1_700_000_003_000,
  193. createdAtSeq: 4,
  194. kind: "timelineMarker",
  195. marker: {
  196. type: "modelChange",
  197. fromProvider: "primary-provider",
  198. fromModel: "glm-5.2",
  199. toProvider: "alternate-provider",
  200. toModel: "glm-5.2",
  201. toThought: "high",
  202. },
  203. };
  204. const view = render(
  205. createElement(
  206. ZCodeIntlProvider,
  207. { initialLocale: "zh-CN" },
  208. createElement(ConversationRowView, { context: currentContext(), row }),
  209. ),
  210. );
  211. expect(view.container.textContent).toContain(
  212. "模型已切换 primary-provider/glm-5.2 → alternate-provider/glm-5.2",
  213. );
  214. act(() => {
  215. replaceProviders([
  216. { providerId: "primary-provider", label: "Primary Coding Plan" },
  217. { providerId: "alternate-provider", label: "Alternate API" },
  218. ]);
  219. view.rerender(
  220. createElement(
  221. ZCodeIntlProvider,
  222. { initialLocale: "zh-CN" },
  223. createElement(ConversationRowView, { context: currentContext(), row: { ...row } }),
  224. ),
  225. );
  226. });
  227. expect(view.container.textContent).toContain(
  228. "模型已切换 Primary Coding Plan/glm-5.2 → Alternate API/glm-5.2",
  229. );
  230. view.unmount();
  231. });
  232. });