|
| 1 | +import type { ExperimentId } from "@roo-code/types" |
| 2 | +import { EXPERIMENT_IDS } from "@roo/experiments" |
| 3 | +import { act, fireEvent, screen, waitFor } from "@testing-library/react" |
| 4 | +import { QueryClient } from "@tanstack/react-query" |
| 5 | +import { beforeEach, describe, expect, it, vi } from "vitest" |
| 6 | + |
| 7 | +import { renderWithExtensionState } from "@/utils/test-utils" |
| 8 | +import { useExtensionState } from "@src/context/ExtensionStateContext" |
| 9 | +import { vscode } from "@src/utils/vscode" |
| 10 | + |
| 11 | +import SettingsView from "../SettingsView" |
| 12 | + |
| 13 | +vi.mock("@src/utils/vscode", () => ({ vscode: { postMessage: vi.fn() } })) |
| 14 | +vi.mock("@/utils/TelemetryClient", () => ({ telemetryClient: { capture: vi.fn() } })) |
| 15 | + |
| 16 | +const postMessage = vi.mocked(vscode.postMessage) |
| 17 | + |
| 18 | +const posted = (type: string) => |
| 19 | + postMessage.mock.calls |
| 20 | + .map((call) => call[0] as { type: string; updatedSettings?: { experiments?: Record<string, boolean> } }) |
| 21 | + .filter((message) => message.type === type) |
| 22 | + |
| 23 | +type HostApi = { |
| 24 | + setExperimentEnabled: (id: ExperimentId, enabled: boolean) => void |
| 25 | + setCurrentApiConfigName: (name: string) => void |
| 26 | +} |
| 27 | + |
| 28 | +let hostApi: HostApi | undefined |
| 29 | + |
| 30 | +function HostProbe() { |
| 31 | + const state = useExtensionState() |
| 32 | + hostApi = { |
| 33 | + setExperimentEnabled: state.setExperimentEnabled, |
| 34 | + setCurrentApiConfigName: state.setCurrentApiConfigName, |
| 35 | + } |
| 36 | + return null |
| 37 | +} |
| 38 | + |
| 39 | +const IMAGE_LABEL = "settings:experimental.IMAGE_GENERATION.name" |
| 40 | +const FOCUS_LABEL = "settings:experimental.PREVENT_FOCUS_DISRUPTION.name" |
| 41 | +const IMAGE_ID = EXPERIMENT_IDS.IMAGE_GENERATION as ExperimentId |
| 42 | +const FOCUS_ID = EXPERIMENT_IDS.PREVENT_FOCUS_DISRUPTION as ExperimentId |
| 43 | + |
| 44 | +const renderExperimentalTab = () => |
| 45 | + renderWithExtensionState( |
| 46 | + <> |
| 47 | + <HostProbe /> |
| 48 | + <SettingsView onDone={vi.fn()} targetSection="experimental" /> |
| 49 | + </>, |
| 50 | + { |
| 51 | + queryClient: new QueryClient({ defaultOptions: { queries: { retry: false } } }), |
| 52 | + state: { experiments: { [IMAGE_ID]: false } }, |
| 53 | + }, |
| 54 | + ) |
| 55 | + |
| 56 | +/** The checkbox of the setting whose own label carries the given translation key. */ |
| 57 | +const settingCheckbox = (labelKey: string) => |
| 58 | + Array.from(document.querySelectorAll<HTMLInputElement>("input[type=checkbox]")).find((element) => { |
| 59 | + const label = element.closest("label") |
| 60 | + return !!label && String(label.textContent || "").includes(labelKey) |
| 61 | + }) |
| 62 | + |
| 63 | +const requireCheckbox = (labelKey: string) => { |
| 64 | + const checkbox = settingCheckbox(labelKey) |
| 65 | + if (!checkbox) { |
| 66 | + throw new Error("no checkbox rendered for " + labelKey) |
| 67 | + } |
| 68 | + return checkbox |
| 69 | +} |
| 70 | + |
| 71 | +const toggleExperiment = async (labelKey: string) => { |
| 72 | + await act(async () => { |
| 73 | + fireEvent.click(requireCheckbox(labelKey)) |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +const clickSave = async () => { |
| 78 | + await waitFor(() => expect(screen.getByTestId("save-button")).toBeEnabled()) |
| 79 | + fireEvent.click(screen.getByTestId("save-button")) |
| 80 | +} |
| 81 | + |
| 82 | +const openExperimentalTab = async () => { |
| 83 | + renderExperimentalTab() |
| 84 | + await waitFor(() => expect(screen.getByTestId("tab-experimental")).toBeInTheDocument()) |
| 85 | + fireEvent.click(screen.getByTestId("tab-experimental")) |
| 86 | + await waitFor(() => expect(settingCheckbox(IMAGE_LABEL)).toBeTruthy()) |
| 87 | +} |
| 88 | + |
| 89 | +describe("SettingsView - experiment edits after the host confirms them", () => { |
| 90 | + beforeEach(() => { |
| 91 | + postMessage.mockClear() |
| 92 | + hostApi = undefined |
| 93 | + }) |
| 94 | + |
| 95 | + it("does not write a confirmed experiment back over a newer value from elsewhere", async () => { |
| 96 | + await openExperimentalTab() |
| 97 | + |
| 98 | + // This view edits one experiment and saves it. |
| 99 | + await toggleExperiment(IMAGE_LABEL) |
| 100 | + await clickSave() |
| 101 | + const firstSave = posted("updateSettings").at(-1) |
| 102 | + expect(firstSave?.updatedSettings?.experiments).toEqual({ [IMAGE_ID]: true }) |
| 103 | + |
| 104 | + // The host confirms that value, and afterwards a different webview turns the same |
| 105 | + // experiment off while this view is no longer editing it. |
| 106 | + await act(async () => { |
| 107 | + hostApi?.setExperimentEnabled(IMAGE_ID, true) |
| 108 | + }) |
| 109 | + await act(async () => { |
| 110 | + hostApi?.setExperimentEnabled(IMAGE_ID, false) |
| 111 | + }) |
| 112 | + |
| 113 | + // An unrelated save must not resurrect this view's copy of that experiment. |
| 114 | + await toggleExperiment(FOCUS_LABEL) |
| 115 | + await clickSave() |
| 116 | + const secondExperiments = posted("updateSettings").at(-1)?.updatedSettings?.experiments ?? {} |
| 117 | + expect(secondExperiments).not.toHaveProperty(String(IMAGE_ID)) |
| 118 | + expect(secondExperiments).toEqual({ [FOCUS_ID]: false }) |
| 119 | + }, 20000) |
| 120 | + |
| 121 | + it("keeps an experiment intent the host has not confirmed yet", async () => { |
| 122 | + await openExperimentalTab() |
| 123 | + |
| 124 | + // Save posts the intent while the host state still reports the old value, so neither the |
| 125 | + // submitted baseline nor the local copy may be dropped before that value arrives. |
| 126 | + await toggleExperiment(IMAGE_LABEL) |
| 127 | + await clickSave() |
| 128 | + const firstSave = posted("updateSettings").at(-1) |
| 129 | + expect(firstSave?.updatedSettings?.experiments).toEqual({ [IMAGE_ID]: true }) |
| 130 | + expect(requireCheckbox(IMAGE_LABEL).checked).toBe(true) |
| 131 | + |
| 132 | + await toggleExperiment(FOCUS_LABEL) |
| 133 | + await clickSave() |
| 134 | + const secondExperiments = posted("updateSettings").at(-1)?.updatedSettings?.experiments ?? {} |
| 135 | + expect(secondExperiments).toEqual({ [FOCUS_ID]: false }) |
| 136 | + expect(requireCheckbox(IMAGE_LABEL).checked).toBe(true) |
| 137 | + }, 20000) |
| 138 | + |
| 139 | + it("does not carry a pending experiment edit into another profile's save", async () => { |
| 140 | + await openExperimentalTab() |
| 141 | + |
| 142 | + // An experiment is edited but never saved, and then the settings view switches to a |
| 143 | + // different api configuration, which resets the edit session. |
| 144 | + await toggleExperiment(IMAGE_LABEL) |
| 145 | + await waitFor(() => expect(screen.getByTestId("save-button")).toBeEnabled()) |
| 146 | + await act(async () => { |
| 147 | + hostApi?.setCurrentApiConfigName("another-profile") |
| 148 | + }) |
| 149 | + await waitFor(() => expect(screen.getByTestId("save-button")).toBeDisabled()) |
| 150 | + |
| 151 | + // The next save belongs to the new session and posts only its own edit. |
| 152 | + await toggleExperiment(FOCUS_LABEL) |
| 153 | + await clickSave() |
| 154 | + const experiments = posted("updateSettings").at(-1)?.updatedSettings?.experiments ?? {} |
| 155 | + expect(experiments).not.toHaveProperty(String(IMAGE_ID)) |
| 156 | + expect(experiments).toEqual({ [FOCUS_ID]: false }) |
| 157 | + }, 20000) |
| 158 | + |
| 159 | + it("re-adopts a saved experiment when the host answer arrives after a profile change", async () => { |
| 160 | + await openExperimentalTab() |
| 161 | + |
| 162 | + // The save is posted, but a profile snapshot arrives before the host answer reaches this |
| 163 | + // view, so the cache reverts to the live value and the session resets. |
| 164 | + await toggleExperiment(IMAGE_LABEL) |
| 165 | + await clickSave() |
| 166 | + const firstSave = posted("updateSettings").at(-1) |
| 167 | + expect(firstSave?.updatedSettings?.experiments).toEqual({ [IMAGE_ID]: true }) |
| 168 | + await act(async () => { |
| 169 | + hostApi?.setCurrentApiConfigName("another-profile") |
| 170 | + }) |
| 171 | + await waitFor(() => expect(screen.getByTestId("save-button")).toBeDisabled()) |
| 172 | + expect(requireCheckbox(IMAGE_LABEL).checked).toBe(false) |
| 173 | + |
| 174 | + // The answer to the earlier save then arrives: the persisted value is the saved one, so |
| 175 | + // the view has to show it and must not post the reverted value back on a later save. |
| 176 | + await act(async () => { |
| 177 | + hostApi?.setExperimentEnabled(IMAGE_ID, true) |
| 178 | + }) |
| 179 | + await waitFor(() => expect(requireCheckbox(IMAGE_LABEL).checked).toBe(true)) |
| 180 | + await toggleExperiment(FOCUS_LABEL) |
| 181 | + await clickSave() |
| 182 | + const experiments = posted("updateSettings").at(-1)?.updatedSettings?.experiments ?? {} |
| 183 | + expect(experiments).not.toHaveProperty(String(IMAGE_ID)) |
| 184 | + expect(experiments).toEqual({ [FOCUS_ID]: false }) |
| 185 | + }, 20000) |
| 186 | +}) |
0 commit comments