|
1 | | -import { createElement } from "react"; |
| 1 | +// @vitest-environment jsdom |
| 2 | +import { act, createElement } from "react"; |
2 | 3 | import { renderToStaticMarkup } from "react-dom/server"; |
3 | | -import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 4 | +import { createRoot, type Root } from "react-dom/client"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
4 | 6 |
|
5 | 7 | const stageArtworkState = vi.hoisted(() => ({ |
6 | 8 | mode: "none" as "artwork" | "none", |
@@ -107,3 +109,76 @@ describe("ComposerPrimaryActions", () => { |
107 | 109 | expect(markup).not.toContain("stage-nightly"); |
108 | 110 | }); |
109 | 111 | }); |
| 112 | + |
| 113 | +describe("ComposerPrimaryActions stop pending state", () => { |
| 114 | + function runningProps(canInterrupt: boolean) { |
| 115 | + return { |
| 116 | + compact: true, |
| 117 | + canOperateThread: true, |
| 118 | + pendingAction: null, |
| 119 | + isRunning: canInterrupt, |
| 120 | + canInterrupt, |
| 121 | + canResume: !canInterrupt, |
| 122 | + showPlanFollowUpPrompt: false, |
| 123 | + promptHasText: false, |
| 124 | + isSendBusy: false, |
| 125 | + sendDisabledReason: null, |
| 126 | + isConnecting: false, |
| 127 | + isEnvironmentUnavailable: false, |
| 128 | + isPreparingWorktree: false, |
| 129 | + hasSendableContent: false, |
| 130 | + onPreviousPendingQuestion: () => {}, |
| 131 | + onInterrupt: () => {}, |
| 132 | + onResume: () => {}, |
| 133 | + onImplementPlanInNewThread: () => {}, |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + let container: HTMLDivElement; |
| 138 | + let root: Root; |
| 139 | + const render = (canInterrupt: boolean) => |
| 140 | + act(() => root.render(createElement(ComposerPrimaryActions, runningProps(canInterrupt)))); |
| 141 | + const slot = () => [...container.querySelectorAll("button")].at(-1)!; |
| 142 | + const clickStop = () => act(() => slot().click()); |
| 143 | + |
| 144 | + beforeEach(() => { |
| 145 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 146 | + vi.useFakeTimers(); |
| 147 | + container = document.createElement("div"); |
| 148 | + document.body.append(container); |
| 149 | + root = createRoot(container); |
| 150 | + render(true); |
| 151 | + }); |
| 152 | + afterEach(() => { |
| 153 | + act(() => root.unmount()); |
| 154 | + container.remove(); |
| 155 | + vi.useRealTimers(); |
| 156 | + vi.unstubAllGlobals(); |
| 157 | + }); |
| 158 | + |
| 159 | + it("blocks Resume briefly after a stop lands so repeat clicks do not resume", () => { |
| 160 | + clickStop(); |
| 161 | + expect(slot()).toMatchObject({ ariaLabel: "Stopping", disabled: true }); |
| 162 | + |
| 163 | + render(false); |
| 164 | + expect(slot()).toMatchObject({ ariaLabel: "Resume thread", disabled: true }); |
| 165 | + |
| 166 | + act(() => vi.advanceTimersByTime(1_000)); |
| 167 | + expect(slot().disabled).toBe(false); |
| 168 | + }); |
| 169 | + |
| 170 | + it("enables Stop at once for a new run started after the stop lands", () => { |
| 171 | + clickStop(); |
| 172 | + render(false); |
| 173 | + render(true); |
| 174 | + |
| 175 | + expect(slot()).toMatchObject({ ariaLabel: "Stop generation", disabled: false }); |
| 176 | + }); |
| 177 | + |
| 178 | + it("re-enables Stop when the stop never lands", () => { |
| 179 | + clickStop(); |
| 180 | + |
| 181 | + act(() => vi.advanceTimersByTime(10_000)); |
| 182 | + expect(slot()).toMatchObject({ ariaLabel: "Stop generation", disabled: false }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments