1- import { describe , it , expect } from "vitest" ;
2- import { parseConflictContent } from "./file-parser" ;
1+ import { describe , it , expect , vi } from "vitest" ;
2+ import { parseConflictContent , normalizeParsers , runParser , parseFormat } from "./file-parser" ;
33
44describe ( "parseConflictContent" , ( ) => {
55 const makeConflict = ( ours : string , theirs : string ) =>
@@ -16,7 +16,7 @@ ${theirs}
1616
1717 it ( "parses simple JSON conflict" , async ( ) => {
1818 const raw = makeConflict ( ` "value": 1` , ` "value": 2` ) ;
19- const result = await parseConflictContent ( raw , { parsers : "json" } ) ;
19+ const result = await parseConflictContent ( raw , { parsers : "json" , filename : "" } ) ;
2020
2121 expect ( result . format ) . toBe ( "json" ) ;
2222 expect ( result . ours ) . toEqual ( { name : "test" , value : 1 } ) ;
@@ -40,7 +40,7 @@ theirs: 2
4040
4141 it ( "respects explicit parsers array (json5 fallback)" , async ( ) => {
4242 const raw = makeConflict ( ` value: 1,` , ` value: 2,` ) ;
43- const result = await parseConflictContent ( raw , { parsers : [ "json5" ] } ) ;
43+ const result = await parseConflictContent ( raw , { parsers : [ "json5" ] , filename : "" } ) ;
4444 expect ( result . format ) . toBe ( "json5" ) ;
4545 expect ( result . ours ) . toMatchObject ( { value : 1 } ) ;
4646 expect ( result . theirs ) . toMatchObject ( { value : 2 } ) ;
@@ -53,15 +53,15 @@ theirs: 2
5353 parser : ( s : string ) => ( { parsed : s . trim ( ) } ) ,
5454 } ;
5555
56- const result = await parseConflictContent ( raw , { parsers : custom } ) ;
56+ const result = await parseConflictContent ( raw , { parsers : custom , filename : "" } ) ;
5757 expect ( result . format ) . toBe ( "custom" ) ;
5858 expect ( result . ours ) . toMatchObject ( { parsed : expect . stringContaining ( "ours-side" ) } ) ;
5959 expect ( result . theirs ) . toMatchObject ( { parsed : expect . stringContaining ( "theirs-side" ) } ) ;
6060 } ) ;
6161
6262 it ( "throws if parsing fails for all parsers" , async ( ) => {
6363 const raw = "invalid" ;
64- await expect ( parseConflictContent ( raw , { parsers : [ "json" ] } ) ) . rejects . toThrow (
64+ await expect ( parseConflictContent ( raw , { parsers : [ "json" ] , filename : "" } ) ) . rejects . toThrow (
6565 / F a i l e d t o p a r s e / ,
6666 ) ;
6767 } ) ;
@@ -72,6 +72,74 @@ theirs: 2
7272only ours
7373>>>>>>> theirs
7474` ;
75- await expect ( parseConflictContent ( raw , { parsers : "json" } ) ) . rejects . toThrow ( / e m p t y c o n t e n t / ) ;
75+ await expect ( parseConflictContent ( raw , { parsers : "json" , filename : "" } ) ) . rejects . toThrow (
76+ / e m p t y c o n t e n t / ,
77+ ) ;
78+ } ) ;
79+ } ) ;
80+
81+ describe ( "normalizeParsers" , ( ) => {
82+ it ( "returns auto parsers when parsers is 'auto'" , ( ) => {
83+ const result = normalizeParsers ( { parsers : "auto" , filename : "test.json" } ) ;
84+ expect ( result ) . toEqual ( [ "json" , "json5" , "yaml" , "toml" , "xml" ] ) ;
85+ } ) ;
86+
87+ it ( "returns single parser when parsers is string" , ( ) => {
88+ const result = normalizeParsers ( { parsers : "yaml" , filename : "test.json" } ) ;
89+ expect ( result ) . toEqual ( [ "yaml" ] ) ;
90+ } ) ;
91+
92+ it ( "falls back to extension-based parser" , ( ) => {
93+ const result = normalizeParsers ( { filename : "config.toml" } ) ;
94+ expect ( result ) . toEqual ( [ "toml" ] ) ;
95+ } ) ;
96+ } ) ;
97+
98+ describe ( "runParser" , ( ) => {
99+ it ( "logs debug message on parser failure" , async ( ) => {
100+ const consoleSpy = vi . spyOn ( console , "debug" ) . mockImplementation ( ( ) => { } ) ;
101+ await expect ( runParser ( "invalid" , [ "json" ] ) ) . rejects . toThrow ( ) ;
102+ expect ( consoleSpy ) . toHaveBeenCalledWith (
103+ expect . stringContaining ( "Parser json failed:" ) ,
104+ expect . any ( Error ) ,
105+ ) ;
106+ consoleSpy . mockRestore ( ) ;
107+ } ) ;
108+ } ) ;
109+
110+ describe ( "parseFormat" , ( ) => {
111+ it ( "throws error for missing json5 dependency" , async ( ) => {
112+ vi . doMock ( "json5" , ( ) => {
113+ throw new Error ( "Module not found" ) ;
114+ } ) ;
115+ await expect ( parseFormat ( "json5" , "{}" ) ) . rejects . toThrow ( / j s o n 5 p a r s e r n o t i n s t a l l e d / ) ;
116+ } ) ;
117+
118+ it ( "throws error for missing yaml dependency" , async ( ) => {
119+ vi . doMock ( "yaml" , ( ) => {
120+ throw new Error ( "Module not found" ) ;
121+ } ) ;
122+ await expect ( parseFormat ( "yaml" , "key: value" ) ) . rejects . toThrow ( / y a m l p a r s e r n o t i n s t a l l e d / ) ;
123+ } ) ;
124+
125+ it ( "throws error for missing toml dependency" , async ( ) => {
126+ vi . doMock ( "smol-toml" , ( ) => {
127+ throw new Error ( "Module not found" ) ;
128+ } ) ;
129+ await expect ( parseFormat ( "toml" , "key = 'value'" ) ) . rejects . toThrow ( / t o m l p a r s e r n o t i n s t a l l e d / ) ;
130+ } ) ;
131+
132+ it ( "parses xml successfully" , async ( ) => {
133+ const result = await parseFormat ( "xml" , "<root><key>value</key></root>" ) ;
134+ expect ( result ) . toHaveProperty ( "root" ) ;
135+ } ) ;
136+
137+ it ( "throws error for missing xml dependency" , async ( ) => {
138+ vi . doMock ( "fast-xml-parser" , ( ) => {
139+ throw new Error ( "Module not found" ) ;
140+ } ) ;
141+ await expect ( parseFormat ( "xml" , "<root></root>" ) ) . rejects . toThrow (
142+ / f a s t - x m l - p a r s e r n o t i n s t a l l e d / ,
143+ ) ;
76144 } ) ;
77145} ) ;
0 commit comments