settingsSyncService.test.ts 53 KB

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