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" ;
3
3
4
4
describe ( "parseConflictContent" , ( ) => {
5
5
const makeConflict = ( ours : string , theirs : string ) =>
@@ -16,7 +16,7 @@ ${theirs}
16
16
17
17
it ( "parses simple JSON conflict" , async ( ) => {
18
18
const raw = makeConflict ( ` "value": 1` , ` "value": 2` ) ;
19
- const result = await parseConflictContent ( raw , { parsers : "json" } ) ;
19
+ const result = await parseConflictContent ( raw , { parsers : "json" , filename : "" } ) ;
20
20
21
21
expect ( result . format ) . toBe ( "json" ) ;
22
22
expect ( result . ours ) . toEqual ( { name : "test" , value : 1 } ) ;
@@ -40,7 +40,7 @@ theirs: 2
40
40
41
41
it ( "respects explicit parsers array (json5 fallback)" , async ( ) => {
42
42
const raw = makeConflict ( ` value: 1,` , ` value: 2,` ) ;
43
- const result = await parseConflictContent ( raw , { parsers : [ "json5" ] } ) ;
43
+ const result = await parseConflictContent ( raw , { parsers : [ "json5" ] , filename : "" } ) ;
44
44
expect ( result . format ) . toBe ( "json5" ) ;
45
45
expect ( result . ours ) . toMatchObject ( { value : 1 } ) ;
46
46
expect ( result . theirs ) . toMatchObject ( { value : 2 } ) ;
@@ -53,15 +53,15 @@ theirs: 2
53
53
parser : ( s : string ) => ( { parsed : s . trim ( ) } ) ,
54
54
} ;
55
55
56
- const result = await parseConflictContent ( raw , { parsers : custom } ) ;
56
+ const result = await parseConflictContent ( raw , { parsers : custom , filename : "" } ) ;
57
57
expect ( result . format ) . toBe ( "custom" ) ;
58
58
expect ( result . ours ) . toMatchObject ( { parsed : expect . stringContaining ( "ours-side" ) } ) ;
59
59
expect ( result . theirs ) . toMatchObject ( { parsed : expect . stringContaining ( "theirs-side" ) } ) ;
60
60
} ) ;
61
61
62
62
it ( "throws if parsing fails for all parsers" , async ( ) => {
63
63
const raw = "invalid" ;
64
- await expect ( parseConflictContent ( raw , { parsers : [ "json" ] } ) ) . rejects . toThrow (
64
+ await expect ( parseConflictContent ( raw , { parsers : [ "json" ] , filename : "" } ) ) . rejects . toThrow (
65
65
/ F a i l e d t o p a r s e / ,
66
66
) ;
67
67
} ) ;
@@ -72,6 +72,74 @@ theirs: 2
72
72
only ours
73
73
>>>>>>> theirs
74
74
` ;
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
+ ) ;
76
144
} ) ;
77
145
} ) ;
0 commit comments