|
1 | 1 | import type { PermissionOption } from "@agentclientprotocol/sdk"; |
2 | 2 | import { useSettingsStore } from "@posthog/ui/features/settings/settingsStore"; |
3 | 3 | import { Theme } from "@radix-ui/themes"; |
4 | | -import { render, screen } from "@testing-library/react"; |
| 4 | +import { act, render, screen, waitFor } from "@testing-library/react"; |
5 | 5 | import userEvent from "@testing-library/user-event"; |
6 | 6 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
7 | 7 | import { PlanApprovalSelector } from "./PlanApprovalSelector"; |
@@ -92,6 +92,91 @@ describe("PlanApprovalSelector", () => { |
92 | 92 | expect(useSettingsStore.getState().lastPlanApprovalMode).toBe("auto"); |
93 | 93 | }); |
94 | 94 |
|
| 95 | + it("picks up a remembered mode that loads after mount", async () => { |
| 96 | + // Settings persist asynchronously (an IPC round trip on desktop), so the |
| 97 | + // selector can mount before `lastPlanApprovalMode` has loaded from disk. |
| 98 | + const user = userEvent.setup(); |
| 99 | + const { onSelect } = renderSelector([AUTO, ACCEPT_EDITS, DEFAULT_MODE]); |
| 100 | + |
| 101 | + // The remembered choice loads in after mount. |
| 102 | + act(() => { |
| 103 | + useSettingsStore.setState({ lastPlanApprovalMode: "acceptEdits" }); |
| 104 | + }); |
| 105 | + |
| 106 | + await user.click(screen.getByText("Approve and proceed")); |
| 107 | + |
| 108 | + expect(onSelect).toHaveBeenCalledWith("acceptEdits"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("does not clobber a mode the user already picked", async () => { |
| 112 | + const user = userEvent.setup(); |
| 113 | + const { onSelect } = renderSelector([AUTO, ACCEPT_EDITS, DEFAULT_MODE]); |
| 114 | + |
| 115 | + await user.click(screen.getByRole("button", { name: "Mode" })); |
| 116 | + await user.click(await screen.findByText("Manually approve edits")); |
| 117 | + // The dropdown applies the pick when its close animation completes, not |
| 118 | + // synchronously with the click, so wait for it to actually land before |
| 119 | + // moving on — otherwise this race decides the test's outcome. |
| 120 | + await waitFor(() => |
| 121 | + expect(screen.getByRole("button", { name: "Mode" })).toHaveTextContent( |
| 122 | + "Manually approve edits", |
| 123 | + ), |
| 124 | + ); |
| 125 | + |
| 126 | + // The remembered choice loads in after the user already picked a mode. |
| 127 | + act(() => { |
| 128 | + useSettingsStore.setState({ lastPlanApprovalMode: "acceptEdits" }); |
| 129 | + }); |
| 130 | + |
| 131 | + await user.click(screen.getByText("Approve and proceed")); |
| 132 | + |
| 133 | + expect(onSelect).toHaveBeenCalledWith("default"); |
| 134 | + }); |
| 135 | + |
| 136 | + it("drops a manual pick when a later approval request reuses this instance", async () => { |
| 137 | + const user = userEvent.setup(); |
| 138 | + const onSelect = vi.fn(); |
| 139 | + const options = [AUTO, ACCEPT_EDITS, DEFAULT_MODE]; |
| 140 | + const { rerender } = render( |
| 141 | + <Theme> |
| 142 | + <PlanApprovalSelector |
| 143 | + toolCall={toolCall} |
| 144 | + options={options} |
| 145 | + onSelect={onSelect} |
| 146 | + onCancel={vi.fn()} |
| 147 | + /> |
| 148 | + </Theme>, |
| 149 | + ); |
| 150 | + |
| 151 | + await user.click(screen.getByRole("button", { name: "Mode" })); |
| 152 | + await user.click(await screen.findByText("Manually approve edits")); |
| 153 | + // The dropdown applies the pick when its close animation completes, not |
| 154 | + // synchronously with the click — wait for it to land before rerendering, |
| 155 | + // or the pending pick can apply after (and survive) the reset below. |
| 156 | + await waitFor(() => |
| 157 | + expect(screen.getByRole("button", { name: "Mode" })).toHaveTextContent( |
| 158 | + "Manually approve edits", |
| 159 | + ), |
| 160 | + ); |
| 161 | + |
| 162 | + // The component isn't guaranteed to unmount between requests; a new |
| 163 | + // toolCallId means a new request even if this instance is reused. |
| 164 | + rerender( |
| 165 | + <Theme> |
| 166 | + <PlanApprovalSelector |
| 167 | + toolCall={{ ...toolCall, toolCallId: "plan-2" } as PermissionToolCall} |
| 168 | + options={options} |
| 169 | + onSelect={onSelect} |
| 170 | + onCancel={vi.fn()} |
| 171 | + /> |
| 172 | + </Theme>, |
| 173 | + ); |
| 174 | + |
| 175 | + await user.click(screen.getByText("Approve and proceed")); |
| 176 | + |
| 177 | + expect(onSelect).toHaveBeenCalledWith("auto"); |
| 178 | + }); |
| 179 | + |
95 | 180 | it("rejects with the typed feedback", async () => { |
96 | 181 | const user = userEvent.setup(); |
97 | 182 | const { onSelect } = renderSelector([DEFAULT_MODE, REJECT]); |
|
0 commit comments