chatLoading.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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("animate-spin");
  24. expect(html).toContain('data-zcode-chat-loading-animate="true"');
  25. expect(html).toContain('role="status"');
  26. expect(html).toContain('aria-label="加载中..."');
  27. expect(html).not.toContain('data-testid="chat-loading-text"');
  28. expect(html).not.toContain("正在思考");
  29. });
  30. it.each([
  31. [
  32. "permission",
  33. {
  34. interactionId: "permission-1",
  35. kind: "permission",
  36. anchorRowId: 1,
  37. createdAt: 1,
  38. payload: {
  39. kind: "permission",
  40. toolCallId: "tool-1",
  41. toolName: "Bash",
  42. summary: "需要确认",
  43. detail: {},
  44. options: [],
  45. },
  46. },
  47. ],
  48. [
  49. "AskUserQuestion",
  50. {
  51. interactionId: "ask-1",
  52. kind: "userInput",
  53. anchorRowId: 2,
  54. createdAt: 2,
  55. payload: {
  56. kind: "userInput",
  57. prompt: "请选择",
  58. freeText: false,
  59. toolName: "AskUserQuestion",
  60. toolCallId: "tool-2",
  61. },
  62. },
  63. ],
  64. ] satisfies ReadonlyArray<readonly [string, PendingInteraction]>)(
  65. "%s pending interaction blocks ChatLoading",
  66. (_label, interaction) => {
  67. expect(hasChatLoadingBlockingInteraction([interaction])).toBe(true);
  68. },
  69. );
  70. // 软门禁(workspace-hook-trust-soft-gate.md D4)下 workspaceHookReview 不再阻塞
  71. // 聊天 loading——审核改为非阻塞的常驻提示条,用户可继续对话。
  72. it("does not treat workspaceHookReview as a ChatLoading blocker (soft gate)", () => {
  73. const interaction = {
  74. interactionId: "workspace-review-1",
  75. kind: "workspaceHookReview",
  76. anchorRowId: null,
  77. createdAt: 4,
  78. payload: { kind: "workspaceHookReview" },
  79. } as unknown as PendingInteraction;
  80. expect(hasChatLoadingBlockingInteraction([interaction])).toBe(false);
  81. });
  82. it("does not treat other userInput interactions as ChatLoading blockers", () => {
  83. const interaction: PendingInteraction = {
  84. interactionId: "plan-1",
  85. kind: "userInput",
  86. anchorRowId: 3,
  87. createdAt: 3,
  88. payload: {
  89. kind: "userInput",
  90. prompt: "批准计划",
  91. freeText: true,
  92. toolName: "ExitPlanMode",
  93. toolCallId: "tool-3",
  94. },
  95. };
  96. expect(hasChatLoadingBlockingInteraction([interaction])).toBe(false);
  97. });
  98. it.each([
  99. ["compact", true],
  100. ["goalVerifier", true],
  101. ["primaryTurn", false],
  102. ["foregroundSubagent", false],
  103. ["goalContinuation", false],
  104. ["turnSteer", false],
  105. ] satisfies ReadonlyArray<readonly [ActiveWorkSummary["kind"], boolean]>)(
  106. "%s active work blocker = %s",
  107. (kind, expected) => {
  108. expect(hasChatLoadingBlockingActiveWork([{ kind, startedAt: 1 }])).toBe(expected);
  109. },
  110. );
  111. });