11import { describe , it , expect } from 'bun:test' ;
2+ import { mkdtempSync , rmSync , writeFileSync } from 'fs' ;
3+ import { tmpdir } from 'os' ;
4+ import { join } from 'path' ;
25import { default as uploadCommand } from '../../../src/commands/file/upload' ;
36
47const baseConfig = {
@@ -27,6 +30,22 @@ const baseFlags = {
2730 async : false ,
2831} ;
2932
33+ async function captureStdout ( fn : ( ) => Promise < void > ) : Promise < string > {
34+ const originalWrite = process . stdout . write ;
35+ let output = '' ;
36+ process . stdout . write = ( ( chunk : string | Uint8Array ) => {
37+ output += typeof chunk === 'string' ? chunk : Buffer . from ( chunk ) . toString ( 'utf-8' ) ;
38+ return true ;
39+ } ) as typeof process . stdout . write ;
40+
41+ try {
42+ await fn ( ) ;
43+ return output ;
44+ } finally {
45+ process . stdout . write = originalWrite ;
46+ }
47+ }
48+
3049describe ( 'file upload command' , ( ) => {
3150 it ( 'has correct name' , ( ) => {
3251 expect ( uploadCommand . name ) . toBe ( 'file upload' ) ;
@@ -45,17 +64,22 @@ describe('file upload command', () => {
4564 } ) ;
4665
4766 it ( 'shows dry-run output with file info' , async ( ) => {
48- let captured = '' ;
49- const origWrite = process . stdout . write ;
50- process . stdout . write = ( chunk : any ) : any => { captured += String ( chunk ) ; return true ; } ;
51-
52- await uploadCommand . execute (
53- { ...baseConfig , dryRun : true } ,
54- { ...baseFlags , dryRun : true , file : '/dev/null' , purpose : 'vision' } ,
55- ) ;
56-
57- process . stdout . write = origWrite ;
58- expect ( captured ) . toContain ( '/dev/null' ) ;
59- expect ( captured ) . toContain ( 'vision' ) ;
67+ const tempDir = mkdtempSync ( join ( tmpdir ( ) , 'mmx-upload-test-' ) ) ;
68+ const filePath = join ( tempDir , 'fixture.bin' ) ;
69+ writeFileSync ( filePath , 'fixture' ) ;
70+
71+ try {
72+ const captured = await captureStdout ( async ( ) => {
73+ await uploadCommand . execute (
74+ { ...baseConfig , dryRun : true } ,
75+ { ...baseFlags , dryRun : true , file : filePath , purpose : 'vision' } ,
76+ ) ;
77+ } ) ;
78+
79+ expect ( captured ) . toContain ( filePath ) ;
80+ expect ( captured ) . toContain ( 'vision' ) ;
81+ } finally {
82+ rmSync ( tempDir , { recursive : true , force : true } ) ;
83+ }
6084 } ) ;
61- } ) ;
85+ } ) ;
0 commit comments