| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- import type { MutableRefObject } from "react";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- createWorkspaceFileComposerMention,
- dispatchWorkspaceFileDragState,
- hasWorkspaceFileDragPayload,
- isWorkspaceFileDragStateEvent,
- parseWorkspaceFileDragPayload,
- serializeWorkspaceFileDragPayload,
- WORKSPACE_FILE_DRAG_STATE_EVENT,
- WORKSPACE_FILE_DRAG_MIME,
- } from "@/lib/workspaceFileDrag.js";
- import { appendWorkspaceFileMentionToComposer } from "@/lib/workspaceFileComposer.js";
- import type { LexicalChatInputHandle } from "@/LexicalChatInput.js";
- describe("workspace file drag payload", () => {
- afterEach(() => {
- vi.unstubAllGlobals();
- });
- it("round-trips valid file payloads", () => {
- const payload = {
- type: "file" as const,
- workspacePath: "/workspace",
- workspaceIdentity: "ssh://dev/workspace",
- path: "/workspace/src/index.ts",
- relativePath: "src/index.ts",
- name: "index.ts",
- };
- expect(parseWorkspaceFileDragPayload(serializeWorkspaceFileDragPayload(payload))).toEqual(
- payload,
- );
- });
- it("round-trips valid directory payloads", () => {
- const payload = {
- type: "directory" as const,
- workspacePath: "/workspace",
- path: "/workspace/src",
- relativePath: "src",
- name: "src",
- };
- expect(parseWorkspaceFileDragPayload(serializeWorkspaceFileDragPayload(payload))).toEqual(
- payload,
- );
- });
- it("rejects malformed payloads", () => {
- expect(parseWorkspaceFileDragPayload("not-json")).toBeNull();
- expect(parseWorkspaceFileDragPayload(JSON.stringify({ type: "directory" }))).toBeNull();
- expect(parseWorkspaceFileDragPayload(JSON.stringify({ type: "unknown" }))).toBeNull();
- });
- it("detects workspace file drags from dataTransfer types before payload data is readable", () => {
- const dataTransfer = {
- types: [WORKSPACE_FILE_DRAG_MIME],
- getData: () => "",
- } as unknown as DataTransfer;
- expect(hasWorkspaceFileDragPayload(dataTransfer)).toBe(true);
- });
- it("dispatches drag state changes for prompt editors before dragover", () => {
- const windowTarget = new EventTarget();
- vi.stubGlobal("window", windowTarget);
- const states: boolean[] = [];
- windowTarget.addEventListener(WORKSPACE_FILE_DRAG_STATE_EVENT, (event) => {
- if (isWorkspaceFileDragStateEvent(event)) {
- states.push(event.detail.dragging);
- }
- });
- dispatchWorkspaceFileDragState(true);
- dispatchWorkspaceFileDragState(false);
- expect(states).toEqual([true, false]);
- });
- it("uses relative mention links inside the same workspace", () => {
- expect(
- createWorkspaceFileComposerMention(
- {
- type: "file",
- workspacePath: "/workspace",
- path: "/workspace/src/index.ts",
- relativePath: "src/index.ts",
- name: "index.ts",
- },
- "/workspace",
- ),
- ).toMatchObject({
- markdown: "[index.ts](./src/index.ts)",
- value: "src/index.ts",
- data: {
- path: "/workspace/src/index.ts",
- relativePath: "src/index.ts",
- },
- });
- });
- it("uses absolute mention links across workspaces to avoid resolving against the active project", () => {
- expect(
- createWorkspaceFileComposerMention(
- {
- type: "file",
- workspacePath: "/other-workspace",
- path: "/other-workspace/src/index.ts",
- relativePath: "src/index.ts",
- name: "index.ts",
- },
- "/workspace",
- ),
- ).toMatchObject({
- markdown: "[index.ts](/other-workspace/src/index.ts)",
- value: "/other-workspace/src/index.ts",
- data: {
- path: "/other-workspace/src/index.ts",
- relativePath: "/other-workspace/src/index.ts",
- },
- });
- });
- it("appends dropped file mentions through the shared composer helper", () => {
- let nextText = "";
- const appendFileMention = vi.fn();
- const focus = vi.fn();
- const inputApiRef = {
- current: {
- appendFileMention,
- focus,
- },
- } as MutableRefObject<LexicalChatInputHandle | null>;
- const result = appendWorkspaceFileMentionToComposer({
- inputApiRef,
- currentMarkdown: "请检查",
- payload: {
- type: "file",
- workspacePath: "/workspace",
- path: "/workspace/src/index.ts",
- relativePath: "src/index.ts",
- name: "index.ts",
- },
- workspacePath: "/workspace",
- onTextChange: (text) => {
- nextText = text;
- },
- });
- expect(result).toBe("请检查 [index.ts](./src/index.ts) ");
- expect(nextText).toBe(result);
- expect(appendFileMention).toHaveBeenCalledWith(
- "index.ts",
- "src/index.ts",
- "[index.ts](./src/index.ts)",
- {
- kind: "file",
- path: "/workspace/src/index.ts",
- relativePath: "src/index.ts",
- },
- );
- expect(focus).toHaveBeenCalled();
- });
- });
|