@@ -37,6 +37,116 @@ function savePreviewSession(stateHome: string): void {
3737 ) ;
3838}
3939
40+ describe ( "preview status selection and logs" , ( ) => {
41+ const saved = {
42+ pid : 4321 ,
43+ wrapperIdentity : "wrapper-birth" ,
44+ port : 32600 ,
45+ projectDir,
46+ logPath : "/tmp/managed-preview.log" ,
47+ } ;
48+ const managed : ActiveServer = { ...server , port : saved . port , pidSource : "os" } ;
49+
50+ it ( "prefers the saved preview over a lower sibling when the requested port is absent" , async ( ) => {
51+ const stateHome = tempDir ( "hf-preview-status-" ) ;
52+ writePreviewSession ( saved , stateHome ) ;
53+ const before = readFileSync ( previewSessionPath ( projectDir , stateHome ) , "utf8" ) ;
54+ const status = await readBackgroundPreviewStatus ( projectDir , 32521 , {
55+ stateHome,
56+ preferredPort : 32521 ,
57+ scan : async ( ) => [ { ...managed , port : 32530 , pid : "5555" } , managed ] ,
58+ identity : ( ) => saved . wrapperIdentity ,
59+ } ) ;
60+
61+ expect ( status ) . toMatchObject ( { port : 32600 , pid : 4321 , logPath : saved . logPath } ) ;
62+ expect ( readFileSync ( previewSessionPath ( projectDir , stateHome ) , "utf8" ) ) . toBe ( before ) ;
63+ } ) ;
64+
65+ it ( "prefers the exact requested endpoint regardless of the supplied discovery order" , async ( ) => {
66+ const stateHome = tempDir ( "hf-preview-status-" ) ;
67+ writePreviewSession ( saved , stateHome ) ;
68+ const requested = { ...managed , port : 32580 , pid : "8888" } ;
69+ const status = await readBackgroundPreviewStatus ( projectDir , requested . port , {
70+ stateHome,
71+ preferredPort : requested . port ,
72+ scan : async ( ) => [ { ...managed , port : 32530 , pid : "5555" } , managed , requested ] ,
73+ } ) ;
74+
75+ expect ( status ) . toMatchObject ( { port : requested . port , pid : 8888 } ) ;
76+ expect ( status ) . not . toHaveProperty ( "logPath" ) ;
77+ } ) ;
78+
79+ it . each ( [ "4321" , "9876" ] ) ( "keeps the log for a confirmed managed listener %s" , async ( pid ) => {
80+ const stateHome = tempDir ( "hf-preview-status-" ) ;
81+ writePreviewSession ( saved , stateHome ) ;
82+ const status = await readBackgroundPreviewStatus ( projectDir , saved . port , {
83+ stateHome,
84+ scan : async ( ) => [ { ...managed , pid } ] ,
85+ identity : ( ) => saved . wrapperIdentity ,
86+ isDescendant : ( child , parent ) => child === 9876 && parent === saved . pid ,
87+ } ) ;
88+
89+ expect ( status ) . toMatchObject ( { pid : Number ( pid ) , port : saved . port , logPath : saved . logPath } ) ;
90+ } ) ;
91+
92+ it . each ( [
93+ { name : "another listener" , pid : "5555" , identity : "wrapper-birth" , recorded : "wrapper-birth" } ,
94+ {
95+ name : "a recycled wrapper" ,
96+ pid : "4321" ,
97+ identity : "another-birth" ,
98+ recorded : "wrapper-birth" ,
99+ } ,
100+ { name : "an unavailable identity" , pid : "4321" , identity : null , recorded : "wrapper-birth" } ,
101+ { name : "a legacy record" , pid : "4321" , identity : null , recorded : undefined } ,
102+ {
103+ name : "an unknown listener PID" ,
104+ pid : null ,
105+ identity : "wrapper-birth" ,
106+ recorded : "wrapper-birth" ,
107+ } ,
108+ ] ) ( "does not attach a managed log to $name" , async ( { pid, identity, recorded } ) => {
109+ const stateHome = tempDir ( "hf-preview-status-" ) ;
110+ writePreviewSession ( { ...saved , wrapperIdentity : recorded } , stateHome ) ;
111+ const before = readFileSync ( previewSessionPath ( projectDir , stateHome ) , "utf8" ) ;
112+ const status = await readBackgroundPreviewStatus ( projectDir , saved . port , {
113+ stateHome,
114+ scan : async ( ) => [ { ...managed , pid } ] ,
115+ identity : ( ) => identity ,
116+ isDescendant : ( ) => false ,
117+ } ) ;
118+
119+ expect ( status ) . toMatchObject ( { port : saved . port , pid : pid === null ? null : Number ( pid ) } ) ;
120+ expect ( status ) . not . toHaveProperty ( "logPath" ) ;
121+ expect ( readFileSync ( previewSessionPath ( projectDir , stateHome ) , "utf8" ) ) . toBe ( before ) ;
122+ } ) ;
123+
124+ it ( "does not trust a self-reported PID to associate a log" , async ( ) => {
125+ const stateHome = tempDir ( "hf-preview-status-" ) ;
126+ writePreviewSession ( saved , stateHome ) ;
127+ const status = await readBackgroundPreviewStatus ( projectDir , saved . port , {
128+ stateHome,
129+ scan : async ( ) => [ { ...managed , pidSource : "self-reported" } ] ,
130+ identity : ( ) => saved . wrapperIdentity ,
131+ } ) ;
132+
133+ expect ( status ) . toMatchObject ( { port : saved . port , pid : saved . pid } ) ;
134+ expect ( status ) . not . toHaveProperty ( "logPath" ) ;
135+ } ) ;
136+
137+ it ( "reports an unmanaged endpoint without inventing a log path" , async ( ) => {
138+ const stateHome = tempDir ( "hf-preview-status-" ) ;
139+ const status = await readBackgroundPreviewStatus ( projectDir , managed . port , {
140+ stateHome,
141+ scan : async ( ) => [ managed ] ,
142+ } ) ;
143+
144+ expect ( status ) . toMatchObject ( { port : managed . port , pid : saved . pid } ) ;
145+ expect ( status ) . not . toHaveProperty ( "logPath" ) ;
146+ expect ( existsSync ( previewSessionPath ( projectDir , stateHome ) ) ) . toBe ( false ) ;
147+ } ) ;
148+ } ) ;
149+
40150/** Launch deps whose wrapper (PID 4321) brings up `server` (PID 9876) on spawn and takes it down on kill. */
41151function reachableChildDependencies ( stateHome : string , { immortal = false } = { } ) {
42152 let spawned = false ;
@@ -330,6 +440,7 @@ describe("background preview lifecycle", () => {
330440 writePreviewSession (
331441 {
332442 pid : 8765 ,
443+ wrapperIdentity : "custom-wrapper-birth" ,
333444 port : 41402 ,
334445 projectDir : otherProjectDir ,
335446 logPath : "/tmp/custom.log" ,
@@ -348,6 +459,7 @@ describe("background preview lifecycle", () => {
348459
349460 const statuses = await listBackgroundPreviewStatuses ( {
350461 stateHome,
462+ identity : ( ) => "custom-wrapper-birth" ,
351463 scan : async ( startPort ) =>
352464 startPort === 41402
353465 ? [
@@ -357,6 +469,7 @@ describe("background preview lifecycle", () => {
357469 projectDir : otherProjectDir ,
358470 version : "test" ,
359471 pid : "8765" ,
472+ pidSource : "os" ,
360473 } ,
361474 ]
362475 : [ ] ,
0 commit comments