From 1fbfd06b7b2ed02e43f91b7584381eb1129a65c0 Mon Sep 17 00:00:00 2001 From: Nate Date: Sun, 27 Jul 2025 12:45:03 -0700 Subject: [PATCH] new test --- .../__tests__/TUIChat.toolPermission.test.tsx | 200 ++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 src/ui/__tests__/TUIChat.toolPermission.test.tsx diff --git a/src/ui/__tests__/TUIChat.toolPermission.test.tsx b/src/ui/__tests__/TUIChat.toolPermission.test.tsx new file mode 100644 index 000000000..0251be67e --- /dev/null +++ b/src/ui/__tests__/TUIChat.toolPermission.test.tsx @@ -0,0 +1,200 @@ +import { render } from "ink-testing-library"; +import React from "react"; +import { jest } from "@jest/globals"; +import { ToolPermissionSelector } from "../components/ToolPermissionSelector.js"; + +describe("TUIChat - Tool Permission Tests", () => { + it("shows ToolPermissionSelector with correct content", async () => { + const handleResponse = jest.fn(); + + const { lastFrame } = render( + + ); + + await new Promise((resolve) => setTimeout(resolve, 50)); + + const frame = lastFrame(); + + // Verify ToolPermissionSelector is shown + expect(frame).toContain("Edit"); + expect(frame).toContain("Would you like to continue?"); + expect(frame).toContain("Continue"); + expect(frame).toContain("Cancel"); + expect(frame).toContain("(tab)"); + expect(frame).toContain("(esc)"); + }); + + it("handles keyboard shortcuts for permission approval and rejection", async () => { + const handleResponse = jest.fn(); + + const { stdin } = render( + + ); + + await new Promise((resolve) => setTimeout(resolve, 50)); + + // Test Tab key for approval + stdin.write("\t"); + + await new Promise((resolve) => setTimeout(resolve, 50)); + + expect(handleResponse).toHaveBeenCalledWith("test-request-123", true); + }); + + it("handles escape key for rejection", async () => { + const handleResponse = jest.fn(); + + const { stdin } = render( + + ); + + await new Promise((resolve) => setTimeout(resolve, 50)); + + // Test escape key for rejection + stdin.write("\x1b"); // ESC key + + await new Promise((resolve) => setTimeout(resolve, 50)); + + expect(handleResponse).toHaveBeenCalledWith("test-request-456", false); + }); + + it("handles 'n' key for rejection", async () => { + const handleResponse = jest.fn(); + + const { stdin } = render( + + ); + + await new Promise((resolve) => setTimeout(resolve, 50)); + + // Test 'n' key for rejection + stdin.write("n"); + + await new Promise((resolve) => setTimeout(resolve, 50)); + + expect(handleResponse).toHaveBeenCalledWith("test-request-789", false); + }); + + it("handles arrow key navigation in permission selector", async () => { + const handleResponse = jest.fn(); + + const { lastFrame, stdin } = render( + + ); + + await new Promise((resolve) => setTimeout(resolve, 50)); + + // Initial state should show "> Continue" selected + let frame = lastFrame(); + expect(frame).toMatch(/>\s+Continue/); + + // Press down arrow to select "Cancel" + stdin.write("\x1B[B"); // Down arrow + + await new Promise((resolve) => setTimeout(resolve, 50)); + + frame = lastFrame(); + expect(frame).toMatch(/>\s+Cancel/); + + // Press up arrow to go back to "Continue" + stdin.write("\x1B[A"); // Up arrow + + await new Promise((resolve) => setTimeout(resolve, 50)); + + frame = lastFrame(); + expect(frame).toMatch(/>\s+Continue/); + + // Press Enter to confirm selection + stdin.write("\r"); + + await new Promise((resolve) => setTimeout(resolve, 50)); + + expect(handleResponse).toHaveBeenCalledWith("test-request-123", true); + }); + + it("shows tool result with red dot and 'Cancelled by user' message", async () => { + // Import the components we need for this test + const { default: ToolResultSummary } = await import("../ToolResultSummary.js"); + const { MemoizedMessage } = await import("../components/MemoizedMessage.js"); + + // Test the ToolResultSummary component directly + const { lastFrame: summaryFrame } = render( + + ); + + const summary = summaryFrame(); + expect(summary).toContain("Cancelled by user"); + + // Test the MemoizedMessage component with a tool-result message + const message = { + role: "assistant", + content: "Edit", + messageType: "tool-result" as const, + toolName: "Edit", + toolResult: "Permission denied by user", + }; + + const { lastFrame: messageFrame } = render( + + ); + + await new Promise((resolve) => setTimeout(resolve, 100)); + + const messageOutput = messageFrame(); + + // Verify the red dot appears + expect(messageOutput).toContain("●"); + + // Verify the tool name appears + expect(messageOutput).toContain("Edit"); + + // The MemoizedMessage component correctly identifies this as a failure + // because message.toolResult includes "Permission denied" + // This causes the red dot to be shown + + // Verify the correct data is passed to the component + expect(message.toolResult).toBe("Permission denied by user"); + expect(message.messageType).toBe("tool-result"); + }); +}); \ No newline at end of file