11import { test } from 'vitest' ;
22import assert from 'node:assert/strict' ;
3- import { withCapturedStderr , withInteractiveStderr , withStreamTty } from './stream-capture.ts' ;
3+ import {
4+ beginStderrCapture ,
5+ withCapturedStderr ,
6+ withInteractiveStderr ,
7+ withStreamTty ,
8+ withStreamTtyAsync ,
9+ } from './stream-capture.ts' ;
410
511function hasOwnWrite ( stream : NodeJS . WriteStream ) : boolean {
612 return Object . prototype . hasOwnProperty . call ( stream , 'write' ) ;
713}
814
915test ( 'withCapturedStderr restores an inherited write without leaving an own property' , ( ) => {
10- assert . equal ( hasOwnWrite ( process . stderr ) , false , 'precondition: write comes from the prototype ') ;
16+ const writeBefore = Object . getOwnPropertyDescriptor ( process . stderr , ' write') ;
1117 const captured = withCapturedStderr ( ( ) => {
1218 process . stderr . write ( 'into the capture\n' ) ;
1319 } ) ;
1420 assert . equal ( captured , 'into the capture\n' ) ;
15- assert . equal ( hasOwnWrite ( process . stderr ) , false , 'a real write must flow after restore' ) ;
16- assert . equal ( process . stderr . write ( 'back to the real stream\n' ) , true ) ;
21+ assert . deepEqual ( Object . getOwnPropertyDescriptor ( process . stderr , 'write' ) , writeBefore ) ;
1722} ) ;
1823
1924test ( 'withCapturedStderr puts back a pre-existing own-property write' , ( ) => {
@@ -27,43 +32,61 @@ test('withCapturedStderr puts back a pre-existing own-property write', () => {
2732 withCapturedStderr ( ( ) => {
2833 process . stderr . write ( 'captured\n' ) ;
2934 } ) ;
35+ assert . equal ( hasOwnWrite ( process . stderr ) , true ) ;
36+ process . stderr . write ( 'sentinel\n' ) ;
37+ assert . equal ( sentinelHits , 1 , 'the pre-capture own write must be the live one again' ) ;
3038 } finally {
31- // If restore left its own stub in place, the sentinel below never runs.
39+ // Success: drop the restored sentinel so the prototype write is live again.
40+ // Failure mid-assert: drop whatever capture stub is still installed.
41+ delete stream . write ;
42+ }
43+ } ) ;
44+
45+ test ( 'passthroughBufferWrites forwards Buffers to the real stream with the stream as receiver' , ( ) => {
46+ const capture = beginStderrCapture ( { passthroughBufferWrites : true } ) ;
47+ try {
48+ // An empty Buffer reaches the real Writable.write without emitting output; called
49+ // unbound it throws while reading `this._writableState`.
50+ assert . equal ( process . stderr . write ( Buffer . alloc ( 0 ) ) , true ) ;
51+ } finally {
52+ capture . restore ( ) ;
3253 }
33- assert . equal ( hasOwnWrite ( process . stderr ) , true ) ;
34- process . stderr . write ( 'sentinel\n' ) ;
35- assert . equal ( sentinelHits , 1 , 'the pre-capture own write must be the live one again' ) ;
36- delete stream . write ;
3754} ) ;
3855
3956test ( 'withInteractiveStderr hides CI, marks the stream a TTY, and restores all three' , ( ) => {
4057 const originalCi = process . env . CI ;
58+ const originalIsTTY = Object . getOwnPropertyDescriptor ( process . stderr , 'isTTY' ) ;
4159 process . env . CI = '1' ;
4260 try {
4361 const captured = withInteractiveStderr ( ( ) => {
4462 assert . equal ( process . env . CI , undefined ) ;
4563 assert . equal ( process . stderr . isTTY , true ) ;
46- const reporterNeedsTTY = process . stderr . isTTY === true && ! process . env . CI ;
47- assert . equal ( reporterNeedsTTY , true ) ;
4864 process . stderr . write ( 'interactive line\n' ) ;
4965 } ) ;
5066 assert . equal ( captured , 'interactive line\n' ) ;
5167 assert . equal ( process . env . CI , '1' ) ;
52- assert . equal ( process . stderr . isTTY , undefined ) ;
53- assert . equal ( hasOwnWrite ( process . stderr ) , false ) ;
68+ assert . deepEqual ( Object . getOwnPropertyDescriptor ( process . stderr , ' isTTY' ) , originalIsTTY ) ;
69+ assert . deepEqual ( Object . getOwnPropertyDescriptor ( process . stderr , 'write' ) , undefined ) ;
5470 } finally {
5571 if ( originalCi === undefined ) delete process . env . CI ;
5672 else process . env . CI = originalCi ;
5773 }
5874} ) ;
5975
60- test ( 'withStreamTty leaves no own isTTY behind on a stream that never owned one' , ( ) => {
61- assert . equal (
62- Object . prototype . hasOwnProperty . call ( process . stdout , 'isTTY' ) ,
63- false ,
64- 'precondition: stdout inherits isTTY' ,
65- ) ;
66- const sawTty = withStreamTty ( process . stdout , true , ( ) => process . stdout . isTTY === true ) ;
67- assert . equal ( sawTty , true ) ;
68- assert . equal ( Object . prototype . hasOwnProperty . call ( process . stdout , 'isTTY' ) , false ) ;
76+ test ( 'withStreamTty restores the original isTTY descriptor state' , ( ) => {
77+ const isTTYBefore = Object . getOwnPropertyDescriptor ( process . stdout , 'isTTY' ) ;
78+ const toggled = ! isTTYBefore ?. value ;
79+ const sawToggled = withStreamTty ( process . stdout , toggled , ( ) => process . stdout . isTTY === toggled ) ;
80+ assert . equal ( sawToggled , true ) ;
81+ assert . deepEqual ( Object . getOwnPropertyDescriptor ( process . stdout , 'isTTY' ) , isTTYBefore ) ;
82+ } ) ;
83+
84+ test ( 'withStreamTtyAsync keeps the toggle across awaits and restores the original descriptor' , async ( ) => {
85+ const isTTYBefore = Object . getOwnPropertyDescriptor ( process . stderr , 'isTTY' ) ;
86+ const survivedAwait = await withStreamTtyAsync ( process . stderr , true , async ( ) => {
87+ await Promise . resolve ( ) ;
88+ return process . stderr . isTTY === true ;
89+ } ) ;
90+ assert . equal ( survivedAwait , true ) ;
91+ assert . deepEqual ( Object . getOwnPropertyDescriptor ( process . stderr , 'isTTY' ) , isTTYBefore ) ;
6992} ) ;
0 commit comments