settingsSyncService.test.ts 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. import {
  2. existsSync,
  3. lstatSync,
  4. mkdirSync,
  5. mkdtempSync,
  6. readFileSync,
  7. rmSync,
  8. writeFileSync,
  9. } from "node:fs";
  10. import { tmpdir } from "node:os";
  11. import { dirname, join } from "node:path";
  12. import { afterEach, describe, expect, it, vi } from "vitest";
  13. import type { ISettingService } from "../src/setting/setting.js";
  14. const originalHome = process.env.HOME;
  15. const tempHomes: string[] = [];
  16. function makeTempHome(): string {
  17. const home = mkdtempSync(join(tmpdir(), "zcode-settings-sync-home-"));
  18. tempHomes.push(home);
  19. return home;
  20. }
  21. function writeSkill(rootPath: string, folder: string, body: string): void {
  22. const skillDir = join(rootPath, folder);
  23. mkdirSync(skillDir, { recursive: true });
  24. writeFileSync(join(skillDir, "SKILL.md"), body, "utf-8");
  25. }
  26. function writeCommand(rootPath: string, fileName: string, body: string): void {
  27. const filePath = join(rootPath, fileName);
  28. mkdirSync(dirname(filePath), { recursive: true });
  29. writeFileSync(filePath, body, "utf-8");
  30. }
  31. function writePlugin(
  32. rootPath: string,
  33. folder: string,
  34. manifest: Record<string, unknown>,
  35. manifestDir = ".zcode-plugin",
  36. ): string {
  37. const pluginDir = join(rootPath, folder);
  38. mkdirSync(join(pluginDir, manifestDir), { recursive: true });
  39. writeFileSync(join(pluginDir, manifestDir, "plugin.json"), JSON.stringify(manifest), "utf-8");
  40. return pluginDir;
  41. }
  42. function writeJsonConfig(filePath: string, value: Record<string, unknown>): void {
  43. mkdirSync(dirname(filePath), { recursive: true });
  44. writeFileSync(filePath, JSON.stringify(value, null, 2), "utf-8");
  45. }
  46. function writeTextConfig(filePath: string, value: string): void {
  47. mkdirSync(dirname(filePath), { recursive: true });
  48. writeFileSync(filePath, value, "utf-8");
  49. }
  50. function createSettingService(): ISettingService {
  51. return {
  52. async get() {
  53. return {};
  54. },
  55. async update() {},
  56. async updateDataBaseDir() {},
  57. };
  58. }
  59. afterEach(() => {
  60. process.env.HOME = originalHome;
  61. vi.resetModules();
  62. while (tempHomes.length > 0) {
  63. const home = tempHomes.pop();
  64. if (home) {
  65. rmSync(home, { recursive: true, force: true });
  66. }
  67. }
  68. });
  69. describe("settingsSyncService Provider 导入边界", () => {
  70. it("尚未实现 Provider 数据迁移时明确返回 skipped", async () => {
  71. const { createSettingsSyncService } =
  72. await import("../src/settings-sync/settingsSyncService.js");
  73. const service = createSettingsSyncService({ settingService: createSettingService() });
  74. await expect(
  75. service.importSelected({
  76. selections: [{ agent: "claudeCode", category: "providers" }],
  77. }),
  78. ).resolves.toEqual({
  79. successCount: 0,
  80. skippedCount: 1,
  81. failedCount: 0,
  82. taskResults: [
  83. {
  84. agent: "claudeCode",
  85. category: "providers",
  86. status: "skipped",
  87. importedCount: 0,
  88. skippedCount: 1,
  89. failedCount: 0,
  90. },
  91. ],
  92. });
  93. });
  94. });
  95. describe("settingsSyncService Claude AGENTS.md migration", () => {
  96. it("copies Claude user memory into a fresh default ZCode AGENTS.md", async () => {
  97. const home = makeTempHome();
  98. const workspacePath = join(home, "workspace");
  99. process.env.HOME = home;
  100. writeTextConfig(join(home, ".claude", "CLAUDE.md"), "claude memory");
  101. const { createSettingsSyncService } =
  102. await import("../src/settings-sync/settingsSyncService.js");
  103. const service = createSettingsSyncService({ settingService: createSettingService() });
  104. await expect(service.getClaudeAgentsFileMigrationStatus({ workspacePath })).resolves.toEqual({
  105. sourcePath: join(home, ".claude", "CLAUDE.md"),
  106. targetPath: join(home, ".zcode", "AGENTS.md"),
  107. sourceExists: true,
  108. targetExists: false,
  109. supported: true,
  110. });
  111. await expect(
  112. service.copyClaudeAgentsFileToZcodeAgentsFile({
  113. workspacePath,
  114. overwrite: false,
  115. }),
  116. ).resolves.toEqual({
  117. sourcePath: join(home, ".claude", "CLAUDE.md"),
  118. targetPath: join(home, ".zcode", "AGENTS.md"),
  119. status: "copied",
  120. overwritten: false,
  121. });
  122. expect(readFileSync(join(home, ".zcode", "AGENTS.md"), "utf-8")).toBe("claude memory");
  123. });
  124. it("detects and overwrites default ZCode AGENTS.md from Claude user memory", async () => {
  125. const home = makeTempHome();
  126. const workspacePath = join(home, "workspace");
  127. process.env.HOME = home;
  128. writeTextConfig(join(home, ".claude", "CLAUDE.md"), "claude memory");
  129. writeTextConfig(join(home, ".zcode", "AGENTS.md"), "existing agents");
  130. const { createSettingsSyncService } =
  131. await import("../src/settings-sync/settingsSyncService.js");
  132. const service = createSettingsSyncService({ settingService: createSettingService() });
  133. const status = await service.getClaudeAgentsFileMigrationStatus({ workspacePath });
  134. expect(status).toEqual({
  135. sourcePath: join(home, ".claude", "CLAUDE.md"),
  136. targetPath: join(home, ".zcode", "AGENTS.md"),
  137. sourceExists: true,
  138. targetExists: true,
  139. supported: true,
  140. });
  141. await expect(
  142. service.copyClaudeAgentsFileToZcodeAgentsFile({
  143. workspacePath,
  144. overwrite: false,
  145. }),
  146. ).resolves.toEqual({
  147. sourcePath: join(home, ".claude", "CLAUDE.md"),
  148. targetPath: join(home, ".zcode", "AGENTS.md"),
  149. status: "skipped",
  150. overwritten: false,
  151. skippedReason: "targetExists",
  152. });
  153. expect(readFileSync(join(home, ".zcode", "AGENTS.md"), "utf-8")).toBe("existing agents");
  154. await expect(
  155. service.copyClaudeAgentsFileToZcodeAgentsFile({
  156. workspacePath,
  157. overwrite: true,
  158. }),
  159. ).resolves.toEqual({
  160. sourcePath: join(home, ".claude", "CLAUDE.md"),
  161. targetPath: join(home, ".zcode", "AGENTS.md"),
  162. status: "copied",
  163. overwritten: true,
  164. });
  165. expect(readFileSync(join(home, ".zcode", "AGENTS.md"), "utf-8")).toBe("claude memory");
  166. });
  167. it("reports missing Claude user memory as unsupported", async () => {
  168. const home = makeTempHome();
  169. const workspacePath = join(home, "workspace");
  170. process.env.HOME = home;
  171. const { createSettingsSyncService } =
  172. await import("../src/settings-sync/settingsSyncService.js");
  173. const service = createSettingsSyncService({ settingService: createSettingService() });
  174. await expect(service.getClaudeAgentsFileMigrationStatus({ workspacePath })).resolves.toEqual({
  175. sourcePath: join(home, ".claude", "CLAUDE.md"),
  176. targetPath: join(home, ".zcode", "AGENTS.md"),
  177. sourceExists: false,
  178. targetExists: false,
  179. supported: false,
  180. unavailableReason: "missingSource",
  181. });
  182. await expect(
  183. service.copyClaudeAgentsFileToZcodeAgentsFile({
  184. workspacePath,
  185. overwrite: true,
  186. }),
  187. ).resolves.toEqual({
  188. sourcePath: join(home, ".claude", "CLAUDE.md"),
  189. targetPath: join(home, ".zcode", "AGENTS.md"),
  190. status: "skipped",
  191. overwritten: false,
  192. skippedReason: "missingSource",
  193. });
  194. });
  195. });
  196. describe("settingsSyncService commands import", () => {
  197. it("does not scan external commands during first-run detection", async () => {
  198. const home = makeTempHome();
  199. const workspacePath = join(home, "workspace");
  200. process.env.HOME = home;
  201. writeCommand(
  202. join(home, ".claude", "commands"),
  203. "review.md",
  204. "---\ndescription: review\n---\nreview body",
  205. );
  206. const { createSettingsSyncService } =
  207. await import("../src/settings-sync/settingsSyncService.js");
  208. const service = createSettingsSyncService({ settingService: createSettingService() });
  209. await expect(service.detect({ workspacePath })).resolves.toEqual({ agents: [] });
  210. });
  211. it("scans and imports selected user commands into ZCode as symlinks by default", async () => {
  212. const home = makeTempHome();
  213. const workspacePath = join(home, "workspace");
  214. process.env.HOME = home;
  215. writeCommand(
  216. join(home, ".claude", "commands"),
  217. "review.md",
  218. "---\ndescription: review\nargument-hint: <file>\n---\nreview body",
  219. );
  220. writeCommand(
  221. join(home, ".claude", "commands"),
  222. "Git/commit.md",
  223. "---\ndescription: commit\n---\ncommit body",
  224. );
  225. const { createSettingsSyncService } =
  226. await import("../src/settings-sync/settingsSyncService.js");
  227. const service = createSettingsSyncService({ settingService: createSettingService() });
  228. const discovery = await service.detect({
  229. workspacePath,
  230. categories: ["commands"],
  231. intent: "manualImport",
  232. });
  233. expect(discovery.agents).toEqual([
  234. {
  235. agent: "claudeCode",
  236. discovered: true,
  237. categories: [
  238. {
  239. category: "commands",
  240. discoveredCount: 2,
  241. importableCount: 2,
  242. skippedCount: 0,
  243. sourcePaths: [join(home, ".claude", "commands")],
  244. sourceRoots: [
  245. {
  246. scope: "global",
  247. path: join(home, ".claude", "commands"),
  248. discoveredCount: 2,
  249. importableCount: 2,
  250. skippedCount: 0,
  251. commands: [
  252. {
  253. name: "/Git/commit",
  254. path: join(home, ".claude", "commands", "Git", "commit.md"),
  255. importable: true,
  256. description: "commit",
  257. },
  258. {
  259. name: "/review",
  260. path: join(home, ".claude", "commands", "review.md"),
  261. importable: true,
  262. description: "review",
  263. argumentHint: "<file>",
  264. },
  265. ],
  266. },
  267. ],
  268. selectedByDefault: false,
  269. },
  270. ],
  271. },
  272. ]);
  273. const result = await service.importSelected({
  274. workspacePath,
  275. selections: [{ agent: "claudeCode", category: "commands" }],
  276. });
  277. const importedPath = join(home, ".zcode", "commands", "review.md");
  278. expect(result.successCount).toBe(2);
  279. expect(result.skippedCount).toBe(0);
  280. expect(result.failedCount).toBe(0);
  281. expect(result.taskResults[0]?.commandResults).toEqual([
  282. {
  283. name: "/Git/commit",
  284. path: join(home, ".claude", "commands", "Git", "commit.md"),
  285. sourceScope: "global",
  286. status: "imported",
  287. },
  288. {
  289. name: "/review",
  290. path: join(home, ".claude", "commands", "review.md"),
  291. sourceScope: "global",
  292. status: "imported",
  293. },
  294. ]);
  295. expect(existsSync(importedPath)).toBe(true);
  296. // Windows 使用硬链接(普通文件),非 Windows 使用 symlink
  297. if (process.platform === "win32") {
  298. expect(lstatSync(importedPath).isFile()).toBe(true);
  299. } else {
  300. expect(lstatSync(importedPath).isSymbolicLink()).toBe(true);
  301. }
  302. expect(readFileSync(importedPath, "utf-8")).toContain("review body");
  303. expect(existsSync(join(home, ".zcode", "commands", "Git", "commit.md"))).toBe(true);
  304. });
  305. it("scans global Claude commands without an active workspace", async () => {
  306. const home = makeTempHome();
  307. process.env.HOME = home;
  308. writeCommand(join(home, ".claude", "commands"), "dev.md", "dev body");
  309. const { createSettingsSyncService } =
  310. await import("../src/settings-sync/settingsSyncService.js");
  311. const service = createSettingsSyncService({ settingService: createSettingService() });
  312. const discovery = await service.detect({
  313. categories: ["commands"],
  314. intent: "manualImport",
  315. });
  316. expect(discovery.agents).toEqual([
  317. {
  318. agent: "claudeCode",
  319. discovered: true,
  320. categories: [
  321. {
  322. category: "commands",
  323. discoveredCount: 1,
  324. importableCount: 1,
  325. skippedCount: 0,
  326. sourcePaths: [join(home, ".claude", "commands")],
  327. sourceRoots: [
  328. {
  329. scope: "global",
  330. path: join(home, ".claude", "commands"),
  331. discoveredCount: 1,
  332. importableCount: 1,
  333. skippedCount: 0,
  334. commands: [
  335. {
  336. name: "/dev",
  337. path: join(home, ".claude", "commands", "dev.md"),
  338. importable: true,
  339. },
  340. ],
  341. },
  342. ],
  343. selectedByDefault: false,
  344. },
  345. ],
  346. },
  347. ]);
  348. });
  349. it("imports only selected command paths from a source", async () => {
  350. const home = makeTempHome();
  351. const workspacePath = join(home, "workspace");
  352. process.env.HOME = home;
  353. writeCommand(join(home, ".codex", "commands"), "review.md", "review body");
  354. writeCommand(join(home, ".codex", "commands"), "write.md", "write body");
  355. const { createSettingsSyncService } =
  356. await import("../src/settings-sync/settingsSyncService.js");
  357. const service = createSettingsSyncService({ settingService: createSettingService() });
  358. const result = await service.importSelected({
  359. workspacePath,
  360. selections: [
  361. {
  362. agent: "codexCli",
  363. category: "commands",
  364. sourceScope: "global",
  365. commandPaths: [join(home, ".codex", "commands", "write.md")],
  366. },
  367. ],
  368. });
  369. expect(result.successCount).toBe(1);
  370. expect(existsSync(join(home, ".zcode", "commands", "write.md"))).toBe(true);
  371. expect(existsSync(join(home, ".zcode", "commands", "review.md"))).toBe(false);
  372. });
  373. it("imports selected commands into the requested project target as copies", async () => {
  374. const home = makeTempHome();
  375. const workspacePath = join(home, "workspace");
  376. process.env.HOME = home;
  377. writeCommand(join(home, ".qwen", "commands"), "review/code.md", "review body");
  378. const { createSettingsSyncService } =
  379. await import("../src/settings-sync/settingsSyncService.js");
  380. const service = createSettingsSyncService({ settingService: createSettingService() });
  381. const result = await service.importSelected({
  382. workspacePath,
  383. selections: [
  384. {
  385. agent: "qwenCode",
  386. category: "commands",
  387. sourceScope: "global",
  388. targetScope: "project",
  389. importMode: "copy",
  390. commandPaths: [join(home, ".qwen", "commands", "review", "code.md")],
  391. },
  392. ],
  393. });
  394. const importedPath = join(workspacePath, ".zcode", "commands", "review", "code.md");
  395. expect(result.successCount).toBe(1);
  396. expect(existsSync(importedPath)).toBe(true);
  397. expect(lstatSync(importedPath).isSymbolicLink()).toBe(false);
  398. expect(readFileSync(importedPath, "utf-8")).toContain("review body");
  399. expect(existsSync(join(home, ".zcode", "commands", "review", "code.md"))).toBe(false);
  400. });
  401. it("does not scan .agents commands for import", async () => {
  402. const home = makeTempHome();
  403. const workspacePath = join(home, "workspace");
  404. process.env.HOME = home;
  405. writeCommand(join(home, ".agents", "commands"), "already.md", "already");
  406. writeCommand(join(workspacePath, ".agents", "commands"), "project.md", "project");
  407. const { createSettingsSyncService } =
  408. await import("../src/settings-sync/settingsSyncService.js");
  409. const service = createSettingsSyncService({ settingService: createSettingService() });
  410. await expect(
  411. service.detect({
  412. workspacePath,
  413. categories: ["commands"],
  414. intent: "manualImport",
  415. }),
  416. ).resolves.toEqual({ agents: [] });
  417. });
  418. it("skips command imports when target file or same command name exists", async () => {
  419. const home = makeTempHome();
  420. const workspacePath = join(home, "workspace");
  421. process.env.HOME = home;
  422. writeCommand(join(home, ".claude", "commands"), "review.md", "source");
  423. writeCommand(join(home, ".codex", "commands"), "other-review.md", "source");
  424. writeCommand(join(home, ".zcode", "commands"), "review.md", "existing");
  425. const { createSettingsSyncService } =
  426. await import("../src/settings-sync/settingsSyncService.js");
  427. const service = createSettingsSyncService({ settingService: createSettingService() });
  428. const discovery = await service.detect({
  429. workspacePath,
  430. categories: ["commands"],
  431. intent: "manualImport",
  432. });
  433. expect(discovery.agents[0]?.categories[0]?.sourceRoots?.[0]?.commands).toEqual([
  434. {
  435. name: "/review",
  436. path: join(home, ".claude", "commands", "review.md"),
  437. importable: false,
  438. skipReason: "targetExists",
  439. },
  440. ]);
  441. expect(discovery.agents[1]?.categories[0]?.sourceRoots?.[0]?.commands).toEqual([
  442. {
  443. name: "/other-review",
  444. path: join(home, ".codex", "commands", "other-review.md"),
  445. importable: true,
  446. },
  447. ]);
  448. writeCommand(join(home, ".zcode", "commands"), "other-review.md", "existing");
  449. const result = await service.importSelected({
  450. workspacePath,
  451. selections: [{ agent: "codexCli", category: "commands" }],
  452. });
  453. expect(result.successCount).toBe(0);
  454. expect(result.skippedCount).toBe(1);
  455. expect(result.taskResults[0]?.commandResults).toEqual([
  456. {
  457. name: "/other-review",
  458. path: join(home, ".codex", "commands", "other-review.md"),
  459. sourceScope: "global",
  460. status: "skipped",
  461. skipReason: "targetExists",
  462. },
  463. ]);
  464. });
  465. });
  466. describe("settingsSyncService plugins import", () => {
  467. it("does not scan external plugins during first-run detection", async () => {
  468. const home = makeTempHome();
  469. const workspacePath = join(home, "workspace");
  470. process.env.HOME = home;
  471. writePlugin(join(home, ".claude", "plugins"), "review-plugin", {
  472. name: "review-plugin",
  473. });
  474. const { createSettingsSyncService } =
  475. await import("../src/settings-sync/settingsSyncService.js");
  476. const service = createSettingsSyncService({ settingService: createSettingService() });
  477. await expect(service.detect({ workspacePath })).resolves.toEqual({ agents: [] });
  478. });
  479. it("scans and imports selected user plugins as symlinks by default", async () => {
  480. const home = makeTempHome();
  481. const workspacePath = join(home, "workspace");
  482. process.env.HOME = home;
  483. const pluginPath = writePlugin(join(home, ".claude", "plugins"), "review-plugin", {
  484. name: "review-plugin",
  485. version: "1.2.3",
  486. description: "review",
  487. });
  488. const { createSettingsSyncService } =
  489. await import("../src/settings-sync/settingsSyncService.js");
  490. const service = createSettingsSyncService({ settingService: createSettingService() });
  491. const discovery = await service.detect({
  492. workspacePath,
  493. categories: ["plugins"],
  494. intent: "manualImport",
  495. });
  496. expect(discovery.agents).toEqual([
  497. {
  498. agent: "claudeCode",
  499. discovered: true,
  500. categories: [
  501. {
  502. category: "plugins",
  503. discoveredCount: 1,
  504. importableCount: 1,
  505. skippedCount: 0,
  506. sourcePaths: [join(home, ".claude", "plugins")],
  507. sourceRoots: [
  508. {
  509. scope: "global",
  510. path: join(home, ".claude", "plugins"),
  511. discoveredCount: 1,
  512. importableCount: 1,
  513. skippedCount: 0,
  514. plugins: [
  515. {
  516. name: "review-plugin",
  517. path: pluginPath,
  518. importable: true,
  519. version: "1.2.3",
  520. },
  521. ],
  522. },
  523. ],
  524. selectedByDefault: false,
  525. },
  526. ],
  527. },
  528. ]);
  529. const result = await service.importSelected({
  530. workspacePath,
  531. selections: [{ agent: "claudeCode", category: "plugins" }],
  532. });
  533. const importedPath = join(home, ".zcode", "plugins", "review-plugin");
  534. const config = JSON.parse(
  535. readFileSync(join(home, ".zcode", "cli", "config.json"), "utf-8"),
  536. ) as { plugins?: { dirs?: string[] } };
  537. expect(result.successCount).toBe(1);
  538. expect(result.skippedCount).toBe(0);
  539. expect(result.failedCount).toBe(0);
  540. expect(result.taskResults[0]?.pluginResults).toEqual([
  541. {
  542. name: "review-plugin",
  543. path: pluginPath,
  544. sourceScope: "global",
  545. status: "imported",
  546. version: "1.2.3",
  547. },
  548. ]);
  549. expect(existsSync(join(importedPath, ".zcode-plugin", "plugin.json"))).toBe(true);
  550. expect(lstatSync(importedPath).isSymbolicLink()).toBe(true);
  551. expect(config.plugins?.dirs).toEqual([importedPath]);
  552. });
  553. it("imports Claude-manifest plugins into the requested project target as copies", async () => {
  554. const home = makeTempHome();
  555. const workspacePath = join(home, "workspace");
  556. process.env.HOME = home;
  557. const pluginPath = writePlugin(
  558. join(home, ".codex", "plugins"),
  559. "writer-plugin",
  560. { name: "writer-plugin" },
  561. ".claude-plugin",
  562. );
  563. const { createSettingsSyncService } =
  564. await import("../src/settings-sync/settingsSyncService.js");
  565. const service = createSettingsSyncService({ settingService: createSettingService() });
  566. const result = await service.importSelected({
  567. workspacePath,
  568. selections: [
  569. {
  570. agent: "codexCli",
  571. category: "plugins",
  572. sourceScope: "global",
  573. targetScope: "project",
  574. importMode: "copy",
  575. pluginPaths: [pluginPath],
  576. },
  577. ],
  578. });
  579. const importedPath = join(workspacePath, ".zcode", "plugins", "writer-plugin");
  580. const config = JSON.parse(
  581. readFileSync(join(workspacePath, ".zcode", "config.json"), "utf-8"),
  582. ) as { plugins?: { dirs?: string[] } };
  583. expect(result.successCount).toBe(1);
  584. expect(lstatSync(importedPath).isSymbolicLink()).toBe(false);
  585. expect(existsSync(join(importedPath, ".claude-plugin", "plugin.json"))).toBe(true);
  586. expect(config.plugins?.dirs).toEqual([importedPath]);
  587. expect(existsSync(join(home, ".zcode", "plugins", "writer-plugin"))).toBe(false);
  588. });
  589. it("skips plugin imports when target directory or same plugin id exists", async () => {
  590. const home = makeTempHome();
  591. const workspacePath = join(home, "workspace");
  592. process.env.HOME = home;
  593. writePlugin(join(home, ".claude", "plugins"), "review-plugin", {
  594. name: "review-plugin",
  595. });
  596. writePlugin(join(home, ".codex", "plugins"), "other-review", {
  597. name: "other-review",
  598. });
  599. writePlugin(join(home, ".zcode", "plugins"), "review-plugin", {
  600. name: "review-plugin",
  601. });
  602. const configuredPluginPath = writePlugin(join(home, "configured"), "other-review", {
  603. name: "other-review",
  604. });
  605. mkdirSync(join(home, ".zcode", "cli"), { recursive: true });
  606. writeFileSync(
  607. join(home, ".zcode", "cli", "config.json"),
  608. JSON.stringify({ plugins: { dirs: [configuredPluginPath] } }),
  609. "utf-8",
  610. );
  611. const { createSettingsSyncService } =
  612. await import("../src/settings-sync/settingsSyncService.js");
  613. const service = createSettingsSyncService({ settingService: createSettingService() });
  614. const discovery = await service.detect({
  615. workspacePath,
  616. categories: ["plugins"],
  617. intent: "manualImport",
  618. });
  619. expect(discovery.agents[0]?.categories[0]?.sourceRoots?.[0]?.plugins).toEqual([
  620. {
  621. name: "review-plugin",
  622. path: join(home, ".claude", "plugins", "review-plugin"),
  623. importable: false,
  624. skipReason: "targetExists",
  625. },
  626. ]);
  627. expect(discovery.agents[1]?.categories[0]?.sourceRoots?.[0]?.plugins).toEqual([
  628. {
  629. name: "other-review",
  630. path: join(home, ".codex", "plugins", "other-review"),
  631. importable: false,
  632. skipReason: "sameNameExists",
  633. },
  634. ]);
  635. const result = await service.importSelected({
  636. workspacePath,
  637. selections: [{ agent: "codexCli", category: "plugins" }],
  638. });
  639. expect(result.successCount).toBe(0);
  640. expect(result.skippedCount).toBe(1);
  641. expect(result.taskResults[0]?.pluginResults).toEqual([
  642. {
  643. name: "other-review",
  644. path: join(home, ".codex", "plugins", "other-review"),
  645. sourceScope: "global",
  646. status: "skipped",
  647. skipReason: "sameNameExists",
  648. },
  649. ]);
  650. });
  651. });
  652. describe("settingsSyncService MCP server import", () => {
  653. it("does not scan external MCP servers during first-run detection", async () => {
  654. const home = makeTempHome();
  655. const workspacePath = join(home, "workspace");
  656. process.env.HOME = home;
  657. writeJsonConfig(join(home, ".claude", "settings.json"), {
  658. mcpServers: {
  659. browser: { command: "npx", args: ["-y", "browser-mcp"], timeout: 30 },
  660. },
  661. });
  662. const { createSettingsSyncService } =
  663. await import("../src/settings-sync/settingsSyncService.js");
  664. const service = createSettingsSyncService({ settingService: createSettingService() });
  665. await expect(service.detect({ workspacePath })).resolves.toEqual({ agents: [] });
  666. });
  667. it("scans and imports selected Claude MCP servers into global ZCode config", async () => {
  668. const home = makeTempHome();
  669. const workspacePath = join(home, "workspace");
  670. process.env.HOME = home;
  671. writeJsonConfig(join(home, ".claude", "settings.json"), {
  672. mcpServers: {
  673. browser: { command: "npx", args: ["-y", "browser-mcp"] },
  674. },
  675. });
  676. const { createSettingsSyncService } =
  677. await import("../src/settings-sync/settingsSyncService.js");
  678. const service = createSettingsSyncService({ settingService: createSettingService() });
  679. const discovery = await service.detect({
  680. workspacePath,
  681. categories: ["mcpServers"],
  682. intent: "manualImport",
  683. });
  684. expect(discovery.agents).toEqual([
  685. {
  686. agent: "claudeCode",
  687. discovered: true,
  688. categories: [
  689. {
  690. category: "mcpServers",
  691. discoveredCount: 1,
  692. importableCount: 1,
  693. skippedCount: 0,
  694. sourcePaths: [join(home, ".claude", "settings.json")],
  695. sourceRoots: [
  696. {
  697. scope: "global",
  698. path: join(home, ".claude", "settings.json"),
  699. discoveredCount: 1,
  700. importableCount: 1,
  701. skippedCount: 0,
  702. mcpServers: [
  703. {
  704. name: "browser",
  705. path: `${join(home, ".claude", "settings.json")}#browser`,
  706. importable: true,
  707. },
  708. ],
  709. },
  710. ],
  711. selectedByDefault: false,
  712. },
  713. ],
  714. },
  715. ]);
  716. const result = await service.importSelected({
  717. workspacePath,
  718. selections: [
  719. {
  720. agent: "claudeCode",
  721. category: "mcpServers",
  722. mcpServerPaths: [`${join(home, ".claude", "settings.json")}#browser`],
  723. },
  724. ],
  725. });
  726. const config = JSON.parse(
  727. readFileSync(join(home, ".zcode", "cli", "config.json"), "utf-8"),
  728. ) as { mcp?: { servers?: Record<string, unknown> } };
  729. expect(result.successCount).toBe(1);
  730. expect(result.skippedCount).toBe(0);
  731. expect(result.taskResults[0]?.mcpServerResults).toEqual([
  732. {
  733. name: "browser",
  734. path: `${join(home, ".claude", "settings.json")}#browser`,
  735. sourceScope: "global",
  736. status: "imported",
  737. },
  738. ]);
  739. expect(config.mcp?.servers?.browser).toEqual({
  740. command: "npx",
  741. args: ["-y", "browser-mcp"],
  742. });
  743. });
  744. it("does not migrate external MCP timeout fields", async () => {
  745. const home = makeTempHome();
  746. const workspacePath = join(home, "workspace");
  747. process.env.HOME = home;
  748. writeTextConfig(
  749. join(home, ".codex", "config.toml"),
  750. [
  751. "[mcp_servers.context7]",
  752. 'command = "npx"',
  753. 'args = ["-y", "@upstash/context7-mcp"]',
  754. "startup_timeout_sec = 120",
  755. "timeout = 30",
  756. ].join("\n"),
  757. );
  758. const { createSettingsSyncService } =
  759. await import("../src/settings-sync/settingsSyncService.js");
  760. const service = createSettingsSyncService({ settingService: createSettingService() });
  761. const result = await service.importSelected({
  762. workspacePath,
  763. selections: [{ agent: "codexCli", category: "mcpServers" }],
  764. });
  765. const config = JSON.parse(
  766. readFileSync(join(home, ".zcode", "cli", "config.json"), "utf-8"),
  767. ) as { mcp?: { servers?: Record<string, unknown> } };
  768. expect(result.successCount).toBe(1);
  769. expect(config.mcp?.servers?.context7).toEqual({
  770. command: "npx",
  771. args: ["-y", "@upstash/context7-mcp"],
  772. });
  773. });
  774. it("imports Codex TOML MCP servers into the requested project target", async () => {
  775. const home = makeTempHome();
  776. const workspacePath = join(home, "workspace");
  777. process.env.HOME = home;
  778. writeTextConfig(
  779. join(home, ".codex", "config.toml"),
  780. ["[mcp_servers.context7]", 'command = "npx"', 'args = ["-y", "@upstash/context7-mcp"]'].join(
  781. "\n",
  782. ),
  783. );
  784. const { createSettingsSyncService } =
  785. await import("../src/settings-sync/settingsSyncService.js");
  786. const service = createSettingsSyncService({ settingService: createSettingService() });
  787. const result = await service.importSelected({
  788. workspacePath,
  789. selections: [
  790. {
  791. agent: "codexCli",
  792. category: "mcpServers",
  793. sourceScope: "global",
  794. targetScope: "project",
  795. },
  796. ],
  797. });
  798. const config = JSON.parse(
  799. readFileSync(join(workspacePath, ".zcode", "config.json"), "utf-8"),
  800. ) as { mcp?: { servers?: Record<string, unknown> } };
  801. expect(result.successCount).toBe(1);
  802. expect(config.mcp?.servers?.context7).toEqual({
  803. command: "npx",
  804. args: ["-y", "@upstash/context7-mcp"],
  805. });
  806. expect(existsSync(join(home, ".zcode", "cli", "config.json"))).toBe(false);
  807. });
  808. it("normalizes OpenCode command arrays when importing MCP servers", async () => {
  809. const home = makeTempHome();
  810. const workspacePath = join(home, "workspace");
  811. process.env.HOME = home;
  812. writeJsonConfig(join(home, ".config", "opencode", "opencode.json"), {
  813. mcp: {
  814. browser: {
  815. type: "local",
  816. command: ["npx", "-y", "browser-mcp"],
  817. env: { DEBUG: "1" },
  818. },
  819. },
  820. });
  821. const { createSettingsSyncService } =
  822. await import("../src/settings-sync/settingsSyncService.js");
  823. const service = createSettingsSyncService({ settingService: createSettingService() });
  824. const result = await service.importSelected({
  825. workspacePath,
  826. selections: [{ agent: "openCode", category: "mcpServers" }],
  827. });
  828. const config = JSON.parse(
  829. readFileSync(join(home, ".zcode", "cli", "config.json"), "utf-8"),
  830. ) as { mcp?: { servers?: Record<string, unknown> } };
  831. expect(result.successCount).toBe(1);
  832. expect(config.mcp?.servers?.browser).toEqual({
  833. type: "stdio",
  834. command: "npx",
  835. args: ["-y", "browser-mcp"],
  836. env: { DEBUG: "1" },
  837. });
  838. });
  839. it("skips imported MCP servers when the target already has the same name", async () => {
  840. const home = makeTempHome();
  841. const workspacePath = join(home, "workspace");
  842. process.env.HOME = home;
  843. writeJsonConfig(join(home, ".claude", "settings.json"), {
  844. mcpServers: {
  845. browser: { command: "external" },
  846. },
  847. });
  848. writeJsonConfig(join(home, ".zcode", "cli", "config.json"), {
  849. mcp: {
  850. servers: {
  851. browser: { command: "existing" },
  852. },
  853. },
  854. });
  855. const { createSettingsSyncService } =
  856. await import("../src/settings-sync/settingsSyncService.js");
  857. const service = createSettingsSyncService({ settingService: createSettingService() });
  858. const discovery = await service.detect({
  859. workspacePath,
  860. categories: ["mcpServers"],
  861. intent: "manualImport",
  862. });
  863. expect(discovery.agents[0]?.categories[0]?.sourceRoots?.[0]?.mcpServers).toEqual([
  864. {
  865. name: "browser",
  866. path: `${join(home, ".claude", "settings.json")}#browser`,
  867. importable: false,
  868. skipReason: "sameNameExists",
  869. },
  870. ]);
  871. const result = await service.importSelected({
  872. workspacePath,
  873. selections: [{ agent: "claudeCode", category: "mcpServers" }],
  874. });
  875. const config = JSON.parse(
  876. readFileSync(join(home, ".zcode", "cli", "config.json"), "utf-8"),
  877. ) as { mcp?: { servers?: Record<string, unknown> } };
  878. expect(result.successCount).toBe(0);
  879. expect(result.skippedCount).toBe(1);
  880. expect(config.mcp?.servers?.browser).toEqual({ command: "existing" });
  881. });
  882. });
  883. describe("settingsSyncService skills import", () => {
  884. it("uses platform-specific directory symlink types", async () => {
  885. const { getSkillDirectorySymlinkType } =
  886. await import("../src/settings-sync/settingsSyncService.js");
  887. expect(getSkillDirectorySymlinkType("darwin")).toBe("dir");
  888. expect(getSkillDirectorySymlinkType("linux")).toBe("dir");
  889. expect(getSkillDirectorySymlinkType("win32")).toBe("junction");
  890. });
  891. it("does not scan external skills during first-run detection", async () => {
  892. const home = makeTempHome();
  893. const workspacePath = join(home, "workspace");
  894. process.env.HOME = home;
  895. writeSkill(
  896. join(home, ".claude", "skills"),
  897. "review",
  898. "---\nname: review\nversion: 1.2.3\ndescription: review\n---\nbody",
  899. );
  900. const { createSettingsSyncService } =
  901. await import("../src/settings-sync/settingsSyncService.js");
  902. const service = createSettingsSyncService({ settingService: createSettingService() });
  903. await expect(service.detect({ workspacePath })).resolves.toEqual({ agents: [] });
  904. });
  905. it("imports selected user skills into the ZCode user skills directory as symlinks by default", async () => {
  906. const home = makeTempHome();
  907. const workspacePath = join(home, "workspace");
  908. process.env.HOME = home;
  909. writeSkill(
  910. join(home, ".claude", "skills"),
  911. "review",
  912. "---\nname: review\nversion: 1.2.3\ndescription: review\n---\nbody",
  913. );
  914. const { createSettingsSyncService } =
  915. await import("../src/settings-sync/settingsSyncService.js");
  916. const service = createSettingsSyncService({ settingService: createSettingService() });
  917. const discovery = await service.detect({
  918. workspacePath,
  919. categories: ["skills"],
  920. intent: "manualImport",
  921. });
  922. expect(discovery.agents).toEqual([
  923. {
  924. agent: "claudeCode",
  925. discovered: true,
  926. categories: [
  927. {
  928. category: "skills",
  929. discoveredCount: 1,
  930. importableCount: 1,
  931. skippedCount: 0,
  932. sourcePaths: [join(home, ".claude", "skills")],
  933. sourceRoots: [
  934. {
  935. scope: "global",
  936. path: join(home, ".claude", "skills"),
  937. discoveredCount: 1,
  938. importableCount: 1,
  939. skippedCount: 0,
  940. skills: [
  941. {
  942. name: "review",
  943. path: join(home, ".claude", "skills", "review"),
  944. importable: true,
  945. version: "1.2.3",
  946. },
  947. ],
  948. },
  949. ],
  950. selectedByDefault: true,
  951. },
  952. ],
  953. },
  954. ]);
  955. const result = await service.importSelected({
  956. workspacePath,
  957. selections: [{ agent: "claudeCode", category: "skills" }],
  958. });
  959. expect(result.successCount).toBe(1);
  960. expect(result.skippedCount).toBe(0);
  961. expect(result.failedCount).toBe(0);
  962. expect(result.taskResults[0]?.skillResults).toEqual([
  963. {
  964. name: "review",
  965. path: join(home, ".claude", "skills", "review"),
  966. sourceScope: "global",
  967. status: "imported",
  968. version: "1.2.3",
  969. },
  970. ]);
  971. const importedDirectoryPath = join(home, ".zcode", "skills", "review");
  972. const importedPath = join(importedDirectoryPath, "SKILL.md");
  973. expect(existsSync(importedPath)).toBe(true);
  974. expect(lstatSync(importedDirectoryPath).isSymbolicLink()).toBe(true);
  975. expect(readFileSync(importedPath, "utf-8")).toContain("name: review");
  976. expect(readFileSync(importedPath, "utf-8")).toContain("version: 1.2.3");
  977. });
  978. it("imports only selected skill paths from a source", async () => {
  979. const home = makeTempHome();
  980. const workspacePath = join(home, "workspace");
  981. process.env.HOME = home;
  982. writeSkill(
  983. join(home, ".claude", "skills"),
  984. "review",
  985. "---\nname: review\ndescription: review\n---\nreview-body",
  986. );
  987. writeSkill(
  988. join(home, ".claude", "skills"),
  989. "writer",
  990. "---\nname: writer\ndescription: writer\n---\nwriter-body",
  991. );
  992. const { createSettingsSyncService } =
  993. await import("../src/settings-sync/settingsSyncService.js");
  994. const service = createSettingsSyncService({ settingService: createSettingService() });
  995. const result = await service.importSelected({
  996. workspacePath,
  997. selections: [
  998. {
  999. agent: "claudeCode",
  1000. category: "skills",
  1001. sourceScope: "global",
  1002. skillPaths: [join(home, ".claude", "skills", "writer")],
  1003. },
  1004. ],
  1005. });
  1006. expect(result.successCount).toBe(1);
  1007. expect(result.skippedCount).toBe(0);
  1008. expect(result.failedCount).toBe(0);
  1009. expect(existsSync(join(home, ".zcode", "skills", "writer", "SKILL.md"))).toBe(true);
  1010. expect(existsSync(join(home, ".zcode", "skills", "review", "SKILL.md"))).toBe(false);
  1011. });
  1012. it("imports selected skills into the requested project target", async () => {
  1013. const home = makeTempHome();
  1014. const workspacePath = join(home, "workspace");
  1015. process.env.HOME = home;
  1016. writeSkill(
  1017. join(home, ".claude", "skills"),
  1018. "review",
  1019. "---\nname: review\ndescription: review\n---\nreview-body",
  1020. );
  1021. const { createSettingsSyncService } =
  1022. await import("../src/settings-sync/settingsSyncService.js");
  1023. const service = createSettingsSyncService({ settingService: createSettingService() });
  1024. const result = await service.importSelected({
  1025. workspacePath,
  1026. selections: [
  1027. {
  1028. agent: "claudeCode",
  1029. category: "skills",
  1030. sourceScope: "global",
  1031. targetScope: "project",
  1032. skillPaths: [join(home, ".claude", "skills", "review")],
  1033. },
  1034. ],
  1035. });
  1036. expect(result.successCount).toBe(1);
  1037. expect(result.skippedCount).toBe(0);
  1038. expect(result.failedCount).toBe(0);
  1039. expect(existsSync(join(workspacePath, ".zcode", "skills", "review", "SKILL.md"))).toBe(true);
  1040. expect(existsSync(join(home, ".zcode", "skills", "review", "SKILL.md"))).toBe(false);
  1041. });
  1042. it("can import selected skills as symlinks", async () => {
  1043. const home = makeTempHome();
  1044. const workspacePath = join(home, "workspace");
  1045. process.env.HOME = home;
  1046. writeSkill(
  1047. join(home, ".claude", "skills"),
  1048. "linked-review",
  1049. "---\nname: linked-review\ndescription: review\n---\nreview-body",
  1050. );
  1051. const { createSettingsSyncService } =
  1052. await import("../src/settings-sync/settingsSyncService.js");
  1053. const service = createSettingsSyncService({ settingService: createSettingService() });
  1054. const result = await service.importSelected({
  1055. workspacePath,
  1056. selections: [
  1057. {
  1058. agent: "claudeCode",
  1059. category: "skills",
  1060. sourceScope: "global",
  1061. importMode: "symlink",
  1062. skillPaths: [join(home, ".claude", "skills", "linked-review")],
  1063. },
  1064. ],
  1065. });
  1066. const targetPath = join(home, ".zcode", "skills", "linked-review");
  1067. expect(result.successCount).toBe(1);
  1068. expect(lstatSync(targetPath).isSymbolicLink()).toBe(true);
  1069. expect(readFileSync(join(targetPath, "SKILL.md"), "utf-8")).toContain("linked-review");
  1070. });
  1071. it("can import selected skills as copies when requested", async () => {
  1072. const home = makeTempHome();
  1073. const workspacePath = join(home, "workspace");
  1074. process.env.HOME = home;
  1075. writeSkill(
  1076. join(home, ".claude", "skills"),
  1077. "copied-review",
  1078. "---\nname: copied-review\ndescription: review\n---\nreview-body",
  1079. );
  1080. const { createSettingsSyncService } =
  1081. await import("../src/settings-sync/settingsSyncService.js");
  1082. const service = createSettingsSyncService({ settingService: createSettingService() });
  1083. const result = await service.importSelected({
  1084. workspacePath,
  1085. selections: [
  1086. {
  1087. agent: "claudeCode",
  1088. category: "skills",
  1089. sourceScope: "global",
  1090. importMode: "copy",
  1091. skillPaths: [join(home, ".claude", "skills", "copied-review")],
  1092. },
  1093. ],
  1094. });
  1095. const targetPath = join(home, ".zcode", "skills", "copied-review");
  1096. expect(result.successCount).toBe(1);
  1097. expect(lstatSync(targetPath).isSymbolicLink()).toBe(false);
  1098. expect(readFileSync(join(targetPath, "SKILL.md"), "utf-8")).toContain("copied-review");
  1099. });
  1100. it("scans and imports global skills without an active workspace", async () => {
  1101. const home = makeTempHome();
  1102. process.env.HOME = home;
  1103. writeSkill(
  1104. join(home, ".codex", "skills"),
  1105. "commit-helper",
  1106. "---\nname: commit-helper\ndescription: commit\n---\nbody",
  1107. );
  1108. const { createSettingsSyncService } =
  1109. await import("../src/settings-sync/settingsSyncService.js");
  1110. const service = createSettingsSyncService({ settingService: createSettingService() });
  1111. const discovery = await service.detect({
  1112. categories: ["skills"],
  1113. intent: "manualImport",
  1114. });
  1115. expect(discovery.agents).toEqual([
  1116. {
  1117. agent: "codexCli",
  1118. discovered: true,
  1119. categories: [
  1120. {
  1121. category: "skills",
  1122. discoveredCount: 1,
  1123. importableCount: 1,
  1124. skippedCount: 0,
  1125. sourcePaths: [join(home, ".codex", "skills")],
  1126. sourceRoots: [
  1127. {
  1128. scope: "global",
  1129. path: join(home, ".codex", "skills"),
  1130. discoveredCount: 1,
  1131. importableCount: 1,
  1132. skippedCount: 0,
  1133. skills: [
  1134. {
  1135. name: "commit-helper",
  1136. path: join(home, ".codex", "skills", "commit-helper"),
  1137. importable: true,
  1138. },
  1139. ],
  1140. },
  1141. ],
  1142. selectedByDefault: true,
  1143. },
  1144. ],
  1145. },
  1146. ]);
  1147. const result = await service.importSelected({
  1148. selections: [{ agent: "codexCli", category: "skills" }],
  1149. });
  1150. expect(result.successCount).toBe(1);
  1151. expect(existsSync(join(home, ".zcode", "skills", "commit-helper", "SKILL.md"))).toBe(true);
  1152. });
  1153. it("does not scan .agents skills for import", async () => {
  1154. const home = makeTempHome();
  1155. const workspacePath = join(home, "workspace");
  1156. process.env.HOME = home;
  1157. writeSkill(
  1158. join(home, ".agents", "skills"),
  1159. "already-readable",
  1160. "---\nname: already-readable\ndescription: agents\n---\nbody",
  1161. );
  1162. writeSkill(
  1163. join(workspacePath, ".agents", "skills"),
  1164. "project-readable",
  1165. "---\nname: project-readable\ndescription: project agents\n---\nbody",
  1166. );
  1167. const { createSettingsSyncService } =
  1168. await import("../src/settings-sync/settingsSyncService.js");
  1169. const service = createSettingsSyncService({ settingService: createSettingService() });
  1170. await expect(
  1171. service.detect({
  1172. workspacePath,
  1173. categories: ["skills"],
  1174. intent: "manualImport",
  1175. }),
  1176. ).resolves.toEqual({ agents: [] });
  1177. });
  1178. it("scans newly supported non-.agents skill roots", async () => {
  1179. const home = makeTempHome();
  1180. const workspacePath = join(home, "workspace");
  1181. process.env.HOME = home;
  1182. writeSkill(
  1183. join(home, ".codeium", "windsurf", "skills"),
  1184. "wave",
  1185. "---\nname: wave\ndescription: windsurf\n---\nbody",
  1186. );
  1187. writeSkill(
  1188. join(home, ".openclaw", "skills"),
  1189. "claw",
  1190. "---\nname: claw\ndescription: openclaw\n---\nbody",
  1191. );
  1192. writeSkill(
  1193. join(home, ".qwen", "skills"),
  1194. "qwen-skill",
  1195. "---\nname: qwen-skill\ndescription: qwen code\n---\nbody",
  1196. );
  1197. writeSkill(
  1198. join(home, ".qoder", "skills"),
  1199. "qode-skill",
  1200. "---\nname: qode-skill\ndescription: qode\n---\nbody",
  1201. );
  1202. writeSkill(
  1203. join(home, ".qoder-cn", "skills"),
  1204. "qode-cn-skill",
  1205. "---\nname: qode-cn-skill\ndescription: qode cn\n---\nbody",
  1206. );
  1207. writeSkill(
  1208. join(home, ".trae-cn", "skills"),
  1209. "trae-cn-skill",
  1210. "---\nname: trae-cn-skill\ndescription: trae cn\n---\nbody",
  1211. );
  1212. writeSkill(
  1213. join(workspacePath, ".codebuddy", "skills"),
  1214. "buddy",
  1215. "---\nname: buddy\ndescription: codebuddy\n---\nbody",
  1216. );
  1217. const { createSettingsSyncService } =
  1218. await import("../src/settings-sync/settingsSyncService.js");
  1219. const service = createSettingsSyncService({ settingService: createSettingService() });
  1220. const discovery = await service.detect({
  1221. workspacePath,
  1222. categories: ["skills"],
  1223. intent: "manualImport",
  1224. });
  1225. expect(discovery.agents.map((agent) => agent.agent)).toEqual([
  1226. "openClaw",
  1227. "qwenCode",
  1228. "qode",
  1229. "qodeCn",
  1230. "windsurf",
  1231. "traeCn",
  1232. "codeBuddy",
  1233. ]);
  1234. expect(discovery.agents[0]?.categories[0]?.sourceRoots?.[0]).toMatchObject({
  1235. scope: "global",
  1236. path: join(home, ".openclaw", "skills"),
  1237. discoveredCount: 1,
  1238. importableCount: 1,
  1239. });
  1240. expect(discovery.agents[1]?.categories[0]?.sourceRoots?.[0]).toMatchObject({
  1241. scope: "global",
  1242. path: join(home, ".qwen", "skills"),
  1243. discoveredCount: 1,
  1244. importableCount: 1,
  1245. });
  1246. expect(discovery.agents[2]?.categories[0]?.sourceRoots?.[0]).toMatchObject({
  1247. scope: "global",
  1248. path: join(home, ".qoder", "skills"),
  1249. discoveredCount: 1,
  1250. importableCount: 1,
  1251. });
  1252. expect(discovery.agents[3]?.categories[0]?.sourceRoots?.[0]).toMatchObject({
  1253. scope: "global",
  1254. path: join(home, ".qoder-cn", "skills"),
  1255. discoveredCount: 1,
  1256. importableCount: 1,
  1257. });
  1258. expect(discovery.agents[4]?.categories[0]?.sourceRoots?.[0]).toMatchObject({
  1259. scope: "global",
  1260. path: join(home, ".codeium", "windsurf", "skills"),
  1261. discoveredCount: 1,
  1262. importableCount: 1,
  1263. });
  1264. expect(discovery.agents[5]?.categories[0]?.sourceRoots?.[0]).toMatchObject({
  1265. scope: "global",
  1266. path: join(home, ".trae-cn", "skills"),
  1267. discoveredCount: 1,
  1268. importableCount: 1,
  1269. });
  1270. expect(discovery.agents[6]?.categories[0]?.sourceRoots?.[0]).toMatchObject({
  1271. scope: "project",
  1272. path: join(workspacePath, ".codebuddy", "skills"),
  1273. discoveredCount: 1,
  1274. importableCount: 1,
  1275. });
  1276. });
  1277. it("imports workspace-level Codex skills but skips same-name target directories", async () => {
  1278. const home = makeTempHome();
  1279. const workspacePath = join(home, "workspace");
  1280. process.env.HOME = home;
  1281. writeSkill(
  1282. join(workspacePath, ".codex", "skills"),
  1283. "review",
  1284. "---\nname: review\ndescription: source\n---\nsource-body",
  1285. );
  1286. writeSkill(
  1287. join(workspacePath, ".zcode", "skills"),
  1288. "review",
  1289. "---\nname: review\ndescription: existing\n---\nexisting-body",
  1290. );
  1291. const { createSettingsSyncService } =
  1292. await import("../src/settings-sync/settingsSyncService.js");
  1293. const service = createSettingsSyncService({ settingService: createSettingService() });
  1294. const result = await service.importSelected({
  1295. workspacePath,
  1296. selections: [{ agent: "codexCli", category: "skills", sourceScope: "project" }],
  1297. });
  1298. expect(result.successCount).toBe(0);
  1299. expect(result.skippedCount).toBe(1);
  1300. expect(result.failedCount).toBe(0);
  1301. expect(result.taskResults[0]?.skillResults).toEqual([
  1302. {
  1303. name: "review",
  1304. path: join(workspacePath, ".codex", "skills", "review"),
  1305. sourceScope: "project",
  1306. status: "skipped",
  1307. skipReason: "targetExists",
  1308. },
  1309. ]);
  1310. expect(
  1311. readFileSync(join(workspacePath, ".zcode", "skills", "review", "SKILL.md"), "utf-8"),
  1312. ).toContain("existing-body");
  1313. });
  1314. it("classifies workspace-level skill roots as project sources", async () => {
  1315. const home = makeTempHome();
  1316. const workspacePath = join(home, "workspace");
  1317. process.env.HOME = home;
  1318. writeSkill(
  1319. join(workspacePath, ".claude", "skills"),
  1320. "repo-review",
  1321. "---\nname: repo-review\ndescription: project claude\n---\nbody",
  1322. );
  1323. writeSkill(
  1324. join(workspacePath, ".codex", "skills"),
  1325. "repo-helper",
  1326. "---\nname: repo-helper\ndescription: project codex\n---\nbody",
  1327. );
  1328. writeSkill(
  1329. join(workspacePath, ".opencode", "skills"),
  1330. "repo-automation",
  1331. "---\nname: repo-automation\ndescription: project opencode\n---\nbody",
  1332. );
  1333. const { createSettingsSyncService } =
  1334. await import("../src/settings-sync/settingsSyncService.js");
  1335. const service = createSettingsSyncService({ settingService: createSettingService() });
  1336. const discovery = await service.detect({
  1337. workspacePath,
  1338. categories: ["skills"],
  1339. intent: "manualImport",
  1340. });
  1341. expect(discovery.agents).toEqual([
  1342. {
  1343. agent: "claudeCode",
  1344. discovered: true,
  1345. categories: [
  1346. {
  1347. category: "skills",
  1348. discoveredCount: 1,
  1349. importableCount: 1,
  1350. skippedCount: 0,
  1351. sourcePaths: [join(workspacePath, ".claude", "skills")],
  1352. sourceRoots: [
  1353. {
  1354. scope: "project",
  1355. path: join(workspacePath, ".claude", "skills"),
  1356. discoveredCount: 1,
  1357. importableCount: 1,
  1358. skippedCount: 0,
  1359. skills: [
  1360. {
  1361. name: "repo-review",
  1362. path: join(workspacePath, ".claude", "skills", "repo-review"),
  1363. importable: true,
  1364. },
  1365. ],
  1366. },
  1367. ],
  1368. selectedByDefault: true,
  1369. },
  1370. ],
  1371. },
  1372. {
  1373. agent: "codexCli",
  1374. discovered: true,
  1375. categories: [
  1376. {
  1377. category: "skills",
  1378. discoveredCount: 1,
  1379. importableCount: 1,
  1380. skippedCount: 0,
  1381. sourcePaths: [join(workspacePath, ".codex", "skills")],
  1382. sourceRoots: [
  1383. {
  1384. scope: "project",
  1385. path: join(workspacePath, ".codex", "skills"),
  1386. discoveredCount: 1,
  1387. importableCount: 1,
  1388. skippedCount: 0,
  1389. skills: [
  1390. {
  1391. name: "repo-helper",
  1392. path: join(workspacePath, ".codex", "skills", "repo-helper"),
  1393. importable: true,
  1394. },
  1395. ],
  1396. },
  1397. ],
  1398. selectedByDefault: true,
  1399. },
  1400. ],
  1401. },
  1402. {
  1403. agent: "openCode",
  1404. discovered: true,
  1405. categories: [
  1406. {
  1407. category: "skills",
  1408. discoveredCount: 1,
  1409. importableCount: 1,
  1410. skippedCount: 0,
  1411. sourcePaths: [join(workspacePath, ".opencode", "skills")],
  1412. sourceRoots: [
  1413. {
  1414. scope: "project",
  1415. path: join(workspacePath, ".opencode", "skills"),
  1416. discoveredCount: 1,
  1417. importableCount: 1,
  1418. skippedCount: 0,
  1419. skills: [
  1420. {
  1421. name: "repo-automation",
  1422. path: join(workspacePath, ".opencode", "skills", "repo-automation"),
  1423. importable: true,
  1424. },
  1425. ],
  1426. },
  1427. ],
  1428. selectedByDefault: true,
  1429. },
  1430. ],
  1431. },
  1432. ]);
  1433. });
  1434. it("skips imported skills when an existing ZCode skill has the same name", async () => {
  1435. const home = makeTempHome();
  1436. const workspacePath = join(home, "workspace");
  1437. process.env.HOME = home;
  1438. writeSkill(
  1439. join(home, ".codex", "skills"),
  1440. "external-review",
  1441. "---\nname: review\ndescription: source\n---\nsource-body",
  1442. );
  1443. writeSkill(
  1444. join(home, ".zcode", "skills"),
  1445. "current-review",
  1446. "---\nname: review\ndescription: existing\n---\nexisting-body",
  1447. );
  1448. const { createSettingsSyncService } =
  1449. await import("../src/settings-sync/settingsSyncService.js");
  1450. const service = createSettingsSyncService({ settingService: createSettingService() });
  1451. const discovery = await service.detect({
  1452. workspacePath,
  1453. categories: ["skills"],
  1454. intent: "manualImport",
  1455. });
  1456. expect(discovery.agents[0]?.categories[0]).toMatchObject({
  1457. discoveredCount: 1,
  1458. importableCount: 0,
  1459. skippedCount: 1,
  1460. sourcePaths: [join(home, ".codex", "skills")],
  1461. sourceRoots: [
  1462. {
  1463. scope: "global",
  1464. path: join(home, ".codex", "skills"),
  1465. discoveredCount: 1,
  1466. importableCount: 0,
  1467. skippedCount: 1,
  1468. skills: [
  1469. {
  1470. name: "review",
  1471. path: join(home, ".codex", "skills", "external-review"),
  1472. importable: false,
  1473. skipReason: "sameNameExists",
  1474. },
  1475. ],
  1476. },
  1477. ],
  1478. selectedByDefault: false,
  1479. });
  1480. const result = await service.importSelected({
  1481. workspacePath,
  1482. selections: [{ agent: "codexCli", category: "skills" }],
  1483. });
  1484. expect(result.successCount).toBe(0);
  1485. expect(result.skippedCount).toBe(1);
  1486. expect(result.failedCount).toBe(0);
  1487. expect(result.taskResults[0]?.skillResults).toEqual([
  1488. {
  1489. name: "review",
  1490. path: join(home, ".codex", "skills", "external-review"),
  1491. sourceScope: "global",
  1492. status: "skipped",
  1493. skipReason: "sameNameExists",
  1494. },
  1495. ]);
  1496. expect(existsSync(join(home, ".zcode", "skills", "external-review"))).toBe(false);
  1497. expect(
  1498. readFileSync(join(home, ".zcode", "skills", "current-review", "SKILL.md"), "utf-8"),
  1499. ).toContain("existing-body");
  1500. });
  1501. it("reports target directory conflicts as skip reasons", async () => {
  1502. const home = makeTempHome();
  1503. const workspacePath = join(home, "workspace");
  1504. process.env.HOME = home;
  1505. writeSkill(
  1506. join(home, ".claude", "skills"),
  1507. "review",
  1508. "---\nname: external-review\ndescription: source\n---\nsource-body",
  1509. );
  1510. writeSkill(
  1511. join(home, ".zcode", "skills"),
  1512. "review",
  1513. "---\nname: existing-review\ndescription: existing\n---\nexisting-body",
  1514. );
  1515. const { createSettingsSyncService } =
  1516. await import("../src/settings-sync/settingsSyncService.js");
  1517. const service = createSettingsSyncService({ settingService: createSettingService() });
  1518. const discovery = await service.detect({
  1519. workspacePath,
  1520. categories: ["skills"],
  1521. intent: "manualImport",
  1522. });
  1523. expect(discovery.agents[0]?.categories[0]?.sourceRoots?.[0]?.skills).toEqual([
  1524. {
  1525. name: "external-review",
  1526. path: join(home, ".claude", "skills", "review"),
  1527. importable: false,
  1528. skipReason: "targetExists",
  1529. },
  1530. ]);
  1531. });
  1532. });