codeBlockVisibilitySubscription.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { act, createElement, type ReactNode } from "react";
  2. import { createRoot, type Root } from "react-dom/client";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. vi.mock("@/components/ui/code-viewer.js", () => ({
  5. CodeViewer: () => createElement("pre", { "data-code-viewer": "" }),
  6. }));
  7. vi.mock("@/components/ai-elements/mermaid-block.js", () => ({
  8. MermaidBlock: () => createElement("div", { "data-mermaid-block": "" }),
  9. }));
  10. vi.mock("@/components/ai-elements/diagram-preview-dialog.js", () => ({
  11. DiagramPreviewDialog: () => null,
  12. }));
  13. vi.mock("@/ControlHintTooltip.js", () => ({
  14. ControlHintTooltip: ({ children }: { children?: ReactNode }) => children,
  15. }));
  16. vi.mock("@/components/ui/button.js", () => ({
  17. Button: ({ children, ...props }: { children?: ReactNode }) =>
  18. createElement("button", props, children),
  19. }));
  20. vi.mock("@/lib/fileDisplay.js", () => ({
  21. FileDisplayIcon: () => createElement("span", { "data-file-icon": "" }),
  22. resolveFileDisplayDescriptor: () => ({ fileIconSrc: "file.svg" }),
  23. }));
  24. vi.mock("@/i18n/IntlProvider.js", () => ({
  25. useZCodeIntl: () => ({
  26. intl: {
  27. formatMessage: ({ id }: { id: string }) => id,
  28. },
  29. }),
  30. }));
  31. vi.mock("@/logger.js", () => ({
  32. logger: {
  33. debug: vi.fn(),
  34. },
  35. }));
  36. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  37. const element = {
  38. addEventListener: () => {},
  39. appendChild: (child: { parentNode?: unknown }) => {
  40. child.parentNode = element;
  41. return child;
  42. },
  43. childNodes: [] as unknown[],
  44. getAttribute: () => null,
  45. insertBefore: (child: { parentNode?: unknown }) => {
  46. child.parentNode = element;
  47. return child;
  48. },
  49. nodeName: tagName.toUpperCase(),
  50. nodeType: 1,
  51. ownerDocument,
  52. parentNode: null as unknown,
  53. removeAttribute: () => {},
  54. removeChild: (child: { parentNode?: unknown }) => {
  55. child.parentNode = null;
  56. return child;
  57. },
  58. removeEventListener: () => {},
  59. setAttribute: () => {},
  60. style: {},
  61. tagName: tagName.toUpperCase(),
  62. };
  63. return element as unknown as Element;
  64. }
  65. function installMinimalDom() {
  66. const addEventListener = vi.fn();
  67. const removeEventListener = vi.fn();
  68. const documentMock = {
  69. addEventListener,
  70. createElement: (tagName: string) =>
  71. createMinimalElement(documentMock as unknown as Document, tagName),
  72. createElementNS: (_namespace: string, tagName: string) =>
  73. createMinimalElement(documentMock as unknown as Document, tagName),
  74. createTextNode: (nodeValue: string) => ({
  75. nodeType: 3,
  76. nodeValue,
  77. ownerDocument: documentMock,
  78. parentNode: null,
  79. }),
  80. documentElement: {
  81. classList: {
  82. contains: () => false,
  83. },
  84. },
  85. hidden: false,
  86. nodeType: 9,
  87. removeEventListener,
  88. visibilityState: "visible",
  89. } as unknown as Document;
  90. const windowMock = {
  91. addEventListener: () => {},
  92. document: documentMock,
  93. HTMLIFrameElement: function HTMLIFrameElement() {},
  94. HTMLElement: function HTMLElement() {},
  95. Node: function Node() {},
  96. removeEventListener: () => {},
  97. };
  98. Object.defineProperty(globalThis, "document", {
  99. configurable: true,
  100. value: documentMock,
  101. });
  102. Object.defineProperty(globalThis, "window", {
  103. configurable: true,
  104. value: windowMock,
  105. });
  106. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  107. configurable: true,
  108. value: true,
  109. });
  110. return {
  111. container: createMinimalElement(documentMock),
  112. addEventListener,
  113. removeEventListener,
  114. };
  115. }
  116. describe("CodeBlock visibility subscription", () => {
  117. afterEach(() => {
  118. delete (globalThis as { document?: unknown }).document;
  119. delete (globalThis as { window?: unknown }).window;
  120. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
  121. });
  122. it("does not subscribe non-mermaid code blocks to document visibility changes", async () => {
  123. const { CodeBlock } = await import("@/components/ai-elements/code-block.js");
  124. const { container, addEventListener } = installMinimalDom();
  125. const root: Root = createRoot(container);
  126. act(() => {
  127. root.render(
  128. createElement(CodeBlock, {
  129. code: "const answer = 42;",
  130. language: "ts",
  131. }),
  132. );
  133. });
  134. expect(
  135. addEventListener.mock.calls.filter(([eventName]) => eventName === "visibilitychange"),
  136. ).toHaveLength(0);
  137. act(() => {
  138. root.unmount();
  139. });
  140. });
  141. it("subscribes mermaid code blocks to document visibility changes", async () => {
  142. const { CodeBlock } = await import("@/components/ai-elements/code-block.js");
  143. const { container, addEventListener } = installMinimalDom();
  144. const root: Root = createRoot(container);
  145. act(() => {
  146. root.render(
  147. createElement(CodeBlock, {
  148. code: "graph TD\nA-->B",
  149. language: "mermaid",
  150. }),
  151. );
  152. });
  153. expect(
  154. addEventListener.mock.calls.filter(([eventName]) => eventName === "visibilitychange"),
  155. ).toHaveLength(1);
  156. act(() => {
  157. root.unmount();
  158. });
  159. });
  160. });