@@ -3,7 +3,13 @@ import { tmpdir } from "node:os";
33import { join , resolve } from "node:path" ;
44import * as clack from "@clack/prompts" ;
55import { afterEach , describe , expect , it , vi } from "vitest" ;
6- import { handlePreviewKillAll , handlePreviewList , studioLandingSearch } from "./preview.js" ;
6+ import { runCommand } from "citty" ;
7+ import {
8+ default as previewCommand ,
9+ handlePreviewKillAll ,
10+ handlePreviewList ,
11+ studioLandingSearch ,
12+ } from "./preview.js" ;
713
814const tempDirs : string [ ] = [ ] ;
915
@@ -67,9 +73,9 @@ describe("preview --kill-all", () => {
6773 const log = vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } ) ;
6874 const warn = vi . spyOn ( clack . log , "warn" ) . mockImplementation ( ( ) => { } ) ;
6975
70- await handlePreviewKillAll ( 3002 , {
76+ await handlePreviewKillAll ( 3002 , false , {
7177 listManaged : async ( ) => [ session ( 41402 , "/tmp/unprovable" ) , session ( 41403 , "/tmp/healthy" ) ] ,
72- stopManaged : async ( projectDir ) => {
78+ stopManaged : async ( projectDir : string ) => {
7379 if ( projectDir === "/tmp/unprovable" ) throw new Error ( "ownership failed" ) ;
7480 return true ;
7581 } ,
@@ -85,7 +91,7 @@ describe("preview --kill-all", () => {
8591 it ( "reports nothing to kill when no preview is running" , async ( ) => {
8692 const log = vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } ) ;
8793
88- await handlePreviewKillAll ( 3002 , {
94+ await handlePreviewKillAll ( 3002 , false , {
8995 listManaged : async ( ) => [ ] ,
9096 killScanned : async ( ) => ( { killed : 0 , unverified : [ ] } ) ,
9197 } ) ;
@@ -99,7 +105,7 @@ describe("preview --list", () => {
99105 it ( "prefers the managed record over the same server's own self-report" , async ( ) => {
100106 const log = vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } ) ;
101107
102- await handlePreviewList ( 3002 , {
108+ await handlePreviewList ( 3002 , false , {
103109 listManaged : async ( ) => [
104110 { pid : 99 , port : 3002 , projectDir : resolve ( "/tmp/demo" ) , logPath : "/tmp/demo.log" } ,
105111 ] ,
@@ -120,3 +126,93 @@ describe("preview --list", () => {
120126 log . mockRestore ( ) ;
121127 } ) ;
122128} ) ;
129+
130+ describe ( "preview lifecycle JSON failures" , ( ) => {
131+ it . each ( [
132+ [
133+ "list" ,
134+ ( ) =>
135+ handlePreviewList ( 3002 , true , {
136+ scan : async ( ) => {
137+ throw new Error ( "list probe failed" ) ;
138+ } ,
139+ listManaged : async ( ) => [ ] ,
140+ } ) ,
141+ "preview-list-failed" ,
142+ ] ,
143+ [
144+ "kill-all" ,
145+ ( ) =>
146+ handlePreviewKillAll ( 3002 , true , {
147+ listManaged : async ( ) => [ ] ,
148+ killScanned : async ( ) => {
149+ throw new Error ( "scan failed" ) ;
150+ } ,
151+ } ) ,
152+ "preview-kill-all-failed" ,
153+ ] ,
154+ ] as const ) ( "wraps %s failures in one JSON document" , async ( operation , run , code ) => {
155+ const log = vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } ) ;
156+ const error = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
157+
158+ await run ( ) ;
159+
160+ expect ( log ) . toHaveBeenCalledOnce ( ) ;
161+ const [ line ] = log . mock . calls [ 0 ] as [ string ] ;
162+ expect ( JSON . parse ( line ) ) . toMatchObject ( {
163+ schemaVersion : 1 ,
164+ operation,
165+ ok : false ,
166+ error : { code } ,
167+ } ) ;
168+ expect ( error ) . not . toHaveBeenCalled ( ) ;
169+ } ) ;
170+
171+ it ( "keeps stopping after a record whose ownership cannot be proven" , async ( ) => {
172+ const log = vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } ) ;
173+ const session = ( port : number , projectDir : string ) => ( {
174+ pid : 4321 ,
175+ port,
176+ projectDir,
177+ logPath : `${ projectDir } .log` ,
178+ } ) ;
179+
180+ await handlePreviewKillAll ( 3002 , true , {
181+ listManaged : async ( ) => [ session ( 41402 , "/tmp/unprovable" ) , session ( 41403 , "/tmp/healthy" ) ] ,
182+ stopManaged : async ( projectDir ) => {
183+ if ( projectDir === "/tmp/unprovable" ) throw new Error ( "ownership failed" ) ;
184+ return true ;
185+ } ,
186+ killScanned : async ( ) => ( { killed : 0 , unverified : [ ] } ) ,
187+ } ) ;
188+
189+ const [ line ] = log . mock . calls [ 0 ] as [ string ] ;
190+ // The second record must still be stopped AND the first must be reported:
191+ // propagating the first failure left every later preview running, unlisted.
192+ expect ( JSON . parse ( line ) ) . toMatchObject ( {
193+ operation : "kill-all" ,
194+ ok : true ,
195+ result : { state : "killed-all" , stopped : 1 , failed : [ "/tmp/unprovable: ownership failed" ] } ,
196+ } ) ;
197+ } ) ;
198+
199+ it ( "wraps stop failures in one JSON document" , async ( ) => {
200+ const missing = join ( tmpdir ( ) , `hf-preview-missing-${ process . pid } -${ Date . now ( ) } ` ) ;
201+ const log = vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } ) ;
202+ const error = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
203+
204+ await runCommand ( previewCommand , {
205+ rawArgs : [ missing , "--stop" , "--json" ] ,
206+ } ) ;
207+
208+ expect ( log ) . toHaveBeenCalledOnce ( ) ;
209+ const [ line ] = log . mock . calls [ 0 ] as [ string ] ;
210+ expect ( JSON . parse ( line ) ) . toMatchObject ( {
211+ schemaVersion : 1 ,
212+ operation : "stop" ,
213+ ok : false ,
214+ error : { code : "preview-stop-failed" } ,
215+ } ) ;
216+ expect ( error ) . not . toHaveBeenCalled ( ) ;
217+ } ) ;
218+ } ) ;
0 commit comments