json-merge-resolver
Version:
A rules-based JSON conflict resolver that parses Git conflict markers, reconstructs ours/theirs, and merges with deterministic strategies — beyond line-based merges.
3 lines (2 loc) • 4.83 kB
JavaScript
import{a as g,b as k,c as f,d as i}from"./chunk-W2IBPGJM.mjs";import{a as p}from"./chunk-KGHZITSW.mjs";import{b as d}from"./chunk-2SJFRDGH.mjs";import"./chunk-WFDJ25NC.mjs";import"./chunk-Q4E7EFS7.mjs";import"./chunk-JD45QQDD.mjs";import"./chunk-ICS3PEIS.mjs";import"./chunk-NHG7C4LQ.mjs";import"./chunk-IOL4YJZP.mjs";import{k as m}from"./chunk-HB6Q755Y.mjs";import"./chunk-AUIVUOKV.mjs";import{a,b as s,e as u,g as o,h as t}from"./chunk-WP4ACXPN.mjs";import"./chunk-3PFH3U2T.mjs";import"./chunk-76CZ6Z7T.mjs";import l from"fs";import w from"path";import*as y from"child_process";t.mock("node:fs");t.mock("node:child_process");t.mock("./index",()=>({resolveConflicts:t.fn()}));t.mock("./utils",()=>({restoreBackups:t.fn()}));t.mock("./normalizer",()=>({DEFAULT_CONFIG:{defaultStrategy:"merge"}}));t.mock("./merge-processor",()=>({resolveGitMergeFiles:t.fn()}));t.mock("node:url",()=>({pathToFileURL:t.fn(()=>({href:"file:///test/config.js"}))}));a("cli helpers",()=>{u(()=>{t.clearAllMocks()}),a("findGitRoot",()=>{s("returns git root from execSync",()=>{t.spyOn(y,"execSync").mockReturnValue(`/git/root
`);let e=g();o(e).toBe("/git/root")}),s("falls back to process.cwd() on error",()=>{t.spyOn(y,"execSync").mockImplementation(()=>{throw new Error("no git")});let e=g();o(e).toBe(process.cwd())})}),a("parseArgs",()=>{s("parses include/exclude/matcher/debug/sidecar",()=>{let n=i(["node","cli","--include","a.json,b.json","--exclude","c.json","--matcher","micromatch","--debug","--sidecar"]);o(n.overrides).toEqual({include:["a.json","b.json"],exclude:["c.json"],matcher:"micromatch",debug:!0,writeConflictSidecar:!0}),o(n.init).toBe(!1)}),s("sets init flag",()=>{let e=i(["node","cli","--init"]);o(e.init).toBe(!0)}),s("sets restore with undefined",()=>{let e=i(["node","cli","--restore"]);o(e.restore).toBe(void 0)}),s("sets restore with custom directory",()=>{let e=i(["node","cli","--restore","custom-backup"]);o(e.restore).toBe("custom-backup")}),s("warns on unknown option",()=>{let e=t.spyOn(console,"warn").mockImplementation(()=>{});i(["node","cli","--unknown"]),o(e).toHaveBeenCalledWith("Unknown option: --unknown")}),s("detects git merge files with 3 positional args",()=>{let e=i(["node","cli","ours.json","base.json","theirs.json"]);o(e.gitMergeFiles).toEqual(["ours.json","base.json","theirs.json"])}),s("skips positional args in git merge mode with flags",()=>{let e=i(["node","cli","ours.json","base.json","theirs.json","--debug"]);o(e.gitMergeFiles).toEqual(["ours.json","base.json","theirs.json"]),o(e.overrides.debug).toBe(!0)}),s("handles --include without value",()=>{let e=i(["node","cli","--include"]);o(e.overrides.include).toEqual([])}),s("handles --exclude without value",()=>{let e=i(["node","cli","--exclude"]);o(e.overrides.exclude).toEqual([])})}),a("initConfig",()=>{let e="/tmp/test-cli",n=w.join(e,"git-json-resolver.config.js");s("writes starter config if none exists",()=>{l.existsSync.mockReturnValue(!1);let r=t.spyOn(l,"writeFileSync").mockImplementation(()=>{}),c=t.spyOn(console,"log").mockImplementation(()=>{});f(e),o(r).toHaveBeenCalled(),o(c).toHaveBeenCalledWith(`Created starter config at ${n}`)}),s("exits if config already exists",()=>{l.existsSync.mockReturnValue(!0);let r=t.spyOn(process,"exit").mockImplementation(()=>{throw new Error("exit")}),c=t.spyOn(console,"error").mockImplementation(()=>{});o(()=>f(e)).toThrow("exit"),o(c).toHaveBeenCalledWith(`Config file already exists: ${n}`),o(r).toHaveBeenCalledWith(1)})}),a("loadConfigFile",()=>{s("returns {} if no config found",async()=>{l.existsSync.mockReturnValue(!1);let e=await k();o(e).toEqual({})})})});a("CLI execution logic",()=>{u(()=>{t.clearAllMocks()}),s("should handle restore mode logic",async()=>{let e=t.spyOn(console,"log").mockImplementation(()=>{}),n=t.spyOn(process,"exit").mockImplementation(()=>{throw new Error("exit")}),r="custom-backup",c={backupDir:".merge-backups"};try{await m(r||c.backupDir||".merge-backups"),console.log(`Restored backups from ${r}`),process.exit(0)}catch{}o(m).toHaveBeenCalledWith("custom-backup"),o(e).toHaveBeenCalledWith("Restored backups from custom-backup"),o(n).toHaveBeenCalledWith(0)}),s("should handle git merge mode logic",async()=>{let e=["ours.json","base.json","theirs.json"],n={debug:!0},[r,c,h]=e;await d(r,c,h,n),o(d).toHaveBeenCalledWith("ours.json","base.json","theirs.json",{debug:!0})}),s("should handle standard mode logic",async()=>{await p({matcher:"picomatch",debug:!0}),o(p).toHaveBeenCalledWith({matcher:"picomatch",debug:!0})}),s("should handle error case",()=>{let e=t.spyOn(console,"error").mockImplementation(()=>{}),n=t.spyOn(process,"exit").mockImplementation(()=>{throw new Error("exit")}),r=new Error("Test error");try{console.error("Failed:",r),process.exit(1)}catch{}o(e).toHaveBeenCalledWith("Failed:",r),o(n).toHaveBeenCalledWith(1)})});