chatLoading.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import type { ActiveWorkSummary, PendingInteraction } from "@zcode/shared/zcode-protocol-v4";
  5. import { ChatLoading } from "@/components/ai-elements/chat-loading.js";
  6. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  7. import {
  8. hasChatLoadingBlockingActiveWork,
  9. hasChatLoadingBlockingInteraction,
  10. } from "@/v4/chatLoadingVisibility.js";
  11. function renderChatLoading() {
  12. return renderToStaticMarkup(
  13. createElement(
  14. ZCodeIntlProvider,
  15. { initialLocale: "zh-CN" },
  16. createElement(ChatLoading, { loading: true }),
  17. ),
  18. );
  19. }
  20. describe("ChatLoading", () => {
  21. it("只展示旋转 loading 图标,不渲染可见文案", () => {
  22. const html = renderChatLoading();
  23. expect(html).toContain('role="status"');
  24. expect(html).toContain('aria-label="加载中..."');
  25. expect(html).not.toContain('data-testid="chat-loading-text"');
  26. expect(html).not.toContain("正在思考");
  27. });
  28. it.each([
  29. [
  30. "permission",
  31. {
  32. interactionId: "permission-1",
  33. kind: "permission",
  34. anchorRowId: 1,
  35. createdAt: 1,
  36. payload: {
  37. kind: "permission",
  38. toolCallId: "tool-1",
  39. toolName: "Bash",
  40. summary: "需要确认",
  41. detail: {},
  42. options: [],
  43. },
  44. },
  45. ],
  46. [
  47. "AskUserQuestion",
  48. {
  49. interactionId: "ask-1",
  50. kind: "userInput",
  51. anchorRowId: 2,
  52. createdAt: 2,
  53. payload: {
  54. kind: "userInput",
  55. prompt: "请选择",
  56. freeText: false,
  57. toolName: "AskUserQuestion",
  58. toolCallId: "tool-2",
  59. },
  60. },
  61. ],
  62. ] satisfies ReadonlyArray<readonly [string, PendingInteraction]>)(
  63. "%s pending interaction blocks ChatLoading",
  64. (_label, interaction) => {
  65. expect(hasChatLoadingBlockingInteraction([interaction])).toBe(true);
  66. },
  67. );
  68. // 软门禁(workspace-hook-trust-soft-gate.md D4)下 workspaceHookReview 不再阻塞
  69. // 聊天 loading——审核改为非阻塞的常驻提示条,用户可继续对话。
  70. it("does not treat workspaceHookReview as a ChatLoading blocker (soft gate)", () => {
  71. const interaction = {
  72. interactionId: "workspace-review-1",
  73. kind: "workspaceHookReview",
  74. anchorRowId: null,
  75. createdAt: 4,
  76. payload: { kind: "workspaceHookReview" },
  77. } as unknown as PendingInteraction;
  78. expect(hasChatLoadingBlockingInteraction([interaction])).toBe(false);
  79. });
  80. it("does not treat other userInput interactions as ChatLoading blockers", () => {
  81. const interaction: PendingInteraction = {
  82. interactionId: "plan-1",
  83. kind: "userInput",
  84. anchorRowId: 3,
  85. createdAt: 3,
  86. payload: {
  87. kind: "userInput",
  88. prompt: "批准计划",
  89. freeText: true,
  90. toolName: "ExitPlanMode",
  91. toolCallId: "tool-3",
  92. },
  93. };
  94. expect(hasChatLoadingBlockingInteraction([interaction])).toBe(false);
  95. });
  96. it.each([
  97. ["compact", true],
  98. ["goalVerifier", true],
  99. ["primaryTurn", false],
  100. ["foregroundSubagent", false],
  101. ["goalContinuation", false],
  102. ["turnSteer", false],
  103. ] satisfies ReadonlyArray<readonly [ActiveWorkSummary["kind"], boolean]>)(
  104. "%s active work blocker = %s",
  105. (kind, expected) => {
  106. expect(hasChatLoadingBlockingActiveWork([{ kind, startedAt: 1 }])).toBe(expected);
  107. },
  108. );
  109. });