nandi/oripublic Fork 0
00224b167266e7d496672adfe1e60fcffeab7e63
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

PromptInput.test.tsx · 224 lines · 6.0 KBTypeScript Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { act, fireEvent, render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { PromptInput } from "./PromptInput";

const { searchFilesMock, listSkillsMock } = vi.hoisted(() => ({
	searchFilesMock: vi.fn(),
	listSkillsMock: vi.fn(),
}));

vi.mock("../api", () => ({
	searchFiles: searchFilesMock,
	listSkills: listSkillsMock,
}));

function type(textarea: HTMLTextAreaElement, value: string) {
	fireEvent.change(textarea, { target: { value } });
}

async function settle() {
	// The file search is debounced (120 ms); flush timers and promises.
	await act(async () => {
		await new Promise((resolve) => setTimeout(resolve, 150));
	});
}

describe("PromptInput", () => {
	beforeEach(() => {
		searchFilesMock.mockReset();
		listSkillsMock.mockReset();
		searchFilesMock.mockResolvedValue({
			root: "/w",
			files: [
				{ name: "main.go", path: "/w/src/main.go", relPath: "src/main.go" },
				{
					name: "main_test.go",
					path: "/w/src/main_test.go",
					relPath: "src/main_test.go",
				},
			],
		});
		listSkillsMock.mockResolvedValue({
			skills: [
				{
					name: "quality",
					description: "Measure code quality",
					path: "/p",
					source: "project",
				},
			],
		});
	});

	it("sends the trimmed text on Enter and clears the field", () => {
		const onSend = vi.fn();
		render(
			<PromptInput
				turnActive={false}
				commands={[]}
				onSend={onSend}
				onCancel={() => {}}
			/>,
		);
		const input = screen.getByLabelText("prompt") as HTMLTextAreaElement;

		type(input, "  hello  ");
		fireEvent.keyDown(input, { key: "Enter" });
		expect(onSend).toHaveBeenCalledWith("hello", []);
		expect(input.value).toBe("");
	});

	it("opens the file popup on @, navigates with arrows and inserts the pick as an attachment", async () => {
		const onSend = vi.fn();
		render(
			<PromptInput
				turnActive={false}
				commands={[]}
				onSend={onSend}
				onCancel={() => {}}
			/>,
		);
		const input = screen.getByLabelText("prompt") as HTMLTextAreaElement;

		type(input, "look at @ma");
		await settle();
		expect(searchFilesMock).toHaveBeenCalledWith("ma", 30);
		const options = screen.getAllByRole("option");
		expect(options.map((o) => o.textContent)).toEqual([
			"src/main.go",
			"src/main_test.go",
		]);
		expect(options[0].getAttribute("aria-selected")).toBe("true");

		fireEvent.keyDown(input, { key: "ArrowDown" });
		expect(screen.getAllByRole("option")[1].getAttribute("aria-selected")).toBe(
			"true",
		);
		fireEvent.keyDown(input, { key: "ArrowUp" });
		expect(screen.getAllByRole("option")[0].getAttribute("aria-selected")).toBe(
			"true",
		);

		// Enter picks instead of sending while the popup is open.
		fireEvent.keyDown(input, { key: "Enter" });
		expect(onSend).not.toHaveBeenCalled();
		expect(input.value).toBe("look at @src/main.go ");
		expect(screen.queryByRole("listbox")).toBeNull();

		type(input, "look at @src/main.go please");
		fireEvent.keyDown(input, { key: "Enter" });
		expect(onSend).toHaveBeenCalledWith("look at @src/main.go please", [
			{ path: "/w/src/main.go", name: "src/main.go" },
		]);
	});

	it("drops an attachment whose mention was deleted before sending", async () => {
		const onSend = vi.fn();
		render(
			<PromptInput
				turnActive={false}
				commands={[]}
				onSend={onSend}
				onCancel={() => {}}
			/>,
		);
		const input = screen.getByLabelText("prompt") as HTMLTextAreaElement;

		type(input, "@ma");
		await settle();
		fireEvent.keyDown(input, { key: "Tab" });
		expect(input.value).toBe("@src/main.go ");

		type(input, "no mention any more");
		fireEvent.keyDown(input, { key: "Enter" });
		expect(onSend).toHaveBeenCalledWith("no mention any more", []);
	});

	it("closes the popup with Escape and does not reopen for the same trigger", async () => {
		render(
			<PromptInput
				turnActive={false}
				commands={[]}
				onSend={() => {}}
				onCancel={() => {}}
			/>,
		);
		const input = screen.getByLabelText("prompt") as HTMLTextAreaElement;

		type(input, "@ma");
		await settle();
		expect(screen.getByRole("listbox")).toBeDefined();
		fireEvent.keyDown(input, { key: "Escape" });
		expect(screen.queryByRole("listbox")).toBeNull();

		type(input, "@mai");
		await settle();
		expect(screen.queryByRole("listbox")).toBeNull();
	});

	it("does not open the popup for an @ inside a word", async () => {
		render(
			<PromptInput
				turnActive={false}
				commands={[]}
				onSend={() => {}}
				onCancel={() => {}}
			/>,
		);
		type(screen.getByLabelText("prompt") as HTMLTextAreaElement, "me@example");
		await settle();
		expect(searchFilesMock).not.toHaveBeenCalled();
		expect(screen.queryByRole("listbox")).toBeNull();
	});

	it("opens the skill popup on a leading slash, merging skills and agent commands", async () => {
		const onSend = vi.fn();
		render(
			<PromptInput
				turnActive={false}
				commands={[{ name: "compact", description: "Summarise the thread" }]}
				onSend={onSend}
				onCancel={() => {}}
			/>,
		);
		const input = screen.getByLabelText("prompt") as HTMLTextAreaElement;

		type(input, "/");
		await settle();
		const options = screen.getAllByRole("option");
		expect(
			options.map((o) => o.querySelector(".completion-label")?.textContent),
		).toEqual(["compact", "quality"]);
		expect(options[0].querySelector(".completion-source")?.textContent).toBe(
			"command",
		);
		expect(options[1].querySelector(".completion-source")?.textContent).toBe(
			"skill",
		);
		expect(screen.getByText("Measure code quality")).toBeDefined();

		type(input, "/qua");
		expect(screen.getAllByRole("option")).toHaveLength(1);

		fireEvent.mouseDown(screen.getByRole("option"));
		expect(input.value).toBe("/quality ");

		type(input, "/quality src");
		fireEvent.keyDown(input, { key: "Enter" });
		expect(onSend).toHaveBeenCalledWith("/quality src", []);
	});

	it("shows Stop instead of Send while a turn runs", () => {
		const onCancel = vi.fn();
		render(
			<PromptInput
				turnActive={true}
				commands={[]}
				onSend={() => {}}
				onCancel={onCancel}
			/>,
		);
		fireEvent.click(screen.getByRole("button", { name: "Stop" }));
		expect(onCancel).toHaveBeenCalled();
	});
});