Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2
3import {
4 COMPOSER_MENTION_DRAG_TYPE,
5 COMPOSER_PULL_REQUEST_DRAG_TYPE,
6 encodePullRequestDragPayload,
7 type ComposerMentionDropHost,
8 composerMentionFromTreePath,
9 dataTransferHasComposerMention,
10 makeComposerMentionDragHandlers,
11} from "./composerMentionDrag.ts";
12
13const makeDragEvent = (options?: {
14 mention?: string;
15 types?: ReadonlyArray<string>;
16 format?: string;
17}) => {
18 const mention = options?.mention ?? "[index.md](docs/index.md)";
19 const format = options?.format ?? COMPOSER_MENTION_DRAG_TYPE;
20 const calls: Array<string> = [];
21 const event = {
22 dataTransfer: {
23 types: options?.types ?? [format, "text/plain"],
24 getData: (requested: string) => (requested === format ? mention : ""),
25 dropEffect: "none",
26 },
27 nativeEvent: {
28 stopPropagation: () => void calls.push("nativeStopPropagation"),
29 },
30 preventDefault: () => void calls.push("preventDefault"),
31 stopPropagation: () => void calls.push("stopPropagation"),
32 };
33 return { event, calls };
34};
35
36const makeHost = (insertResult = true) => {
37 const log: Array<string> = [];
38 const host: ComposerMentionDropHost = {
39 insertMentionAtEnd: (text) => {
40 log.push(`insert:${text}`);
41 return insertResult;
42 },
43 insertPullRequestAtEnd: (pullRequest) => {
44 log.push(`pr:${pullRequest.number}`);
45 return insertResult;
46 },
47 setDragActive: (active) => void log.push(`active:${active}`),
48 onInsertRejected: () => void log.push("rejected"),
49 };
50 return { host, log };
51};
52
53describe("composerMentionFromTreePath", () => {
54 it("serializes a file path into a mention", () => {
55 expect(composerMentionFromTreePath("docs/index.md")).toBe("[index.md](docs/index.md)");
56 });
57
58 it("strips the trailing slash directory rows carry", () => {
59 expect(composerMentionFromTreePath("docs/architecture/")).toBe(
60 "[architecture](docs/architecture)",
61 );
62 });
63
64 it("rejects drags that carry no path", () => {
65 expect(composerMentionFromTreePath("")).toBeNull();
66 expect(composerMentionFromTreePath("/")).toBeNull();
67 });
68});
69
70describe("dataTransferHasComposerMention", () => {
71 it("detects the mention payload among drag types", () => {
72 expect(dataTransferHasComposerMention([COMPOSER_MENTION_DRAG_TYPE, "text/plain"])).toBe(true);
73 expect(dataTransferHasComposerMention(["Files"])).toBe(false);
74 expect(dataTransferHasComposerMention([])).toBe(false);
75 });
76});
77
78describe("makeComposerMentionDragHandlers", () => {
79 it("leaves drags without the mention payload alone", () => {
80 const { host, log } = makeHost();
81 const handlers = makeComposerMentionDragHandlers(host);
82 const { event, calls } = makeDragEvent({ types: ["Files"] });
83 handlers.onDragEnter(event);
84 handlers.onDragOver(event);
85 handlers.onDrop(event);
86 expect(calls).toEqual([]);
87 expect(log).toEqual([]);
88 });
89
90 it("stops the native event too, not just the synthetic one", () => {
91 // React's stopPropagation only halts synthetic dispatch; without the
92 // native stop, the editor's own DOM listeners process the drop and sync
93 // their stale state back over the inserted mention.
94 const { host } = makeHost();
95 const handlers = makeComposerMentionDragHandlers(host);
96 const { event, calls } = makeDragEvent();
97 handlers.onDrop(event);
98 expect(calls).toContain("preventDefault");
99 expect(calls).toContain("stopPropagation");
100 expect(calls).toContain("nativeStopPropagation");
101 });
102
103 it('answers dragover with the "move" effect the tree allows', () => {
104 // Naming an effect outside the source's effectAllowed makes the browser
105 // cancel the drop without ever firing it.
106 const { host } = makeHost();
107 const handlers = makeComposerMentionDragHandlers(host);
108 const { event } = makeDragEvent();
109 handlers.onDragOver(event);
110 expect(event.dataTransfer.dropEffect).toBe("move");
111 });
112
113 it("inserts the mention with its trailing space and clears the highlight", () => {
114 const { host, log } = makeHost();
115 const handlers = makeComposerMentionDragHandlers(host);
116 handlers.onDragEnter(makeDragEvent().event);
117 handlers.onDrop(makeDragEvent().event);
118 expect(log).toEqual(["active:true", "active:false", "insert:[index.md](docs/index.md) "]);
119 });
120
121 it("reports a rejected insert instead of failing silently", () => {
122 const { host, log } = makeHost(false);
123 const handlers = makeComposerMentionDragHandlers(host);
124 handlers.onDrop(makeDragEvent().event);
125 expect(log).toContain("rejected");
126 });
127
128 it("ignores a drop whose payload is empty", () => {
129 const { host, log } = makeHost();
130 const handlers = makeComposerMentionDragHandlers(host);
131 handlers.onDrop(makeDragEvent({ mention: "" }).event);
132 expect(log).toEqual(["active:false"]);
133 });
134
135 it("drops a pull request row as a pull request, not as text", () => {
136 const { host, log } = makeHost();
137 const handlers = makeComposerMentionDragHandlers(host);
138 const pullRequest = {
139 number: 18094,
140 title: "fix(server): queued runs land on the timeline where they started",
141 url: "https://github.com/pingdotgg/t3code/pull/18094",
142 headBranch: "fix/run-timeline-position",
143 baseBranch: "main",
144 state: "open",
145 isDraft: false,
146 } as const;
147 handlers.onDrop(
148 makeDragEvent({
149 format: COMPOSER_PULL_REQUEST_DRAG_TYPE,
150 mention: encodePullRequestDragPayload(pullRequest),
151 }).event,
152 );
153 expect(log).toEqual(["active:false", "pr:18094"]);
154 });
155
156 it("ignores a pull request payload that does not decode", () => {
157 const { host, log } = makeHost();
158 const handlers = makeComposerMentionDragHandlers(host);
159 handlers.onDrop(
160 makeDragEvent({ format: COMPOSER_PULL_REQUEST_DRAG_TYPE, mention: '{"number":0}' }).event,
161 );
162 expect(log).toEqual(["active:false"]);
163 });
164});