config.test.ts 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. createConfig,
  4. resolveWorkspaceStorageDir,
  5. updateUiLocaleInFileConfig,
  6. } from "../src/config/index.js";
  7. import { parseEnvConfig } from "../src/config/env-config.adapter.js";
  8. import {
  9. addSuppressedBuiltinInFileConfig,
  10. loadFileConfig,
  11. removePluginFromFileConfig,
  12. removeSuppressedBuiltinInFileConfig,
  13. updatePluginEnabledInFileConfig,
  14. updatePluginOptionsInFileConfig,
  15. } from "../src/config/file-config.adapter.js";
  16. import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
  17. import { tmpdir } from "node:os";
  18. import { join } from "node:path";
  19. describe("config adapters", () => {
  20. it.each(["zcode.json", ".zcode/config.json", "explicit-project.json"])(
  21. "restricts project runtime parameters while retaining extension settings in %s",
  22. async (fileName) => {
  23. const root = await mkdtemp(join(tmpdir(), "zcode-project-runtime-boundary-"));
  24. const userPath = join(root, ".zcode", "cli", "config.json");
  25. const projectPath = join(root, fileName);
  26. const skillPath = join(root, "skills", "demo", "SKILL.md");
  27. const commandPath = join(root, "commands", "demo.md");
  28. const userConfig = {
  29. network: { httpProxy: "http://user.example:8080", caCertFile: join(root, "user-ca.pem") },
  30. storage: { dir: join(root, "user-storage"), sessionDbPath: join(root, "user.sqlite") },
  31. permission: { mode: "plan", autoApproveHighRisk: false },
  32. plugins: {
  33. dirs: [join(root, "installed-plugin")],
  34. enabledPlugins: { "demo@fixture": false },
  35. },
  36. };
  37. const projectConfig = {
  38. network: {
  39. httpProxy: "http://project.example:8080",
  40. caCertFile: "./repo-ca.pem",
  41. noProxy: "*",
  42. timeout: 1,
  43. },
  44. storage: { dir: "./fake-user-storage", sessionDbPath: "./fake-session.sqlite" },
  45. permission: { mode: "yolo", autoApproveHighRisk: true },
  46. modelStream: { idleTimeoutMs: 1 },
  47. features: { mcp: false, subagent: false, memory: false, skill: false },
  48. memory: { use: false },
  49. logging: { level: "error", format: "text" },
  50. ui: { locale: "zh-CN", theme: "light" },
  51. toolConcurrency: { maxConcurrency: 1 },
  52. modelAnomalyGuard: { toolCallWarningThreshold: 1 },
  53. skills: { enabled: false, roots: ["./extra-skills"], [skillPath]: { enable: false } },
  54. command: { [commandPath]: { enable: false } },
  55. plugins: {
  56. enabled: false,
  57. dirs: ["./fake-plugin"],
  58. suppressedBuiltins: ["builtin@fixture"],
  59. extraKnownMarketplaces: {
  60. fake: { source: { source: "directory", path: "./fake-market" } },
  61. },
  62. enabledPlugins: { "demo@fixture": true },
  63. options: { "demo@fixture": { format: "compact" } },
  64. },
  65. mcp: { servers: { demo: { type: "stdio", command: "node", cwd: "./tools" } } },
  66. hooks: {
  67. events: {
  68. SessionStart: [{ hooks: [{ type: "command", command: "echo project-hook" }] }],
  69. },
  70. },
  71. };
  72. try {
  73. vi.stubEnv("HOME", root);
  74. vi.stubEnv("USERPROFILE", root);
  75. await mkdir(join(root, ".git"));
  76. await mkdir(join(root, ".zcode", "cli"), { recursive: true });
  77. await writeFile(userPath, JSON.stringify(userConfig));
  78. await writeFile(projectPath, JSON.stringify(projectConfig));
  79. const baseline = createConfig({ env: {}, userConfigPath: userPath });
  80. const result = createConfig({
  81. env: {},
  82. userConfigPath: userPath,
  83. workingDirectory: root,
  84. ...(fileName === "explicit-project.json" ? { projectConfigPath: projectPath } : {}),
  85. });
  86. for (const field of [
  87. "network",
  88. "storage",
  89. "permission",
  90. "modelStream",
  91. "features",
  92. "memory",
  93. "logging",
  94. "ui",
  95. "toolConcurrency",
  96. "modelAnomalyGuard",
  97. "skills",
  98. ] as const) {
  99. expect(result.config[field], field).toEqual(baseline.config[field]);
  100. }
  101. expect(result.config.plugins).toEqual({
  102. ...baseline.config.plugins,
  103. enabledPlugins: { "demo@fixture": true },
  104. options: { "demo@fixture": { format: "compact" } },
  105. });
  106. expect(result.config.mcp.servers.demo).toMatchObject({
  107. command: "node",
  108. cwd: join(root, "tools"),
  109. });
  110. expect(result.sources.mcp.serverSources.demo).toBe("project");
  111. expect(result.config.hooks).toEqual(baseline.config.hooks);
  112. expect(result.sources.project.workspaceHookSnapshot).toBeDefined();
  113. expect(result.config.skillOverrides[skillPath]).toEqual({ enable: false });
  114. expect(result.config.commandOverrides[commandPath]).toEqual({ enable: false });
  115. expect(result.sources.project.hasUiLocale).toBe(false);
  116. expect(result.sources.project.hasUiTheme).toBe(false);
  117. expect(resolveWorkspaceStorageDir({ env: {}, workingDirectory: root })).toBe(
  118. userConfig.storage.dir,
  119. );
  120. } finally {
  121. vi.unstubAllEnvs();
  122. await rm(root, { recursive: true, force: true });
  123. }
  124. },
  125. );
  126. it("ignores invalid project runtime fields before validating extension settings", async () => {
  127. const root = await mkdtemp(join(tmpdir(), "zcode-project-runtime-invalid-"));
  128. const projectPath = join(root, "zcode.json");
  129. const skillPath = join(root, "SKILL.md");
  130. try {
  131. await mkdir(join(root, ".git"));
  132. await writeFile(
  133. projectPath,
  134. JSON.stringify({
  135. network: { caCertFile: 123 },
  136. storage: null,
  137. permission: { mode: "invalid" },
  138. skills: { enabled: "invalid", roots: 123, [skillPath]: { enable: false } },
  139. plugins: { dirs: 123, enabled: "invalid", enabledPlugins: { "demo@fixture": true } },
  140. mcp: { servers: { demo: { type: "http", url: "https://example.com/mcp" } } },
  141. }),
  142. );
  143. const result = createConfig({ env: {}, skipUserConfig: true, workingDirectory: root });
  144. expect(result.sources.project.loaded).toBe(true);
  145. expect(result.sources.project.diagnostics).toEqual([]);
  146. expect(result.config.network.caCertFile).toBeUndefined();
  147. expect(result.config.mcp.servers.demo).toMatchObject({ url: "https://example.com/mcp" });
  148. expect(result.config.plugins.enabledPlugins["demo@fixture"]).toBe(true);
  149. expect(result.config.skillOverrides[skillPath]).toEqual({ enable: false });
  150. } finally {
  151. await rm(root, { recursive: true, force: true });
  152. }
  153. });
  154. it("migrates the legacy official CUA plugin id to the canonical id", async () => {
  155. const dir = await mkdtemp(join(tmpdir(), "zcode-config-cua-identity-"));
  156. const configPath = join(dir, "config.json");
  157. try {
  158. await writeFile(
  159. configPath,
  160. JSON.stringify({
  161. plugins: {
  162. enabledPlugins: {
  163. "zcode-cua@zcode-plugins-official": true,
  164. "computer-use@zcode-plugins-official": false,
  165. },
  166. suppressedBuiltins: [
  167. "zcode-cua@zcode-plugins-official",
  168. "computer-use@zcode-plugins-official",
  169. ],
  170. options: {
  171. "zcode-cua@zcode-plugins-official": { apiKey: "legacy-key" },
  172. "computer-use@zcode-plugins-official": { apiKey: "canonical-key" },
  173. },
  174. },
  175. }),
  176. );
  177. const loaded = loadFileConfig(configPath);
  178. expect(loaded.config.plugins?.enabledPlugins).toEqual({
  179. "computer-use@zcode-plugins-official": false,
  180. });
  181. expect(loaded.config.plugins?.suppressedBuiltins).toEqual([
  182. "computer-use@zcode-plugins-official",
  183. ]);
  184. expect(loaded.config.plugins?.options).toEqual({
  185. "computer-use@zcode-plugins-official": { apiKey: "canonical-key" },
  186. });
  187. } finally {
  188. await rm(dir, { force: true, recursive: true });
  189. }
  190. });
  191. it("persists the canonical CUA plugin id while loading legacy config", async () => {
  192. const dir = await mkdtemp(join(tmpdir(), "zcode-config-cua-writeback-"));
  193. const path = join(dir, "config.json");
  194. await writeFile(
  195. path,
  196. JSON.stringify({
  197. plugins: {
  198. enabledPlugins: { "zcode-cua@zcode-plugins-official": true },
  199. suppressedBuiltins: ["zcode-cua@zcode-plugins-official"],
  200. options: {
  201. "zcode-cua@zcode-plugins-official": { apiKey: "legacy-key" },
  202. },
  203. },
  204. }),
  205. );
  206. loadFileConfig(path);
  207. expect(JSON.parse(await readFile(path, "utf8"))).toEqual({
  208. plugins: {
  209. enabledPlugins: { "computer-use@zcode-plugins-official": true },
  210. suppressedBuiltins: ["computer-use@zcode-plugins-official"],
  211. options: {
  212. "computer-use@zcode-plugins-official": { apiKey: "legacy-key" },
  213. },
  214. },
  215. });
  216. await rm(dir, { recursive: true, force: true });
  217. });
  218. it("writes canonical CUA ids when removing legacy config entries", async () => {
  219. const dir = await mkdtemp(join(tmpdir(), "zcode-config-cua-write-migration-"));
  220. const configPath = join(dir, "config.json");
  221. try {
  222. await writeFile(
  223. configPath,
  224. JSON.stringify({
  225. plugins: {
  226. enabledPlugins: { "zcode-cua@zcode-plugins-official": true },
  227. suppressedBuiltins: ["zcode-cua@zcode-plugins-official"],
  228. },
  229. }),
  230. );
  231. expect(
  232. (
  233. await removeSuppressedBuiltinInFileConfig(
  234. configPath,
  235. "computer-use@zcode-plugins-official",
  236. )
  237. ).suppressed,
  238. ).toBe(false);
  239. expect(
  240. (await removePluginFromFileConfig(configPath, "computer-use@zcode-plugins-official"))
  241. .removedEnabled,
  242. ).toBe(true);
  243. expect(JSON.parse(await readFile(configPath, "utf8"))).toEqual({
  244. plugins: { enabledPlugins: {}, suppressedBuiltins: [], options: {} },
  245. });
  246. } finally {
  247. await rm(dir, { force: true, recursive: true });
  248. }
  249. });
  250. it("clears only explicitly selected Plugin option keys", async () => {
  251. const dir = await mkdtemp(join(tmpdir(), "zcode-config-plugin-option-clear-"));
  252. const configPath = join(dir, "config.json");
  253. const pluginId = "secret-plugin@fixture";
  254. try {
  255. await writeFile(
  256. configPath,
  257. JSON.stringify({
  258. plugins: {
  259. enabledPlugins: { [pluginId]: true },
  260. options: {
  261. [pluginId]: {
  262. token: "secret",
  263. region: "us-east-1",
  264. },
  265. },
  266. },
  267. }),
  268. );
  269. await updatePluginOptionsInFileConfig(configPath, pluginId, { region: "eu-west-1" }, [
  270. "token",
  271. ]);
  272. expect(JSON.parse(await readFile(configPath, "utf8"))).toEqual({
  273. plugins: {
  274. enabledPlugins: { [pluginId]: true },
  275. options: {
  276. [pluginId]: {
  277. region: "eu-west-1",
  278. },
  279. },
  280. },
  281. });
  282. } finally {
  283. await rm(dir, { force: true, recursive: true });
  284. }
  285. });
  286. it("deep merges Workspace Plugin enablement and options over User config", async () => {
  287. const dir = await mkdtemp(join(tmpdir(), "zcode-config-workspace-plugins-"));
  288. const userPath = join(dir, "user.json");
  289. const workspacePath = join(dir, ".zcode", "config.json");
  290. try {
  291. await mkdir(join(dir, ".zcode"), { recursive: true });
  292. await writeFile(
  293. userPath,
  294. JSON.stringify({
  295. plugins: {
  296. dirs: [join(dir, "user-plugin")],
  297. enabledPlugins: {
  298. "workspace-plugin@fixture": true,
  299. "user-only@fixture": true,
  300. },
  301. options: {
  302. "workspace-plugin@fixture": {
  303. inherited: "user",
  304. overridden: "user",
  305. },
  306. },
  307. },
  308. }),
  309. );
  310. await writeFile(
  311. workspacePath,
  312. JSON.stringify({
  313. plugins: {
  314. dirs: ["./workspace-plugin", join(dir, "absolute-workspace-plugin")],
  315. enabledPlugins: {
  316. "workspace-plugin@fixture": false,
  317. },
  318. options: {
  319. "workspace-plugin@fixture": {
  320. overridden: "workspace",
  321. workspaceOnly: true,
  322. },
  323. },
  324. },
  325. }),
  326. );
  327. const result = createConfig({
  328. env: {},
  329. userConfigPath: userPath,
  330. projectConfigPath: workspacePath,
  331. workingDirectory: dir,
  332. });
  333. expect(result.config.plugins.enabledPlugins).toEqual({
  334. "workspace-plugin@fixture": false,
  335. "user-only@fixture": true,
  336. });
  337. expect(result.config.plugins.options).toEqual({
  338. "workspace-plugin@fixture": {
  339. inherited: "user",
  340. overridden: "workspace",
  341. workspaceOnly: true,
  342. },
  343. });
  344. expect(result.sources.project.paths).toEqual([workspacePath]);
  345. expect(result.sources.plugins.enabled).toEqual({
  346. "user-only@fixture": "user",
  347. "workspace-plugin@fixture": "workspace",
  348. });
  349. expect(result.sources.plugins.dirs).toEqual({
  350. user: [join(dir, "user-plugin")],
  351. workspace: [],
  352. });
  353. expect(result.config.plugins.dirs).toEqual([join(dir, "user-plugin")]);
  354. expect(result.sources.plugins.options).toEqual({
  355. "workspace-plugin@fixture": {
  356. inherited: "user",
  357. overridden: "workspace",
  358. workspaceOnly: "workspace",
  359. },
  360. });
  361. } finally {
  362. await rm(dir, { force: true, recursive: true });
  363. }
  364. });
  365. it.each(["zcode.json", ".zcode/config.json", "explicit-project.json"])(
  366. "ignores plugin directories from project config %s",
  367. async (fileName) => {
  368. const dir = await mkdtemp(join(tmpdir(), "zcode-project-plugin-dirs-"));
  369. const projectConfigPath = join(dir, fileName);
  370. try {
  371. await mkdir(join(dir, ".git"));
  372. await mkdir(join(dir, ".zcode"));
  373. await writeFile(
  374. projectConfigPath,
  375. JSON.stringify({ plugins: { dirs: ["./plugin", join(dir, "absolute-plugin")] } }),
  376. );
  377. const result = createConfig({
  378. env: {},
  379. skipUserConfig: true,
  380. ...(fileName === "explicit-project.json"
  381. ? { projectConfigPath }
  382. : { workingDirectory: dir }),
  383. });
  384. expect(result.sources.project.loaded).toBe(true);
  385. expect(result.config.plugins.dirs).toEqual([]);
  386. expect(result.sources.plugins.dirs.workspace).toEqual([]);
  387. expect(JSON.parse(await readFile(projectConfigPath, "utf8")).plugins.dirs).toHaveLength(2);
  388. } finally {
  389. await rm(dir, { force: true, recursive: true });
  390. }
  391. },
  392. );
  393. it("keeps Marketplace declarations in the User config layer", async () => {
  394. const dir = await mkdtemp(join(tmpdir(), "zcode-config-workspace-marketplaces-"));
  395. const userPath = join(dir, "user.json");
  396. const workspacePath = join(dir, "workspace.json");
  397. try {
  398. await writeFile(
  399. userPath,
  400. JSON.stringify({
  401. plugins: {
  402. extraKnownMarketplaces: {
  403. "shared-market": {
  404. source: { source: "github", repo: "example/user-market" },
  405. },
  406. "user-market": {
  407. source: { source: "url", url: "https://example.test/user-market.json" },
  408. },
  409. },
  410. },
  411. }),
  412. );
  413. await writeFile(
  414. workspacePath,
  415. JSON.stringify({
  416. plugins: {
  417. extraKnownMarketplaces: {
  418. "shared-market": {
  419. source: { source: "directory", path: "./workspace-market" },
  420. },
  421. "workspace-market": {
  422. source: { source: "file", path: "./workspace-market.json" },
  423. },
  424. },
  425. },
  426. }),
  427. );
  428. const result = createConfig({
  429. env: {},
  430. userConfigPath: userPath,
  431. projectConfigPath: workspacePath,
  432. });
  433. expect(result.config.plugins.extraKnownMarketplaces).toEqual({
  434. "shared-market": {
  435. source: { source: "github", repo: "example/user-market" },
  436. },
  437. "user-market": {
  438. source: { source: "url", url: "https://example.test/user-market.json" },
  439. },
  440. });
  441. expect(result.sources.plugins.marketplaces).toEqual({
  442. "shared-market": "user",
  443. "user-market": "user",
  444. });
  445. } finally {
  446. await rm(dir, { force: true, recursive: true });
  447. }
  448. });
  449. it("parses the supported ZCODE environment surface", () => {
  450. const config = parseEnvConfig({
  451. ZCODE_STORAGE_DIR: "/tmp/zcode",
  452. ZCODE_HTTP_PROXY: "http://127.0.0.1:7890",
  453. ZCODE_NO_PROXY: "localhost,127.0.0.1",
  454. ZCODE_AGENT_CA_CERT: "/tmp/zcode-ca.pem",
  455. ZCODE_HTTP_TIMEOUT: "1234",
  456. ZCODE_LOCALE: "zh-CN",
  457. ZCODE_LOG_FORMAT: "json",
  458. });
  459. expect(config.model).toBeUndefined();
  460. expect(config.storage?.dir).toBe("/tmp/zcode");
  461. expect(config.network?.httpProxy).toBe("http://127.0.0.1:7890");
  462. expect(config.network?.noProxy).toBe("localhost,127.0.0.1");
  463. expect(config.network?.caCertFile).toBe("/tmp/zcode-ca.pem");
  464. expect(config.network?.timeout).toBe(1234);
  465. expect(config.ui).toBeUndefined();
  466. expect(config.logging?.format).toBe("json");
  467. expect(config.logging?.level).toBeUndefined();
  468. });
  469. it("loads the supported memory config fields", async () => {
  470. const dir = await mkdtemp(join(tmpdir(), "zcode-config-memory-"));
  471. const path = join(dir, "config.json");
  472. try {
  473. await writeFile(
  474. path,
  475. JSON.stringify({
  476. memory: {
  477. use: false,
  478. },
  479. }),
  480. );
  481. const loaded = loadFileConfig(path);
  482. const merged = createConfig({ env: {}, userConfigPath: path });
  483. expect(loaded.loaded).toBe(true);
  484. expect(loaded.config.memory).toEqual({
  485. use: false,
  486. });
  487. expect(merged.config.memory).toEqual({
  488. use: false,
  489. });
  490. } finally {
  491. await rm(dir, { force: true, recursive: true });
  492. }
  493. });
  494. it("parses session database path environment overrides", () => {
  495. expect(
  496. parseEnvConfig({
  497. ZCODE_SESSION_DB_PATH: "/tmp/zcode/db/db.sqlite",
  498. }).storage?.sessionDbPath,
  499. ).toBe("/tmp/zcode/db/db.sqlite");
  500. expect(
  501. parseEnvConfig({
  502. ZCODE_SESSION_DB: "/tmp/zcode/legacy.sqlite",
  503. }).storage?.sessionDbPath,
  504. ).toBe("/tmp/zcode/legacy.sqlite");
  505. });
  506. it("loads no-proxy rules from file config", async () => {
  507. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  508. const path = join(dir, "config.json");
  509. try {
  510. await writeFile(
  511. path,
  512. JSON.stringify({
  513. network: {
  514. httpProxy: "http://127.0.0.1:7890",
  515. noProxy: "localhost,127.0.0.1",
  516. caCertFile: "/tmp/zcode-ca.pem",
  517. },
  518. }),
  519. );
  520. const merged = createConfig({ env: {}, userConfigPath: path });
  521. expect(merged.config.network.httpProxy).toBe("http://127.0.0.1:7890");
  522. expect(merged.config.network.noProxy).toBe("localhost,127.0.0.1");
  523. expect(merged.config.network.caCertFile).toBe("/tmp/zcode-ca.pem");
  524. } finally {
  525. await rm(dir, { force: true, recursive: true });
  526. }
  527. });
  528. it("ignores undeclared ZCODE environment switches", () => {
  529. const config = parseEnvConfig({
  530. ZCODE_LOG_LEVEL: "debug",
  531. ZCODE_UNDECLARED_FLAG: "1",
  532. });
  533. expect(config).toEqual({});
  534. });
  535. it("loads yolo mode from file config", async () => {
  536. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  537. const path = join(dir, "config.json");
  538. try {
  539. await writeFile(path, JSON.stringify({ permission: { mode: "yolo" } }));
  540. const result = loadFileConfig(path);
  541. expect(result.loaded).toBe(true);
  542. expect(result.config.permission?.mode).toBe("yolo");
  543. } finally {
  544. await rm(dir, { force: true, recursive: true });
  545. }
  546. });
  547. it("loads edit mode from file config", async () => {
  548. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  549. const path = join(dir, "config.json");
  550. try {
  551. await writeFile(path, JSON.stringify({ permission: { mode: "edit" } }));
  552. const result = loadFileConfig(path);
  553. expect(result.loaded).toBe(true);
  554. expect(result.config.permission?.mode).toBe("edit");
  555. } finally {
  556. await rm(dir, { force: true, recursive: true });
  557. }
  558. });
  559. it("loads skill/command enable overrides from file config", async () => {
  560. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  561. const path = join(dir, "config.json");
  562. try {
  563. await writeFile(
  564. path,
  565. JSON.stringify({
  566. skill: { "/abs/skills/demo/SKILL.md": { enable: false } },
  567. command: { "/abs/commands/demo.md": { enable: false } },
  568. }),
  569. );
  570. const result = loadFileConfig(path);
  571. const merged = createConfig({ env: {}, userConfigPath: path });
  572. expect(result.loaded).toBe(true);
  573. // JSON 的 skill/command 字段映射到 runtime 的 skillOverrides/commandOverrides
  574. expect(result.config.skillOverrides?.["/abs/skills/demo/SKILL.md"]?.enable).toBe(false);
  575. expect(result.config.commandOverrides?.["/abs/commands/demo.md"]?.enable).toBe(false);
  576. expect(merged.config.skillOverrides["/abs/skills/demo/SKILL.md"]?.enable).toBe(false);
  577. expect(merged.config.commandOverrides["/abs/commands/demo.md"]?.enable).toBe(false);
  578. } finally {
  579. await rm(dir, { force: true, recursive: true });
  580. }
  581. });
  582. it("parses plugins.suppressedBuiltins as a string array", async () => {
  583. const dir = await mkdtemp(join(tmpdir(), "zcode-config-suppressed-"));
  584. const path = join(dir, "config.json");
  585. await writeFile(
  586. path,
  587. JSON.stringify({ plugins: { suppressedBuiltins: ["skill-creator@zcode-plugins-official"] } }),
  588. );
  589. const loaded = loadFileConfig(path);
  590. expect(loaded.config.plugins.suppressedBuiltins).toEqual([
  591. "skill-creator@zcode-plugins-official",
  592. ]);
  593. });
  594. it("maps skills path enable entries to skill overrides", async () => {
  595. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  596. const path = join(dir, "config.json");
  597. try {
  598. await writeFile(
  599. path,
  600. JSON.stringify({
  601. skills: {
  602. enabled: true,
  603. roots: ["/abs/skills"],
  604. "/abs/skills/disabled/SKILL.md": { enable: false },
  605. "/abs/skills/enabled/SKILL.md": { enable: true },
  606. },
  607. }),
  608. );
  609. const result = loadFileConfig(path);
  610. const merged = createConfig({ env: {}, userConfigPath: path });
  611. expect(result.loaded).toBe(true);
  612. expect(result.config.skills).toEqual({
  613. enabled: true,
  614. roots: ["/abs/skills"],
  615. });
  616. expect(result.config.skillOverrides?.["/abs/skills/disabled/SKILL.md"]?.enable).toBe(false);
  617. expect(result.config.skillOverrides?.["/abs/skills/enabled/SKILL.md"]?.enable).toBe(true);
  618. expect(merged.config.skillOverrides["/abs/skills/disabled/SKILL.md"]?.enable).toBe(false);
  619. expect(merged.config.skillOverrides["/abs/skills/enabled/SKILL.md"]?.enable).toBe(true);
  620. } finally {
  621. await rm(dir, { force: true, recursive: true });
  622. }
  623. });
  624. it("loads top-level skills config from file config", async () => {
  625. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  626. const path = join(dir, "config.json");
  627. try {
  628. await writeFile(
  629. path,
  630. JSON.stringify({
  631. skills: {
  632. enabled: false,
  633. includeInstructions: false,
  634. metadataBudget: 1024,
  635. roots: ["/abs/skills"],
  636. },
  637. }),
  638. );
  639. const result = loadFileConfig(path);
  640. const merged = createConfig({ env: {}, userConfigPath: path });
  641. expect(result.loaded).toBe(true);
  642. expect(result.config.skills).toEqual({
  643. enabled: false,
  644. includeInstructions: false,
  645. metadataBudget: 1024,
  646. roots: ["/abs/skills"],
  647. });
  648. expect(merged.config.skills.enabled).toBe(false);
  649. expect(merged.config.skills.includeInstructions).toBe(false);
  650. expect(merged.config.skills.metadataBudget).toBe(1024);
  651. expect(merged.config.skills.roots).toEqual(["/abs/skills"]);
  652. } finally {
  653. await rm(dir, { force: true, recursive: true });
  654. }
  655. });
  656. it("loads UI locale from file config", async () => {
  657. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  658. const path = join(dir, "config.json");
  659. try {
  660. await writeFile(path, JSON.stringify({ ui: { locale: "zh-CN" } }));
  661. const result = loadFileConfig(path);
  662. const merged = createConfig({ env: {}, userConfigPath: path });
  663. expect(result.loaded).toBe(true);
  664. expect(result.config.ui?.locale).toBe("zh-CN");
  665. expect(merged.config.ui.locale).toBe("zh-CN");
  666. } finally {
  667. await rm(dir, { force: true, recursive: true });
  668. }
  669. });
  670. it("loads auto UI locale from file config", async () => {
  671. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  672. const path = join(dir, "config.json");
  673. try {
  674. await writeFile(path, JSON.stringify({ ui: { locale: "auto" } }));
  675. const result = loadFileConfig(path);
  676. const merged = createConfig({ env: {}, userConfigPath: path });
  677. expect(result.loaded).toBe(true);
  678. expect(result.config.ui?.locale).toBe("auto");
  679. expect(merged.config.ui.locale).toBe("auto");
  680. } finally {
  681. await rm(dir, { force: true, recursive: true });
  682. }
  683. });
  684. it("loads UI theme from file config", async () => {
  685. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  686. const path = join(dir, "config.json");
  687. try {
  688. await writeFile(path, JSON.stringify({ ui: { theme: "light" } }));
  689. const result = loadFileConfig(path);
  690. const merged = createConfig({ env: {}, userConfigPath: path });
  691. expect(result.loaded).toBe(true);
  692. expect(result.config.ui?.theme).toBe("light");
  693. expect(merged.config.ui.theme).toBe("light");
  694. } finally {
  695. await rm(dir, { force: true, recursive: true });
  696. }
  697. });
  698. it("rejects unsupported UI theme values from file config", async () => {
  699. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  700. const path = join(dir, "config.json");
  701. try {
  702. await writeFile(path, JSON.stringify({ ui: { theme: "sepia" } }));
  703. const result = loadFileConfig(path);
  704. expect(result.loaded).toBe(false);
  705. expect(result.config.ui).toBeUndefined();
  706. } finally {
  707. await rm(dir, { force: true, recursive: true });
  708. }
  709. });
  710. it("patches UI locale while preserving unrelated config fields", async () => {
  711. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  712. const path = join(dir, "config.json");
  713. try {
  714. await writeFile(
  715. path,
  716. JSON.stringify({
  717. model: {
  718. main: "openai/gpt-5.1",
  719. },
  720. permission: {
  721. mode: "plan",
  722. },
  723. ui: {
  724. density: "compact",
  725. locale: "en-US",
  726. },
  727. }),
  728. );
  729. const result = await updateUiLocaleInFileConfig(path, "zh-CN");
  730. const written = JSON.parse(await readFile(path, "utf-8"));
  731. expect(result).toEqual({
  732. locale: "zh-CN",
  733. path,
  734. });
  735. expect(written).toMatchObject({
  736. model: {
  737. main: "openai/gpt-5.1",
  738. },
  739. permission: {
  740. mode: "plan",
  741. },
  742. ui: {
  743. density: "compact",
  744. locale: "zh-CN",
  745. },
  746. });
  747. } finally {
  748. await rm(dir, { force: true, recursive: true });
  749. }
  750. });
  751. it("creates config files when patching UI locale", async () => {
  752. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  753. const path = join(dir, "nested", "config.json");
  754. try {
  755. await updateUiLocaleInFileConfig(path, "auto");
  756. const written = JSON.parse(await readFile(path, "utf-8"));
  757. expect(written).toEqual({
  758. ui: {
  759. locale: "auto",
  760. },
  761. });
  762. } finally {
  763. await rm(dir, { force: true, recursive: true });
  764. }
  765. });
  766. it("rejects unsupported UI locale values from file config", async () => {
  767. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  768. const path = join(dir, "config.json");
  769. try {
  770. await writeFile(path, JSON.stringify({ ui: { locale: "fr-FR" } }));
  771. const result = loadFileConfig(path);
  772. expect(result.loaded).toBe(false);
  773. expect(result.config.ui).toBeUndefined();
  774. } finally {
  775. await rm(dir, { force: true, recursive: true });
  776. }
  777. });
  778. it("loads MCP server config from file config", async () => {
  779. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  780. const path = join(dir, "config.json");
  781. try {
  782. await writeFile(
  783. path,
  784. JSON.stringify({
  785. features: {
  786. mcp: true,
  787. },
  788. mcp: {
  789. servers: {
  790. local: {
  791. type: "stdio",
  792. command: "node",
  793. args: ["server.js"],
  794. cwd: ".",
  795. env: {
  796. EXAMPLE: "1",
  797. },
  798. },
  799. remote: {
  800. type: "http",
  801. url: "https://example.com/mcp",
  802. headers: {
  803. Authorization: "Bearer token",
  804. },
  805. oauth: {
  806. type: "client_credentials",
  807. clientId: "zcode-client",
  808. clientSecret: "secret",
  809. scope: "mcp:tools",
  810. },
  811. timeoutMs: 30000,
  812. },
  813. events: {
  814. type: "sse",
  815. url: "https://example.com/sse",
  816. oauth: {
  817. type: "authorization_code",
  818. clientName: "ZCode",
  819. redirectPath: "/oauth/callback/mcp/figma",
  820. scope: "mcp:connect",
  821. },
  822. enabled: false,
  823. },
  824. },
  825. },
  826. }),
  827. );
  828. const result = loadFileConfig(path);
  829. expect(result.loaded).toBe(true);
  830. expect(result.config.features?.mcp).toBe(true);
  831. expect(result.config.mcp?.servers?.local).toMatchObject({
  832. type: "stdio",
  833. command: "node",
  834. args: ["server.js"],
  835. });
  836. expect(result.config.mcp?.servers?.remote).toMatchObject({
  837. type: "http",
  838. url: "https://example.com/mcp",
  839. oauth: {
  840. type: "client_credentials",
  841. clientId: "zcode-client",
  842. clientSecret: "secret",
  843. scope: "mcp:tools",
  844. },
  845. timeoutMs: 30000,
  846. });
  847. expect(result.config.mcp?.servers?.events).toMatchObject({
  848. type: "sse",
  849. oauth: {
  850. type: "authorization_code",
  851. clientName: "ZCode",
  852. redirectPath: "/oauth/callback/mcp/figma",
  853. scope: "mcp:connect",
  854. },
  855. enabled: false,
  856. });
  857. } finally {
  858. await rm(dir, { force: true, recursive: true });
  859. }
  860. });
  861. it("keeps an MCP server disabled when legacy enable:false coexists with enabled:true", async () => {
  862. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  863. const path = join(dir, "config.json");
  864. try {
  865. await writeFile(
  866. path,
  867. JSON.stringify({
  868. mcp: {
  869. servers: {
  870. // 桌面端「停用」写入 enable:false,而外部导入/手工编辑的同一条配置里
  871. // 已经带着 enabled:true。禁用意图必须胜出。
  872. playwright: {
  873. type: "stdio",
  874. command: "npx",
  875. args: ["-y", "@playwright/mcp@latest"],
  876. enable: false,
  877. enabled: true,
  878. },
  879. },
  880. },
  881. }),
  882. );
  883. const result = loadFileConfig(path);
  884. expect(result.config.mcp?.servers?.playwright?.enabled).toBe(false);
  885. } finally {
  886. await rm(dir, { force: true, recursive: true });
  887. }
  888. });
  889. it("keeps an MCP server disabled when enabled:false coexists with legacy enable:true", async () => {
  890. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  891. const path = join(dir, "config.json");
  892. try {
  893. await writeFile(
  894. path,
  895. JSON.stringify({
  896. mcp: {
  897. servers: {
  898. playwright: {
  899. type: "stdio",
  900. command: "npx",
  901. args: ["-y", "@playwright/mcp@latest"],
  902. enable: true,
  903. enabled: false,
  904. },
  905. },
  906. },
  907. }),
  908. );
  909. const result = loadFileConfig(path);
  910. expect(result.config.mcp?.servers?.playwright?.enabled).toBe(false);
  911. } finally {
  912. await rm(dir, { force: true, recursive: true });
  913. }
  914. });
  915. it("keeps an MCP server enabled when both enable and enabled are true", async () => {
  916. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  917. const path = join(dir, "config.json");
  918. try {
  919. await writeFile(
  920. path,
  921. JSON.stringify({
  922. mcp: {
  923. servers: {
  924. playwright: {
  925. type: "stdio",
  926. command: "npx",
  927. args: ["-y", "@playwright/mcp@latest"],
  928. enable: true,
  929. enabled: true,
  930. },
  931. },
  932. },
  933. }),
  934. );
  935. const result = loadFileConfig(path);
  936. expect(result.config.mcp?.servers?.playwright?.enabled).toBe(true);
  937. } finally {
  938. await rm(dir, { force: true, recursive: true });
  939. }
  940. });
  941. it("preserves fresh HTTP MCP config without writing implicit OAuth", async () => {
  942. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  943. const path = join(dir, "config.json");
  944. try {
  945. await writeFile(
  946. path,
  947. JSON.stringify({
  948. mcp: {
  949. servers: {
  950. notion: {
  951. type: "http",
  952. url: "https://mcp.notion.com/mcp",
  953. timeoutMs: 30000,
  954. },
  955. },
  956. },
  957. }),
  958. );
  959. const result = loadFileConfig(path);
  960. expect(result.loaded).toBe(true);
  961. expect(result.config.mcp?.servers?.notion).toMatchObject({
  962. type: "http",
  963. url: "https://mcp.notion.com/mcp",
  964. timeoutMs: 30000,
  965. });
  966. expect(result.config.mcp?.servers?.notion?.oauth).toBeUndefined();
  967. } finally {
  968. await rm(dir, { force: true, recursive: true });
  969. }
  970. });
  971. it("skips invalid MCP OAuth config without dropping unrelated config", async () => {
  972. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  973. const path = join(dir, "config.json");
  974. try {
  975. await writeFile(
  976. path,
  977. JSON.stringify({
  978. mcp: {
  979. servers: {
  980. broken: {
  981. type: "http",
  982. url: "https://example.com/mcp",
  983. oauth: {
  984. type: "client_credentials",
  985. clientId: "zcode-client",
  986. },
  987. },
  988. },
  989. },
  990. plugins: {
  991. enabled: true,
  992. },
  993. }),
  994. );
  995. const result = loadFileConfig(path);
  996. expect(result.loaded).toBe(true);
  997. expect(result.config.mcp?.servers).not.toHaveProperty("broken");
  998. expect(result.config.plugins?.enabled).toBe(true);
  999. expect(result.diagnostics).toEqual([
  1000. expect.objectContaining({
  1001. code: "config_mcp_server_invalid",
  1002. path: "mcp.servers.broken",
  1003. severity: "warning",
  1004. }),
  1005. ]);
  1006. } finally {
  1007. await rm(dir, { force: true, recursive: true });
  1008. }
  1009. });
  1010. it("normalizes app-managed MCP config aliases from file config", async () => {
  1011. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1012. const path = join(dir, "config.json");
  1013. try {
  1014. await writeFile(
  1015. path,
  1016. JSON.stringify({
  1017. mcp: {
  1018. servers: {
  1019. "figma-dev-mode-mcp-server": {
  1020. type: "stdio",
  1021. command: "npx",
  1022. args: ["-y", "figma-developer-mcp"],
  1023. enable: false,
  1024. },
  1025. "chrome-devtools": {
  1026. command: "npx",
  1027. args: ["-y", "chrome-devtools-mcp@latest"],
  1028. },
  1029. node_repl: {
  1030. command: "/opt/tools/node_repl",
  1031. args: [],
  1032. timeout: 30,
  1033. startup_timeout_sec: 120,
  1034. },
  1035. },
  1036. },
  1037. }),
  1038. );
  1039. const result = loadFileConfig(path);
  1040. expect(result.loaded).toBe(true);
  1041. expect(result.config.mcp?.servers?.["figma-dev-mode-mcp-server"]).toMatchObject({
  1042. type: "stdio",
  1043. command: "npx",
  1044. enabled: false,
  1045. });
  1046. expect(result.config.mcp?.servers?.["figma-dev-mode-mcp-server"]).not.toHaveProperty(
  1047. "enable",
  1048. );
  1049. expect(result.config.mcp?.servers?.["chrome-devtools"]).toMatchObject({
  1050. type: "stdio",
  1051. command: "npx",
  1052. args: ["-y", "chrome-devtools-mcp@latest"],
  1053. });
  1054. expect(result.config.mcp?.servers?.node_repl).toMatchObject({
  1055. type: "stdio",
  1056. command: "/opt/tools/node_repl",
  1057. });
  1058. expect(result.config.mcp?.servers?.node_repl).not.toHaveProperty("timeout");
  1059. expect(result.config.mcp?.servers?.node_repl).not.toHaveProperty("startup_timeout_sec");
  1060. } finally {
  1061. await rm(dir, { force: true, recursive: true });
  1062. }
  1063. });
  1064. it("normalizes legacy MCP http_headers to runtime headers", async () => {
  1065. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1066. const path = join(dir, "config.json");
  1067. try {
  1068. await writeFile(
  1069. path,
  1070. JSON.stringify({
  1071. mcp: {
  1072. servers: {
  1073. web_search_prime: {
  1074. type: "http",
  1075. url: "https://bigmodel.example.test/mcp",
  1076. http_headers: {
  1077. Authorization: "Bearer token",
  1078. },
  1079. },
  1080. },
  1081. },
  1082. }),
  1083. );
  1084. const result = loadFileConfig(path);
  1085. expect(result.loaded).toBe(true);
  1086. expect(result.config.mcp?.servers?.web_search_prime).toMatchObject({
  1087. type: "http",
  1088. headers: {
  1089. Authorization: "Bearer token",
  1090. },
  1091. });
  1092. expect(result.config.mcp?.servers?.web_search_prime).not.toHaveProperty("http_headers");
  1093. } finally {
  1094. await rm(dir, { force: true, recursive: true });
  1095. }
  1096. });
  1097. it("normalizes legacy MCP remote/environment fields without dropping plugins config", async () => {
  1098. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1099. const path = join(dir, "config.json");
  1100. try {
  1101. await writeFile(
  1102. path,
  1103. JSON.stringify({
  1104. mcp: {
  1105. servers: {
  1106. "GLM-Image-MCP": {
  1107. enabled: true,
  1108. environment: {
  1109. Z_AI_API_KEY: "test-key",
  1110. Z_AI_MODE: "ZHIPU",
  1111. },
  1112. command: "npx",
  1113. args: ["-y", "@z_ai/mcp-server"],
  1114. type: "stdio",
  1115. },
  1116. "GLM-WebReader-MCP": {
  1117. enabled: true,
  1118. headers: {
  1119. Authorization: "Bearer token",
  1120. },
  1121. type: "remote",
  1122. url: "https://open.bigmodel.cn/api/mcp/web_reader/mcp",
  1123. },
  1124. },
  1125. },
  1126. plugins: {
  1127. enabledPlugins: {
  1128. "superpowers@zcode-plugins-official": true,
  1129. "skill-creator@zcode-plugins-official": false,
  1130. },
  1131. },
  1132. }),
  1133. );
  1134. const result = loadFileConfig(path);
  1135. const merged = createConfig({ env: {}, userConfigPath: path });
  1136. expect(result.loaded).toBe(true);
  1137. expect(result.diagnostics).toEqual([]);
  1138. expect(result.config.mcp?.servers?.["GLM-Image-MCP"]).toMatchObject({
  1139. type: "stdio",
  1140. env: {
  1141. Z_AI_API_KEY: "test-key",
  1142. Z_AI_MODE: "ZHIPU",
  1143. },
  1144. });
  1145. expect(result.config.mcp?.servers?.["GLM-Image-MCP"]).not.toHaveProperty("environment");
  1146. expect(result.config.mcp?.servers?.["GLM-WebReader-MCP"]).toMatchObject({
  1147. headers: {
  1148. Authorization: "Bearer token",
  1149. },
  1150. type: "http",
  1151. url: "https://open.bigmodel.cn/api/mcp/web_reader/mcp",
  1152. });
  1153. expect(merged.config.plugins.enabledPlugins).toMatchObject({
  1154. "skill-creator@zcode-plugins-official": false,
  1155. "superpowers@zcode-plugins-official": true,
  1156. });
  1157. } finally {
  1158. await rm(dir, { force: true, recursive: true });
  1159. }
  1160. });
  1161. it("keeps non-MCP config when one MCP server is invalid", async () => {
  1162. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1163. const logDir = join(dir, "log");
  1164. const path = join(dir, "config.json");
  1165. try {
  1166. await writeFile(
  1167. path,
  1168. JSON.stringify({
  1169. mcp: {
  1170. servers: {
  1171. valid: {
  1172. type: "remote",
  1173. url: "https://example.com/mcp",
  1174. },
  1175. broken: {
  1176. type: "stdio",
  1177. args: ["server.js"],
  1178. },
  1179. },
  1180. },
  1181. plugins: {
  1182. enabledPlugins: {
  1183. "android-emulator@zcode-plugins-official": true,
  1184. },
  1185. },
  1186. }),
  1187. );
  1188. const result = loadFileConfig(path);
  1189. const merged = createConfig({
  1190. env: { ZCODE_LOG_DIR: logDir },
  1191. userConfigPath: path,
  1192. });
  1193. expect(result.loaded).toBe(true);
  1194. expect(result.config.mcp?.servers?.valid).toMatchObject({
  1195. type: "http",
  1196. url: "https://example.com/mcp",
  1197. });
  1198. expect(result.config.mcp?.servers).not.toHaveProperty("broken");
  1199. expect(
  1200. result.config.plugins?.enabledPlugins?.["android-emulator@zcode-plugins-official"],
  1201. ).toBe(true);
  1202. expect(result.diagnostics).toEqual([
  1203. expect.objectContaining({
  1204. code: "config_mcp_server_invalid",
  1205. path: "mcp.servers.broken",
  1206. severity: "warning",
  1207. }),
  1208. ]);
  1209. expect(merged.sources.user.diagnostics).toEqual(result.diagnostics);
  1210. expect(merged.config.plugins.enabledPlugins["android-emulator@zcode-plugins-official"]).toBe(
  1211. true,
  1212. );
  1213. await expect(readLogEntries(logDir)).resolves.toEqual([
  1214. expect.objectContaining({
  1215. event: "config.mcp_server.skipped",
  1216. level: "warn",
  1217. message: "MCP server config skipped",
  1218. module: "adapters.config",
  1219. context: expect.objectContaining({
  1220. configPath: path,
  1221. configScope: "user",
  1222. diagnosticCode: "config_mcp_server_invalid",
  1223. diagnosticPath: "mcp.servers.broken",
  1224. }),
  1225. }),
  1226. ]);
  1227. } finally {
  1228. await rm(dir, { force: true, recursive: true });
  1229. }
  1230. });
  1231. it("reports invalid discovered project config diagnostics", async () => {
  1232. const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1233. const logDir = join(root, "log");
  1234. const projectConfigPath = join(root, "zcode.json");
  1235. try {
  1236. await mkdir(join(root, ".git"));
  1237. await writeFile(
  1238. projectConfigPath,
  1239. JSON.stringify({
  1240. hooks: {
  1241. enabled: "invalid",
  1242. },
  1243. }),
  1244. );
  1245. const result = createConfig({
  1246. env: { ZCODE_LOG_DIR: logDir },
  1247. skipUserConfig: true,
  1248. workingDirectory: root,
  1249. });
  1250. expect(result.sources.project.loaded).toBe(false);
  1251. expect(result.sources.project.diagnostics).toEqual([
  1252. expect.objectContaining({
  1253. code: "config_file_invalid",
  1254. message: expect.stringContaining("hooks.enabled: Invalid input: expected boolean"),
  1255. severity: "error",
  1256. }),
  1257. ]);
  1258. await expect(readLogEntries(logDir)).resolves.toEqual([
  1259. expect.objectContaining({
  1260. event: "config.file.invalid",
  1261. level: "warn",
  1262. message: "Config file failed to load",
  1263. module: "adapters.config",
  1264. context: expect.objectContaining({
  1265. configPath: projectConfigPath,
  1266. configScope: "project",
  1267. diagnosticCode: "config_file_invalid",
  1268. }),
  1269. }),
  1270. ]);
  1271. } finally {
  1272. await rm(root, { force: true, recursive: true });
  1273. }
  1274. });
  1275. it("discovers project MCP config from the worktree root", async () => {
  1276. const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1277. const cwd = join(root, "packages", "app");
  1278. try {
  1279. await mkdir(join(root, ".git"));
  1280. await mkdir(cwd, { recursive: true });
  1281. await writeFile(
  1282. join(root, "zcode.json"),
  1283. JSON.stringify({
  1284. mcp: {
  1285. servers: {
  1286. project: {
  1287. type: "stdio",
  1288. command: "node",
  1289. args: ["server.js"],
  1290. cwd: "tools",
  1291. },
  1292. },
  1293. },
  1294. }),
  1295. );
  1296. const result = createConfig({
  1297. env: {},
  1298. skipUserConfig: true,
  1299. workingDirectory: cwd,
  1300. });
  1301. expect(result.sources.project.loaded).toBe(true);
  1302. expect(result.sources.project.paths).toEqual([join(root, "zcode.json")]);
  1303. expect(result.sources.project.mcpServerNames).toEqual(["project"]);
  1304. expect(result.config.mcp.servers.project).toMatchObject({
  1305. type: "stdio",
  1306. command: "node",
  1307. cwd: join(root, "tools"),
  1308. });
  1309. } finally {
  1310. await rm(root, { force: true, recursive: true });
  1311. }
  1312. });
  1313. it("merges project configs from root to cwd with nearest config winning", async () => {
  1314. const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1315. const cwd = join(root, "packages", "app");
  1316. const cwdConfigDir = join(cwd, ".zcode");
  1317. try {
  1318. await mkdir(join(root, ".git"));
  1319. await mkdir(cwdConfigDir, { recursive: true });
  1320. await writeFile(
  1321. join(root, "zcode.json"),
  1322. JSON.stringify({
  1323. mcp: {
  1324. servers: {
  1325. project: {
  1326. type: "stdio",
  1327. command: "root-server",
  1328. },
  1329. },
  1330. },
  1331. }),
  1332. );
  1333. await writeFile(
  1334. join(cwdConfigDir, "config.json"),
  1335. JSON.stringify({
  1336. mcp: {
  1337. servers: {
  1338. project: {
  1339. type: "stdio",
  1340. command: "nearest-server",
  1341. },
  1342. },
  1343. },
  1344. }),
  1345. );
  1346. const result = createConfig({
  1347. env: {},
  1348. skipUserConfig: true,
  1349. workingDirectory: cwd,
  1350. });
  1351. expect(result.sources.project.paths).toEqual([
  1352. join(root, "zcode.json"),
  1353. join(cwdConfigDir, "config.json"),
  1354. ]);
  1355. expect(result.config.mcp.servers.project).toMatchObject({
  1356. type: "stdio",
  1357. command: "nearest-server",
  1358. cwd,
  1359. });
  1360. } finally {
  1361. await rm(root, { force: true, recursive: true });
  1362. }
  1363. });
  1364. it("lets user MCP servers override same-name project MCP servers", async () => {
  1365. const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1366. const userConfigPath = join(root, "home", "config.json");
  1367. try {
  1368. await mkdir(join(root, ".git"));
  1369. await mkdir(join(root, "home"), { recursive: true });
  1370. await writeFile(
  1371. join(root, "zcode.json"),
  1372. JSON.stringify({
  1373. mcp: {
  1374. servers: {
  1375. projectOnly: {
  1376. type: "stdio",
  1377. command: "project-only-server",
  1378. },
  1379. shared: {
  1380. type: "stdio",
  1381. command: "project-server",
  1382. },
  1383. },
  1384. },
  1385. }),
  1386. );
  1387. await writeFile(
  1388. userConfigPath,
  1389. JSON.stringify({
  1390. mcp: {
  1391. servers: {
  1392. shared: {
  1393. type: "stdio",
  1394. command: "user-server",
  1395. },
  1396. userOnly: {
  1397. type: "stdio",
  1398. command: "user-only-server",
  1399. },
  1400. },
  1401. },
  1402. }),
  1403. );
  1404. const result = createConfig({
  1405. env: {},
  1406. userConfigPath,
  1407. workingDirectory: root,
  1408. });
  1409. expect(result.config.mcp.servers.shared).toMatchObject({
  1410. type: "stdio",
  1411. command: "user-server",
  1412. });
  1413. expect(result.config.mcp.servers.projectOnly).toMatchObject({
  1414. command: "project-only-server",
  1415. });
  1416. expect(result.config.mcp.servers.userOnly).toMatchObject({
  1417. command: "user-only-server",
  1418. });
  1419. expect(result.sources.user.mcpServerNames).toEqual(["shared", "userOnly"]);
  1420. expect(result.sources.project.mcpServerNames.toSorted()).toEqual(["projectOnly", "shared"]);
  1421. expect(result.sources.mcp.serverSources).toMatchObject({
  1422. projectOnly: "project",
  1423. shared: "user",
  1424. userOnly: "user",
  1425. });
  1426. } finally {
  1427. await rm(root, { force: true, recursive: true });
  1428. }
  1429. });
  1430. it("does not scan project config above the worktree root", async () => {
  1431. const parent = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1432. const root = join(parent, "repo");
  1433. const cwd = join(root, "packages", "app");
  1434. try {
  1435. await mkdir(join(root, ".git"), { recursive: true });
  1436. await mkdir(cwd, { recursive: true });
  1437. await writeFile(
  1438. join(parent, "zcode.json"),
  1439. JSON.stringify({
  1440. mcp: {
  1441. servers: {
  1442. parent: {
  1443. type: "stdio",
  1444. command: "parent-server",
  1445. },
  1446. },
  1447. },
  1448. }),
  1449. );
  1450. const result = createConfig({
  1451. env: {},
  1452. skipUserConfig: true,
  1453. workingDirectory: cwd,
  1454. });
  1455. expect(result.sources.project.loaded).toBe(false);
  1456. expect(result.config.mcp.servers.parent).toBeUndefined();
  1457. } finally {
  1458. await rm(parent, { force: true, recursive: true });
  1459. }
  1460. });
  1461. it("keeps CLI feature overrides above project MCP config", async () => {
  1462. const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1463. try {
  1464. await mkdir(join(root, ".git"));
  1465. await writeFile(
  1466. join(root, "zcode.json"),
  1467. JSON.stringify({
  1468. features: {
  1469. mcp: true,
  1470. },
  1471. }),
  1472. );
  1473. const result = createConfig({
  1474. cliOverrides: {
  1475. features: {
  1476. mcp: false,
  1477. },
  1478. },
  1479. env: {},
  1480. skipUserConfig: true,
  1481. workingDirectory: root,
  1482. });
  1483. expect(result.config.features.mcp).toBe(false);
  1484. } finally {
  1485. await rm(root, { force: true, recursive: true });
  1486. }
  1487. });
  1488. it("loads process hook config from file config", async () => {
  1489. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1490. const path = join(dir, "config.json");
  1491. try {
  1492. await writeFile(
  1493. path,
  1494. JSON.stringify({
  1495. hooks: {
  1496. enabled: true,
  1497. timeoutMs: 5000,
  1498. maxOutputBytes: 8192,
  1499. events: {
  1500. PreToolUse: [
  1501. {
  1502. matcher: "Bash",
  1503. hooks: [
  1504. {
  1505. type: "process",
  1506. command: "node",
  1507. args: ["hooks/pre-tool.js"],
  1508. enabled: false,
  1509. customFlag: "keep",
  1510. },
  1511. ],
  1512. },
  1513. ],
  1514. },
  1515. },
  1516. }),
  1517. );
  1518. const result = loadFileConfig(path);
  1519. expect(result.loaded).toBe(true);
  1520. expect(result.config.hooks).toMatchObject({
  1521. enabled: true,
  1522. timeoutMs: 5000,
  1523. maxOutputBytes: 8192,
  1524. });
  1525. expect(result.config.hooks?.events?.PreToolUse?.[0]?.hooks[0]).toMatchObject({
  1526. type: "process",
  1527. command: "node",
  1528. args: ["hooks/pre-tool.js"],
  1529. enabled: false,
  1530. customFlag: "keep",
  1531. });
  1532. } finally {
  1533. await rm(dir, { force: true, recursive: true });
  1534. }
  1535. });
  1536. it("drops hooks from discovered project config files because project hooks are untrusted code", async () => {
  1537. const root = await mkdtemp(join(tmpdir(), "zcode-project-hooks-"));
  1538. try {
  1539. await mkdir(join(root, ".git"));
  1540. await mkdir(join(root, ".zcode"), { recursive: true });
  1541. await writeFile(
  1542. join(root, "zcode.json"),
  1543. JSON.stringify({
  1544. hooks: {
  1545. enabled: true,
  1546. events: {
  1547. SessionStart: [
  1548. {
  1549. matcher: "startup",
  1550. hooks: [{ type: "command", command: "echo root-project-hook" }],
  1551. },
  1552. ],
  1553. },
  1554. },
  1555. }),
  1556. );
  1557. await writeFile(
  1558. join(root, ".zcode", "config.json"),
  1559. JSON.stringify({
  1560. hooks: {
  1561. enabled: true,
  1562. events: {
  1563. SessionStart: [
  1564. {
  1565. matcher: "startup",
  1566. hooks: [{ type: "command", command: "echo project-hook" }],
  1567. },
  1568. ],
  1569. },
  1570. },
  1571. }),
  1572. );
  1573. const result = createConfig({
  1574. env: {},
  1575. skipUserConfig: true,
  1576. workingDirectory: root,
  1577. });
  1578. expect(result.sources.project.loaded).toBe(true);
  1579. expect(
  1580. result.sources.project.diagnostics.filter(
  1581. (diagnostic) => diagnostic.code === "config_project_hooks_pending_trust",
  1582. ),
  1583. ).toHaveLength(2);
  1584. for (const configPath of [join(root, "zcode.json"), join(root, ".zcode", "config.json")]) {
  1585. expect(result.sources.project.diagnostics).toContainEqual(
  1586. expect.objectContaining({
  1587. code: "config_project_hooks_pending_trust",
  1588. filePath: configPath,
  1589. message: "Project hooks are pending workspace trust and remain blocked",
  1590. path: "hooks",
  1591. severity: "warning",
  1592. }),
  1593. );
  1594. }
  1595. expect(result.config.hooks.events.SessionStart).toBeUndefined();
  1596. expect(result.config.hooks.enabled).toBe(false);
  1597. } finally {
  1598. await rm(root, { force: true, recursive: true });
  1599. }
  1600. });
  1601. it("keeps user hooks while dropping untrusted project hooks", async () => {
  1602. const root = await mkdtemp(join(tmpdir(), "zcode-user-project-hooks-"));
  1603. const userConfigPath = join(root, "user-config.json");
  1604. try {
  1605. await mkdir(join(root, ".git"));
  1606. await mkdir(join(root, ".zcode"), { recursive: true });
  1607. await writeFile(
  1608. userConfigPath,
  1609. JSON.stringify({
  1610. hooks: {
  1611. enabled: true,
  1612. events: {
  1613. SessionStart: [
  1614. {
  1615. matcher: "startup",
  1616. hooks: [{ type: "command", command: "echo user-hook" }],
  1617. },
  1618. ],
  1619. },
  1620. },
  1621. }),
  1622. );
  1623. await writeFile(
  1624. join(root, ".zcode", "config.json"),
  1625. JSON.stringify({
  1626. hooks: {
  1627. enabled: true,
  1628. events: {
  1629. UserPromptSubmit: [
  1630. {
  1631. matcher: "*",
  1632. hooks: [{ type: "command", command: "echo project-hook" }],
  1633. },
  1634. ],
  1635. },
  1636. },
  1637. }),
  1638. );
  1639. const result = createConfig({
  1640. env: {},
  1641. userConfigPath,
  1642. workingDirectory: root,
  1643. });
  1644. expect(result.sources.user.loaded).toBe(true);
  1645. expect(result.sources.project.loaded).toBe(true);
  1646. expect(result.sources.project.diagnostics).toContainEqual(
  1647. expect.objectContaining({
  1648. code: "config_project_hooks_pending_trust",
  1649. path: "hooks",
  1650. severity: "warning",
  1651. }),
  1652. );
  1653. expect(result.config.hooks.enabled).toBe(true);
  1654. expect(result.config.hooks.events.SessionStart?.[0]?.hooks[0]).toMatchObject({
  1655. type: "command",
  1656. command: "echo user-hook",
  1657. source: { kind: "user", path: userConfigPath },
  1658. });
  1659. expect(result.config.hooks.events.UserPromptSubmit).toBeUndefined();
  1660. } finally {
  1661. await rm(root, { force: true, recursive: true });
  1662. }
  1663. });
  1664. it("loads model stream idle timeout config from file config", async () => {
  1665. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1666. const path = join(dir, "config.json");
  1667. try {
  1668. await writeFile(
  1669. path,
  1670. JSON.stringify({
  1671. modelStream: {
  1672. idleTimeoutMs: 120000,
  1673. },
  1674. }),
  1675. );
  1676. const result = loadFileConfig(path);
  1677. expect(result.loaded).toBe(true);
  1678. expect(result.config.modelStream).toMatchObject({
  1679. idleTimeoutMs: 120000,
  1680. });
  1681. } finally {
  1682. await rm(dir, { force: true, recursive: true });
  1683. }
  1684. });
  1685. it("merges model stream idle timeout into runtime config", async () => {
  1686. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1687. const path = join(dir, "config.json");
  1688. try {
  1689. await writeFile(
  1690. path,
  1691. JSON.stringify({
  1692. modelStream: {
  1693. idleTimeoutMs: 240000,
  1694. },
  1695. }),
  1696. );
  1697. const result = createConfig({ env: {}, userConfigPath: path });
  1698. expect(result.config.modelStream.idleTimeoutMs).toBe(240000);
  1699. } finally {
  1700. await rm(dir, { force: true, recursive: true });
  1701. }
  1702. });
  1703. it("loads model anomaly guard config from file config", async () => {
  1704. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1705. const path = join(dir, "config.json");
  1706. try {
  1707. await writeFile(
  1708. path,
  1709. JSON.stringify({
  1710. modelAnomalyGuard: {
  1711. repeatedToolCallWarningThreshold: 4,
  1712. maxBudgetWarningsPerTurn: 2,
  1713. },
  1714. }),
  1715. );
  1716. const result = loadFileConfig(path);
  1717. expect(result.loaded).toBe(true);
  1718. expect(result.config.modelAnomalyGuard).toMatchObject({
  1719. repeatedToolCallWarningThreshold: 4,
  1720. maxBudgetWarningsPerTurn: 2,
  1721. });
  1722. } finally {
  1723. await rm(dir, { force: true, recursive: true });
  1724. }
  1725. });
  1726. it("rejects unknown hook events from file config", async () => {
  1727. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1728. const path = join(dir, "config.json");
  1729. try {
  1730. await writeFile(
  1731. path,
  1732. JSON.stringify({
  1733. hooks: {
  1734. enabled: true,
  1735. events: {
  1736. BeforeEverything: [
  1737. {
  1738. hooks: [
  1739. {
  1740. type: "process",
  1741. command: "node",
  1742. },
  1743. ],
  1744. },
  1745. ],
  1746. },
  1747. },
  1748. }),
  1749. );
  1750. const result = loadFileConfig(path);
  1751. expect(result.loaded).toBe(false);
  1752. expect(result.config).toEqual({});
  1753. } finally {
  1754. await rm(dir, { force: true, recursive: true });
  1755. }
  1756. });
  1757. // Provider/Model 配置已归 Registry 管理;通用配置忽略退场字段,不得重新推导模型选择。
  1758. it.each([
  1759. { model: { provider: "example", model: "example-model", apiKey: "fixture" } },
  1760. { model: { main: "example/main", lite: "example/lite" } },
  1761. { model: "example/main", modelSmall: "example/small", smallModel: "example/small" },
  1762. {
  1763. provider: {
  1764. example: {
  1765. kind: "unsupported",
  1766. npm: "fixture",
  1767. models: { tiny: { limit: { context: "invalid" } } },
  1768. },
  1769. },
  1770. },
  1771. { modelCatalog: { overrides: { "example/model": { supportsReasoning: true } } } },
  1772. ])(
  1773. "ignores retired provider config while preserving active configuration: %j",
  1774. async (legacy) => {
  1775. const dir = await mkdtemp(join(tmpdir(), "zcode-config-retired-model-"));
  1776. try {
  1777. const path = join(dir, "config.json");
  1778. await writeFile(path, JSON.stringify({ ...legacy, features: { memory: false } }));
  1779. const result = loadFileConfig(path);
  1780. expect(result.loaded).toBe(true);
  1781. expect(result.config).toMatchObject({ features: { memory: false } });
  1782. for (const key of ["model", "provider", "modelCatalog", "modelSmall", "smallModel"])
  1783. expect(result.config).not.toHaveProperty(key);
  1784. } finally {
  1785. await rm(dir, { recursive: true, force: true });
  1786. }
  1787. },
  1788. );
  1789. it("rejects invalid active configuration", async () => {
  1790. const dir = await mkdtemp(join(tmpdir(), "zcode-config-invalid-"));
  1791. try {
  1792. const path = join(dir, "config.json");
  1793. await writeFile(path, JSON.stringify({ network: { timeout: "invalid" } }));
  1794. const result = loadFileConfig(path);
  1795. expect(result.loaded).toBe(false);
  1796. expect(result.config).toEqual({});
  1797. } finally {
  1798. await rm(dir, { recursive: true, force: true });
  1799. }
  1800. });
  1801. it("removes a plugin's enabled flag and options on uninstall while preserving others", async () => {
  1802. const dir = await mkdtemp(join(tmpdir(), "zcode-config-plugin-remove-"));
  1803. const configPath = join(dir, "config.json");
  1804. try {
  1805. await writeFile(
  1806. configPath,
  1807. JSON.stringify({
  1808. plugins: {
  1809. enabledPlugins: {
  1810. "hello@market": true,
  1811. "keep@market": true,
  1812. },
  1813. options: {
  1814. "hello@market": { default_device: "iPhone 16" },
  1815. "keep@market": { foo: "bar" },
  1816. },
  1817. },
  1818. }),
  1819. );
  1820. const result = await removePluginFromFileConfig(configPath, "hello@market");
  1821. expect(result.removedEnabled).toBe(true);
  1822. expect(result.removedOptions).toBe(true);
  1823. const parsed = JSON.parse(await readFile(configPath, "utf8")) as {
  1824. plugins: {
  1825. enabledPlugins: Record<string, boolean>;
  1826. options: Record<string, unknown>;
  1827. };
  1828. };
  1829. expect(parsed.plugins.enabledPlugins).toEqual({ "keep@market": true });
  1830. expect(parsed.plugins.options).toEqual({ "keep@market": { foo: "bar" } });
  1831. } finally {
  1832. await rm(dir, { force: true, recursive: true });
  1833. }
  1834. });
  1835. it("is a no-op when removing a plugin that has no config footprint", async () => {
  1836. const dir = await mkdtemp(join(tmpdir(), "zcode-config-plugin-remove-missing-"));
  1837. const configPath = join(dir, "config.json");
  1838. try {
  1839. await writeFile(
  1840. configPath,
  1841. JSON.stringify({ plugins: { enabledPlugins: { "keep@market": true } } }),
  1842. );
  1843. const before = await readFile(configPath, "utf8");
  1844. const result = await removePluginFromFileConfig(configPath, "missing@market");
  1845. expect(result.removedEnabled).toBe(false);
  1846. expect(result.removedOptions).toBe(false);
  1847. // 未命中任何键时不应改写文件内容。
  1848. expect(await readFile(configPath, "utf8")).toBe(before);
  1849. } finally {
  1850. await rm(dir, { force: true, recursive: true });
  1851. }
  1852. });
  1853. it("adds and removes a suppressed builtin id atomically", async () => {
  1854. const dir = await mkdtemp(join(tmpdir(), "zcode-config-suppress-patch-"));
  1855. const path = join(dir, "config.json");
  1856. const id = "skill-creator@zcode-plugins-official";
  1857. const added = await addSuppressedBuiltinInFileConfig(path, id);
  1858. expect(added.suppressed).toBe(true);
  1859. expect(JSON.parse(await readFile(path, "utf8")).plugins.suppressedBuiltins).toEqual([id]);
  1860. // idempotent add
  1861. await addSuppressedBuiltinInFileConfig(path, id);
  1862. expect(JSON.parse(await readFile(path, "utf8")).plugins.suppressedBuiltins).toEqual([id]);
  1863. const removed = await removeSuppressedBuiltinInFileConfig(path, id);
  1864. expect(removed.suppressed).toBe(false);
  1865. expect(JSON.parse(await readFile(path, "utf8")).plugins.suppressedBuiltins).toEqual([]);
  1866. });
  1867. });
  1868. async function readLogEntries(logDir: string): Promise<Array<Record<string, unknown>>> {
  1869. const files = await readdir(logDir);
  1870. expect(files).toHaveLength(1);
  1871. const content = await readFile(join(logDir, files[0]!), "utf8");
  1872. return content.trim().split("\n").map(JSON.parse) as Array<Record<string, unknown>>;
  1873. }