workspaceFileDrag.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import type { MutableRefObject } from "react";
  2. import { afterEach, describe, expect, it, vi } from "vitest";
  3. import {
  4. createWorkspaceFileComposerMention,
  5. dispatchWorkspaceFileDragState,
  6. hasWorkspaceFileDragPayload,
  7. isWorkspaceFileDragStateEvent,
  8. parseWorkspaceFileDragPayload,
  9. serializeWorkspaceFileDragPayload,
  10. WORKSPACE_FILE_DRAG_STATE_EVENT,
  11. WORKSPACE_FILE_DRAG_MIME,
  12. } from "@/lib/workspaceFileDrag.js";
  13. import { appendWorkspaceFileMentionToComposer } from "@/lib/workspaceFileComposer.js";
  14. import type { LexicalChatInputHandle } from "@/LexicalChatInput.js";
  15. describe("workspace file drag payload", () => {
  16. afterEach(() => {
  17. vi.unstubAllGlobals();
  18. });
  19. it("round-trips valid file payloads", () => {
  20. const payload = {
  21. type: "file" as const,
  22. workspacePath: "/workspace",
  23. workspaceIdentity: "ssh://dev/workspace",
  24. path: "/workspace/src/index.ts",
  25. relativePath: "src/index.ts",
  26. name: "index.ts",
  27. };
  28. expect(parseWorkspaceFileDragPayload(serializeWorkspaceFileDragPayload(payload))).toEqual(
  29. payload,
  30. );
  31. });
  32. it("round-trips valid directory payloads", () => {
  33. const payload = {
  34. type: "directory" as const,
  35. workspacePath: "/workspace",
  36. path: "/workspace/src",
  37. relativePath: "src",
  38. name: "src",
  39. };
  40. expect(parseWorkspaceFileDragPayload(serializeWorkspaceFileDragPayload(payload))).toEqual(
  41. payload,
  42. );
  43. });
  44. it("rejects malformed payloads", () => {
  45. expect(parseWorkspaceFileDragPayload("not-json")).toBeNull();
  46. expect(parseWorkspaceFileDragPayload(JSON.stringify({ type: "directory" }))).toBeNull();
  47. expect(parseWorkspaceFileDragPayload(JSON.stringify({ type: "unknown" }))).toBeNull();
  48. });
  49. it("detects workspace file drags from dataTransfer types before payload data is readable", () => {
  50. const dataTransfer = {
  51. types: [WORKSPACE_FILE_DRAG_MIME],
  52. getData: () => "",
  53. } as unknown as DataTransfer;
  54. expect(hasWorkspaceFileDragPayload(dataTransfer)).toBe(true);
  55. });
  56. it("dispatches drag state changes for prompt editors before dragover", () => {
  57. const windowTarget = new EventTarget();
  58. vi.stubGlobal("window", windowTarget);
  59. const states: boolean[] = [];
  60. windowTarget.addEventListener(WORKSPACE_FILE_DRAG_STATE_EVENT, (event) => {
  61. if (isWorkspaceFileDragStateEvent(event)) {
  62. states.push(event.detail.dragging);
  63. }
  64. });
  65. dispatchWorkspaceFileDragState(true);
  66. dispatchWorkspaceFileDragState(false);
  67. expect(states).toEqual([true, false]);
  68. });
  69. it("uses relative mention links inside the same workspace", () => {
  70. expect(
  71. createWorkspaceFileComposerMention(
  72. {
  73. type: "file",
  74. workspacePath: "/workspace",
  75. path: "/workspace/src/index.ts",
  76. relativePath: "src/index.ts",
  77. name: "index.ts",
  78. },
  79. "/workspace",
  80. ),
  81. ).toMatchObject({
  82. markdown: "[index.ts](./src/index.ts)",
  83. value: "src/index.ts",
  84. data: {
  85. path: "/workspace/src/index.ts",
  86. relativePath: "src/index.ts",
  87. },
  88. });
  89. });
  90. it("uses absolute mention links across workspaces to avoid resolving against the active project", () => {
  91. expect(
  92. createWorkspaceFileComposerMention(
  93. {
  94. type: "file",
  95. workspacePath: "/other-workspace",
  96. path: "/other-workspace/src/index.ts",
  97. relativePath: "src/index.ts",
  98. name: "index.ts",
  99. },
  100. "/workspace",
  101. ),
  102. ).toMatchObject({
  103. markdown: "[index.ts](/other-workspace/src/index.ts)",
  104. value: "/other-workspace/src/index.ts",
  105. data: {
  106. path: "/other-workspace/src/index.ts",
  107. relativePath: "/other-workspace/src/index.ts",
  108. },
  109. });
  110. });
  111. it("appends dropped file mentions through the shared composer helper", () => {
  112. let nextText = "";
  113. const appendFileMention = vi.fn();
  114. const focus = vi.fn();
  115. const inputApiRef = {
  116. current: {
  117. appendFileMention,
  118. focus,
  119. },
  120. } as MutableRefObject<LexicalChatInputHandle | null>;
  121. const result = appendWorkspaceFileMentionToComposer({
  122. inputApiRef,
  123. currentMarkdown: "请检查",
  124. payload: {
  125. type: "file",
  126. workspacePath: "/workspace",
  127. path: "/workspace/src/index.ts",
  128. relativePath: "src/index.ts",
  129. name: "index.ts",
  130. },
  131. workspacePath: "/workspace",
  132. onTextChange: (text) => {
  133. nextText = text;
  134. },
  135. });
  136. expect(result).toBe("请检查 [index.ts](./src/index.ts) ");
  137. expect(nextText).toBe(result);
  138. expect(appendFileMention).toHaveBeenCalledWith(
  139. "index.ts",
  140. "src/index.ts",
  141. "[index.ts](./src/index.ts)",
  142. {
  143. kind: "file",
  144. path: "/workspace/src/index.ts",
  145. relativePath: "src/index.ts",
  146. },
  147. );
  148. expect(focus).toHaveBeenCalled();
  149. });
  150. });