|
| 1 | +import type { PrReviewComment } from "@posthog/shared"; |
| 2 | +import { Theme } from "@radix-ui/themes"; |
| 3 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 4 | +import userEvent from "@testing-library/user-event"; |
| 5 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import type { PrCommentMetadata } from "../types"; |
| 7 | +import { PrCommentThread } from "./PrCommentThread"; |
| 8 | + |
| 9 | +const { reply, resolve, sendPromptToAgent } = vi.hoisted(() => ({ |
| 10 | + reply: vi.fn(), |
| 11 | + resolve: vi.fn(), |
| 12 | + sendPromptToAgent: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../hooks/usePrCommentActions", () => ({ |
| 16 | + usePrCommentActions: () => ({ reply, resolve }), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("../../sessions/sendPromptToAgent", () => ({ sendPromptToAgent })); |
| 20 | + |
| 21 | +function makeComment(): PrReviewComment { |
| 22 | + return { |
| 23 | + id: 42, |
| 24 | + body: "Could this use the shared helper?", |
| 25 | + created_at: "2026-07-14T12:00:00Z", |
| 26 | + user: { |
| 27 | + login: "reviewer", |
| 28 | + avatar_url: "", |
| 29 | + }, |
| 30 | + } as PrReviewComment; |
| 31 | +} |
| 32 | + |
| 33 | +function makeMetadata(): PrCommentMetadata { |
| 34 | + return { |
| 35 | + kind: "pr-comment", |
| 36 | + threadId: 42, |
| 37 | + nodeId: "thread-node", |
| 38 | + isResolved: false, |
| 39 | + comments: [makeComment()], |
| 40 | + isOutdated: false, |
| 41 | + isFileLevel: false, |
| 42 | + startLine: 8, |
| 43 | + endLine: 8, |
| 44 | + side: "additions", |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +function renderThread() { |
| 49 | + return render( |
| 50 | + <Theme> |
| 51 | + <PrCommentThread |
| 52 | + taskId="task-1" |
| 53 | + prUrl="https://github.com/PostHog/posthog/pull/1" |
| 54 | + filePath="src/example.ts" |
| 55 | + metadata={makeMetadata()} |
| 56 | + /> |
| 57 | + </Theme>, |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +describe("PrCommentThread", () => { |
| 62 | + beforeEach(() => { |
| 63 | + vi.clearAllMocks(); |
| 64 | + reply.mockResolvedValue(true); |
| 65 | + resolve.mockResolvedValue(true); |
| 66 | + sendPromptToAgent.mockResolvedValue(true); |
| 67 | + }); |
| 68 | + |
| 69 | + it("sends a custom chat message with the review context", async () => { |
| 70 | + const user = userEvent.setup(); |
| 71 | + renderThread(); |
| 72 | + |
| 73 | + await user.click(screen.getByRole("button", { name: "Chat" })); |
| 74 | + await user.type( |
| 75 | + screen.getByPlaceholderText("Ask the agent about this comment..."), |
| 76 | + "Check whether this helper already exists", |
| 77 | + ); |
| 78 | + await user.click(screen.getByRole("button", { name: "Send" })); |
| 79 | + |
| 80 | + expect(sendPromptToAgent).toHaveBeenCalledWith( |
| 81 | + "task-1", |
| 82 | + expect.stringContaining("Could this use the shared helper?"), |
| 83 | + ); |
| 84 | + expect(sendPromptToAgent).toHaveBeenCalledWith( |
| 85 | + "task-1", |
| 86 | + expect.stringContaining("Check whether this helper already exists"), |
| 87 | + ); |
| 88 | + await waitFor(() => |
| 89 | + expect( |
| 90 | + screen.queryByPlaceholderText("Ask the agent about this comment..."), |
| 91 | + ).not.toBeInTheDocument(), |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it("keeps the custom message available when sending fails", async () => { |
| 96 | + sendPromptToAgent.mockResolvedValue(false); |
| 97 | + const user = userEvent.setup(); |
| 98 | + renderThread(); |
| 99 | + |
| 100 | + await user.click(screen.getByRole("button", { name: "Chat" })); |
| 101 | + const textarea = screen.getByPlaceholderText( |
| 102 | + "Ask the agent about this comment...", |
| 103 | + ); |
| 104 | + await user.type(textarea, "Keep this draft"); |
| 105 | + await user.click(screen.getByRole("button", { name: "Send" })); |
| 106 | + |
| 107 | + await waitFor(() => |
| 108 | + expect( |
| 109 | + screen.getByPlaceholderText("Ask the agent about this comment..."), |
| 110 | + ).toHaveValue("Keep this draft"), |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it("keeps a reply composer opened while a chat message is sending", async () => { |
| 115 | + let resolveSend: ((success: boolean) => void) | undefined; |
| 116 | + sendPromptToAgent.mockReturnValue( |
| 117 | + new Promise<boolean>((resolve) => { |
| 118 | + resolveSend = resolve; |
| 119 | + }), |
| 120 | + ); |
| 121 | + const user = userEvent.setup(); |
| 122 | + renderThread(); |
| 123 | + |
| 124 | + await user.click(screen.getByRole("button", { name: "Chat" })); |
| 125 | + await user.type( |
| 126 | + screen.getByPlaceholderText("Ask the agent about this comment..."), |
| 127 | + "Check this", |
| 128 | + ); |
| 129 | + await user.click(screen.getByRole("button", { name: "Send" })); |
| 130 | + await user.click(screen.getByRole("button", { name: "Close composer" })); |
| 131 | + await user.click(screen.getByRole("button", { name: "Reply" })); |
| 132 | + await user.type(screen.getByPlaceholderText("Write a reply..."), "Keep me"); |
| 133 | + |
| 134 | + resolveSend?.(true); |
| 135 | + |
| 136 | + await waitFor(() => |
| 137 | + expect(screen.getByPlaceholderText("Write a reply...")).toHaveValue( |
| 138 | + "Keep me", |
| 139 | + ), |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it("still posts replies without sending them to chat", async () => { |
| 144 | + const user = userEvent.setup(); |
| 145 | + renderThread(); |
| 146 | + |
| 147 | + await user.click(screen.getByRole("button", { name: "Reply" })); |
| 148 | + await user.type(screen.getByPlaceholderText("Write a reply..."), "Done"); |
| 149 | + await user.click(screen.getByRole("button", { name: "Reply" })); |
| 150 | + |
| 151 | + expect(reply).toHaveBeenCalledWith(42, "Done"); |
| 152 | + expect(sendPromptToAgent).not.toHaveBeenCalled(); |
| 153 | + }); |
| 154 | +}); |
0 commit comments