Skip to content

Commit 9b89246

Browse files
committed
Add unit tests
1 parent b757224 commit 9b89246

File tree

1 file changed

+75
-7
lines changed

1 file changed

+75
-7
lines changed

lib/src/file-parser.test.ts

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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

44
describe("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
/Failed to parse/,
6666
);
6767
});
@@ -72,6 +72,74 @@ theirs: 2
7272
only ours
7373
>>>>>>> theirs
7474
`;
75-
await expect(parseConflictContent(raw, { parsers: "json" })).rejects.toThrow(/empty content/);
75+
await expect(parseConflictContent(raw, { parsers: "json", filename: "" })).rejects.toThrow(
76+
/empty content/,
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(/json5 parser not installed/);
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(/yaml parser not installed/);
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(/toml parser not installed/);
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+
/fast-xml-parser not installed/,
143+
);
76144
});
77145
});

0 commit comments

Comments
 (0)