1- import type { ReactElement } from "react" ;
1+ import { act , type ReactElement } from "react" ;
2+ import { create , type ReactTestRenderer } from "react-test-renderer" ;
23import { renderToStaticMarkup } from "react-dom/server" ;
34import type { EnvironmentId } from "@t3tools/contracts" ;
45import * as Cause from "effect/Cause" ;
56import { AsyncResult } from "effect/unstable/reactivity" ;
6- import { beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
7+ import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
78
89const testState = vi . hoisted ( ( ) => ( {
910 updateServer : vi . fn ( ) ,
@@ -30,7 +31,18 @@ vi.mock("./ui/toast", () => ({
3031 toastManager : { add : testState . toast } ,
3132} ) ) ;
3233
33- import { ServerUpdateAction , ServerUpdateProgress } from "./ServerUpdateAction" ;
34+ import {
35+ readConfirmDialogState ,
36+ registerConfirmDialogHost ,
37+ resetConfirmDialogForTests ,
38+ respondToConfirmDialog ,
39+ } from "~/confirmDialog" ;
40+ import {
41+ ServerUpdateAction ,
42+ ServerUpdateProgress ,
43+ ServerUpdatesAction ,
44+ type ServerUpdateTarget ,
45+ } from "./ServerUpdateAction" ;
3446
3547type ActionElement = ReactElement < {
3648 readonly onClick ?: ( ) => void ;
@@ -194,6 +206,149 @@ describe("ServerUpdateAction", () => {
194206 } ) ;
195207} ) ;
196208
209+ describe ( "ServerUpdatesAction" , ( ) => {
210+ let renderer : ReactTestRenderer | undefined ;
211+ const targets : ReadonlyArray < ServerUpdateTarget > = [
212+ {
213+ environmentId : "batch-a" as EnvironmentId ,
214+ serverLabel : "Laptop" ,
215+ selfUpdate : "boot-service" ,
216+ targetVersion : "0.0.31" ,
217+ threadContinuation : true ,
218+ continueThreadsAfterServerUpdate : true ,
219+ } ,
220+ {
221+ environmentId : "batch-b" as EnvironmentId ,
222+ serverLabel : "Office" ,
223+ selfUpdate : "respawn" ,
224+ targetVersion : "0.0.31" ,
225+ threadContinuation : true ,
226+ continueThreadsAfterServerUpdate : false ,
227+ } ,
228+ {
229+ environmentId : "batch-c" as EnvironmentId ,
230+ serverLabel : "Manual" ,
231+ selfUpdate : null ,
232+ targetVersion : "0.0.31" ,
233+ } ,
234+ ] ;
235+ const success = AsyncResult . success ( { targetVersion : "0.0.31" , method : "boot-service" as const } ) ;
236+
237+ async function mount ( batch = targets ) {
238+ await act ( async ( ) => {
239+ renderer = create ( < ServerUpdatesAction targets = { batch } /> ) ;
240+ } ) ;
241+ return renderer ! . root . findByType ( "button" ) ;
242+ }
243+
244+ beforeEach ( ( ) => {
245+ vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
246+ testState . updateServer . mockReset ( ) ;
247+ testState . toast . mockReset ( ) ;
248+ resetConfirmDialogForTests ( ) ;
249+ } ) ;
250+ afterEach ( async ( ) => {
251+ await act ( async ( ) => {
252+ renderer ?. unmount ( ) ;
253+ } ) ;
254+ renderer = undefined ;
255+ resetConfirmDialogForTests ( ) ;
256+ vi . unstubAllGlobals ( ) ;
257+ } ) ;
258+
259+ it ( "updates both supported machines with their own continuation preference and skips the manual machine" , async ( ) => {
260+ testState . updateServer . mockResolvedValue ( success ) ;
261+ const button = await mount ( ) ;
262+ await act ( async ( ) => {
263+ button . props . onClick ( ) ;
264+ } ) ;
265+
266+ expect ( testState . updateServer . mock . calls . map ( ( [ target ] ) => target ) ) . toEqual ( [
267+ {
268+ environmentId : "batch-a" ,
269+ input : { targetVersion : "0.0.31" , continueRunningThreads : true } ,
270+ } ,
271+ { environmentId : "batch-b" , input : { targetVersion : "0.0.31" } } ,
272+ ] ) ;
273+ expect ( testState . toast . mock . calls . map ( ( [ toast ] ) => toast . title ) ) . toEqual ( [
274+ "Laptop updated" ,
275+ "Office updated" ,
276+ ] ) ;
277+ } ) ;
278+
279+ it ( "names a failed machine while letting the other machine complete" , async ( ) => {
280+ testState . updateServer
281+ . mockResolvedValueOnce ( AsyncResult . failure ( Cause . fail ( new Error ( "Download failed" ) ) ) )
282+ . mockResolvedValueOnce ( success ) ;
283+ const button = await mount ( ) ;
284+ await act ( async ( ) => {
285+ button . props . onClick ( ) ;
286+ } ) ;
287+
288+ expect ( testState . updateServer ) . toHaveBeenCalledTimes ( 2 ) ;
289+ expect ( testState . toast ) . toHaveBeenCalledWith ( {
290+ type : "error" ,
291+ title : "Laptop update failed" ,
292+ description : "Download failed" ,
293+ } ) ;
294+ expect ( testState . toast ) . toHaveBeenCalledWith (
295+ expect . objectContaining ( { type : "success" , title : "Office updated" } ) ,
296+ ) ;
297+ expect ( button . props . disabled ) . toBe ( false ) ;
298+ } ) ;
299+
300+ it ( "starts each machine once when double-clicked and disables the action until both finish" , async ( ) => {
301+ const completions : Array < ( ) => void > = [ ] ;
302+ testState . updateServer . mockImplementation (
303+ ( ) =>
304+ new Promise ( ( resolve ) => {
305+ completions . push ( ( ) => resolve ( success ) ) ;
306+ } ) ,
307+ ) ;
308+ const button = await mount ( ) ;
309+ await act ( async ( ) => {
310+ button . props . onClick ( ) ;
311+ button . props . onClick ( ) ;
312+ } ) ;
313+ expect ( testState . updateServer ) . toHaveBeenCalledTimes ( 2 ) ;
314+ expect ( button . props . disabled ) . toBe ( true ) ;
315+ await act ( async ( ) => {
316+ completions [ 0 ] ! ( ) ;
317+ } ) ;
318+ expect ( button . props . disabled ) . toBe ( true ) ;
319+ await act ( async ( ) => {
320+ completions [ 1 ] ! ( ) ;
321+ } ) ;
322+ expect ( button . props . disabled ) . toBe ( false ) ;
323+ expect ( testState . toast ) . toHaveBeenCalledTimes ( 2 ) ;
324+ } ) ;
325+
326+ it ( "asks once for desktop machines and cancels the entire batch" , async ( ) => {
327+ registerConfirmDialogHost ( ) ;
328+ const button = await mount (
329+ targets . map ( ( target , index ) =>
330+ index < 2 ? { ...target , selfUpdate : "desktop-managed" , desktopAppUpdate : true } : target ,
331+ ) ,
332+ ) ;
333+ await act ( async ( ) => {
334+ button . props . onClick ( ) ;
335+ } ) ;
336+ const confirmation = readConfirmDialogState ( ) ;
337+ expect ( confirmation ) . toEqual (
338+ expect . objectContaining ( {
339+ status : "confirming" ,
340+ message : expect . stringContaining ( "Laptop, Office" ) ,
341+ } ) ,
342+ ) ;
343+ expect ( testState . updateServer ) . not . toHaveBeenCalled ( ) ;
344+ await act ( async ( ) => {
345+ respondToConfirmDialog ( false ) ;
346+ } ) ;
347+ expect ( testState . updateServer ) . not . toHaveBeenCalled ( ) ;
348+ expect ( button . props . disabled ) . toBe ( false ) ;
349+ } ) ;
350+ } ) ;
351+
197352describe ( "ServerUpdateProgress" , ( ) => {
198353 it ( "shows one calm status row for the restart wait" , ( ) => {
199354 const markup = renderToStaticMarkup (
0 commit comments