fix: failing tests

This commit is contained in:
Tomasz Stefaniak 2025-10-07 15:53:25 -07:00
parent 9b6dd9a21e
commit 0858aa41b0
3 changed files with 40 additions and 24 deletions

View file

@ -53,7 +53,7 @@ export function EditMessageSelector({
setIsEditing(false);
// Reset text to original
const content =
typeof userMessages[selectedIndex].item.message.content === "string"
typeof userMessages[selectedIndex]?.item.message.content === "string"
? userMessages[selectedIndex].item.message.content
: "";
textBuffer.setText(content);
@ -79,22 +79,32 @@ export function EditMessageSelector({
} else {
// If not editing, handle navigation
if (key.upArrow || input === "k") {
setSelectedIndex((prev) =>
prev > 0 ? prev - 1 : userMessages.length - 1,
);
// Only navigate if there are messages
if (userMessages.length > 0) {
setSelectedIndex((prev) =>
prev > 0 ? prev - 1 : userMessages.length - 1,
);
}
} else if (key.downArrow || input === "j") {
setSelectedIndex((prev) =>
prev < userMessages.length - 1 ? prev + 1 : 0,
);
// Only navigate if there are messages
if (userMessages.length > 0) {
setSelectedIndex((prev) =>
prev < userMessages.length - 1 ? prev + 1 : 0,
);
}
} else if (key.return) {
// Start editing the selected message - set cursor to end
const content =
typeof userMessages[selectedIndex]?.item.message.content === "string"
? userMessages[selectedIndex].item.message.content
: "";
textBuffer.setCursor(content.length);
setCursorPosition(content.length);
setIsEditing(true);
// Only allow editing if there are messages
if (userMessages.length > 0) {
const content =
typeof userMessages[selectedIndex]?.item.message.content ===
"string"
? userMessages[selectedIndex].item.message.content
: "";
textBuffer.setCursor(content.length);
setCursorPosition(content.length);
setIsEditing(true);
}
} else if (key.escape || (key.ctrl && input === "d")) {
onExit();
}

View file

@ -1,7 +1,7 @@
/* eslint-disable max-lines */
import { type AssistantConfig } from "@continuedev/sdk";
import { Box, Text, useApp, useInput } from "ink";
import React, { useCallback, useState } from "react";
import React, { useCallback, useRef, useState } from "react";
import { getAllSlashCommands } from "../commands/commands.js";
import { useServices } from "../hooks/useService.js";
@ -130,7 +130,7 @@ const UserInput: React.FC<UserInputProps> = ({
}) => {
const [textBuffer] = useState(() => new TextBuffer());
const [inputHistory] = useState(() => new InputHistory());
const [lastEscapePress, setLastEscapePress] = useState<number>(0);
const lastEscapePressRef = useRef<number>(0);
// Stable callback for TextBuffer state changes to prevent race conditions
const onStateChange = useCallback(() => {
@ -636,16 +636,16 @@ const UserInput: React.FC<UserInputProps> = ({
!showFileSearch &&
!showBashMode &&
onShowEditSelector &&
now - lastEscapePress < 500
now - lastEscapePressRef.current < 500
) {
// Double escape detected
onShowEditSelector();
setLastEscapePress(0); // Reset to prevent triple-escape
lastEscapePressRef.current = 0; // Reset to prevent triple-escape
return true;
}
// Track single escape press
setLastEscapePress(now);
lastEscapePressRef.current = now;
return false;
};

View file

@ -117,16 +117,22 @@ describe("TUIChat - Message Edit Feature", () => {
const { lastFrame, stdin } = renderInMode(mode);
// Perform various operations
stdin.write("\u001b\u001b"); // Open edit
await new Promise((resolve) => setTimeout(resolve, 50));
stdin.write("\u001b");
stdin.write("\u001b"); // Open edit with separate Esc presses
await new Promise((resolve) => setTimeout(resolve, 100));
stdin.write("k"); // Navigate
// Verify selector opened
let frame = lastFrame();
expect(frame).toBeDefined();
expect(frame).toContain("No user messages to edit");
stdin.write("k"); // Navigate (no-op with 0 messages)
await new Promise((resolve) => setTimeout(resolve, 50));
stdin.write("\u001b"); // Close
await new Promise((resolve) => setTimeout(resolve, 50));
await new Promise((resolve) => setTimeout(resolve, 100));
const frame = lastFrame();
frame = lastFrame();
// UI should remain stable and edit selector should be closed
expect(frame).toBeDefined();