feedbackSubmitForm.test.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. readCurrentAgentModelContext,
  7. shouldAutoCloseFeedbackOnTicketCreated,
  8. } from "@/feedback/FeedbackSubmitForm.js";
  9. import { startSimplifiedFeedbackSubmission } from "@/feedback/feedbackSubmitSubmission.js";
  10. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  11. import type { IFeedbackService } from "@zcode/services";
  12. import type { FeedbackTicketDetail, FeedbackTicketSummary, IPlatformService } from "@zcode/shared";
  13. import { TabStoreProvider } from "@/store/TabStoreProvider.js";
  14. import { dismissFeedbackSubmissionJob } from "@/feedback/feedbackSubmissionJob.js";
  15. function createFeedbackService(): IFeedbackService {
  16. return {
  17. getDeviceSnapshot: vi.fn(),
  18. create: vi.fn(),
  19. list: vi.fn(),
  20. get: vi.fn(),
  21. comment: vi.fn(),
  22. uploadAttachmentData: vi.fn(),
  23. uploadAttachmentWithProgress: vi.fn(),
  24. prepareCompactLogArchive: vi.fn(),
  25. cleanupPreparedLogArchive: vi.fn(),
  26. onDynamicUploadProgress: vi.fn(),
  27. cancelCreate: vi.fn(),
  28. cancelUpload: vi.fn(),
  29. onDynamicUpdate: vi.fn(),
  30. } as unknown as IFeedbackService;
  31. }
  32. function createTicket(id: string): FeedbackTicketDetail {
  33. const summary: FeedbackTicketSummary = {
  34. id,
  35. title: "功能建议",
  36. type: "feature",
  37. status: "已提交",
  38. created_at: "2026-06-17T10:00:00.000Z",
  39. updated_at: "2026-06-17T10:00:00.000Z",
  40. };
  41. return {
  42. ...summary,
  43. description: "desc",
  44. attachments: [],
  45. comments: [],
  46. events: [],
  47. };
  48. }
  49. function renderFeedbackSubmitForm(locale: "zh-CN" | "en-US" = "zh-CN") {
  50. return renderToStaticMarkup(
  51. createElement(
  52. ZCodeIntlProvider,
  53. { initialLocale: locale },
  54. createElement(
  55. TabStoreProvider,
  56. null,
  57. createElement(FeedbackSubmitForm, {
  58. feedbackService: createFeedbackService(),
  59. platform: {} as IPlatformService,
  60. onSubmitted: vi.fn(),
  61. onViewTickets: vi.fn(),
  62. onCancel: vi.fn(),
  63. }),
  64. ),
  65. ),
  66. );
  67. }
  68. describe("FeedbackSubmitForm", () => {
  69. it.each([
  70. ["zh-CN", "问题描述"],
  71. ["en-US", "Issue description"],
  72. ] as const)("%s 问题描述明确声明必填并提供可访问名称", (locale, label) => {
  73. const html = renderFeedbackSubmitForm(locale);
  74. expect(html).toMatch(/<span[^>]*aria-hidden="true"[^>]*>\*<\/span>/);
  75. const textarea = html.match(/<textarea\b[^>]*>/)?.[0];
  76. expect(textarea).toContain('required=""');
  77. expect(textarea).toContain('aria-required="true"');
  78. expect(textarea).toContain(`aria-label="${label}"`);
  79. });
  80. it("uses English copy when the app locale is English", () => {
  81. const html = renderFeedbackSubmitForm("en-US");
  82. expect(html).toContain("Issue description");
  83. expect(html).toContain("Screenshots");
  84. expect(html).not.toContain(">Logs</h3>");
  85. expect(html).toContain("Upload diagnostic logs");
  86. expect(html).toContain("On by default");
  87. expect(html).toContain("safe projection of CLI configuration");
  88. expect(html).toContain("model-call traces");
  89. expect(html).not.toContain("问题描述");
  90. expect(html).not.toContain("反馈类型");
  91. });
  92. it("从当前模型配置读取选中的模型展示名", () => {
  93. const selectedModel = "315c04c4-1234-5678-9abc-000000000000/claude-opus-4-7";
  94. const context = readCurrentAgentModelContext([
  95. {
  96. id: "model",
  97. name: "模型",
  98. category: "model",
  99. type: "select",
  100. currentValue: selectedModel,
  101. options: [
  102. { value: "another-model", name: "b2" },
  103. { value: selectedModel, name: "a1" },
  104. ],
  105. },
  106. ]);
  107. expect(context).toEqual({
  108. model: selectedModel,
  109. display: "a1",
  110. optionCount: 2,
  111. optionsPreview: ["b2", "a1"],
  112. });
  113. });
  114. it("不再展示旧提交弹窗里的分类字段,但保留简化日志开关", () => {
  115. const html = renderFeedbackSubmitForm();
  116. expect(html).toContain("问题描述");
  117. expect(html).toContain("截图");
  118. expect(html).toContain("联系方式");
  119. expect(html).toContain("日志");
  120. expect(html).toContain("上传诊断日志");
  121. expect(html).toContain('aria-checked="true"');
  122. expect(html).toContain("默认开启");
  123. expect(html).toContain("CLI 配置中的安全投影");
  124. expect(html).toContain("当天模型调用轨迹");
  125. expect(html).not.toContain("Issue description");
  126. expect(html).not.toContain("Screenshots");
  127. expect(html).not.toContain("反馈类型");
  128. expect(html).not.toContain("功能模块");
  129. expect(html).not.toContain("影响程度");
  130. expect(html).not.toContain("当前模型型号");
  131. expect(html).not.toContain("附带完整日志");
  132. });
  133. it("截图附件区域保持可聚焦,方便用户定位截图上传入口", () => {
  134. const html = renderFeedbackSubmitForm();
  135. expect(html).toContain("粘贴、拖拽图片到这里,或选择文件。");
  136. expect(html).toContain('tabindex="0"');
  137. });
  138. it("只有存在后台附件上传时才在工单创建后收起提交窗口", () => {
  139. expect(shouldAutoCloseFeedbackOnTicketCreated({ includeLogs: true, screenshotCount: 0 })).toBe(
  140. true,
  141. );
  142. expect(shouldAutoCloseFeedbackOnTicketCreated({ includeLogs: false, screenshotCount: 1 })).toBe(
  143. true,
  144. );
  145. expect(shouldAutoCloseFeedbackOnTicketCreated({ includeLogs: false, screenshotCount: 0 })).toBe(
  146. false,
  147. );
  148. });
  149. it("提交 helper 会保留入口预设的反馈类型和日志开关", async () => {
  150. const feedbackService = createFeedbackService();
  151. vi.mocked(feedbackService.getDeviceSnapshot).mockResolvedValue({});
  152. vi.mocked(feedbackService.create).mockResolvedValue(createTicket("ticket-feature"));
  153. const completed = vi.fn();
  154. const ticketCreated = vi.fn();
  155. const job = await startSimplifiedFeedbackSubmission({
  156. feedbackService,
  157. title: "增加帮助菜单",
  158. description: " 我想建议:增加帮助菜单 \n",
  159. contact: "",
  160. screenshots: [],
  161. includeLogs: false,
  162. ticketType: "feature",
  163. ticketSeverity: "P3-低",
  164. ticketModule: "其它",
  165. modelContext: {},
  166. locale: "zh-CN",
  167. formatMessage: (descriptor: { id: string }) => descriptor.id,
  168. copy: {
  169. connectingLabel: "connecting",
  170. connectingDetail: "detail",
  171. cancelingCreateLabel: "canceling",
  172. cancelingCreateDetail: "canceling detail",
  173. canceledLabel: "canceled",
  174. canceledDetail: "canceled detail",
  175. uploadingScreenshotLabel: "uploading",
  176. submittedLabel: "submitted",
  177. submittedDetail: "submitted detail",
  178. failedLabel: "failed",
  179. networkErrorDetail: "network error",
  180. postCreateNetworkErrorDetail: "created but upload failed",
  181. pausingLogLabel: "paused",
  182. pausingLogDetail: "paused detail",
  183. exportingLogLabel: "exporting",
  184. exportingLogDetail: "exporting detail",
  185. uploadingLogLabel: "uploading log",
  186. logUploadSuccessLabel: "log done",
  187. logUploadPausedLabel: "log paused",
  188. logUploadPausedDetail: "log paused detail",
  189. preparingUploadDetail: "preparing",
  190. },
  191. onTicketCreated: ticketCreated,
  192. onCompleted: completed,
  193. onError: vi.fn(),
  194. });
  195. await job.done.finally(() => dismissFeedbackSubmissionJob(job.id));
  196. expect(feedbackService.create).toHaveBeenCalledWith(
  197. expect.objectContaining({
  198. title: "增加帮助菜单",
  199. type: "feature",
  200. severity: "P3-低",
  201. module: "其它",
  202. description: expect.stringContaining("反馈类型: feature"),
  203. }),
  204. expect.objectContaining({
  205. operationId: expect.stringMatching(/^feedback-create-/),
  206. }),
  207. );
  208. expect(feedbackService.prepareCompactLogArchive).not.toHaveBeenCalled();
  209. expect(ticketCreated).toHaveBeenCalledWith("ticket-feature");
  210. expect(completed).toHaveBeenCalledWith("ticket-feature");
  211. expect(job.formDraft).toEqual({
  212. title: "增加帮助菜单",
  213. description: " 我想建议:增加帮助菜单 \n",
  214. contact: "",
  215. screenshots: [],
  216. includeLogs: false,
  217. type: "feature",
  218. severity: "P3-低",
  219. module: "其它",
  220. });
  221. expect(Object.isFrozen(job.formDraft)).toBe(true);
  222. expect(Object.isFrozen(job.formDraft.screenshots)).toBe(true);
  223. });
  224. });