Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadTerminalDrawer.test.ts

145 lines4.4 KB

1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 shouldClearTerminalSelectionAction,
5 shouldHandleTerminalExit,
6 terminalContextMenuItems,
7 terminalSelectionLineRange,
8 terminalSelectionMenuItems,
9 terminalThemeFromApp,
10} from "./ThreadTerminalDrawer";
11​
12describe("terminal selection menus", () => {
13 it("omits Add to chat when the terminal has no chat target", () => {
14 expect(terminalSelectionMenuItems().map(({ id }) => id)).toEqual(["add-to-chat", "copy"]);
15 expect(terminalContextMenuItems({ hasSelection: true }).map(({ id }) => id)).toEqual([
16 "add-to-chat",
17 "copy",
18 "paste",
19 ]);
20​
21 expect(terminalSelectionMenuItems({ canAddToChat: false }).map(({ id }) => id)).toEqual([
22 "copy",
23 ]);
24 expect(
25 terminalContextMenuItems({ hasSelection: true, canAddToChat: false }).map(({ id }) => id),
26 ).toEqual(["copy", "paste"]);
27 });
28});
29​
30describe("terminalThemeFromApp", () => {
31 afterEach(() => {
32 vi.unstubAllGlobals();
33 });
34​
35 it("uses terminal colors inherited by the mount instead of a light document theme", () => {
36 const root = { classList: { contains: () => false } };
37 const body = {};
38 const drawer = {};
39 let canvasColor = "#000";
40 const colors: Record<string, [number, number, number, number]> = {
41 "#000": [0, 0, 0, 255],
42 "#fff": [255, 255, 255, 255],
43 "#ddd": [221, 221, 221, 255],
44 "#111": [17, 17, 17, 255],
45 };
46​
47 vi.stubGlobal("document", {
48 documentElement: root,
49 body,
50 querySelector: () => drawer,
51 createElement: () => ({
52 width: 0,
53 height: 0,
54 getContext: () => ({
55 clearRect: () => undefined,
56 fillRect: () => undefined,
57 get fillStyle() {
58 return canvasColor;
59 },
60 set fillStyle(value: string) {
61 canvasColor = value;
62 },
63 getImageData: () => ({ data: colors[canvasColor] ?? [0, 0, 0, 0] }),
64 }),
65 }),
66 });
67 vi.stubGlobal("getComputedStyle", (element: object) => {
68 const local = element === drawer;
69 const values = local
70 ? {
71 "--terminal-background": "#000",
72 "--terminal-foreground": "#fff",
73 "--terminal-cursor": "#ddd",
74 "--terminal-selection-background": "rgba(255, 255, 255, 0.2)",
75 }
76 : {
77 "--terminal-background": "#fff",
78 "--terminal-foreground": "#111",
79 };
80 return {
81 backgroundColor: local ? "#000" : "#fff",
82 color: local ? "#fff" : "#111",
83 colorScheme: local ? "dark" : "light",
84 getPropertyValue: (name: string) => values[name as keyof typeof values] ?? "",
85 };
86 });
87​
88 const theme = terminalThemeFromApp();
89​
90 expect(theme.background).toEqual({ r: 0, g: 0, b: 0 });
91 expect(theme.foreground).toEqual({ r: 255, g: 255, b: 255 });
92 expect(theme.cursor).toEqual({ r: 221, g: 221, b: 221 });
93 });
94});
95​
96describe("terminal selection actions", () => {
97 it("clears a pending or currently owned menu when the selection disappears", () => {
98 expect(
99 shouldClearTerminalSelectionAction({
100 actionPending: true,
101 openMenuRequestId: null,
102 currentRequestId: 4,
103 }),
104 ).toBe(true);
105 expect(
106 shouldClearTerminalSelectionAction({
107 actionPending: false,
108 openMenuRequestId: 4,
109 currentRequestId: 4,
110 }),
111 ).toBe(true);
112 });
113​
114 it("does not let an old selection popup cancel its replacement right-click menu", () => {
115 expect(
116 shouldClearTerminalSelectionAction({
117 actionPending: false,
118 openMenuRequestId: 3,
119 currentRequestId: 4,
120 }),
121 ).toBe(false);
122 expect(
123 shouldClearTerminalSelectionAction({
124 actionPending: false,
125 openMenuRequestId: null,
126 currentRequestId: 4,
127 }),
128 ).toBe(false);
129 });
130​
131 it("uses Ghostty's physical screen range for visually wrapped selections", () => {
132 expect(
133 terminalSelectionLineRange({
134 start: { y: 4 },
135 end: { y: 6 },
136 }),
137 ).toEqual({ lineStart: 5, lineEnd: 7 });
138 });
139​
140 it("handles an exit that lands while the terminal surface is still loading", () => {
141 expect(shouldHandleTerminalExit("exited", "running", false)).toBe(true);
142 expect(shouldHandleTerminalExit("exited", "exited", false)).toBe(false);
143 expect(shouldHandleTerminalExit("closed", "running", true)).toBe(false);
144 });
145});