installer-targets.test.ts 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. /**
  2. * Multi-target installer tests.
  3. *
  4. * Each `AgentTarget` is exercised against the same contract:
  5. * - `install` writes the expected files
  6. * - re-running `install` is byte-identical (idempotent)
  7. * - sibling MCP servers / unrelated config is preserved
  8. * - `uninstall` reverses `install`
  9. * - `printConfig` returns parseable, non-empty content
  10. *
  11. * For agent-config destinations we redirect HOME to a tmpdir via
  12. * `os.homedir` spying, and CWD via `process.chdir` — same pattern as
  13. * the legacy `installer.test.ts`. No real `~/.claude/` etc. ever
  14. * touched.
  15. */
  16. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  17. import * as fs from 'fs';
  18. import * as path from 'path';
  19. import * as os from 'os';
  20. import { ALL_TARGETS, getTarget, resolveTargetFlag } from '../src/installer/targets/registry';
  21. import { uninstallTargets } from '../src/installer';
  22. import { upsertTomlTable, removeTomlTable, buildTomlTable } from '../src/installer/targets/toml';
  23. import { cleanupLegacyHooks } from '../src/installer/targets/claude';
  24. function mkTmpDir(label: string): string {
  25. return fs.mkdtempSync(path.join(os.tmpdir(), `cg-targets-${label}-`));
  26. }
  27. // `os.homedir` is non-configurable on Node, so we redirect it via the
  28. // `$HOME` (POSIX) / `$USERPROFILE` (Windows) env vars that
  29. // `os.homedir()` reads first. Same trick the rest of the suite uses
  30. // when it needs a mock home.
  31. function setHome(dir: string): { restore: () => void } {
  32. const prev = {
  33. HOME: process.env.HOME,
  34. USERPROFILE: process.env.USERPROFILE,
  35. APPDATA: process.env.APPDATA,
  36. XDG_CONFIG_HOME: process.env.XDG_CONFIG_HOME,
  37. HERMES_HOME: process.env.HERMES_HOME,
  38. };
  39. process.env.HOME = dir;
  40. process.env.USERPROFILE = dir;
  41. process.env.APPDATA = path.join(dir, '.config');
  42. process.env.XDG_CONFIG_HOME = path.join(dir, '.config');
  43. delete process.env.HERMES_HOME;
  44. return {
  45. restore() {
  46. if (prev.HOME === undefined) delete process.env.HOME; else process.env.HOME = prev.HOME;
  47. if (prev.USERPROFILE === undefined) delete process.env.USERPROFILE; else process.env.USERPROFILE = prev.USERPROFILE;
  48. if (prev.APPDATA === undefined) delete process.env.APPDATA; else process.env.APPDATA = prev.APPDATA;
  49. if (prev.XDG_CONFIG_HOME === undefined) delete process.env.XDG_CONFIG_HOME; else process.env.XDG_CONFIG_HOME = prev.XDG_CONFIG_HOME;
  50. if (prev.HERMES_HOME === undefined) delete process.env.HERMES_HOME; else process.env.HERMES_HOME = prev.HERMES_HOME;
  51. },
  52. };
  53. }
  54. // A marker-delimited CodeGraph block exactly as a previous installer
  55. // wrote it. Issue #529: the installer no longer writes an instructions
  56. // file, but install (self-heal on upgrade) and uninstall both still
  57. // strip a block a prior install left, so we plant this to exercise it.
  58. const LEGACY_BLOCK = [
  59. '<!-- CODEGRAPH_START -->',
  60. '## CodeGraph',
  61. '',
  62. 'Prefer `codegraph_search` / `codegraph_callers` over grep.',
  63. '<!-- CODEGRAPH_END -->',
  64. ].join('\n');
  65. describe('Installer targets — contract', () => {
  66. let tmpHome: string;
  67. let tmpCwd: string;
  68. let origCwd: string;
  69. let homeRestore: { restore: () => void };
  70. beforeEach(() => {
  71. tmpHome = mkTmpDir('home');
  72. tmpCwd = mkTmpDir('cwd');
  73. origCwd = process.cwd();
  74. process.chdir(tmpCwd);
  75. homeRestore = setHome(tmpHome);
  76. });
  77. afterEach(() => {
  78. homeRestore.restore();
  79. process.chdir(origCwd);
  80. fs.rmSync(tmpHome, { recursive: true, force: true });
  81. fs.rmSync(tmpCwd, { recursive: true, force: true });
  82. });
  83. for (const target of ALL_TARGETS) {
  84. describe(target.id, () => {
  85. const supportedLocations = (['global', 'local'] as const).filter((l) =>
  86. target.supportsLocation(l),
  87. );
  88. for (const location of supportedLocations) {
  89. describe(`location=${location}`, () => {
  90. it('install writes files; detect.alreadyConfigured becomes true', () => {
  91. expect(target.detect(location).alreadyConfigured).toBe(false);
  92. const result = target.install(location, { autoAllow: true });
  93. expect(result.files.length).toBeGreaterThan(0);
  94. for (const file of result.files) {
  95. if (file.action !== 'unchanged') {
  96. expect(fs.existsSync(file.path)).toBe(true);
  97. }
  98. }
  99. expect(target.detect(location).alreadyConfigured).toBe(true);
  100. });
  101. it('re-running install is idempotent (no actions other than unchanged)', () => {
  102. target.install(location, { autoAllow: true });
  103. const second = target.install(location, { autoAllow: true });
  104. for (const file of second.files) {
  105. expect(file.action).toBe('unchanged');
  106. }
  107. });
  108. it('install preserves a pre-existing sibling MCP server (where applicable)', () => {
  109. // Plant a sibling entry in the same JSON config, install,
  110. // and verify the sibling survives. Skip for Codex (TOML)
  111. // and any target with no JSON config — they get covered
  112. // by their own dedicated tests below.
  113. const paths = target.describePaths(location);
  114. // Match .json or .jsonc — opencode prefers .jsonc.
  115. const jsonPath = paths.find((p) => /\.jsonc?$/.test(p));
  116. if (!jsonPath) return;
  117. // Seed pre-existing config.
  118. fs.mkdirSync(path.dirname(jsonPath), { recursive: true });
  119. const seed: Record<string, any> = { mcpServers: { other: { command: 'x' } } };
  120. // opencode uses `mcp` not `mcpServers`. Match its shape too.
  121. if (target.id === 'opencode') {
  122. delete seed.mcpServers;
  123. seed.mcp = { other: { type: 'local', command: ['x'], enabled: true } };
  124. }
  125. fs.writeFileSync(jsonPath, JSON.stringify(seed, null, 2) + '\n');
  126. target.install(location, { autoAllow: true });
  127. const after = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'));
  128. if (target.id === 'opencode') {
  129. expect(after.mcp.other).toBeDefined();
  130. expect(after.mcp.codegraph).toBeDefined();
  131. } else {
  132. expect(after.mcpServers.other).toBeDefined();
  133. expect(after.mcpServers.codegraph).toBeDefined();
  134. }
  135. });
  136. it('uninstall reverses install (alreadyConfigured returns to false)', () => {
  137. target.install(location, { autoAllow: true });
  138. expect(target.detect(location).alreadyConfigured).toBe(true);
  139. target.uninstall(location);
  140. expect(target.detect(location).alreadyConfigured).toBe(false);
  141. });
  142. it('printConfig returns non-empty output without writing anything', () => {
  143. const before = listAllFiles(tmpHome).concat(listAllFiles(tmpCwd));
  144. const out = target.printConfig(location);
  145. expect(out.length).toBeGreaterThan(0);
  146. const after = listAllFiles(tmpHome).concat(listAllFiles(tmpCwd));
  147. expect(after.sort()).toEqual(before.sort());
  148. });
  149. });
  150. }
  151. });
  152. }
  153. });
  154. describe('Installer targets — partial-state idempotency', () => {
  155. let tmpHome: string;
  156. let tmpCwd: string;
  157. let origCwd: string;
  158. let homeRestore: { restore: () => void };
  159. beforeEach(() => {
  160. tmpHome = mkTmpDir('home');
  161. tmpCwd = mkTmpDir('cwd');
  162. origCwd = process.cwd();
  163. process.chdir(tmpCwd);
  164. homeRestore = setHome(tmpHome);
  165. });
  166. afterEach(() => {
  167. homeRestore.restore();
  168. process.chdir(origCwd);
  169. fs.rmSync(tmpHome, { recursive: true, force: true });
  170. fs.rmSync(tmpCwd, { recursive: true, force: true });
  171. });
  172. it('codex: install writes config.toml but never an AGENTS.md instructions file (#529)', () => {
  173. const codex = getTarget('codex')!;
  174. const first = codex.install('global', { autoAllow: false });
  175. const agentsMd = path.join(tmpHome, '.codex', 'AGENTS.md');
  176. // No instructions file is created, and no file action references it.
  177. expect(fs.existsSync(agentsMd)).toBe(false);
  178. expect(first.files.some((f) => f.path.endsWith('AGENTS.md'))).toBe(false);
  179. expect(first.files.some((f) => f.path.endsWith('config.toml'))).toBe(true);
  180. // Re-install is fully unchanged (config.toml only, nothing to strip).
  181. const second = codex.install('global', { autoAllow: false });
  182. for (const f of second.files) expect(f.action).toBe('unchanged');
  183. });
  184. it('codex: install strips a legacy AGENTS.md codegraph block, keeping user content (#529)', () => {
  185. const codex = getTarget('codex')!;
  186. const dir = path.join(tmpHome, '.codex');
  187. fs.mkdirSync(dir, { recursive: true });
  188. const agentsMd = path.join(dir, 'AGENTS.md');
  189. fs.writeFileSync(agentsMd, `# My codex notes\n\nBe terse.\n\n${LEGACY_BLOCK}\n`);
  190. const result = codex.install('global', { autoAllow: false });
  191. const body = fs.readFileSync(agentsMd, 'utf-8');
  192. expect(body).toContain('# My codex notes');
  193. expect(body).toContain('Be terse.');
  194. expect(body).not.toContain('CODEGRAPH_START');
  195. // The strip is reported as a 'removed' action on AGENTS.md.
  196. const mdEntry = result.files.find((f) => f.path.endsWith('AGENTS.md'));
  197. expect(mdEntry?.action).toBe('removed');
  198. });
  199. it('opencode: prefers .jsonc when both .json and .jsonc exist', () => {
  200. const opencode = getTarget('opencode')!;
  201. const dir = path.join(tmpHome, '.config', 'opencode');
  202. fs.mkdirSync(dir, { recursive: true });
  203. fs.writeFileSync(path.join(dir, 'opencode.json'), '{\n "$schema": "https://opencode.ai/config.json"\n}\n');
  204. fs.writeFileSync(path.join(dir, 'opencode.jsonc'), '{\n "$schema": "https://opencode.ai/config.json"\n}\n');
  205. const result = opencode.install('global', { autoAllow: true });
  206. const written = result.files.find((f) => /\.jsonc$/.test(f.path))!;
  207. expect(written).toBeDefined();
  208. expect(written.action).not.toBe('not-found');
  209. // The .json file is left alone.
  210. const jsonText = fs.readFileSync(path.join(dir, 'opencode.json'), 'utf-8');
  211. expect(jsonText).not.toContain('codegraph');
  212. });
  213. it('opencode: uses .json when only .json exists (no .jsonc)', () => {
  214. const opencode = getTarget('opencode')!;
  215. const dir = path.join(tmpHome, '.config', 'opencode');
  216. fs.mkdirSync(dir, { recursive: true });
  217. fs.writeFileSync(path.join(dir, 'opencode.json'), '{\n "$schema": "https://opencode.ai/config.json"\n}\n');
  218. const result = opencode.install('global', { autoAllow: true });
  219. expect(result.files[0].path).toMatch(/opencode\.json$/);
  220. expect(fs.existsSync(path.join(dir, 'opencode.jsonc'))).toBe(false);
  221. });
  222. it('opencode: defaults to .jsonc for fresh installs (no existing file)', () => {
  223. const opencode = getTarget('opencode')!;
  224. const result = opencode.install('global', { autoAllow: true });
  225. expect(result.files[0].path).toMatch(/opencode\.jsonc$/);
  226. expect(result.files[0].action).toBe('created');
  227. });
  228. it('opencode: preserves line and block comments through install + idempotent re-run', () => {
  229. const opencode = getTarget('opencode')!;
  230. const dir = path.join(tmpHome, '.config', 'opencode');
  231. fs.mkdirSync(dir, { recursive: true });
  232. const file = path.join(dir, 'opencode.jsonc');
  233. const original = [
  234. '{',
  235. ' // top-level note about my opencode setup',
  236. ' "$schema": "https://opencode.ai/config.json",',
  237. ' /* multi-line block comment',
  238. ' describing the providers section */',
  239. ' "providers": {',
  240. ' "anthropic": { "model": "claude-opus-4-7" } // pinned',
  241. ' }',
  242. '}',
  243. '',
  244. ].join('\n');
  245. fs.writeFileSync(file, original);
  246. opencode.install('global', { autoAllow: true });
  247. const afterInstall = fs.readFileSync(file, 'utf-8');
  248. expect(afterInstall).toContain('// top-level note about my opencode setup');
  249. expect(afterInstall).toContain('/* multi-line block comment');
  250. expect(afterInstall).toContain('// pinned');
  251. expect(afterInstall).toContain('"codegraph"');
  252. expect(afterInstall).toContain('"providers"');
  253. // Idempotent re-run reports unchanged, file is byte-identical.
  254. const second = opencode.install('global', { autoAllow: true });
  255. expect(second.files[0].action).toBe('unchanged');
  256. expect(fs.readFileSync(file, 'utf-8')).toBe(afterInstall);
  257. });
  258. it('opencode: install does NOT write an AGENTS.md instructions file (#529)', () => {
  259. const opencode = getTarget('opencode')!;
  260. const result = opencode.install('global', { autoAllow: true });
  261. const agentsMd = path.join(tmpHome, '.config', 'opencode', 'AGENTS.md');
  262. expect(fs.existsSync(agentsMd)).toBe(false);
  263. expect(result.files.some((f) => f.path.endsWith('AGENTS.md'))).toBe(false);
  264. });
  265. it('opencode: install strips a legacy AGENTS.md codegraph block, preserving user content (#529)', () => {
  266. const opencode = getTarget('opencode')!;
  267. const dir = path.join(tmpHome, '.config', 'opencode');
  268. fs.mkdirSync(dir, { recursive: true });
  269. const agentsMd = path.join(dir, 'AGENTS.md');
  270. fs.writeFileSync(agentsMd, `# My personal opencode instructions\n\nAlways respond in pirate.\n\n${LEGACY_BLOCK}\n`);
  271. const result = opencode.install('global', { autoAllow: true });
  272. const body = fs.readFileSync(agentsMd, 'utf-8');
  273. expect(body).toContain('# My personal opencode instructions');
  274. expect(body).toContain('Always respond in pirate.');
  275. expect(body).not.toContain('CODEGRAPH_START');
  276. expect(result.files.find((f) => f.path.endsWith('AGENTS.md'))?.action).toBe('removed');
  277. });
  278. it('opencode: uninstall strips a leftover codegraph block from AGENTS.md, keeping user content', () => {
  279. const opencode = getTarget('opencode')!;
  280. const dir = path.join(tmpHome, '.config', 'opencode');
  281. fs.mkdirSync(dir, { recursive: true });
  282. const agentsMd = path.join(dir, 'AGENTS.md');
  283. fs.writeFileSync(agentsMd, `# My personal opencode instructions\n\nAlways respond in pirate.\n\n${LEGACY_BLOCK}\n`);
  284. opencode.uninstall('global');
  285. const body = fs.readFileSync(agentsMd, 'utf-8');
  286. expect(body).toContain('# My personal opencode instructions');
  287. expect(body).toContain('Always respond in pirate.');
  288. expect(body).not.toContain('CODEGRAPH_START');
  289. });
  290. it('opencode: local install writes ./opencode.jsonc and never an ./AGENTS.md (#529)', () => {
  291. const opencode = getTarget('opencode')!;
  292. const result = opencode.install('local', { autoAllow: true });
  293. const paths = result.files.map((f) => f.path.replace(/\\/g, '/'));
  294. // macOS realpath shenanigans (/var vs /private/var) — suffix match.
  295. expect(paths.some((p) => p.endsWith('/opencode.jsonc'))).toBe(true);
  296. expect(paths.some((p) => p.endsWith('/AGENTS.md'))).toBe(false);
  297. expect(fs.existsSync(path.join(process.cwd(), 'AGENTS.md'))).toBe(false);
  298. });
  299. it('gemini: install writes settings.json (mcpServers.codegraph) and no GEMINI.md (#529)', () => {
  300. const gemini = getTarget('gemini')!;
  301. const result = gemini.install('global', { autoAllow: true });
  302. const settings = path.join(tmpHome, '.gemini', 'settings.json');
  303. const geminiMd = path.join(tmpHome, '.gemini', 'GEMINI.md');
  304. expect(result.files.some((f) => f.path === settings)).toBe(true);
  305. expect(result.files.some((f) => f.path === geminiMd)).toBe(false);
  306. expect(fs.existsSync(geminiMd)).toBe(false);
  307. const cfg = JSON.parse(fs.readFileSync(settings, 'utf-8'));
  308. expect(cfg.mcpServers.codegraph).toEqual({ type: 'stdio', command: 'codegraph', args: ['serve', '--mcp'] });
  309. });
  310. it('gemini: install preserves pre-existing settings (security.auth survives)', () => {
  311. const gemini = getTarget('gemini')!;
  312. const settings = path.join(tmpHome, '.gemini', 'settings.json');
  313. fs.mkdirSync(path.dirname(settings), { recursive: true });
  314. fs.writeFileSync(settings, JSON.stringify({
  315. security: { auth: { selectedType: 'oauth-personal' } },
  316. }, null, 2) + '\n');
  317. gemini.install('global', { autoAllow: true });
  318. const after = JSON.parse(fs.readFileSync(settings, 'utf-8'));
  319. expect(after.security?.auth?.selectedType).toBe('oauth-personal');
  320. expect(after.mcpServers?.codegraph).toBeDefined();
  321. });
  322. it('gemini: uninstall strips codegraph but leaves pre-existing settings (security.auth) intact', () => {
  323. const gemini = getTarget('gemini')!;
  324. const settings = path.join(tmpHome, '.gemini', 'settings.json');
  325. fs.mkdirSync(path.dirname(settings), { recursive: true });
  326. fs.writeFileSync(settings, JSON.stringify({
  327. security: { auth: { selectedType: 'oauth-personal' } },
  328. }, null, 2) + '\n');
  329. gemini.install('global', { autoAllow: true });
  330. gemini.uninstall('global');
  331. const after = JSON.parse(fs.readFileSync(settings, 'utf-8'));
  332. expect(after.security?.auth?.selectedType).toBe('oauth-personal');
  333. expect(after.mcpServers).toBeUndefined();
  334. });
  335. it('gemini: local install writes ./.gemini/settings.json and never a ./GEMINI.md (#529)', () => {
  336. const gemini = getTarget('gemini')!;
  337. const result = gemini.install('local', { autoAllow: true });
  338. const paths = result.files.map((f) => f.path.replace(/\\/g, '/'));
  339. expect(paths.some((p) => p.endsWith('/.gemini/settings.json'))).toBe(true);
  340. expect(paths.some((p) => p.endsWith('/GEMINI.md'))).toBe(false);
  341. expect(fs.existsSync(path.join(process.cwd(), 'GEMINI.md'))).toBe(false);
  342. });
  343. it('gemini: uninstall strips a leftover GEMINI.md codegraph block, keeping user content', () => {
  344. const gemini = getTarget('gemini')!;
  345. const geminiMd = path.join(tmpHome, '.gemini', 'GEMINI.md');
  346. fs.mkdirSync(path.dirname(geminiMd), { recursive: true });
  347. fs.writeFileSync(geminiMd, `# My personal Gemini context\n\nAlways respond concisely.\n\n${LEGACY_BLOCK}\n`);
  348. gemini.uninstall('global');
  349. const body = fs.readFileSync(geminiMd, 'utf-8');
  350. expect(body).toContain('# My personal Gemini context');
  351. expect(body).toContain('Always respond concisely.');
  352. expect(body).not.toContain('CODEGRAPH_START');
  353. });
  354. it('kiro: install writes settings/mcp.json (mcpServers.codegraph) and no steering doc (#529)', () => {
  355. const kiro = getTarget('kiro')!;
  356. const result = kiro.install('global', { autoAllow: true });
  357. const mcp = path.join(tmpHome, '.kiro', 'settings', 'mcp.json');
  358. const steering = path.join(tmpHome, '.kiro', 'steering', 'codegraph.md');
  359. expect(result.files.some((f) => f.path === mcp)).toBe(true);
  360. expect(result.files.some((f) => f.path === steering)).toBe(false);
  361. expect(fs.existsSync(steering)).toBe(false);
  362. const cfg = JSON.parse(fs.readFileSync(mcp, 'utf-8'));
  363. expect(cfg.mcpServers.codegraph).toEqual({ type: 'stdio', command: 'codegraph', args: ['serve', '--mcp'] });
  364. });
  365. it('kiro: install deletes a leftover steering codegraph.md (self-heal) (#529)', () => {
  366. const kiro = getTarget('kiro')!;
  367. const steering = path.join(tmpHome, '.kiro', 'steering', 'codegraph.md');
  368. fs.mkdirSync(path.dirname(steering), { recursive: true });
  369. fs.writeFileSync(steering, `${LEGACY_BLOCK}\n`);
  370. const result = kiro.install('global', { autoAllow: true });
  371. expect(fs.existsSync(steering)).toBe(false);
  372. expect(result.files.find((f) => f.path === steering)?.action).toBe('removed');
  373. });
  374. it('kiro: install preserves a pre-existing sibling MCP server in mcp.json', () => {
  375. const kiro = getTarget('kiro')!;
  376. const mcp = path.join(tmpHome, '.kiro', 'settings', 'mcp.json');
  377. fs.mkdirSync(path.dirname(mcp), { recursive: true });
  378. fs.writeFileSync(mcp, JSON.stringify({
  379. mcpServers: { other: { command: 'uvx', args: ['other-server'] } },
  380. }, null, 2) + '\n');
  381. kiro.install('global', { autoAllow: true });
  382. const after = JSON.parse(fs.readFileSync(mcp, 'utf-8'));
  383. expect(after.mcpServers.other).toBeDefined();
  384. expect(after.mcpServers.codegraph).toBeDefined();
  385. });
  386. it('kiro: uninstall strips codegraph but leaves sibling MCP servers intact', () => {
  387. const kiro = getTarget('kiro')!;
  388. const mcp = path.join(tmpHome, '.kiro', 'settings', 'mcp.json');
  389. fs.mkdirSync(path.dirname(mcp), { recursive: true });
  390. fs.writeFileSync(mcp, JSON.stringify({
  391. mcpServers: { other: { command: 'uvx', args: ['other-server'] } },
  392. }, null, 2) + '\n');
  393. kiro.install('global', { autoAllow: true });
  394. kiro.uninstall('global');
  395. const after = JSON.parse(fs.readFileSync(mcp, 'utf-8'));
  396. expect(after.mcpServers.other).toBeDefined();
  397. expect(after.mcpServers.codegraph).toBeUndefined();
  398. });
  399. it('kiro: uninstall removes a leftover steering codegraph.md file outright', () => {
  400. const kiro = getTarget('kiro')!;
  401. const steering = path.join(tmpHome, '.kiro', 'steering', 'codegraph.md');
  402. fs.mkdirSync(path.dirname(steering), { recursive: true });
  403. fs.writeFileSync(steering, `${LEGACY_BLOCK}\n`);
  404. kiro.uninstall('global');
  405. expect(fs.existsSync(steering)).toBe(false);
  406. });
  407. it('kiro: uninstall removes our steering doc but leaves a sibling (product.md) untouched', () => {
  408. const kiro = getTarget('kiro')!;
  409. const sibling = path.join(tmpHome, '.kiro', 'steering', 'product.md');
  410. const ours = path.join(tmpHome, '.kiro', 'steering', 'codegraph.md');
  411. fs.mkdirSync(path.dirname(sibling), { recursive: true });
  412. fs.writeFileSync(sibling, '# Product\n\nMy team practices.\n');
  413. fs.writeFileSync(ours, `${LEGACY_BLOCK}\n`);
  414. kiro.uninstall('global');
  415. expect(fs.existsSync(ours)).toBe(false);
  416. expect(fs.existsSync(sibling)).toBe(true);
  417. expect(fs.readFileSync(sibling, 'utf-8')).toContain('My team practices.');
  418. });
  419. it('kiro: local install writes ./.kiro/settings/mcp.json and no steering doc (#529)', () => {
  420. const kiro = getTarget('kiro')!;
  421. const result = kiro.install('local', { autoAllow: true });
  422. const paths = result.files.map((f) => f.path.replace(/\\/g, '/'));
  423. expect(paths.some((p) => p.endsWith('/.kiro/settings/mcp.json'))).toBe(true);
  424. expect(paths.some((p) => p.endsWith('/.kiro/steering/codegraph.md'))).toBe(false);
  425. });
  426. it('antigravity: install writes to LEGACY ~/.gemini/antigravity/mcp_config.json when no migration marker', () => {
  427. const antigravity = getTarget('antigravity')!;
  428. antigravity.install('global', { autoAllow: true });
  429. const legacyFile = path.join(tmpHome, '.gemini', 'antigravity', 'mcp_config.json');
  430. expect(fs.existsSync(legacyFile)).toBe(true);
  431. const cfg = JSON.parse(fs.readFileSync(legacyFile, 'utf-8'));
  432. expect(cfg.mcpServers.codegraph).toBeDefined();
  433. // Crucially: does NOT touch the Gemini CLI's settings.json.
  434. expect(fs.existsSync(path.join(tmpHome, '.gemini', 'settings.json'))).toBe(false);
  435. });
  436. it('antigravity: install writes to UNIFIED ~/.gemini/config/mcp_config.json when .migrated marker present', () => {
  437. const antigravity = getTarget('antigravity')!;
  438. // Plant the migration marker — same signal Antigravity itself drops
  439. // when it migrates a user's config.
  440. const unifiedDir = path.join(tmpHome, '.gemini', 'config');
  441. fs.mkdirSync(unifiedDir, { recursive: true });
  442. fs.writeFileSync(path.join(unifiedDir, '.migrated'), '');
  443. antigravity.install('global', { autoAllow: true });
  444. const unifiedFile = path.join(unifiedDir, 'mcp_config.json');
  445. expect(fs.existsSync(unifiedFile)).toBe(true);
  446. const cfg = JSON.parse(fs.readFileSync(unifiedFile, 'utf-8'));
  447. expect(cfg.mcpServers.codegraph).toBeDefined();
  448. // Legacy path is NOT touched when the marker tells us migration happened.
  449. expect(fs.existsSync(path.join(tmpHome, '.gemini', 'antigravity', 'mcp_config.json'))).toBe(false);
  450. });
  451. it('antigravity: install writes to UNIFIED path when ~/.gemini/config/mcp_config.json already exists (even without marker)', () => {
  452. const antigravity = getTarget('antigravity')!;
  453. // Antigravity creates this file on first launch post-migration — its
  454. // presence is the second signal we accept, in case the .migrated
  455. // marker semantics change across Antigravity versions.
  456. const unifiedFile = path.join(tmpHome, '.gemini', 'config', 'mcp_config.json');
  457. fs.mkdirSync(path.dirname(unifiedFile), { recursive: true });
  458. fs.writeFileSync(unifiedFile, JSON.stringify({ mcpServers: {} }, null, 2) + '\n');
  459. antigravity.install('global', { autoAllow: true });
  460. const cfg = JSON.parse(fs.readFileSync(unifiedFile, 'utf-8'));
  461. expect(cfg.mcpServers.codegraph).toBeDefined();
  462. });
  463. it('antigravity: entry has NO `type` field (Antigravity rejects entries with it)', () => {
  464. const antigravity = getTarget('antigravity')!;
  465. // Marker → unified path; doesn't matter which path, just inspect the entry shape.
  466. fs.mkdirSync(path.join(tmpHome, '.gemini', 'config'), { recursive: true });
  467. fs.writeFileSync(path.join(tmpHome, '.gemini', 'config', '.migrated'), '');
  468. antigravity.install('global', { autoAllow: true });
  469. const cfg = JSON.parse(fs.readFileSync(
  470. path.join(tmpHome, '.gemini', 'config', 'mcp_config.json'), 'utf-8'
  471. ));
  472. expect(cfg.mcpServers.codegraph.type).toBeUndefined();
  473. expect(cfg.mcpServers.codegraph.command).toBeDefined();
  474. expect(cfg.mcpServers.codegraph.args).toEqual(['serve', '--mcp']);
  475. });
  476. it('antigravity: install migrates a legacy codegraph entry to the unified path when marker appears', () => {
  477. const antigravity = getTarget('antigravity')!;
  478. // Simulate: user installed on the legacy path, then Antigravity
  479. // migrated their config (dropped the `.migrated` marker + created
  480. // the unified file). Re-running codegraph install should land
  481. // codegraph in the new file AND strip the stale legacy entry.
  482. const legacyFile = path.join(tmpHome, '.gemini', 'antigravity', 'mcp_config.json');
  483. fs.mkdirSync(path.dirname(legacyFile), { recursive: true });
  484. fs.writeFileSync(legacyFile, JSON.stringify({
  485. mcpServers: { codegraph: { command: 'codegraph', args: ['serve', '--mcp'] } },
  486. }, null, 2) + '\n');
  487. fs.mkdirSync(path.join(tmpHome, '.gemini', 'config'), { recursive: true });
  488. fs.writeFileSync(path.join(tmpHome, '.gemini', 'config', '.migrated'), '');
  489. antigravity.install('global', { autoAllow: true });
  490. const unified = JSON.parse(fs.readFileSync(
  491. path.join(tmpHome, '.gemini', 'config', 'mcp_config.json'), 'utf-8'
  492. ));
  493. expect(unified.mcpServers.codegraph).toBeDefined();
  494. // Legacy file's codegraph entry got stripped.
  495. const legacy = JSON.parse(fs.readFileSync(legacyFile, 'utf-8'));
  496. expect(legacy.mcpServers).toBeUndefined();
  497. });
  498. it('antigravity: install preserves a sibling MCP server in mcp_config.json (legacy path)', () => {
  499. const antigravity = getTarget('antigravity')!;
  500. const mcpFile = path.join(tmpHome, '.gemini', 'antigravity', 'mcp_config.json');
  501. fs.mkdirSync(path.dirname(mcpFile), { recursive: true });
  502. fs.writeFileSync(mcpFile, JSON.stringify({
  503. mcpServers: { other: { command: 'uvx', args: ['other-server'] } },
  504. }, null, 2) + '\n');
  505. antigravity.install('global', { autoAllow: true });
  506. const after = JSON.parse(fs.readFileSync(mcpFile, 'utf-8'));
  507. expect(after.mcpServers.other).toBeDefined();
  508. expect(after.mcpServers.codegraph).toBeDefined();
  509. });
  510. it('antigravity: install preserves Antigravity-managed fields on sibling servers (e.g. disabled flag)', () => {
  511. const antigravity = getTarget('antigravity')!;
  512. // Antigravity adds `"disabled": true` to entries the user disables via
  513. // the IDE. Install must not clobber that on sibling entries.
  514. fs.mkdirSync(path.join(tmpHome, '.gemini', 'config'), { recursive: true });
  515. fs.writeFileSync(path.join(tmpHome, '.gemini', 'config', '.migrated'), '');
  516. const unified = path.join(tmpHome, '.gemini', 'config', 'mcp_config.json');
  517. fs.writeFileSync(unified, JSON.stringify({
  518. mcpServers: {
  519. 'code-review-graph': {
  520. command: 'uvx', args: ['code-review-graph', 'serve'], disabled: true,
  521. },
  522. },
  523. }, null, 2) + '\n');
  524. antigravity.install('global', { autoAllow: true });
  525. const after = JSON.parse(fs.readFileSync(unified, 'utf-8'));
  526. expect(after.mcpServers['code-review-graph'].disabled).toBe(true);
  527. expect(after.mcpServers.codegraph).toBeDefined();
  528. });
  529. it('antigravity: uninstall removes only codegraph, sibling MCP server survives', () => {
  530. const antigravity = getTarget('antigravity')!;
  531. const mcpFile = path.join(tmpHome, '.gemini', 'antigravity', 'mcp_config.json');
  532. fs.mkdirSync(path.dirname(mcpFile), { recursive: true });
  533. fs.writeFileSync(mcpFile, JSON.stringify({
  534. mcpServers: { other: { command: 'uvx', args: ['other-server'] } },
  535. }, null, 2) + '\n');
  536. antigravity.install('global', { autoAllow: true });
  537. antigravity.uninstall('global');
  538. const after = JSON.parse(fs.readFileSync(mcpFile, 'utf-8'));
  539. expect(after.mcpServers.other).toBeDefined();
  540. expect(after.mcpServers.codegraph).toBeUndefined();
  541. });
  542. it('antigravity: uninstall sweeps BOTH legacy and unified paths (handles migration half-state)', () => {
  543. const antigravity = getTarget('antigravity')!;
  544. // User had codegraph in BOTH files (e.g. legacy install + post-migration
  545. // re-install before our migration cleanup landed). Uninstall must clean
  546. // both so a "fresh slate" really is fresh.
  547. const legacy = path.join(tmpHome, '.gemini', 'antigravity', 'mcp_config.json');
  548. const unified = path.join(tmpHome, '.gemini', 'config', 'mcp_config.json');
  549. fs.mkdirSync(path.dirname(legacy), { recursive: true });
  550. fs.mkdirSync(path.dirname(unified), { recursive: true });
  551. fs.writeFileSync(legacy, JSON.stringify({
  552. mcpServers: { codegraph: { command: 'codegraph', args: ['serve', '--mcp'] } },
  553. }, null, 2) + '\n');
  554. fs.writeFileSync(unified, JSON.stringify({
  555. mcpServers: { codegraph: { command: 'codegraph', args: ['serve', '--mcp'] } },
  556. }, null, 2) + '\n');
  557. fs.writeFileSync(path.join(path.dirname(unified), '.migrated'), '');
  558. antigravity.uninstall('global');
  559. const legacyAfter = JSON.parse(fs.readFileSync(legacy, 'utf-8'));
  560. const unifiedAfter = JSON.parse(fs.readFileSync(unified, 'utf-8'));
  561. expect(legacyAfter.mcpServers).toBeUndefined();
  562. expect(unifiedAfter.mcpServers).toBeUndefined();
  563. });
  564. it('antigravity: rejects --location=local with a clear note (global-only IDE)', () => {
  565. const antigravity = getTarget('antigravity')!;
  566. expect(antigravity.supportsLocation('local')).toBe(false);
  567. const result = antigravity.install('local', { autoAllow: true });
  568. expect(result.files).toEqual([]);
  569. expect(result.notes?.join(' ')).toMatch(/no project-local config/);
  570. });
  571. it('antigravity: does not write GEMINI.md (only gemini target owns instructions)', () => {
  572. const antigravity = getTarget('antigravity')!;
  573. antigravity.install('global', { autoAllow: true });
  574. const geminiMd = path.join(tmpHome, '.gemini', 'GEMINI.md');
  575. expect(fs.existsSync(geminiMd)).toBe(false);
  576. });
  577. it('gemini + antigravity: both installed coexist (separate MCP files, shared GEMINI.md)', () => {
  578. const gemini = getTarget('gemini')!;
  579. const antigravity = getTarget('antigravity')!;
  580. gemini.install('global', { autoAllow: true });
  581. antigravity.install('global', { autoAllow: true });
  582. const cliCfg = JSON.parse(fs.readFileSync(path.join(tmpHome, '.gemini', 'settings.json'), 'utf-8'));
  583. // Antigravity lands on the LEGACY path here since no .migrated marker
  584. // was planted — same end-to-end check either way.
  585. const ideCfg = JSON.parse(fs.readFileSync(path.join(tmpHome, '.gemini', 'antigravity', 'mcp_config.json'), 'utf-8'));
  586. expect(cliCfg.mcpServers.codegraph).toBeDefined();
  587. expect(ideCfg.mcpServers.codegraph).toBeDefined();
  588. // Uninstall one — the other's MCP entry must survive.
  589. antigravity.uninstall('global');
  590. const cliAfter = JSON.parse(fs.readFileSync(path.join(tmpHome, '.gemini', 'settings.json'), 'utf-8'));
  591. expect(cliAfter.mcpServers.codegraph).toBeDefined();
  592. });
  593. it('hermes: install adds codegraph MCP server and cli toolset, preserving existing yaml', () => {
  594. const hermes = getTarget('hermes')!;
  595. const config = path.join(tmpHome, '.hermes', 'config.yaml');
  596. fs.mkdirSync(path.dirname(config), { recursive: true });
  597. fs.writeFileSync(config, [
  598. 'model:',
  599. ' default: qwen-3.7',
  600. 'mcp_servers:',
  601. ' other:',
  602. ' command: other',
  603. 'platform_toolsets:',
  604. ' cli:',
  605. ' - hermes-cli',
  606. ' discord:',
  607. ' - hermes-discord',
  608. '',
  609. ].join('\n'));
  610. const result = hermes.install('global', { autoAllow: true });
  611. expect(result.files[0].action).toBe('updated');
  612. const body = fs.readFileSync(config, 'utf-8');
  613. expect(body).toContain('model:\n default: qwen-3.7');
  614. expect(body).toContain('mcp_servers:\n other:\n command: other');
  615. expect(body).toContain(' codegraph:\n command: codegraph');
  616. expect(body).toContain(' - hermes-cli');
  617. expect(body).toContain(' - mcp-codegraph');
  618. expect(body).toContain(' discord:\n - hermes-discord');
  619. const second = hermes.install('global', { autoAllow: true });
  620. expect(second.files[0].action).toBe('unchanged');
  621. });
  622. it('hermes: uninstall removes only codegraph MCP server and toolset entry', () => {
  623. const hermes = getTarget('hermes')!;
  624. const config = path.join(tmpHome, '.hermes', 'config.yaml');
  625. fs.mkdirSync(path.dirname(config), { recursive: true });
  626. hermes.install('global', { autoAllow: true });
  627. fs.appendFileSync(config, 'custom:\n keep: true\n');
  628. hermes.uninstall('global');
  629. const body = fs.readFileSync(config, 'utf-8');
  630. expect(body).not.toContain('codegraph:');
  631. expect(body).not.toContain('mcp-codegraph');
  632. expect(body).toContain('custom:\n keep: true');
  633. });
  634. // Regression for #456: PyYAML's default block style writes list items at the
  635. // SAME indent as the parent key (`cli:` and its `- hermes-cli` are both at
  636. // indent 2). The pre-fix line-based patcher mistook that first list item for
  637. // the next sibling key, truncated the cli block, and spliced `- mcp-codegraph`
  638. // at indent 4 BEFORE the existing items — producing unparseable YAML.
  639. it('hermes: install preserves PyYAML-default list-at-same-indent style (issue #456)', () => {
  640. const hermes = getTarget('hermes')!;
  641. const config = path.join(tmpHome, '.hermes', 'config.yaml');
  642. fs.mkdirSync(path.dirname(config), { recursive: true });
  643. const original = [
  644. 'model:',
  645. ' default: gpt-4o',
  646. 'platform_toolsets:',
  647. ' cli:',
  648. ' - hermes-cli',
  649. ' - browser',
  650. ' - clarify',
  651. ' - terminal',
  652. ' - web',
  653. ' telegram:',
  654. ' - hermes-telegram',
  655. ' discord:',
  656. ' - hermes-discord',
  657. '',
  658. ].join('\n');
  659. fs.writeFileSync(config, original);
  660. hermes.install('global', { autoAllow: true });
  661. const body = fs.readFileSync(config, 'utf-8');
  662. // mcp-codegraph appended at the same 2-space indent as existing items
  663. expect(body).toContain('\n - mcp-codegraph\n');
  664. // hermes-cli preserved
  665. expect(body).toContain('\n - hermes-cli\n');
  666. // Sibling sections kept their indent — `telegram:` is still a key under
  667. // platform_toolsets, not promoted up.
  668. expect(body).toContain('\n telegram:\n - hermes-telegram\n');
  669. expect(body).toContain('\n discord:\n - hermes-discord\n');
  670. // No list items leaked to the platform_toolsets level (indent 0).
  671. expect(body).not.toMatch(/^- browser/m);
  672. expect(body).not.toMatch(/^- hermes-telegram/m);
  673. // The whole platform_toolsets block extracted by line search should
  674. // start with `cli:` and not contain a stray 4-space `mcp-codegraph`
  675. // appearing before the rest of the existing items.
  676. expect(body).toContain(' cli:\n - hermes-cli\n - browser');
  677. // Idempotent
  678. const second = hermes.install('global', { autoAllow: true });
  679. expect(second.files[0]?.action).toBe('unchanged');
  680. });
  681. it('hermes: uninstall reverses the install on a PyYAML-default config', () => {
  682. const hermes = getTarget('hermes')!;
  683. const config = path.join(tmpHome, '.hermes', 'config.yaml');
  684. fs.mkdirSync(path.dirname(config), { recursive: true });
  685. const original = [
  686. 'platform_toolsets:',
  687. ' cli:',
  688. ' - hermes-cli',
  689. ' - browser',
  690. ' telegram:',
  691. ' - hermes-telegram',
  692. '',
  693. ].join('\n');
  694. fs.writeFileSync(config, original);
  695. hermes.install('global', { autoAllow: true });
  696. const installed = fs.readFileSync(config, 'utf-8');
  697. expect(installed).toContain('- mcp-codegraph');
  698. expect(installed).toContain('codegraph:');
  699. hermes.uninstall('global');
  700. const body = fs.readFileSync(config, 'utf-8');
  701. expect(body).not.toContain('mcp-codegraph');
  702. expect(body).not.toContain('command: codegraph');
  703. expect(body).toContain(' cli:\n - hermes-cli\n - browser');
  704. expect(body).toContain(' telegram:\n - hermes-telegram');
  705. });
  706. it('opencode: uninstall removes only mcp.codegraph, preserves comments and siblings', () => {
  707. const opencode = getTarget('opencode')!;
  708. const dir = path.join(tmpHome, '.config', 'opencode');
  709. fs.mkdirSync(dir, { recursive: true });
  710. const file = path.join(dir, 'opencode.jsonc');
  711. fs.writeFileSync(file, [
  712. '{',
  713. ' // important comment',
  714. ' "$schema": "https://opencode.ai/config.json",',
  715. ' "mcp": {',
  716. ' "other": { "type": "local", "command": ["x"], "enabled": true }',
  717. ' }',
  718. '}',
  719. '',
  720. ].join('\n'));
  721. opencode.install('global', { autoAllow: true });
  722. const afterInstall = fs.readFileSync(file, 'utf-8');
  723. expect(afterInstall).toContain('"codegraph"');
  724. expect(afterInstall).toContain('"other"');
  725. opencode.uninstall('global');
  726. const afterUninstall = fs.readFileSync(file, 'utf-8');
  727. expect(afterUninstall).not.toContain('codegraph');
  728. expect(afterUninstall).toContain('// important comment');
  729. expect(afterUninstall).toContain('"other"');
  730. });
  731. it('codex: user-added key inside [mcp_servers.codegraph] survives idempotent re-install', () => {
  732. const codex = getTarget('codex')!;
  733. codex.install('global', { autoAllow: false });
  734. const tomlPath = path.join(tmpHome, '.codex', 'config.toml');
  735. const original = fs.readFileSync(tomlPath, 'utf-8');
  736. // User edits the block to add a custom key.
  737. const edited = original.replace(
  738. 'args = ["serve", "--mcp"]',
  739. 'args = ["serve", "--mcp"]\nenabled = true',
  740. );
  741. fs.writeFileSync(tomlPath, edited);
  742. // Re-install: our serializer doesn't know `enabled = true`, so
  743. // the block no longer matches the canonical form — we'll
  744. // overwrite it. This is the documented contract: we own the
  745. // codegraph block exclusively.
  746. const second = codex.install('global', { autoAllow: false });
  747. const tomlEntry = second.files.find((f) => f.path.endsWith('config.toml'))!;
  748. expect(tomlEntry.action).toBe('updated');
  749. const after = fs.readFileSync(tomlPath, 'utf-8');
  750. expect(after).not.toContain('enabled = true');
  751. });
  752. it('claude: local install writes ./.mcp.json (project scope), not ./.claude.json', () => {
  753. const claude = getTarget('claude')!;
  754. const result = claude.install('local', { autoAllow: false });
  755. // The MCP entry lands in ./.mcp.json — the file Claude Code reads.
  756. expect(result.files.some((f) => f.path.replace(/\\/g, '/').endsWith('/.mcp.json'))).toBe(true);
  757. expect(fs.existsSync(path.join(tmpCwd, '.mcp.json'))).toBe(true);
  758. expect(fs.existsSync(path.join(tmpCwd, '.claude.json'))).toBe(false);
  759. const cfg = JSON.parse(fs.readFileSync(path.join(tmpCwd, '.mcp.json'), 'utf-8'));
  760. expect(cfg.mcpServers.codegraph).toBeDefined();
  761. });
  762. it('claude: install does NOT create a CLAUDE.md instructions file (#529)', () => {
  763. const claude = getTarget('claude')!;
  764. const result = claude.install('local', { autoAllow: false });
  765. const claudeMd = path.join(tmpCwd, '.claude', 'CLAUDE.md');
  766. expect(fs.existsSync(claudeMd)).toBe(false);
  767. expect(result.files.some((f) => f.path.endsWith('CLAUDE.md'))).toBe(false);
  768. });
  769. it('claude: install strips a legacy CLAUDE.md codegraph block, keeping user content (#529)', () => {
  770. const claude = getTarget('claude')!;
  771. const claudeMd = path.join(tmpCwd, '.claude', 'CLAUDE.md');
  772. fs.mkdirSync(path.dirname(claudeMd), { recursive: true });
  773. fs.writeFileSync(claudeMd, `# My project rules\n\nUse tabs.\n\n${LEGACY_BLOCK}\n`);
  774. const result = claude.install('local', { autoAllow: false });
  775. const body = fs.readFileSync(claudeMd, 'utf-8');
  776. expect(body).toContain('# My project rules');
  777. expect(body).toContain('Use tabs.');
  778. expect(body).not.toContain('CODEGRAPH_START');
  779. expect(result.files.find((f) => f.path.endsWith('CLAUDE.md'))?.action).toBe('removed');
  780. });
  781. it('claude: global install targets ~/.claude.json (user scope)', () => {
  782. const claude = getTarget('claude')!;
  783. claude.install('global', { autoAllow: false });
  784. const cfg = JSON.parse(fs.readFileSync(path.join(tmpHome, '.claude.json'), 'utf-8'));
  785. expect(cfg.mcpServers.codegraph).toBeDefined();
  786. });
  787. it('claude: local install migrates a legacy ./.claude.json codegraph entry into ./.mcp.json', () => {
  788. const claude = getTarget('claude')!;
  789. const legacy = path.join(tmpCwd, '.claude.json');
  790. fs.writeFileSync(
  791. legacy,
  792. JSON.stringify({ mcpServers: { codegraph: { type: 'stdio', command: 'codegraph', args: ['serve', '--mcp'] } } }, null, 2),
  793. );
  794. claude.install('local', { autoAllow: false });
  795. // codegraph now lives in .mcp.json; the legacy file (which held only
  796. // codegraph) is gone.
  797. const mcp = JSON.parse(fs.readFileSync(path.join(tmpCwd, '.mcp.json'), 'utf-8'));
  798. expect(mcp.mcpServers.codegraph).toBeDefined();
  799. expect(fs.existsSync(legacy)).toBe(false);
  800. });
  801. it('claude: legacy ./.claude.json migration preserves sibling servers and unrelated keys', () => {
  802. const claude = getTarget('claude')!;
  803. const legacy = path.join(tmpCwd, '.claude.json');
  804. fs.writeFileSync(
  805. legacy,
  806. JSON.stringify({
  807. mcpServers: {
  808. codegraph: { type: 'stdio', command: 'codegraph', args: ['serve', '--mcp'] },
  809. other: { command: 'x' },
  810. },
  811. somethingElse: true,
  812. }, null, 2),
  813. );
  814. claude.install('local', { autoAllow: false });
  815. // Only codegraph is stripped from the legacy file; siblings survive.
  816. const after = JSON.parse(fs.readFileSync(legacy, 'utf-8'));
  817. expect(after.mcpServers.codegraph).toBeUndefined();
  818. expect(after.mcpServers.other).toBeDefined();
  819. expect(after.somethingElse).toBe(true);
  820. const mcp = JSON.parse(fs.readFileSync(path.join(tmpCwd, '.mcp.json'), 'utf-8'));
  821. expect(mcp.mcpServers.codegraph).toBeDefined();
  822. });
  823. it('claude: uninstall strips codegraph from ./.mcp.json and a legacy ./.claude.json', () => {
  824. const claude = getTarget('claude')!;
  825. // A user left with both the working .mcp.json and a stale .claude.json.
  826. fs.writeFileSync(
  827. path.join(tmpCwd, '.mcp.json'),
  828. JSON.stringify({ mcpServers: { codegraph: { command: 'codegraph' } } }, null, 2),
  829. );
  830. fs.writeFileSync(
  831. path.join(tmpCwd, '.claude.json'),
  832. JSON.stringify({ mcpServers: { codegraph: { command: 'codegraph' }, other: { command: 'x' } } }, null, 2),
  833. );
  834. claude.uninstall('local');
  835. const mcp = JSON.parse(fs.readFileSync(path.join(tmpCwd, '.mcp.json'), 'utf-8'));
  836. expect(mcp.mcpServers).toBeUndefined();
  837. const legacy = JSON.parse(fs.readFileSync(path.join(tmpCwd, '.claude.json'), 'utf-8'));
  838. expect(legacy.mcpServers.codegraph).toBeUndefined();
  839. expect(legacy.mcpServers.other).toBeDefined();
  840. });
  841. // ---- Legacy auto-sync hook cleanup ----
  842. // Pre-0.8 installs wrote `codegraph mark-dirty` / `sync-if-dirty`
  843. // hooks to settings.json. Both subcommands were removed from the CLI,
  844. // so the Stop hook fails every turn ("unknown command
  845. // 'sync-if-dirty'"). The installer must strip them on upgrade and
  846. // uninstall — without touching the user's unrelated hooks.
  847. function seedSettings(loc: 'global' | 'local', settings: Record<string, any>): string {
  848. const dir = path.join(loc === 'global' ? tmpHome : tmpCwd, '.claude');
  849. fs.mkdirSync(dir, { recursive: true });
  850. const file = path.join(dir, 'settings.json');
  851. fs.writeFileSync(file, JSON.stringify(settings, null, 2) + '\n');
  852. return file;
  853. }
  854. // Realistic pre-0.8 settings.json: our two auto-sync hooks plus an
  855. // unrelated GitKraken Stop hook the user added (matches the report).
  856. function legacyHookSettings(): Record<string, any> {
  857. return {
  858. hooks: {
  859. PostToolUse: [
  860. { matcher: 'Edit|Write', hooks: [{ type: 'command', command: 'codegraph mark-dirty', async: true }] },
  861. ],
  862. Stop: [
  863. { hooks: [{ type: 'command', command: 'codegraph sync-if-dirty' }] },
  864. { hooks: [{ type: 'command', command: '"/Users/me/gk" ai hook run --host claude-code' }] },
  865. ],
  866. },
  867. };
  868. }
  869. it('claude: install strips stale codegraph auto-sync hooks but keeps the user\'s GitKraken hook', () => {
  870. const claude = getTarget('claude')!;
  871. const file = seedSettings('global', legacyHookSettings());
  872. claude.install('global', { autoAllow: true });
  873. const after = JSON.parse(fs.readFileSync(file, 'utf-8'));
  874. // The only PostToolUse group held mark-dirty → the event is gone.
  875. expect(after.hooks?.PostToolUse).toBeUndefined();
  876. const stopCommands = (after.hooks?.Stop ?? []).flatMap((g: any) =>
  877. (g.hooks ?? []).map((h: any) => h.command),
  878. );
  879. expect(stopCommands).not.toContain('codegraph sync-if-dirty');
  880. // The unrelated GitKraken hook survives untouched.
  881. expect(stopCommands.some((c: string) => c.includes('gk') && c.includes('ai hook run'))).toBe(true);
  882. // Permissions still written as normal alongside the cleanup.
  883. expect(after.permissions?.allow).toContain('mcp__codegraph__codegraph_search');
  884. });
  885. it('claude: cleanupLegacyHooks preserves a sibling hook sharing our matcher group', () => {
  886. const file = seedSettings('global', {
  887. hooks: {
  888. Stop: [
  889. {
  890. hooks: [
  891. { type: 'command', command: 'codegraph sync-if-dirty' },
  892. { type: 'command', command: 'gk ai hook run --host claude-code' },
  893. ],
  894. },
  895. ],
  896. },
  897. });
  898. expect(cleanupLegacyHooks('global').action).toBe('removed');
  899. const after = JSON.parse(fs.readFileSync(file, 'utf-8'));
  900. expect(after.hooks.Stop[0].hooks.map((h: any) => h.command)).toEqual([
  901. 'gk ai hook run --host claude-code',
  902. ]);
  903. });
  904. it('claude: cleanupLegacyHooks is a byte-for-byte no-op without codegraph hooks', () => {
  905. const original =
  906. JSON.stringify({ hooks: { Stop: [{ hooks: [{ type: 'command', command: 'gk ai hook run' }] }] } }, null, 2) + '\n';
  907. const file = seedSettings('global', JSON.parse(original));
  908. expect(cleanupLegacyHooks('global').action).toBe('unchanged');
  909. expect(fs.readFileSync(file, 'utf-8')).toBe(original);
  910. });
  911. it('claude: cleanupLegacyHooks reports not-found when settings.json is absent', () => {
  912. expect(cleanupLegacyHooks('global').action).toBe('not-found');
  913. });
  914. it('claude: re-running install after a legacy cleanup leaves settings.json unchanged', () => {
  915. const claude = getTarget('claude')!;
  916. const file = seedSettings('global', legacyHookSettings());
  917. claude.install('global', { autoAllow: true });
  918. const firstPass = fs.readFileSync(file, 'utf-8');
  919. claude.install('global', { autoAllow: true });
  920. expect(fs.readFileSync(file, 'utf-8')).toBe(firstPass);
  921. });
  922. it('claude: uninstall strips stale hooks written in the npx form (local)', () => {
  923. const claude = getTarget('claude')!;
  924. const file = seedSettings('local', {
  925. hooks: {
  926. PostToolUse: [
  927. { matcher: 'Edit|Write', hooks: [{ type: 'command', command: 'npx @colbymchenry/codegraph mark-dirty', async: true }] },
  928. ],
  929. Stop: [
  930. { hooks: [{ type: 'command', command: 'npx @colbymchenry/codegraph sync-if-dirty' }] },
  931. ],
  932. },
  933. });
  934. claude.uninstall('local');
  935. const after = JSON.parse(fs.readFileSync(file, 'utf-8'));
  936. // Both events emptied → the whole `hooks` object is removed.
  937. expect(after.hooks).toBeUndefined();
  938. });
  939. });
  940. describe('Installer targets — registry', () => {
  941. it('getTarget returns the right target for each id', () => {
  942. expect(getTarget('claude')?.id).toBe('claude');
  943. expect(getTarget('cursor')?.id).toBe('cursor');
  944. expect(getTarget('codex')?.id).toBe('codex');
  945. expect(getTarget('opencode')?.id).toBe('opencode');
  946. expect(getTarget('hermes')?.id).toBe('hermes');
  947. expect(getTarget('gemini')?.id).toBe('gemini');
  948. expect(getTarget('antigravity')?.id).toBe('antigravity');
  949. expect(getTarget('kiro')?.id).toBe('kiro');
  950. expect(getTarget('not-a-real-target')).toBeUndefined();
  951. });
  952. it('resolveTargetFlag handles auto/all/none/csv', () => {
  953. expect(resolveTargetFlag('none', 'global')).toEqual([]);
  954. expect(resolveTargetFlag('all', 'global').length).toBe(ALL_TARGETS.length);
  955. const csv = resolveTargetFlag('claude,cursor', 'global');
  956. expect(csv.map((t) => t.id)).toEqual(['claude', 'cursor']);
  957. });
  958. it('resolveTargetFlag throws on unknown id', () => {
  959. expect(() => resolveTargetFlag('claude,bogus', 'global')).toThrow(/Unknown --target/);
  960. });
  961. });
  962. describe('Installer targets — TOML serializer (Codex backbone)', () => {
  963. it('builds a [mcp_servers.codegraph] block with command + args', () => {
  964. const block = buildTomlTable('mcp_servers.codegraph', {
  965. command: 'codegraph',
  966. args: ['serve', '--mcp'],
  967. });
  968. expect(block).toContain('[mcp_servers.codegraph]');
  969. expect(block).toContain('command = "codegraph"');
  970. expect(block).toContain('args = ["serve", "--mcp"]');
  971. });
  972. it('upsert inserts into empty content', () => {
  973. const block = buildTomlTable('mcp_servers.codegraph', { command: 'codegraph', args: ['serve'] });
  974. const { content, action } = upsertTomlTable('', 'mcp_servers.codegraph', block);
  975. expect(action).toBe('inserted');
  976. expect(content.startsWith('[mcp_servers.codegraph]')).toBe(true);
  977. });
  978. it('upsert is idempotent — second call returns unchanged', () => {
  979. const block = buildTomlTable('mcp_servers.codegraph', { command: 'codegraph', args: ['serve'] });
  980. const first = upsertTomlTable('', 'mcp_servers.codegraph', block);
  981. const second = upsertTomlTable(first.content, 'mcp_servers.codegraph', block);
  982. expect(second.action).toBe('unchanged');
  983. expect(second.content).toBe(first.content);
  984. });
  985. it('upsert replaces an existing block in place, preserving sibling tables', () => {
  986. const existing = [
  987. '[other_table]',
  988. 'foo = "bar"',
  989. '',
  990. '[mcp_servers.codegraph]',
  991. 'command = "old-codegraph"',
  992. 'args = ["old"]',
  993. '',
  994. '[zzz]',
  995. 'baz = "qux"',
  996. '',
  997. ].join('\n');
  998. const newBlock = buildTomlTable('mcp_servers.codegraph', {
  999. command: 'codegraph',
  1000. args: ['serve', '--mcp'],
  1001. });
  1002. const { content, action } = upsertTomlTable(existing, 'mcp_servers.codegraph', newBlock);
  1003. expect(action).toBe('replaced');
  1004. expect(content).toContain('[other_table]');
  1005. expect(content).toContain('foo = "bar"');
  1006. expect(content).toContain('[zzz]');
  1007. expect(content).toContain('baz = "qux"');
  1008. expect(content).toContain('command = "codegraph"');
  1009. expect(content).not.toContain('old-codegraph');
  1010. });
  1011. it('removeTomlTable strips the block and preserves siblings', () => {
  1012. const existing = [
  1013. '[other_table]',
  1014. 'foo = "bar"',
  1015. '',
  1016. '[mcp_servers.codegraph]',
  1017. 'command = "codegraph"',
  1018. 'args = ["serve"]',
  1019. ].join('\n');
  1020. const { content, action } = removeTomlTable(existing, 'mcp_servers.codegraph');
  1021. expect(action).toBe('removed');
  1022. expect(content).toContain('[other_table]');
  1023. expect(content).toContain('foo = "bar"');
  1024. expect(content).not.toContain('mcp_servers.codegraph');
  1025. });
  1026. it('removeTomlTable on missing table returns not-found, no content change', () => {
  1027. const existing = '[other]\nfoo = "bar"\n';
  1028. const { content, action } = removeTomlTable(existing, 'mcp_servers.codegraph');
  1029. expect(action).toBe('not-found');
  1030. expect(content).toBe(existing);
  1031. });
  1032. it('upsert preserves an array-of-tables sibling [[foo]]', () => {
  1033. const existing = [
  1034. '[[foo]]',
  1035. 'name = "a"',
  1036. '',
  1037. '[[foo]]',
  1038. 'name = "b"',
  1039. '',
  1040. ].join('\n');
  1041. const block = buildTomlTable('mcp_servers.codegraph', { command: 'codegraph', args: ['serve'] });
  1042. const { content } = upsertTomlTable(existing, 'mcp_servers.codegraph', block);
  1043. expect(content.match(/\[\[foo\]\]/g)?.length).toBe(2);
  1044. expect(content).toContain('[mcp_servers.codegraph]');
  1045. });
  1046. });
  1047. describe('Installer — uninstallTargets sweep (codegraph uninstall)', () => {
  1048. let tmpHome: string;
  1049. let tmpCwd: string;
  1050. let origCwd: string;
  1051. let homeRestore: { restore: () => void };
  1052. beforeEach(() => {
  1053. tmpHome = mkTmpDir('un-home');
  1054. tmpCwd = mkTmpDir('un-cwd');
  1055. origCwd = process.cwd();
  1056. process.chdir(tmpCwd);
  1057. homeRestore = setHome(tmpHome);
  1058. });
  1059. afterEach(() => {
  1060. homeRestore.restore();
  1061. process.chdir(origCwd);
  1062. fs.rmSync(tmpHome, { recursive: true, force: true });
  1063. fs.rmSync(tmpCwd, { recursive: true, force: true });
  1064. });
  1065. it('sweeps every agent it was installed on and reports removed for each (global)', () => {
  1066. for (const t of ALL_TARGETS) {
  1067. if (t.supportsLocation('global')) t.install('global', { autoAllow: true });
  1068. }
  1069. const reports = uninstallTargets(ALL_TARGETS, 'global');
  1070. for (const t of ALL_TARGETS) {
  1071. const r = reports.find((x) => x.id === t.id)!;
  1072. expect(r.status).toBe('removed');
  1073. expect(r.removedPaths.length).toBeGreaterThan(0);
  1074. // The actual config is gone afterward.
  1075. expect(t.detect('global').alreadyConfigured).toBe(false);
  1076. }
  1077. });
  1078. it('is safe on a clean slate — every agent reports not-configured, nothing removed', () => {
  1079. const reports = uninstallTargets(ALL_TARGETS, 'global');
  1080. for (const r of reports) {
  1081. expect(r.status).toBe('not-configured');
  1082. expect(r.removedPaths).toEqual([]);
  1083. }
  1084. });
  1085. it('reports removed only for agents that were actually configured', () => {
  1086. // Install on Claude only; the rest stay untouched.
  1087. getTarget('claude')!.install('global', { autoAllow: true });
  1088. const reports = uninstallTargets(ALL_TARGETS, 'global');
  1089. const claude = reports.find((r) => r.id === 'claude')!;
  1090. expect(claude.status).toBe('removed');
  1091. expect(claude.displayName).toBe(getTarget('claude')!.displayName);
  1092. for (const r of reports.filter((x) => x.id !== 'claude')) {
  1093. expect(r.status).toBe('not-configured');
  1094. }
  1095. });
  1096. it('marks global-only agents as unsupported for a local sweep (and never touches them)', () => {
  1097. const reports = uninstallTargets(ALL_TARGETS, 'local');
  1098. for (const t of ALL_TARGETS) {
  1099. const r = reports.find((x) => x.id === t.id)!;
  1100. if (t.supportsLocation('local')) {
  1101. expect(r.status).toBe('not-configured');
  1102. } else {
  1103. expect(r.status).toBe('unsupported');
  1104. expect(r.removedPaths).toEqual([]);
  1105. expect(r.notes[0]).toMatch(/global-only/);
  1106. }
  1107. }
  1108. });
  1109. it('is idempotent — a second sweep finds nothing left to remove', () => {
  1110. for (const t of ALL_TARGETS) {
  1111. if (t.supportsLocation('global')) t.install('global', { autoAllow: true });
  1112. }
  1113. const first = uninstallTargets(ALL_TARGETS, 'global');
  1114. expect(first.some((r) => r.status === 'removed')).toBe(true);
  1115. const second = uninstallTargets(ALL_TARGETS, 'global');
  1116. for (const r of second) {
  1117. expect(r.status).toBe('not-configured');
  1118. expect(r.removedPaths).toEqual([]);
  1119. }
  1120. });
  1121. it('a --target subset removes only the chosen agents, leaving siblings configured', () => {
  1122. getTarget('claude')!.install('global', { autoAllow: true });
  1123. getTarget('cursor')!.install('global', { autoAllow: true });
  1124. const reports = uninstallTargets(resolveTargetFlag('claude', 'global'), 'global');
  1125. expect(reports.map((r) => r.id)).toEqual(['claude']);
  1126. expect(reports[0].status).toBe('removed');
  1127. // Cursor was not in the subset — still configured.
  1128. expect(getTarget('cursor')!.detect('global').alreadyConfigured).toBe(true);
  1129. expect(getTarget('claude')!.detect('global').alreadyConfigured).toBe(false);
  1130. });
  1131. });
  1132. describe('Installer — Cursor rules file cleanup on uninstall', () => {
  1133. let tmpHome: string;
  1134. let tmpCwd: string;
  1135. let origCwd: string;
  1136. let homeRestore: { restore: () => void };
  1137. const cursor = getTarget('cursor')!;
  1138. beforeEach(() => {
  1139. tmpHome = mkTmpDir('cur-home');
  1140. tmpCwd = mkTmpDir('cur-cwd');
  1141. origCwd = process.cwd();
  1142. process.chdir(tmpCwd);
  1143. homeRestore = setHome(tmpHome);
  1144. });
  1145. afterEach(() => {
  1146. homeRestore.restore();
  1147. process.chdir(origCwd);
  1148. fs.rmSync(tmpHome, { recursive: true, force: true });
  1149. fs.rmSync(tmpCwd, { recursive: true, force: true });
  1150. });
  1151. const rulesFile = () => path.join(process.cwd(), '.cursor', 'rules', 'codegraph.mdc');
  1152. // The frontmatter a previous install wrote ahead of the marked block.
  1153. // `removeRulesEntry` recognizes it to decide whether the leftover .mdc
  1154. // is ours-to-delete or carries user content worth keeping.
  1155. const MDC_FRONTMATTER = [
  1156. '---',
  1157. 'description: CodeGraph MCP usage guide — when to use which tool',
  1158. 'alwaysApply: true',
  1159. '---',
  1160. '',
  1161. ].join('\n');
  1162. function plantLegacyRulesFile(extra = ''): void {
  1163. fs.mkdirSync(path.dirname(rulesFile()), { recursive: true });
  1164. fs.writeFileSync(rulesFile(), MDC_FRONTMATTER + LEGACY_BLOCK + '\n' + extra);
  1165. }
  1166. it('uninstall deletes a leftover codegraph.mdc entirely (no orphaned frontmatter left behind)', () => {
  1167. plantLegacyRulesFile();
  1168. expect(fs.existsSync(rulesFile())).toBe(true);
  1169. cursor.uninstall('local');
  1170. // The whole file — frontmatter included — is gone, not just the block.
  1171. expect(fs.existsSync(rulesFile())).toBe(false);
  1172. });
  1173. it('install self-heals a leftover codegraph.mdc (#529)', () => {
  1174. plantLegacyRulesFile();
  1175. const result = cursor.install('local', { autoAllow: true });
  1176. expect(fs.existsSync(rulesFile())).toBe(false);
  1177. expect(result.files.some((f) => f.path.endsWith('codegraph.mdc') && f.action === 'removed')).toBe(true);
  1178. });
  1179. it('uninstall preserves user content added outside the codegraph markers (strips only our block)', () => {
  1180. plantLegacyRulesFile('## My own rule\nkeep me\n');
  1181. cursor.uninstall('local');
  1182. expect(fs.existsSync(rulesFile())).toBe(true);
  1183. const after = fs.readFileSync(rulesFile(), 'utf-8');
  1184. expect(after).toContain('keep me');
  1185. // Our tool-usage block is gone.
  1186. expect(after).not.toContain('codegraph_search');
  1187. expect(after).not.toContain('CODEGRAPH_START');
  1188. });
  1189. });
  1190. function listAllFiles(dir: string): string[] {
  1191. if (!fs.existsSync(dir)) return [];
  1192. const out: string[] = [];
  1193. for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
  1194. const full = path.join(dir, entry.name);
  1195. if (entry.isDirectory()) out.push(...listAllFiles(full));
  1196. else out.push(full);
  1197. }
  1198. return out;
  1199. }