1- import { expect , rs , test } from 'rstack/test' ;
1+ import { beforeEach , expect , rs , test } from 'rstack/test' ;
22import { formatFile } from '../../src/fmt/worker.ts' ;
33
44const mocks = rs . hoisted ( ( ) => ( {
5+ readFileCalls : [ ] as string [ ] ,
56 writeFileCalls : [ ] as [ string , string , unknown ] [ ] ,
67} ) ) ;
78
89rs . mock ( 'atomically' , ( ) => ( {
9- readFile : ( ) => Promise . resolve ( 'const value=1' ) ,
10+ readFile : ( path : string ) => {
11+ mocks . readFileCalls . push ( path ) ;
12+ return Promise . resolve ( 'const value=1' ) ;
13+ } ,
1014 writeFile : ( path : string , data : string , options : unknown ) => {
1115 mocks . writeFileCalls . push ( [ path , data , options ] ) ;
1216 return Promise . resolve ( ) ;
1317 } ,
1418} ) ) ;
1519
20+ beforeEach ( ( ) => {
21+ mocks . readFileCalls . length = 0 ;
22+ mocks . writeFileCalls . length = 0 ;
23+ } ) ;
24+
1625test ( 'disables fsync for atomic writes' , async ( ) => {
1726 const filePath = '/virtual/example.ts' ;
1827
@@ -27,9 +36,43 @@ test('disables fsync for atomic writes', async () => {
2736 } ,
2837 true ,
2938 ) ,
30- ) . resolves . toBe ( true ) ;
39+ ) . resolves . toBe ( 'changed' ) ;
3140
3241 expect ( mocks . writeFileCalls ) . toEqual ( [
3342 [ filePath , 'const value = 1;\n' , { encoding : 'utf8' , fsync : false } ] ,
3443 ] ) ;
3544} ) ;
45+
46+ test ( 'infers the parser before formatting' , async ( ) => {
47+ const filePath = '/virtual/example.ts' ;
48+
49+ await expect (
50+ formatFile (
51+ {
52+ path : filePath ,
53+ options : { filepath : filePath } ,
54+ } ,
55+ false ,
56+ ) ,
57+ ) . resolves . toBe ( 'changed' ) ;
58+
59+ expect ( mocks . readFileCalls ) . toEqual ( [ filePath ] ) ;
60+ expect ( mocks . writeFileCalls ) . toEqual ( [ ] ) ;
61+ } ) ;
62+
63+ test ( 'skips unsupported files before reading them' , async ( ) => {
64+ const filePath = '/virtual/example.unknown' ;
65+
66+ await expect (
67+ formatFile (
68+ {
69+ path : filePath ,
70+ options : { filepath : filePath } ,
71+ } ,
72+ true ,
73+ ) ,
74+ ) . resolves . toBe ( 'unsupported' ) ;
75+
76+ expect ( mocks . readFileCalls ) . toEqual ( [ ] ) ;
77+ expect ( mocks . writeFileCalls ) . toEqual ( [ ] ) ;
78+ } ) ;
0 commit comments