@@ -3,7 +3,7 @@ import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
33import { EnvironmentId } from "@t3tools/contracts" ;
44import * as Option from "effect/Option" ;
55import { AsyncResult } from "effect/unstable/reactivity" ;
6- import { act , type ButtonHTMLAttributes } from "react" ;
6+ import { act , useState , type ButtonHTMLAttributes , type ReactNode } from "react" ;
77import { create , type ReactTestRenderer } from "react-test-renderer" ;
88import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
99
@@ -42,6 +42,25 @@ vi.mock("~/state/environments", async () => {
4242 return { useRelayEnvironmentDiscovery : ( ) => useSyncExternalStore ( subscribe , read , read ) } ;
4343} ) ;
4444vi . mock ( "../ConnectionStatusDot" , ( ) => ( { ConnectionStatusDot : ( ) => null } ) ) ;
45+ vi . mock ( "../ui/tooltip" , ( ) => ( {
46+ Tooltip : ( { children } : { children : ReactNode } ) => children ,
47+ TooltipTrigger : ( { children } : { children : ReactNode } ) => < span > { children } </ span > ,
48+ TooltipPopup : ( ) => null ,
49+ } ) ) ;
50+ vi . mock ( "../ui/checkbox" , ( ) => ( {
51+ Checkbox : ( props : {
52+ checked : boolean ;
53+ disabled : boolean ;
54+ onCheckedChange : ( checked : boolean ) => void ;
55+ } ) => (
56+ < input
57+ type = "checkbox"
58+ checked = { props . checked }
59+ disabled = { props . disabled }
60+ onChange = { ( event ) => props . onCheckedChange ( event . target . checked ) }
61+ />
62+ ) ,
63+ } ) ) ;
4564vi . mock ( "../ui/button" , ( ) => ( {
4665 Button : ( { children, ...props } : ButtonHTMLAttributes < HTMLButtonElement > ) => (
4766 < button { ...props } > { children } </ button >
@@ -118,6 +137,7 @@ beforeEach(() => {
118137 error : Option . none ( ) ,
119138 } ;
120139 discovery . listEnvironments . mockReset ( ) . mockResolvedValue ( new Map ( ) ) ;
140+ discovery . register . mockReset ( ) . mockResolvedValue ( AsyncResult . success ( undefined ) ) ;
121141 discovery . refresh . mockReset ( ) . mockImplementation ( async ( ) => {
122142 publish ( { environments : new Map ( ) , refreshing : true , offline : false , error : Option . none ( ) } ) ;
123143 const environments = await discovery . listEnvironments ( ) ;
@@ -133,6 +153,71 @@ afterEach(async () => {
133153} ) ;
134154
135155describe ( "cloud onboarding discovery" , ( ) => {
156+ it ( "signals that the section can expand after initial discovery settles" , async ( ) => {
157+ let finishDiscovery ! : ( environments : DiscoveredEnvironments ) => void ;
158+ discovery . listEnvironments . mockReturnValue (
159+ new Promise ( ( resolve ) => {
160+ finishDiscovery = resolve ;
161+ } ) ,
162+ ) ;
163+ const onDiscoveryReady = vi . fn ( ) ;
164+ await act ( async ( ) => {
165+ renderer = create (
166+ < CloudEnvironmentConnectRows
167+ primaryEnvironmentId = { null }
168+ savedEnvironments = { [ ] }
169+ onDiscoveryReady = { onDiscoveryReady }
170+ /> ,
171+ ) ;
172+ } ) ;
173+ expect ( onDiscoveryReady ) . not . toHaveBeenCalled ( ) ;
174+ await act ( async ( ) => {
175+ finishDiscovery ( linkedMachines ) ;
176+ } ) ;
177+ expect ( onDiscoveryReady ) . toHaveBeenCalledTimes ( 1 ) ;
178+ expect ( renderer ! . root . findByType ( "button" ) . children ) . toEqual ( [ "Connect" ] ) ;
179+ } ) ;
180+
181+ it ( "connects and selects discovered computers by default without overwriting deselection" , async ( ) => {
182+ discovery . listEnvironments . mockResolvedValue ( linkedMachines ) ;
183+ const autoSelectedComputers = new Set < EnvironmentId > ( ) ;
184+ function Setup ( ) {
185+ const [ selectedIds , setSelectedIds ] = useState < ReadonlySet < EnvironmentId > > ( new Set ( ) ) ;
186+ return (
187+ < CloudEnvironmentConnectRows
188+ primaryEnvironmentId = { null }
189+ savedEnvironments = { [ ] }
190+ showSavedEnvironments
191+ selection = { {
192+ autoSelectedComputers,
193+ selectedIds,
194+ onChange : ( id , checked ) =>
195+ setSelectedIds ( ( current ) => {
196+ const next = new Set ( current ) ;
197+ if ( checked ) next . add ( id ) ;
198+ else next . delete ( id ) ;
199+ return next ;
200+ } ) ,
201+ } }
202+ />
203+ ) ;
204+ }
205+ await act ( async ( ) => {
206+ renderer = create ( < Setup /> ) ;
207+ } ) ;
208+
209+ expect ( discovery . register ) . toHaveBeenCalledTimes ( 1 ) ;
210+ expect ( renderer ! . root . findByType ( "input" ) . props . checked ) . toBe ( true ) ;
211+ await act ( async ( ) => {
212+ await renderer ! . root . findByType ( "input" ) . props . onChange ( { target : { checked : false } } ) ;
213+ } ) ;
214+ await act ( async ( ) => {
215+ publish ( { ...discovery . state ! , environments : new Map ( linkedMachines ) } ) ;
216+ } ) ;
217+ expect ( renderer ! . root . findByType ( "input" ) . props . checked ) . toBe ( false ) ;
218+ expect ( discovery . register ) . toHaveBeenCalledTimes ( 1 ) ;
219+ } ) ;
220+
136221 it ( "shows a newly linked computer without remounting and stops polling once found" , async ( ) => {
137222 discovery . listEnvironments
138223 . mockResolvedValueOnce ( new Map ( ) )
@@ -152,6 +237,31 @@ describe("cloud onboarding discovery", () => {
152237 expect ( discovery . listEnvironments ) . toHaveBeenCalledTimes ( 2 ) ;
153238 } ) ;
154239
240+ it ( "keeps a discovered computer visible when it is added to the browser" , async ( ) => {
241+ discovery . listEnvironments . mockResolvedValue ( linkedMachines ) ;
242+ await mount ( ) ;
243+ expect ( renderer ! . root . findByType ( "button" ) . children ) . toEqual ( [ "Connect" ] ) ;
244+ await act ( async ( ) => {
245+ renderer ! . update (
246+ < CloudEnvironmentConnectRows
247+ primaryEnvironmentId = { null }
248+ savedEnvironments = { [
249+ {
250+ environmentId : newMachineId ,
251+ connection : { phase : "connected" , error : null , traceId : null } ,
252+ } ,
253+ ] }
254+ showSavedEnvironments
255+ refreshWhileEmpty
256+ /> ,
257+ ) ;
258+ } ) ;
259+ expect ( renderer ! . root . findAllByType ( "p" ) . map ( ( node ) => node . children ) ) . toContainEqual ( [
260+ "Work laptop" ,
261+ ] ) ;
262+ expect ( renderer ! . root . findByType ( "button" ) . children ) . toEqual ( [ "Connected" ] ) ;
263+ } ) ;
264+
155265 it ( "waits while hidden and refreshes immediately when visible again" , async ( ) => {
156266 page . visibilityState = "hidden" ;
157267 await mount ( ) ;
0 commit comments