test: ✅ Add tests for appearance of Accept All / Reject All buttons + ability to mock IDE messages to webview

This commit is contained in:
Nate 2025-05-11 16:21:15 -07:00
parent 8b418165d9
commit 5658619c81
4 changed files with 90 additions and 3 deletions

View file

@ -40,7 +40,10 @@ export default function AcceptRejectAllButtons({
}
return (
<div className="flex flex-row items-center justify-evenly gap-2 p-1 px-3">
<div
className="flex flex-row items-center justify-evenly gap-2 p-1 px-3"
data-testid="accept-reject-all-buttons"
>
<button
className="flex cursor-pointer flex-row flex-wrap justify-center gap-1 border-none bg-transparent px-2 py-1 text-xs text-gray-300 opacity-80 hover:opacity-100 hover:brightness-125"
onClick={() => handleAcceptOrReject("rejectDiff")}

View file

@ -1,9 +1,11 @@
import { IDE, PromptLog } from "core";
import {
FromIdeProtocol,
FromWebviewProtocol,
ToCoreProtocol,
ToWebviewProtocol,
} from "core/protocol";
import { Message } from "core/protocol/messenger";
import { MessageIde } from "core/protocol/messenger/messageIde";
import {
GeneratorReturnType,
@ -60,13 +62,48 @@ async function defaultMockHandleMessage<T extends keyof FromWebviewProtocol>(
export class MockIdeMessenger implements IIdeMessenger {
ide: IDE;
private messageHandlers: Map<
keyof FromIdeProtocol,
Array<(data: any) => void>
> = new Map();
constructor() {
this.ide = new MessageIde(
(messageType, data) => {
throw new Error("Not implemented");
},
(messageType, callback) => {},
(messageType, callback) => {
// Store the callback in our handlers map
if (!this.messageHandlers.has(messageType)) {
this.messageHandlers.set(messageType, []);
}
this.messageHandlers.get(messageType)?.push(callback);
},
);
}
/**
* Simulates a message being sent from the IDE to the webview
* @param messageType The type of message to send
* @param data The data to send with the message
*/
mockMessageToWebview<T extends keyof ToWebviewProtocol>(
messageType: T,
data: ToWebviewProtocol[T][0],
): void {
// Create a message object that matches what the useWebviewListener hook expects
const messageData: Message<ToWebviewProtocol[T][0]> = {
messageType,
data,
messageId: `mock-${Date.now()}-${Math.random().toString(36).substring(2)}`,
};
// Dispatch a custom message event that the window event listener will pick up
window.dispatchEvent(
new MessageEvent("message", {
data: messageData,
origin: window.location.origin,
}),
);
}
@ -80,7 +117,6 @@ export class MockIdeMessenger implements IIdeMessenger {
content: "This is a test",
},
];
return undefined;
}

View file

@ -59,4 +59,51 @@ describe("Chat page test", () => {
});
expect(await screen.findByText("Hello, world!")).toBeInTheDocument();
});
it("should handle apply updates and display the accept / reject all buttons", async () => {
const { ideMessenger } = await renderWithProviders(<Chat />);
// Use queryByText which returns null when element isn't found
const acceptAllButton = screen.queryByText("Accept All");
const rejectAllButton = screen.queryByText("Reject All");
// Assert that the buttons don't exist
expect(acceptAllButton).not.toBeInTheDocument();
expect(rejectAllButton).not.toBeInTheDocument();
for (let i = 0; i < 5; i++) {
ideMessenger.mockMessageToWebview("updateApplyState", {
status: "streaming",
streamId: `12345`,
});
}
ideMessenger.mockMessageToWebview("updateApplyState", {
status: "done",
streamId: "12345",
});
// Wait for the buttons to appear
await waitFor(() => {
expect(
screen.getByTestId("accept-reject-all-buttons"),
).toBeInTheDocument();
});
// IDE sends back message that it is done
ideMessenger.mockMessageToWebview("updateApplyState", {
status: "closed",
streamId: "12345",
});
// Wait for the buttons to disappear
await waitFor(() => {
expect(
screen.queryByTestId("edit-accept-button"),
).not.toBeInTheDocument();
expect(
screen.queryByTestId("edit-reject-button"),
).not.toBeInTheDocument();
});
});
});

View file

@ -72,6 +72,7 @@ export async function renderWithProviders(
return {
user,
store,
ideMessenger,
...rendered!,
};
}