feedbackSubmitForm.test.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import {
  5. FeedbackSubmitForm,
  6. SubmitProgressView,
  7. readCurrentAgentModelContext,
  8. shouldAutoCloseFeedbackOnTicketCreated,
  9. } from "@/feedback/FeedbackSubmitForm.js";
  10. import { startSimplifiedFeedbackSubmission } from "@/feedback/feedbackSubmitSubmission.js";
  11. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  12. import type { IFeedbackService } from "@zcode/services";
  13. import type { FeedbackTicketDetail, FeedbackTicketSummary, IPlatformService } from "@zcode/shared";
  14. import { TabStoreProvider } from "@/store/TabStoreProvider.js";
  15. import { dismissFeedbackSubmissionJob } from "@/feedback/feedbackSubmissionJob.js";
  16. function createFeedbackService(): IFeedbackService {
  17. return {
  18. getDeviceSnapshot: vi.fn(),
  19. create: vi.fn(),
  20. list: vi.fn(),
  21. get: vi.fn(),
  22. comment: vi.fn(),
  23. uploadAttachmentData: vi.fn(),
  24. uploadAttachmentWithProgress: vi.fn(),
  25. prepareCompactLogArchive: vi.fn(),
  26. cleanupPreparedLogArchive: vi.fn(),
  27. onDynamicUploadProgress: vi.fn(),
  28. cancelCreate: vi.fn(),
  29. cancelUpload: vi.fn(),
  30. onDynamicUpdate: vi.fn(),
  31. } as unknown as IFeedbackService;
  32. }
  33. function createTicket(id: string): FeedbackTicketDetail {
  34. const summary: FeedbackTicketSummary = {
  35. id,
  36. title: "功能建议",
  37. type: "feature",
  38. status: "已提交",
  39. created_at: "2026-06-17T10:00:00.000Z",
  40. updated_at: "2026-06-17T10:00:00.000Z",
  41. };
  42. return {
  43. ...summary,
  44. description: "desc",
  45. attachments: [],
  46. comments: [],
  47. events: [],
  48. };
  49. }
  50. function renderFeedbackSubmitForm(locale: "zh-CN" | "en-US" = "zh-CN") {
  51. return renderToStaticMarkup(
  52. createElement(
  53. ZCodeIntlProvider,
  54. { initialLocale: locale },
  55. createElement(
  56. TabStoreProvider,
  57. null,
  58. createElement(FeedbackSubmitForm, {
  59. feedbackService: createFeedbackService(),
  60. platform: {} as IPlatformService,
  61. onSubmitted: vi.fn(),
  62. onViewTickets: vi.fn(),
  63. onCancel: vi.fn(),
  64. }),
  65. ),
  66. ),
  67. );
  68. }
  69. describe("FeedbackSubmitForm", () => {
  70. it("uses English copy when the app locale is English", () => {
  71. const html = renderFeedbackSubmitForm("en-US");
  72. expect(html).toContain("Issue description");
  73. expect(html).toContain("Screenshots");
  74. expect(html).not.toContain(">Logs</h3>");
  75. expect(html).toContain("Upload diagnostic logs");
  76. expect(html).toContain("On by default");
  77. expect(html).toContain("safe projection of CLI configuration");
  78. expect(html).toContain("model-call traces");
  79. expect(html).not.toContain("问题描述");
  80. expect(html).not.toContain("反馈类型");
  81. });
  82. it("从当前模型配置读取选中的模型展示名", () => {
  83. const selectedModel = "315c04c4-1234-5678-9abc-000000000000/claude-opus-4-7";
  84. const context = readCurrentAgentModelContext([
  85. {
  86. id: "model",
  87. name: "模型",
  88. category: "model",
  89. type: "select",
  90. currentValue: selectedModel,
  91. options: [
  92. { value: "another-model", name: "b2" },
  93. { value: selectedModel, name: "a1" },
  94. ],
  95. },
  96. ]);
  97. expect(context).toEqual({
  98. model: selectedModel,
  99. display: "a1",
  100. optionCount: 2,
  101. optionsPreview: ["b2", "a1"],
  102. });
  103. });
  104. it("不再展示旧提交弹窗里的分类字段,但保留简化日志开关", () => {
  105. const html = renderFeedbackSubmitForm();
  106. expect(html).toContain("问题描述");
  107. expect(html).toContain("截图");
  108. expect(html).toContain("联系方式");
  109. expect(html).toContain("日志");
  110. expect(html).toContain("上传诊断日志");
  111. expect(html).toContain('aria-checked="true"');
  112. expect(html).toContain("默认开启");
  113. expect(html).toContain("CLI 配置中的安全投影");
  114. expect(html).toContain("当天模型调用轨迹");
  115. expect(html).not.toContain("Issue description");
  116. expect(html).not.toContain("Screenshots");
  117. expect(html).not.toContain("反馈类型");
  118. expect(html).not.toContain("功能模块");
  119. expect(html).not.toContain("影响程度");
  120. expect(html).not.toContain("当前模型型号");
  121. expect(html).not.toContain("附带完整日志");
  122. });
  123. it("截图附件区域保持可聚焦,方便用户定位截图上传入口", () => {
  124. const html = renderFeedbackSubmitForm();
  125. expect(html).toContain("粘贴、拖拽图片到这里,或选择文件。");
  126. expect(html).toContain('tabindex="0"');
  127. expect(html).toContain("focus-visible:ring-primary/30");
  128. expect(html).toContain("border-dashed");
  129. });
  130. it("问题描述输入框最多显示 5 行文字", () => {
  131. const html = renderFeedbackSubmitForm();
  132. expect(html).toContain('rows="5"');
  133. expect(html).toContain("h-[136px]");
  134. expect(html).toContain("overflow-y-auto");
  135. expect(html).not.toContain('rows="8"');
  136. expect(html).not.toContain("min-h-[184px]");
  137. });
  138. it("问题描述输入框使用固定尺寸并允许长连续文本换行", () => {
  139. const html = renderFeedbackSubmitForm();
  140. expect(html).toContain("field-sizing-fixed");
  141. expect(html).toContain("max-w-full");
  142. expect(html).toContain("min-w-0");
  143. expect(html).toContain("whitespace-pre-wrap");
  144. expect(html).toContain("break-words");
  145. expect(html).not.toContain("field-sizing-content");
  146. });
  147. it("表单分组不再逐层包卡片,避免弹窗里套太多卡片", () => {
  148. const html = renderFeedbackSubmitForm();
  149. expect(html).toContain('<section class="space-y-2">');
  150. expect(html).not.toContain("border-card-border bg-card px-3 py-2.5");
  151. });
  152. it("提交进度使用独立整行布局,避免英文长文案被底部按钮挤压截断", () => {
  153. const html = renderToStaticMarkup(
  154. createElement(SubmitProgressView, {
  155. progress: {
  156. kind: "creating",
  157. label: "Connecting to feedback service",
  158. detail: "Creating the ticket and preparing screenshot uploads",
  159. indeterminate: true,
  160. },
  161. processingLabel: "Processing",
  162. }),
  163. );
  164. expect(html).toContain("w-full");
  165. expect(html).toContain("Connecting to feedback service");
  166. expect(html).toContain("Creating the ticket and preparing screenshot uploads");
  167. expect(html).not.toContain("truncate");
  168. expect(html).not.toContain("min-w-[220px]");
  169. expect(html).not.toContain("max-w-[420px]");
  170. });
  171. it("只有存在后台附件上传时才在工单创建后收起提交窗口", () => {
  172. expect(shouldAutoCloseFeedbackOnTicketCreated({ includeLogs: true, screenshotCount: 0 })).toBe(
  173. true,
  174. );
  175. expect(shouldAutoCloseFeedbackOnTicketCreated({ includeLogs: false, screenshotCount: 1 })).toBe(
  176. true,
  177. );
  178. expect(shouldAutoCloseFeedbackOnTicketCreated({ includeLogs: false, screenshotCount: 0 })).toBe(
  179. false,
  180. );
  181. });
  182. it("提交 helper 会保留入口预设的反馈类型和日志开关", async () => {
  183. const feedbackService = createFeedbackService();
  184. vi.mocked(feedbackService.getDeviceSnapshot).mockResolvedValue({});
  185. vi.mocked(feedbackService.create).mockResolvedValue(createTicket("ticket-feature"));
  186. const completed = vi.fn();
  187. const ticketCreated = vi.fn();
  188. const job = await startSimplifiedFeedbackSubmission({
  189. feedbackService,
  190. title: "增加帮助菜单",
  191. description: " 我想建议:增加帮助菜单 \n",
  192. contact: "",
  193. screenshots: [],
  194. includeLogs: false,
  195. ticketType: "feature",
  196. ticketSeverity: "P3-低",
  197. ticketModule: "其它",
  198. modelContext: {},
  199. locale: "zh-CN",
  200. formatMessage: (descriptor: { id: string }) => descriptor.id,
  201. copy: {
  202. connectingLabel: "connecting",
  203. connectingDetail: "detail",
  204. cancelingCreateLabel: "canceling",
  205. cancelingCreateDetail: "canceling detail",
  206. canceledLabel: "canceled",
  207. canceledDetail: "canceled detail",
  208. uploadingScreenshotLabel: "uploading",
  209. submittedLabel: "submitted",
  210. submittedDetail: "submitted detail",
  211. failedLabel: "failed",
  212. networkErrorDetail: "network error",
  213. postCreateNetworkErrorDetail: "created but upload failed",
  214. pausingLogLabel: "paused",
  215. pausingLogDetail: "paused detail",
  216. exportingLogLabel: "exporting",
  217. exportingLogDetail: "exporting detail",
  218. uploadingLogLabel: "uploading log",
  219. logUploadSuccessLabel: "log done",
  220. logUploadPausedLabel: "log paused",
  221. logUploadPausedDetail: "log paused detail",
  222. preparingUploadDetail: "preparing",
  223. },
  224. onTicketCreated: ticketCreated,
  225. onCompleted: completed,
  226. onError: vi.fn(),
  227. });
  228. await job.done.finally(() => dismissFeedbackSubmissionJob(job.id));
  229. expect(feedbackService.create).toHaveBeenCalledWith(
  230. expect.objectContaining({
  231. title: "增加帮助菜单",
  232. type: "feature",
  233. severity: "P3-低",
  234. module: "其它",
  235. description: expect.stringContaining("反馈类型: feature"),
  236. }),
  237. expect.objectContaining({
  238. operationId: expect.stringMatching(/^feedback-create-/),
  239. }),
  240. );
  241. expect(feedbackService.prepareCompactLogArchive).not.toHaveBeenCalled();
  242. expect(ticketCreated).toHaveBeenCalledWith("ticket-feature");
  243. expect(completed).toHaveBeenCalledWith("ticket-feature");
  244. expect(job.formDraft).toEqual({
  245. title: "增加帮助菜单",
  246. description: " 我想建议:增加帮助菜单 \n",
  247. contact: "",
  248. screenshots: [],
  249. includeLogs: false,
  250. type: "feature",
  251. severity: "P3-低",
  252. module: "其它",
  253. });
  254. expect(Object.isFrozen(job.formDraft)).toBe(true);
  255. expect(Object.isFrozen(job.formDraft.screenshots)).toBe(true);
  256. });
  257. });