Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import {
4 resolveTerminalSelectionActionPosition,
5 selectPendingTerminalEventEntries,
6 selectTerminalEventEntriesAfterSnapshot,
7 shouldHandleTerminalSelectionMouseUp,
8 terminalSelectionActionDelayForClickCount,
9} from "./ThreadTerminalDrawer";
10
11describe("resolveTerminalSelectionActionPosition", () => {
12 it("prefers the selection rect over the last pointer position", () => {
13 expect(
14 resolveTerminalSelectionActionPosition({
15 bounds: { left: 100, top: 50, width: 500, height: 220 },
16 selectionRect: { right: 260, bottom: 140 },
17 pointer: { x: 520, y: 200 },
18 viewport: { width: 1024, height: 768 },
19 }),
20 ).toEqual({
21 x: 260,
22 y: 144,
23 });
24 });
25
26 it("falls back to the pointer position when no selection rect is available", () => {
27 expect(
28 resolveTerminalSelectionActionPosition({
29 bounds: { left: 100, top: 50, width: 500, height: 220 },
30 selectionRect: null,
31 pointer: { x: 180, y: 130 },
32 viewport: { width: 1024, height: 768 },
33 }),
34 ).toEqual({
35 x: 180,
36 y: 130,
37 });
38 });
39
40 it("clamps the pointer fallback into the terminal drawer bounds", () => {
41 expect(
42 resolveTerminalSelectionActionPosition({
43 bounds: { left: 100, top: 50, width: 500, height: 220 },
44 selectionRect: null,
45 pointer: { x: 720, y: 340 },
46 viewport: { width: 1024, height: 768 },
47 }),
48 ).toEqual({
49 x: 600,
50 y: 270,
51 });
52
53 expect(
54 resolveTerminalSelectionActionPosition({
55 bounds: { left: 100, top: 50, width: 500, height: 220 },
56 selectionRect: null,
57 pointer: { x: 40, y: 20 },
58 viewport: { width: 1024, height: 768 },
59 }),
60 ).toEqual({
61 x: 100,
62 y: 50,
63 });
64 });
65
66 it("delays multi-click selection actions so triple-click selection can complete", () => {
67 expect(terminalSelectionActionDelayForClickCount(1)).toBe(0);
68 expect(terminalSelectionActionDelayForClickCount(2)).toBe(260);
69 expect(terminalSelectionActionDelayForClickCount(3)).toBe(260);
70 });
71
72 it("only handles mouseup when the selection gesture started in the terminal", () => {
73 expect(shouldHandleTerminalSelectionMouseUp(true, 0)).toBe(true);
74 expect(shouldHandleTerminalSelectionMouseUp(false, 0)).toBe(false);
75 expect(shouldHandleTerminalSelectionMouseUp(true, 1)).toBe(false);
76 });
77
78 it("replays only terminal events newer than the open snapshot", () => {
79 expect(
80 selectTerminalEventEntriesAfterSnapshot(
81 [
82 {
83 id: 1,
84 event: {
85 threadId: "thread-1",
86 terminalId: "default",
87 createdAt: "2026-04-02T20:00:00.000Z",
88 type: "output",
89 data: "before",
90 },
91 },
92 {
93 id: 2,
94 event: {
95 threadId: "thread-1",
96 terminalId: "default",
97 createdAt: "2026-04-02T20:00:01.000Z",
98 type: "output",
99 data: "after",
100 },
101 },
102 ],
103 "2026-04-02T20:00:00.500Z",
104 ).map((entry) => entry.id),
105 ).toEqual([2]);
106 });
107
108 it("applies only terminal events that have not already been consumed", () => {
109 expect(
110 selectPendingTerminalEventEntries(
111 [
112 {
113 id: 1,
114 event: {
115 threadId: "thread-1",
116 terminalId: "default",
117 createdAt: "2026-04-02T20:00:00.000Z",
118 type: "output",
119 data: "one",
120 },
121 },
122 {
123 id: 2,
124 event: {
125 threadId: "thread-1",
126 terminalId: "default",
127 createdAt: "2026-04-02T20:00:01.000Z",
128 type: "output",
129 data: "two",
130 },
131 },
132 ],
133 1,
134 ).map((entry) => entry.id),
135 ).toEqual([2]);
136 });
137});