config.test.ts 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860
  1. import { describe, expect, it } from "vitest";
  2. import { ConfigScope, DefaultRuntimeConfig } from "@zcode/contracts";
  3. import {
  4. createConfig,
  5. updateCodingPlanProviderInFileConfig,
  6. updateModelSelectionInFileConfig,
  7. updateUiLocaleInFileConfig,
  8. } from "../src/config/index.js";
  9. import { parseEnvConfig } from "../src/config/env-config.adapter.js";
  10. import {
  11. addSuppressedBuiltinInFileConfig,
  12. loadFileConfig,
  13. removePluginFromFileConfig,
  14. removeSuppressedBuiltinInFileConfig,
  15. updatePluginEnabledInFileConfig,
  16. updatePluginOptionsInFileConfig,
  17. } from "../src/config/file-config.adapter.js";
  18. import { ModelCatalogService } from "../src/model/catalog.js";
  19. import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
  20. import { tmpdir } from "node:os";
  21. import { join } from "node:path";
  22. describe("config adapters", () => {
  23. it("migrates the legacy official CUA plugin id to the canonical id", async () => {
  24. const dir = await mkdtemp(join(tmpdir(), "zcode-config-cua-identity-"));
  25. const configPath = join(dir, "config.json");
  26. try {
  27. await writeFile(
  28. configPath,
  29. JSON.stringify({
  30. plugins: {
  31. enabledPlugins: {
  32. "zcode-cua@zcode-plugins-official": true,
  33. "computer-use@zcode-plugins-official": false,
  34. },
  35. suppressedBuiltins: [
  36. "zcode-cua@zcode-plugins-official",
  37. "computer-use@zcode-plugins-official",
  38. ],
  39. options: {
  40. "zcode-cua@zcode-plugins-official": { apiKey: "legacy-key" },
  41. "computer-use@zcode-plugins-official": { apiKey: "canonical-key" },
  42. },
  43. },
  44. }),
  45. );
  46. const loaded = loadFileConfig(configPath);
  47. expect(loaded.config.plugins?.enabledPlugins).toEqual({
  48. "computer-use@zcode-plugins-official": false,
  49. });
  50. expect(loaded.config.plugins?.suppressedBuiltins).toEqual([
  51. "computer-use@zcode-plugins-official",
  52. ]);
  53. expect(loaded.config.plugins?.options).toEqual({
  54. "computer-use@zcode-plugins-official": { apiKey: "canonical-key" },
  55. });
  56. } finally {
  57. await rm(dir, { force: true, recursive: true });
  58. }
  59. });
  60. it("persists the canonical CUA plugin id while loading legacy config", async () => {
  61. const dir = await mkdtemp(join(tmpdir(), "zcode-config-cua-writeback-"));
  62. const path = join(dir, "config.json");
  63. await writeFile(
  64. path,
  65. JSON.stringify({
  66. plugins: {
  67. enabledPlugins: { "zcode-cua@zcode-plugins-official": true },
  68. suppressedBuiltins: ["zcode-cua@zcode-plugins-official"],
  69. options: {
  70. "zcode-cua@zcode-plugins-official": { apiKey: "legacy-key" },
  71. },
  72. },
  73. }),
  74. );
  75. loadFileConfig(path);
  76. expect(JSON.parse(await readFile(path, "utf8"))).toEqual({
  77. plugins: {
  78. enabledPlugins: { "computer-use@zcode-plugins-official": true },
  79. suppressedBuiltins: ["computer-use@zcode-plugins-official"],
  80. options: {
  81. "computer-use@zcode-plugins-official": { apiKey: "legacy-key" },
  82. },
  83. },
  84. });
  85. await rm(dir, { recursive: true, force: true });
  86. });
  87. it("writes canonical CUA ids when removing legacy config entries", async () => {
  88. const dir = await mkdtemp(join(tmpdir(), "zcode-config-cua-write-migration-"));
  89. const configPath = join(dir, "config.json");
  90. try {
  91. await writeFile(
  92. configPath,
  93. JSON.stringify({
  94. plugins: {
  95. enabledPlugins: { "zcode-cua@zcode-plugins-official": true },
  96. suppressedBuiltins: ["zcode-cua@zcode-plugins-official"],
  97. },
  98. }),
  99. );
  100. expect(
  101. (
  102. await removeSuppressedBuiltinInFileConfig(
  103. configPath,
  104. "computer-use@zcode-plugins-official",
  105. )
  106. ).suppressed,
  107. ).toBe(false);
  108. expect(
  109. (
  110. await removePluginFromFileConfig(configPath, "computer-use@zcode-plugins-official")
  111. ).removedEnabled,
  112. ).toBe(true);
  113. expect(JSON.parse(await readFile(configPath, "utf8"))).toEqual({
  114. plugins: { enabledPlugins: {}, suppressedBuiltins: [] },
  115. });
  116. } finally {
  117. await rm(dir, { force: true, recursive: true });
  118. }
  119. });
  120. it("clears only explicitly selected Plugin option keys", async () => {
  121. const dir = await mkdtemp(join(tmpdir(), "zcode-config-plugin-option-clear-"));
  122. const configPath = join(dir, "config.json");
  123. const pluginId = "secret-plugin@fixture";
  124. try {
  125. await writeFile(
  126. configPath,
  127. JSON.stringify({
  128. plugins: {
  129. enabledPlugins: { [pluginId]: true },
  130. options: {
  131. [pluginId]: {
  132. token: "secret",
  133. region: "us-east-1",
  134. },
  135. },
  136. },
  137. }),
  138. );
  139. await updatePluginOptionsInFileConfig(configPath, pluginId, { region: "eu-west-1" }, [
  140. "token",
  141. ]);
  142. expect(JSON.parse(await readFile(configPath, "utf8"))).toEqual({
  143. plugins: {
  144. enabledPlugins: { [pluginId]: true },
  145. options: {
  146. [pluginId]: {
  147. region: "eu-west-1",
  148. },
  149. },
  150. },
  151. });
  152. } finally {
  153. await rm(dir, { force: true, recursive: true });
  154. }
  155. });
  156. it("deep merges Workspace Plugin enablement and options over User config", async () => {
  157. const dir = await mkdtemp(join(tmpdir(), "zcode-config-workspace-plugins-"));
  158. const userPath = join(dir, "user.json");
  159. const workspacePath = join(dir, ".zcode", "config.json");
  160. try {
  161. await mkdir(join(dir, ".zcode"), { recursive: true });
  162. await writeFile(
  163. userPath,
  164. JSON.stringify({
  165. plugins: {
  166. dirs: [join(dir, "user-plugin")],
  167. enabledPlugins: {
  168. "workspace-plugin@fixture": true,
  169. "user-only@fixture": true,
  170. },
  171. options: {
  172. "workspace-plugin@fixture": {
  173. inherited: "user",
  174. overridden: "user",
  175. },
  176. },
  177. },
  178. }),
  179. );
  180. await writeFile(
  181. workspacePath,
  182. JSON.stringify({
  183. plugins: {
  184. enabledPlugins: {
  185. "workspace-plugin@fixture": false,
  186. },
  187. options: {
  188. "workspace-plugin@fixture": {
  189. overridden: "workspace",
  190. workspaceOnly: true,
  191. },
  192. },
  193. },
  194. }),
  195. );
  196. const result = createConfig({
  197. env: {},
  198. userConfigPath: userPath,
  199. projectConfigPath: workspacePath,
  200. workingDirectory: dir,
  201. });
  202. expect(result.config.plugins.enabledPlugins).toEqual({
  203. "workspace-plugin@fixture": false,
  204. "user-only@fixture": true,
  205. });
  206. expect(result.config.plugins.options).toEqual({
  207. "workspace-plugin@fixture": {
  208. inherited: "user",
  209. overridden: "workspace",
  210. workspaceOnly: true,
  211. },
  212. });
  213. expect(result.sources.project.paths).toEqual([workspacePath]);
  214. expect(result.sources.plugins.enabled).toEqual({
  215. "user-only@fixture": "user",
  216. "workspace-plugin@fixture": "workspace",
  217. });
  218. expect(result.sources.plugins.dirs).toEqual({
  219. user: [join(dir, "user-plugin")],
  220. workspace: [],
  221. });
  222. expect(result.config.plugins.dirs).toEqual([join(dir, "user-plugin")]);
  223. expect(result.sources.plugins.options).toEqual({
  224. "workspace-plugin@fixture": {
  225. inherited: "user",
  226. overridden: "workspace",
  227. workspaceOnly: "workspace",
  228. },
  229. });
  230. } finally {
  231. await rm(dir, { force: true, recursive: true });
  232. }
  233. });
  234. it("keeps Marketplace declarations in the User config layer", async () => {
  235. const dir = await mkdtemp(join(tmpdir(), "zcode-config-workspace-marketplaces-"));
  236. const userPath = join(dir, "user.json");
  237. const workspacePath = join(dir, "workspace.json");
  238. try {
  239. await writeFile(
  240. userPath,
  241. JSON.stringify({
  242. plugins: {
  243. extraKnownMarketplaces: {
  244. "shared-market": {
  245. source: { source: "github", repo: "example/user-market" },
  246. },
  247. "user-market": {
  248. source: { source: "url", url: "https://example.test/user-market.json" },
  249. },
  250. },
  251. },
  252. }),
  253. );
  254. await writeFile(
  255. workspacePath,
  256. JSON.stringify({
  257. plugins: {
  258. extraKnownMarketplaces: {
  259. "shared-market": {
  260. source: { source: "directory", path: "./workspace-market" },
  261. },
  262. "workspace-market": {
  263. source: { source: "file", path: "./workspace-market.json" },
  264. },
  265. },
  266. },
  267. }),
  268. );
  269. const result = createConfig({
  270. env: {},
  271. userConfigPath: userPath,
  272. projectConfigPath: workspacePath,
  273. });
  274. expect(result.config.plugins.extraKnownMarketplaces).toEqual({
  275. "shared-market": {
  276. source: { source: "github", repo: "example/user-market" },
  277. },
  278. "user-market": {
  279. source: { source: "url", url: "https://example.test/user-market.json" },
  280. },
  281. });
  282. expect(result.sources.plugins.marketplaces).toEqual({
  283. "shared-market": "user",
  284. "user-market": "user",
  285. });
  286. } finally {
  287. await rm(dir, { force: true, recursive: true });
  288. }
  289. });
  290. it("keeps Plugin scope priority aligned with User then Workspace", () => {
  291. expect(ConfigScope.User).toBe("user");
  292. expect(ConfigScope.Project).toBe("project");
  293. });
  294. it("parses the supported ZCODE environment surface", () => {
  295. const config = parseEnvConfig({
  296. ZCODE_STORAGE_DIR: "/tmp/zcode",
  297. ZCODE_HTTP_PROXY: "http://127.0.0.1:7890",
  298. ZCODE_NO_PROXY: "localhost,127.0.0.1",
  299. ZCODE_AGENT_CA_CERT: "/tmp/zcode-ca.pem",
  300. ZCODE_HTTP_TIMEOUT: "1234",
  301. ZCODE_LOCALE: "zh-CN",
  302. ZCODE_LOG_FORMAT: "json",
  303. });
  304. expect(config.model).toBeUndefined();
  305. expect(config.storage?.dir).toBe("/tmp/zcode");
  306. expect(config.network?.httpProxy).toBe("http://127.0.0.1:7890");
  307. expect(config.network?.noProxy).toBe("localhost,127.0.0.1");
  308. expect(config.network?.caCertFile).toBe("/tmp/zcode-ca.pem");
  309. expect(config.network?.timeout).toBe(1234);
  310. expect(config.ui).toBeUndefined();
  311. expect(config.logging?.format).toBe("json");
  312. expect(config.logging?.level).toBeUndefined();
  313. });
  314. it("defaults the session database to db/db.sqlite", () => {
  315. const result = createConfig({ env: {}, skipUserConfig: true });
  316. expect(result.config.model).toBeUndefined();
  317. expect(DefaultRuntimeConfig.storage.sessionDbPath).toBe("~/.zcode/cli/db/db.sqlite");
  318. expect(result.config.storage.sessionDbPath).toBe("~/.zcode/cli/db/db.sqlite");
  319. });
  320. it("defaults model stream idle timeout to ten minutes", () => {
  321. const result = createConfig({ env: {}, skipUserConfig: true });
  322. expect(DefaultRuntimeConfig.modelStream.idleTimeoutMs).toBe(600_000);
  323. expect(result.config.modelStream.idleTimeoutMs).toBe(600_000);
  324. });
  325. it("defaults the supported memory switches to enabled", () => {
  326. const result = createConfig({ env: {}, skipUserConfig: true });
  327. expect(result.config.features.memory).toBe(true);
  328. expect(result.config.memory).toEqual({
  329. use: true,
  330. });
  331. });
  332. it("loads the supported memory config fields", async () => {
  333. const dir = await mkdtemp(join(tmpdir(), "zcode-config-memory-"));
  334. const path = join(dir, "config.json");
  335. try {
  336. await writeFile(
  337. path,
  338. JSON.stringify({
  339. memory: {
  340. use: false,
  341. },
  342. }),
  343. );
  344. const loaded = loadFileConfig(path);
  345. const merged = createConfig({ env: {}, userConfigPath: path });
  346. expect(loaded.loaded).toBe(true);
  347. expect(loaded.config.memory).toEqual({
  348. use: false,
  349. });
  350. expect(merged.config.memory).toEqual({
  351. use: false,
  352. });
  353. } finally {
  354. await rm(dir, { force: true, recursive: true });
  355. }
  356. });
  357. it("parses session database path environment overrides", () => {
  358. expect(
  359. parseEnvConfig({
  360. ZCODE_SESSION_DB_PATH: "/tmp/zcode/db/db.sqlite",
  361. }).storage?.sessionDbPath,
  362. ).toBe("/tmp/zcode/db/db.sqlite");
  363. expect(
  364. parseEnvConfig({
  365. ZCODE_SESSION_DB: "/tmp/zcode/legacy.sqlite",
  366. }).storage?.sessionDbPath,
  367. ).toBe("/tmp/zcode/legacy.sqlite");
  368. });
  369. it("loads no-proxy rules from file config", async () => {
  370. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  371. const path = join(dir, "config.json");
  372. try {
  373. await writeFile(
  374. path,
  375. JSON.stringify({
  376. network: {
  377. httpProxy: "http://127.0.0.1:7890",
  378. noProxy: "localhost,127.0.0.1",
  379. caCertFile: "/tmp/zcode-ca.pem",
  380. },
  381. }),
  382. );
  383. const merged = createConfig({ env: {}, userConfigPath: path });
  384. expect(merged.config.network.httpProxy).toBe("http://127.0.0.1:7890");
  385. expect(merged.config.network.noProxy).toBe("localhost,127.0.0.1");
  386. expect(merged.config.network.caCertFile).toBe("/tmp/zcode-ca.pem");
  387. } finally {
  388. await rm(dir, { force: true, recursive: true });
  389. }
  390. });
  391. it("ignores undeclared ZCODE environment switches", () => {
  392. const config = parseEnvConfig({
  393. ZCODE_LOG_LEVEL: "debug",
  394. ZCODE_UNDECLARED_FLAG: "1",
  395. });
  396. expect(config).toEqual({});
  397. });
  398. it("loads yolo mode from file config", async () => {
  399. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  400. const path = join(dir, "config.json");
  401. try {
  402. await writeFile(path, JSON.stringify({ permission: { mode: "yolo" } }));
  403. const result = loadFileConfig(path);
  404. expect(result.loaded).toBe(true);
  405. expect(result.config.permission?.mode).toBe("yolo");
  406. } finally {
  407. await rm(dir, { force: true, recursive: true });
  408. }
  409. });
  410. it("loads edit mode from file config", async () => {
  411. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  412. const path = join(dir, "config.json");
  413. try {
  414. await writeFile(path, JSON.stringify({ permission: { mode: "edit" } }));
  415. const result = loadFileConfig(path);
  416. expect(result.loaded).toBe(true);
  417. expect(result.config.permission?.mode).toBe("edit");
  418. } finally {
  419. await rm(dir, { force: true, recursive: true });
  420. }
  421. });
  422. it("loads skill/command enable overrides from file config", async () => {
  423. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  424. const path = join(dir, "config.json");
  425. try {
  426. await writeFile(
  427. path,
  428. JSON.stringify({
  429. skill: { "/abs/skills/demo/SKILL.md": { enable: false } },
  430. command: { "/abs/commands/demo.md": { enable: false } },
  431. }),
  432. );
  433. const result = loadFileConfig(path);
  434. const merged = createConfig({ env: {}, userConfigPath: path });
  435. expect(result.loaded).toBe(true);
  436. // JSON 的 skill/command 字段映射到 runtime 的 skillOverrides/commandOverrides
  437. expect(result.config.skillOverrides?.["/abs/skills/demo/SKILL.md"]?.enable).toBe(false);
  438. expect(result.config.commandOverrides?.["/abs/commands/demo.md"]?.enable).toBe(false);
  439. expect(merged.config.skillOverrides["/abs/skills/demo/SKILL.md"]?.enable).toBe(false);
  440. expect(merged.config.commandOverrides["/abs/commands/demo.md"]?.enable).toBe(false);
  441. } finally {
  442. await rm(dir, { force: true, recursive: true });
  443. }
  444. });
  445. it("parses plugins.suppressedBuiltins as a string array", async () => {
  446. const dir = await mkdtemp(join(tmpdir(), "zcode-config-suppressed-"));
  447. const path = join(dir, "config.json");
  448. await writeFile(
  449. path,
  450. JSON.stringify({ plugins: { suppressedBuiltins: ["skill-creator@zcode-plugins-official"] } }),
  451. );
  452. const loaded = loadFileConfig(path);
  453. expect(loaded.config.plugins.suppressedBuiltins).toEqual([
  454. "skill-creator@zcode-plugins-official",
  455. ]);
  456. });
  457. it("maps skills path enable entries to skill overrides", async () => {
  458. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  459. const path = join(dir, "config.json");
  460. try {
  461. await writeFile(
  462. path,
  463. JSON.stringify({
  464. skills: {
  465. enabled: true,
  466. roots: ["/abs/skills"],
  467. "/abs/skills/disabled/SKILL.md": { enable: false },
  468. "/abs/skills/enabled/SKILL.md": { enable: true },
  469. },
  470. }),
  471. );
  472. const result = loadFileConfig(path);
  473. const merged = createConfig({ env: {}, userConfigPath: path });
  474. expect(result.loaded).toBe(true);
  475. expect(result.config.skills).toEqual({
  476. enabled: true,
  477. roots: ["/abs/skills"],
  478. });
  479. expect(result.config.skillOverrides?.["/abs/skills/disabled/SKILL.md"]?.enable).toBe(false);
  480. expect(result.config.skillOverrides?.["/abs/skills/enabled/SKILL.md"]?.enable).toBe(true);
  481. expect(merged.config.skillOverrides["/abs/skills/disabled/SKILL.md"]?.enable).toBe(false);
  482. expect(merged.config.skillOverrides["/abs/skills/enabled/SKILL.md"]?.enable).toBe(true);
  483. } finally {
  484. await rm(dir, { force: true, recursive: true });
  485. }
  486. });
  487. it("loads top-level skills config from file config", async () => {
  488. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  489. const path = join(dir, "config.json");
  490. try {
  491. await writeFile(
  492. path,
  493. JSON.stringify({
  494. skills: {
  495. enabled: false,
  496. includeInstructions: false,
  497. metadataBudget: 1024,
  498. roots: ["/abs/skills"],
  499. },
  500. }),
  501. );
  502. const result = loadFileConfig(path);
  503. const merged = createConfig({ env: {}, userConfigPath: path });
  504. expect(result.loaded).toBe(true);
  505. expect(result.config.skills).toEqual({
  506. enabled: false,
  507. includeInstructions: false,
  508. metadataBudget: 1024,
  509. roots: ["/abs/skills"],
  510. });
  511. expect(merged.config.skills.enabled).toBe(false);
  512. expect(merged.config.skills.includeInstructions).toBe(false);
  513. expect(merged.config.skills.metadataBudget).toBe(1024);
  514. expect(merged.config.skills.roots).toEqual(["/abs/skills"]);
  515. } finally {
  516. await rm(dir, { force: true, recursive: true });
  517. }
  518. });
  519. it("loads UI locale from file config", async () => {
  520. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  521. const path = join(dir, "config.json");
  522. try {
  523. await writeFile(path, JSON.stringify({ ui: { locale: "zh-CN" } }));
  524. const result = loadFileConfig(path);
  525. const merged = createConfig({ env: {}, userConfigPath: path });
  526. expect(result.loaded).toBe(true);
  527. expect(result.config.ui?.locale).toBe("zh-CN");
  528. expect(merged.config.ui.locale).toBe("zh-CN");
  529. } finally {
  530. await rm(dir, { force: true, recursive: true });
  531. }
  532. });
  533. it("loads auto UI locale from file config", async () => {
  534. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  535. const path = join(dir, "config.json");
  536. try {
  537. await writeFile(path, JSON.stringify({ ui: { locale: "auto" } }));
  538. const result = loadFileConfig(path);
  539. const merged = createConfig({ env: {}, userConfigPath: path });
  540. expect(result.loaded).toBe(true);
  541. expect(result.config.ui?.locale).toBe("auto");
  542. expect(merged.config.ui.locale).toBe("auto");
  543. } finally {
  544. await rm(dir, { force: true, recursive: true });
  545. }
  546. });
  547. it("loads UI theme 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({ ui: { theme: "light" } }));
  552. const result = loadFileConfig(path);
  553. const merged = createConfig({ env: {}, userConfigPath: path });
  554. expect(result.loaded).toBe(true);
  555. expect(result.config.ui?.theme).toBe("light");
  556. expect(merged.config.ui.theme).toBe("light");
  557. } finally {
  558. await rm(dir, { force: true, recursive: true });
  559. }
  560. });
  561. it("rejects unsupported UI theme values from file config", async () => {
  562. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  563. const path = join(dir, "config.json");
  564. try {
  565. await writeFile(path, JSON.stringify({ ui: { theme: "sepia" } }));
  566. const result = loadFileConfig(path);
  567. expect(result.loaded).toBe(false);
  568. expect(result.config.ui).toBeUndefined();
  569. } finally {
  570. await rm(dir, { force: true, recursive: true });
  571. }
  572. });
  573. it("patches UI locale while preserving unrelated config fields", async () => {
  574. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  575. const path = join(dir, "config.json");
  576. try {
  577. await writeFile(
  578. path,
  579. JSON.stringify({
  580. model: {
  581. main: "openai/gpt-5.1",
  582. },
  583. permission: {
  584. mode: "plan",
  585. },
  586. ui: {
  587. density: "compact",
  588. locale: "en-US",
  589. },
  590. }),
  591. );
  592. const result = await updateUiLocaleInFileConfig(path, "zh-CN");
  593. const written = JSON.parse(await readFile(path, "utf-8"));
  594. expect(result).toEqual({
  595. locale: "zh-CN",
  596. path,
  597. });
  598. expect(written).toMatchObject({
  599. model: {
  600. main: "openai/gpt-5.1",
  601. },
  602. permission: {
  603. mode: "plan",
  604. },
  605. ui: {
  606. density: "compact",
  607. locale: "zh-CN",
  608. },
  609. });
  610. } finally {
  611. await rm(dir, { force: true, recursive: true });
  612. }
  613. });
  614. it("creates config files when patching UI locale", async () => {
  615. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  616. const path = join(dir, "nested", "config.json");
  617. try {
  618. await updateUiLocaleInFileConfig(path, "auto");
  619. const written = JSON.parse(await readFile(path, "utf-8"));
  620. expect(written).toEqual({
  621. ui: {
  622. locale: "auto",
  623. },
  624. });
  625. } finally {
  626. await rm(dir, { force: true, recursive: true });
  627. }
  628. });
  629. it("rejects unsupported UI locale values from file config", async () => {
  630. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  631. const path = join(dir, "config.json");
  632. try {
  633. await writeFile(path, JSON.stringify({ ui: { locale: "fr-FR" } }));
  634. const result = loadFileConfig(path);
  635. expect(result.loaded).toBe(false);
  636. expect(result.config.ui).toBeUndefined();
  637. } finally {
  638. await rm(dir, { force: true, recursive: true });
  639. }
  640. });
  641. it("loads MCP server config from file config", async () => {
  642. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  643. const path = join(dir, "config.json");
  644. try {
  645. await writeFile(
  646. path,
  647. JSON.stringify({
  648. features: {
  649. mcp: true,
  650. },
  651. mcp: {
  652. servers: {
  653. local: {
  654. type: "stdio",
  655. command: "node",
  656. args: ["server.js"],
  657. cwd: ".",
  658. env: {
  659. EXAMPLE: "1",
  660. },
  661. },
  662. remote: {
  663. type: "http",
  664. url: "https://example.com/mcp",
  665. headers: {
  666. Authorization: "Bearer token",
  667. },
  668. oauth: {
  669. type: "client_credentials",
  670. clientId: "zcode-client",
  671. clientSecret: "secret",
  672. scope: "mcp:tools",
  673. },
  674. timeoutMs: 30000,
  675. },
  676. events: {
  677. type: "sse",
  678. url: "https://example.com/sse",
  679. oauth: {
  680. type: "authorization_code",
  681. clientName: "ZCode",
  682. redirectPath: "/oauth/callback/mcp/figma",
  683. scope: "mcp:connect",
  684. },
  685. enabled: false,
  686. },
  687. },
  688. },
  689. }),
  690. );
  691. const result = loadFileConfig(path);
  692. expect(result.loaded).toBe(true);
  693. expect(result.config.features?.mcp).toBe(true);
  694. expect(result.config.mcp?.servers?.local).toMatchObject({
  695. type: "stdio",
  696. command: "node",
  697. args: ["server.js"],
  698. });
  699. expect(result.config.mcp?.servers?.remote).toMatchObject({
  700. type: "http",
  701. url: "https://example.com/mcp",
  702. oauth: {
  703. type: "client_credentials",
  704. clientId: "zcode-client",
  705. clientSecret: "secret",
  706. scope: "mcp:tools",
  707. },
  708. timeoutMs: 30000,
  709. });
  710. expect(result.config.mcp?.servers?.events).toMatchObject({
  711. type: "sse",
  712. oauth: {
  713. type: "authorization_code",
  714. clientName: "ZCode",
  715. redirectPath: "/oauth/callback/mcp/figma",
  716. scope: "mcp:connect",
  717. },
  718. enabled: false,
  719. });
  720. } finally {
  721. await rm(dir, { force: true, recursive: true });
  722. }
  723. });
  724. it("keeps an MCP server disabled when legacy enable:false coexists with enabled:true", async () => {
  725. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  726. const path = join(dir, "config.json");
  727. try {
  728. await writeFile(
  729. path,
  730. JSON.stringify({
  731. mcp: {
  732. servers: {
  733. // 桌面端「停用」写入 enable:false,而外部导入/手工编辑的同一条配置里
  734. // 已经带着 enabled:true。禁用意图必须胜出。
  735. playwright: {
  736. type: "stdio",
  737. command: "npx",
  738. args: ["-y", "@playwright/mcp@latest"],
  739. enable: false,
  740. enabled: true,
  741. },
  742. },
  743. },
  744. }),
  745. );
  746. const result = loadFileConfig(path);
  747. expect(result.config.mcp?.servers?.playwright?.enabled).toBe(false);
  748. } finally {
  749. await rm(dir, { force: true, recursive: true });
  750. }
  751. });
  752. it("keeps an MCP server disabled when enabled:false coexists with legacy enable:true", async () => {
  753. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  754. const path = join(dir, "config.json");
  755. try {
  756. await writeFile(
  757. path,
  758. JSON.stringify({
  759. mcp: {
  760. servers: {
  761. playwright: {
  762. type: "stdio",
  763. command: "npx",
  764. args: ["-y", "@playwright/mcp@latest"],
  765. enable: true,
  766. enabled: false,
  767. },
  768. },
  769. },
  770. }),
  771. );
  772. const result = loadFileConfig(path);
  773. expect(result.config.mcp?.servers?.playwright?.enabled).toBe(false);
  774. } finally {
  775. await rm(dir, { force: true, recursive: true });
  776. }
  777. });
  778. it("keeps an MCP server enabled when both enable and enabled are true", 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. mcp: {
  786. servers: {
  787. playwright: {
  788. type: "stdio",
  789. command: "npx",
  790. args: ["-y", "@playwright/mcp@latest"],
  791. enable: true,
  792. enabled: true,
  793. },
  794. },
  795. },
  796. }),
  797. );
  798. const result = loadFileConfig(path);
  799. expect(result.config.mcp?.servers?.playwright?.enabled).toBe(true);
  800. } finally {
  801. await rm(dir, { force: true, recursive: true });
  802. }
  803. });
  804. it("preserves fresh HTTP MCP config without writing implicit OAuth", async () => {
  805. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  806. const path = join(dir, "config.json");
  807. try {
  808. await writeFile(
  809. path,
  810. JSON.stringify({
  811. mcp: {
  812. servers: {
  813. notion: {
  814. type: "http",
  815. url: "https://mcp.notion.com/mcp",
  816. timeoutMs: 30000,
  817. },
  818. },
  819. },
  820. }),
  821. );
  822. const result = loadFileConfig(path);
  823. expect(result.loaded).toBe(true);
  824. expect(result.config.mcp?.servers?.notion).toMatchObject({
  825. type: "http",
  826. url: "https://mcp.notion.com/mcp",
  827. timeoutMs: 30000,
  828. });
  829. expect(result.config.mcp?.servers?.notion?.oauth).toBeUndefined();
  830. } finally {
  831. await rm(dir, { force: true, recursive: true });
  832. }
  833. });
  834. it("skips invalid MCP OAuth config without dropping unrelated config", async () => {
  835. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  836. const path = join(dir, "config.json");
  837. try {
  838. await writeFile(
  839. path,
  840. JSON.stringify({
  841. mcp: {
  842. servers: {
  843. broken: {
  844. type: "http",
  845. url: "https://example.com/mcp",
  846. oauth: {
  847. type: "client_credentials",
  848. clientId: "zcode-client",
  849. },
  850. },
  851. },
  852. },
  853. plugins: {
  854. enabled: true,
  855. },
  856. }),
  857. );
  858. const result = loadFileConfig(path);
  859. expect(result.loaded).toBe(true);
  860. expect(result.config.mcp?.servers).not.toHaveProperty("broken");
  861. expect(result.config.plugins?.enabled).toBe(true);
  862. expect(result.diagnostics).toEqual([
  863. expect.objectContaining({
  864. code: "config_mcp_server_invalid",
  865. path: "mcp.servers.broken",
  866. severity: "warning",
  867. }),
  868. ]);
  869. } finally {
  870. await rm(dir, { force: true, recursive: true });
  871. }
  872. });
  873. it("normalizes app-managed MCP config aliases from file config", async () => {
  874. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  875. const path = join(dir, "config.json");
  876. try {
  877. await writeFile(
  878. path,
  879. JSON.stringify({
  880. mcp: {
  881. servers: {
  882. "figma-dev-mode-mcp-server": {
  883. type: "stdio",
  884. command: "npx",
  885. args: ["-y", "figma-developer-mcp"],
  886. enable: false,
  887. },
  888. "chrome-devtools": {
  889. command: "npx",
  890. args: ["-y", "chrome-devtools-mcp@latest"],
  891. },
  892. node_repl: {
  893. command: "/opt/tools/node_repl",
  894. args: [],
  895. timeout: 30,
  896. startup_timeout_sec: 120,
  897. },
  898. },
  899. },
  900. }),
  901. );
  902. const result = loadFileConfig(path);
  903. expect(result.loaded).toBe(true);
  904. expect(result.config.mcp?.servers?.["figma-dev-mode-mcp-server"]).toMatchObject({
  905. type: "stdio",
  906. command: "npx",
  907. enabled: false,
  908. });
  909. expect(result.config.mcp?.servers?.["figma-dev-mode-mcp-server"]).not.toHaveProperty(
  910. "enable",
  911. );
  912. expect(result.config.mcp?.servers?.["chrome-devtools"]).toMatchObject({
  913. type: "stdio",
  914. command: "npx",
  915. args: ["-y", "chrome-devtools-mcp@latest"],
  916. });
  917. expect(result.config.mcp?.servers?.node_repl).toMatchObject({
  918. type: "stdio",
  919. command: "/opt/tools/node_repl",
  920. });
  921. expect(result.config.mcp?.servers?.node_repl).not.toHaveProperty("timeout");
  922. expect(result.config.mcp?.servers?.node_repl).not.toHaveProperty("startup_timeout_sec");
  923. } finally {
  924. await rm(dir, { force: true, recursive: true });
  925. }
  926. });
  927. it("normalizes legacy MCP http_headers to runtime headers", async () => {
  928. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  929. const path = join(dir, "config.json");
  930. try {
  931. await writeFile(
  932. path,
  933. JSON.stringify({
  934. mcp: {
  935. servers: {
  936. web_search_prime: {
  937. type: "http",
  938. url: "https://bigmodel.example.test/mcp",
  939. http_headers: {
  940. Authorization: "Bearer token",
  941. },
  942. },
  943. },
  944. },
  945. }),
  946. );
  947. const result = loadFileConfig(path);
  948. expect(result.loaded).toBe(true);
  949. expect(result.config.mcp?.servers?.web_search_prime).toMatchObject({
  950. type: "http",
  951. headers: {
  952. Authorization: "Bearer token",
  953. },
  954. });
  955. expect(result.config.mcp?.servers?.web_search_prime).not.toHaveProperty("http_headers");
  956. } finally {
  957. await rm(dir, { force: true, recursive: true });
  958. }
  959. });
  960. it("normalizes legacy MCP remote/environment fields without dropping plugins config", async () => {
  961. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  962. const path = join(dir, "config.json");
  963. try {
  964. await writeFile(
  965. path,
  966. JSON.stringify({
  967. mcp: {
  968. servers: {
  969. "GLM-Image-MCP": {
  970. enabled: true,
  971. environment: {
  972. Z_AI_API_KEY: "test-key",
  973. Z_AI_MODE: "ZHIPU",
  974. },
  975. command: "npx",
  976. args: ["-y", "@z_ai/mcp-server"],
  977. type: "stdio",
  978. },
  979. "GLM-WebReader-MCP": {
  980. enabled: true,
  981. headers: {
  982. Authorization: "Bearer token",
  983. },
  984. type: "remote",
  985. url: "https://open.bigmodel.cn/api/mcp/web_reader/mcp",
  986. },
  987. },
  988. },
  989. plugins: {
  990. enabledPlugins: {
  991. "superpowers@zcode-plugins-official": true,
  992. "skill-creator@zcode-plugins-official": false,
  993. },
  994. },
  995. }),
  996. );
  997. const result = loadFileConfig(path);
  998. const merged = createConfig({ env: {}, userConfigPath: path });
  999. expect(result.loaded).toBe(true);
  1000. expect(result.diagnostics).toEqual([]);
  1001. expect(result.config.mcp?.servers?.["GLM-Image-MCP"]).toMatchObject({
  1002. type: "stdio",
  1003. env: {
  1004. Z_AI_API_KEY: "test-key",
  1005. Z_AI_MODE: "ZHIPU",
  1006. },
  1007. });
  1008. expect(result.config.mcp?.servers?.["GLM-Image-MCP"]).not.toHaveProperty("environment");
  1009. expect(result.config.mcp?.servers?.["GLM-WebReader-MCP"]).toMatchObject({
  1010. headers: {
  1011. Authorization: "Bearer token",
  1012. },
  1013. type: "http",
  1014. url: "https://open.bigmodel.cn/api/mcp/web_reader/mcp",
  1015. });
  1016. expect(merged.config.plugins.enabledPlugins).toMatchObject({
  1017. "skill-creator@zcode-plugins-official": false,
  1018. "superpowers@zcode-plugins-official": true,
  1019. });
  1020. } finally {
  1021. await rm(dir, { force: true, recursive: true });
  1022. }
  1023. });
  1024. it("keeps non-MCP config when one MCP server is invalid", async () => {
  1025. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1026. const logDir = join(dir, "log");
  1027. const path = join(dir, "config.json");
  1028. try {
  1029. await writeFile(
  1030. path,
  1031. JSON.stringify({
  1032. mcp: {
  1033. servers: {
  1034. valid: {
  1035. type: "remote",
  1036. url: "https://example.com/mcp",
  1037. },
  1038. broken: {
  1039. type: "stdio",
  1040. args: ["server.js"],
  1041. },
  1042. },
  1043. },
  1044. plugins: {
  1045. enabledPlugins: {
  1046. "android-emulator@zcode-plugins-official": true,
  1047. },
  1048. },
  1049. }),
  1050. );
  1051. const result = loadFileConfig(path);
  1052. const merged = createConfig({
  1053. env: { ZCODE_LOG_DIR: logDir },
  1054. userConfigPath: path,
  1055. });
  1056. expect(result.loaded).toBe(true);
  1057. expect(result.config.mcp?.servers?.valid).toMatchObject({
  1058. type: "http",
  1059. url: "https://example.com/mcp",
  1060. });
  1061. expect(result.config.mcp?.servers).not.toHaveProperty("broken");
  1062. expect(
  1063. result.config.plugins?.enabledPlugins?.["android-emulator@zcode-plugins-official"],
  1064. ).toBe(true);
  1065. expect(result.diagnostics).toEqual([
  1066. expect.objectContaining({
  1067. code: "config_mcp_server_invalid",
  1068. path: "mcp.servers.broken",
  1069. severity: "warning",
  1070. }),
  1071. ]);
  1072. expect(merged.sources.user.diagnostics).toEqual(result.diagnostics);
  1073. expect(merged.config.plugins.enabledPlugins["android-emulator@zcode-plugins-official"]).toBe(
  1074. true,
  1075. );
  1076. await expect(readLogEntries(logDir)).resolves.toEqual([
  1077. expect.objectContaining({
  1078. event: "config.mcp_server.skipped",
  1079. level: "warn",
  1080. message: "MCP server config skipped",
  1081. module: "adapters.config",
  1082. context: expect.objectContaining({
  1083. configPath: path,
  1084. configScope: "user",
  1085. diagnosticCode: "config_mcp_server_invalid",
  1086. diagnosticPath: "mcp.servers.broken",
  1087. }),
  1088. }),
  1089. ]);
  1090. } finally {
  1091. await rm(dir, { force: true, recursive: true });
  1092. }
  1093. });
  1094. it("reports invalid discovered project config diagnostics", async () => {
  1095. const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1096. const logDir = join(root, "log");
  1097. const projectConfigPath = join(root, "zcode.json");
  1098. try {
  1099. await mkdir(join(root, ".git"));
  1100. await writeFile(
  1101. projectConfigPath,
  1102. JSON.stringify({
  1103. ui: {
  1104. theme: "sepia",
  1105. },
  1106. }),
  1107. );
  1108. const result = createConfig({
  1109. env: { ZCODE_LOG_DIR: logDir },
  1110. skipUserConfig: true,
  1111. workingDirectory: root,
  1112. });
  1113. expect(result.sources.project.loaded).toBe(false);
  1114. expect(result.sources.project.diagnostics).toEqual([
  1115. expect.objectContaining({
  1116. code: "config_file_invalid",
  1117. message: expect.stringContaining("ui.theme: Invalid option: expected one of"),
  1118. severity: "error",
  1119. }),
  1120. ]);
  1121. await expect(readLogEntries(logDir)).resolves.toEqual([
  1122. expect.objectContaining({
  1123. event: "config.file.invalid",
  1124. level: "warn",
  1125. message: "Config file failed to load",
  1126. module: "adapters.config",
  1127. context: expect.objectContaining({
  1128. configPath: projectConfigPath,
  1129. configScope: "project",
  1130. diagnosticCode: "config_file_invalid",
  1131. }),
  1132. }),
  1133. ]);
  1134. } finally {
  1135. await rm(root, { force: true, recursive: true });
  1136. }
  1137. });
  1138. it("discovers project MCP config from the worktree root", async () => {
  1139. const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1140. const cwd = join(root, "packages", "app");
  1141. try {
  1142. await mkdir(join(root, ".git"));
  1143. await mkdir(cwd, { recursive: true });
  1144. await writeFile(
  1145. join(root, "zcode.json"),
  1146. JSON.stringify({
  1147. mcp: {
  1148. servers: {
  1149. project: {
  1150. type: "stdio",
  1151. command: "node",
  1152. args: ["server.js"],
  1153. cwd: "tools",
  1154. },
  1155. },
  1156. },
  1157. }),
  1158. );
  1159. const result = createConfig({
  1160. env: {},
  1161. skipUserConfig: true,
  1162. workingDirectory: cwd,
  1163. });
  1164. expect(result.sources.project.loaded).toBe(true);
  1165. expect(result.sources.project.paths).toEqual([join(root, "zcode.json")]);
  1166. expect(result.sources.project.mcpServerNames).toEqual(["project"]);
  1167. expect(result.config.mcp.servers.project).toMatchObject({
  1168. type: "stdio",
  1169. command: "node",
  1170. cwd: join(root, "tools"),
  1171. });
  1172. } finally {
  1173. await rm(root, { force: true, recursive: true });
  1174. }
  1175. });
  1176. it("merges project configs from root to cwd with nearest config winning", async () => {
  1177. const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1178. const cwd = join(root, "packages", "app");
  1179. const cwdConfigDir = join(cwd, ".zcode");
  1180. try {
  1181. await mkdir(join(root, ".git"));
  1182. await mkdir(cwdConfigDir, { recursive: true });
  1183. await writeFile(
  1184. join(root, "zcode.json"),
  1185. JSON.stringify({
  1186. mcp: {
  1187. servers: {
  1188. project: {
  1189. type: "stdio",
  1190. command: "root-server",
  1191. },
  1192. },
  1193. },
  1194. }),
  1195. );
  1196. await writeFile(
  1197. join(cwdConfigDir, "config.json"),
  1198. JSON.stringify({
  1199. mcp: {
  1200. servers: {
  1201. project: {
  1202. type: "stdio",
  1203. command: "nearest-server",
  1204. },
  1205. },
  1206. },
  1207. }),
  1208. );
  1209. const result = createConfig({
  1210. env: {},
  1211. skipUserConfig: true,
  1212. workingDirectory: cwd,
  1213. });
  1214. expect(result.sources.project.paths).toEqual([
  1215. join(root, "zcode.json"),
  1216. join(cwdConfigDir, "config.json"),
  1217. ]);
  1218. expect(result.config.mcp.servers.project).toMatchObject({
  1219. type: "stdio",
  1220. command: "nearest-server",
  1221. cwd,
  1222. });
  1223. } finally {
  1224. await rm(root, { force: true, recursive: true });
  1225. }
  1226. });
  1227. it("lets user MCP servers override same-name project MCP servers", async () => {
  1228. const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1229. const userConfigPath = join(root, "home", "config.json");
  1230. try {
  1231. await mkdir(join(root, ".git"));
  1232. await mkdir(join(root, "home"), { recursive: true });
  1233. await writeFile(
  1234. join(root, "zcode.json"),
  1235. JSON.stringify({
  1236. mcp: {
  1237. servers: {
  1238. projectOnly: {
  1239. type: "stdio",
  1240. command: "project-only-server",
  1241. },
  1242. shared: {
  1243. type: "stdio",
  1244. command: "project-server",
  1245. },
  1246. },
  1247. },
  1248. }),
  1249. );
  1250. await writeFile(
  1251. userConfigPath,
  1252. JSON.stringify({
  1253. mcp: {
  1254. servers: {
  1255. shared: {
  1256. type: "stdio",
  1257. command: "user-server",
  1258. },
  1259. userOnly: {
  1260. type: "stdio",
  1261. command: "user-only-server",
  1262. },
  1263. },
  1264. },
  1265. }),
  1266. );
  1267. const result = createConfig({
  1268. env: {},
  1269. userConfigPath,
  1270. workingDirectory: root,
  1271. });
  1272. expect(result.config.mcp.servers.shared).toMatchObject({
  1273. type: "stdio",
  1274. command: "user-server",
  1275. });
  1276. expect(result.config.mcp.servers.projectOnly).toMatchObject({
  1277. command: "project-only-server",
  1278. });
  1279. expect(result.config.mcp.servers.userOnly).toMatchObject({
  1280. command: "user-only-server",
  1281. });
  1282. expect(result.sources.user.mcpServerNames).toEqual(["shared", "userOnly"]);
  1283. expect(result.sources.project.mcpServerNames.toSorted()).toEqual(["projectOnly", "shared"]);
  1284. expect(result.sources.mcp.serverSources).toMatchObject({
  1285. projectOnly: "project",
  1286. shared: "user",
  1287. userOnly: "user",
  1288. });
  1289. } finally {
  1290. await rm(root, { force: true, recursive: true });
  1291. }
  1292. });
  1293. it("does not scan project config above the worktree root", async () => {
  1294. const parent = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1295. const root = join(parent, "repo");
  1296. const cwd = join(root, "packages", "app");
  1297. try {
  1298. await mkdir(join(root, ".git"), { recursive: true });
  1299. await mkdir(cwd, { recursive: true });
  1300. await writeFile(
  1301. join(parent, "zcode.json"),
  1302. JSON.stringify({
  1303. mcp: {
  1304. servers: {
  1305. parent: {
  1306. type: "stdio",
  1307. command: "parent-server",
  1308. },
  1309. },
  1310. },
  1311. }),
  1312. );
  1313. const result = createConfig({
  1314. env: {},
  1315. skipUserConfig: true,
  1316. workingDirectory: cwd,
  1317. });
  1318. expect(result.sources.project.loaded).toBe(false);
  1319. expect(result.config.mcp.servers.parent).toBeUndefined();
  1320. } finally {
  1321. await rm(parent, { force: true, recursive: true });
  1322. }
  1323. });
  1324. it("keeps CLI feature overrides above project MCP config", async () => {
  1325. const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
  1326. try {
  1327. await mkdir(join(root, ".git"));
  1328. await writeFile(
  1329. join(root, "zcode.json"),
  1330. JSON.stringify({
  1331. features: {
  1332. mcp: true,
  1333. },
  1334. }),
  1335. );
  1336. const result = createConfig({
  1337. cliOverrides: {
  1338. features: {
  1339. mcp: false,
  1340. },
  1341. },
  1342. env: {},
  1343. skipUserConfig: true,
  1344. workingDirectory: root,
  1345. });
  1346. expect(result.config.features.mcp).toBe(false);
  1347. } finally {
  1348. await rm(root, { force: true, recursive: true });
  1349. }
  1350. });
  1351. it("loads process hook config from file config", async () => {
  1352. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1353. const path = join(dir, "config.json");
  1354. try {
  1355. await writeFile(
  1356. path,
  1357. JSON.stringify({
  1358. hooks: {
  1359. enabled: true,
  1360. timeoutMs: 5000,
  1361. maxOutputBytes: 8192,
  1362. events: {
  1363. PreToolUse: [
  1364. {
  1365. matcher: "Bash",
  1366. hooks: [
  1367. {
  1368. type: "process",
  1369. command: "node",
  1370. args: ["hooks/pre-tool.js"],
  1371. enabled: false,
  1372. customFlag: "keep",
  1373. },
  1374. ],
  1375. },
  1376. ],
  1377. },
  1378. },
  1379. }),
  1380. );
  1381. const result = loadFileConfig(path);
  1382. expect(result.loaded).toBe(true);
  1383. expect(result.config.hooks).toMatchObject({
  1384. enabled: true,
  1385. timeoutMs: 5000,
  1386. maxOutputBytes: 8192,
  1387. });
  1388. expect(result.config.hooks?.events?.PreToolUse?.[0]?.hooks[0]).toMatchObject({
  1389. type: "process",
  1390. command: "node",
  1391. args: ["hooks/pre-tool.js"],
  1392. enabled: false,
  1393. customFlag: "keep",
  1394. });
  1395. } finally {
  1396. await rm(dir, { force: true, recursive: true });
  1397. }
  1398. });
  1399. it("drops hooks from discovered project config files because project hooks are untrusted code", async () => {
  1400. const root = await mkdtemp(join(tmpdir(), "zcode-project-hooks-"));
  1401. try {
  1402. await mkdir(join(root, ".git"));
  1403. await mkdir(join(root, ".zcode"), { recursive: true });
  1404. await writeFile(
  1405. join(root, "zcode.json"),
  1406. JSON.stringify({
  1407. hooks: {
  1408. enabled: true,
  1409. events: {
  1410. SessionStart: [
  1411. {
  1412. matcher: "startup",
  1413. hooks: [{ type: "command", command: "echo root-project-hook" }],
  1414. },
  1415. ],
  1416. },
  1417. },
  1418. }),
  1419. );
  1420. await writeFile(
  1421. join(root, ".zcode", "config.json"),
  1422. JSON.stringify({
  1423. hooks: {
  1424. enabled: true,
  1425. events: {
  1426. SessionStart: [
  1427. {
  1428. matcher: "startup",
  1429. hooks: [{ type: "command", command: "echo project-hook" }],
  1430. },
  1431. ],
  1432. },
  1433. },
  1434. }),
  1435. );
  1436. const result = createConfig({
  1437. env: {},
  1438. skipUserConfig: true,
  1439. workingDirectory: root,
  1440. });
  1441. expect(result.sources.project.loaded).toBe(true);
  1442. expect(
  1443. result.sources.project.diagnostics.filter(
  1444. (diagnostic) => diagnostic.code === "config_project_hooks_pending_trust",
  1445. ),
  1446. ).toHaveLength(2);
  1447. for (const configPath of [join(root, "zcode.json"), join(root, ".zcode", "config.json")]) {
  1448. expect(result.sources.project.diagnostics).toContainEqual(
  1449. expect.objectContaining({
  1450. code: "config_project_hooks_pending_trust",
  1451. filePath: configPath,
  1452. message: "Project hooks are pending workspace trust and remain blocked",
  1453. path: "hooks",
  1454. severity: "warning",
  1455. }),
  1456. );
  1457. }
  1458. expect(result.config.hooks.events.SessionStart).toBeUndefined();
  1459. expect(result.config.hooks.enabled).toBe(false);
  1460. } finally {
  1461. await rm(root, { force: true, recursive: true });
  1462. }
  1463. });
  1464. it("keeps user hooks while dropping untrusted project hooks", async () => {
  1465. const root = await mkdtemp(join(tmpdir(), "zcode-user-project-hooks-"));
  1466. const userConfigPath = join(root, "user-config.json");
  1467. try {
  1468. await mkdir(join(root, ".git"));
  1469. await mkdir(join(root, ".zcode"), { recursive: true });
  1470. await writeFile(
  1471. userConfigPath,
  1472. JSON.stringify({
  1473. hooks: {
  1474. enabled: true,
  1475. events: {
  1476. SessionStart: [
  1477. {
  1478. matcher: "startup",
  1479. hooks: [{ type: "command", command: "echo user-hook" }],
  1480. },
  1481. ],
  1482. },
  1483. },
  1484. }),
  1485. );
  1486. await writeFile(
  1487. join(root, ".zcode", "config.json"),
  1488. JSON.stringify({
  1489. hooks: {
  1490. enabled: true,
  1491. events: {
  1492. UserPromptSubmit: [
  1493. {
  1494. matcher: "*",
  1495. hooks: [{ type: "command", command: "echo project-hook" }],
  1496. },
  1497. ],
  1498. },
  1499. },
  1500. }),
  1501. );
  1502. const result = createConfig({
  1503. env: {},
  1504. userConfigPath,
  1505. workingDirectory: root,
  1506. });
  1507. expect(result.sources.user.loaded).toBe(true);
  1508. expect(result.sources.project.loaded).toBe(true);
  1509. expect(result.sources.project.diagnostics).toContainEqual(
  1510. expect.objectContaining({
  1511. code: "config_project_hooks_pending_trust",
  1512. path: "hooks",
  1513. severity: "warning",
  1514. }),
  1515. );
  1516. expect(result.config.hooks.enabled).toBe(true);
  1517. expect(result.config.hooks.events.SessionStart?.[0]?.hooks[0]).toMatchObject({
  1518. type: "command",
  1519. command: "echo user-hook",
  1520. source: { kind: "user", path: userConfigPath },
  1521. });
  1522. expect(result.config.hooks.events.UserPromptSubmit).toBeUndefined();
  1523. } finally {
  1524. await rm(root, { force: true, recursive: true });
  1525. }
  1526. });
  1527. it("loads model stream idle timeout config from file config", async () => {
  1528. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1529. const path = join(dir, "config.json");
  1530. try {
  1531. await writeFile(
  1532. path,
  1533. JSON.stringify({
  1534. modelStream: {
  1535. idleTimeoutMs: 120000,
  1536. },
  1537. }),
  1538. );
  1539. const result = loadFileConfig(path);
  1540. expect(result.loaded).toBe(true);
  1541. expect(result.config.modelStream).toMatchObject({
  1542. idleTimeoutMs: 120000,
  1543. });
  1544. } finally {
  1545. await rm(dir, { force: true, recursive: true });
  1546. }
  1547. });
  1548. it("merges model stream idle timeout into runtime config", async () => {
  1549. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1550. const path = join(dir, "config.json");
  1551. try {
  1552. await writeFile(
  1553. path,
  1554. JSON.stringify({
  1555. modelStream: {
  1556. idleTimeoutMs: 240000,
  1557. },
  1558. }),
  1559. );
  1560. const result = createConfig({ env: {}, userConfigPath: path });
  1561. expect(result.config.modelStream.idleTimeoutMs).toBe(240000);
  1562. } finally {
  1563. await rm(dir, { force: true, recursive: true });
  1564. }
  1565. });
  1566. it("loads model anomaly guard config from file config", async () => {
  1567. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1568. const path = join(dir, "config.json");
  1569. try {
  1570. await writeFile(
  1571. path,
  1572. JSON.stringify({
  1573. modelAnomalyGuard: {
  1574. repeatedToolCallWarningThreshold: 4,
  1575. maxBudgetWarningsPerTurn: 2,
  1576. },
  1577. }),
  1578. );
  1579. const result = loadFileConfig(path);
  1580. expect(result.loaded).toBe(true);
  1581. expect(result.config.modelAnomalyGuard).toMatchObject({
  1582. repeatedToolCallWarningThreshold: 4,
  1583. maxBudgetWarningsPerTurn: 2,
  1584. });
  1585. } finally {
  1586. await rm(dir, { force: true, recursive: true });
  1587. }
  1588. });
  1589. it("rejects unknown hook events from file config", async () => {
  1590. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1591. const path = join(dir, "config.json");
  1592. try {
  1593. await writeFile(
  1594. path,
  1595. JSON.stringify({
  1596. hooks: {
  1597. enabled: true,
  1598. events: {
  1599. BeforeEverything: [
  1600. {
  1601. hooks: [
  1602. {
  1603. type: "process",
  1604. command: "node",
  1605. },
  1606. ],
  1607. },
  1608. ],
  1609. },
  1610. },
  1611. }),
  1612. );
  1613. const result = loadFileConfig(path);
  1614. expect(result.loaded).toBe(false);
  1615. expect(result.config).toEqual({});
  1616. } finally {
  1617. await rm(dir, { force: true, recursive: true });
  1618. }
  1619. });
  1620. it("rejects inline model target config from file", async () => {
  1621. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1622. const path = join(dir, "config.json");
  1623. try {
  1624. await writeFile(
  1625. path,
  1626. JSON.stringify({
  1627. model: {
  1628. provider: "default-deepseek",
  1629. model: "deepseek-V4-Pro",
  1630. baseURL: "https://api.deepseek.com",
  1631. apiKey: "secret",
  1632. headers: {
  1633. "X-Title": "Z Code",
  1634. },
  1635. },
  1636. }),
  1637. );
  1638. const result = loadFileConfig(path);
  1639. expect(result.loaded).toBe(false);
  1640. expect(result.config).toEqual({});
  1641. } finally {
  1642. await rm(dir, { force: true, recursive: true });
  1643. }
  1644. });
  1645. it("loads main and lite model references from provider-first config", async () => {
  1646. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1647. const path = join(dir, "config.json");
  1648. try {
  1649. await writeFile(
  1650. path,
  1651. JSON.stringify({
  1652. model: {
  1653. main: "deepseek/deepseek-v4-pro",
  1654. lite: "deepseek/deepseek-chat",
  1655. },
  1656. provider: {
  1657. deepseek: {
  1658. kind: "openai-compatible",
  1659. name: "DeepSeek",
  1660. options: {
  1661. baseURL: "https://api.deepseek.com",
  1662. apiKey: "secret",
  1663. },
  1664. models: {
  1665. "deepseek-v4-pro": {
  1666. name: "DeepSeek V4 Pro",
  1667. },
  1668. "deepseek-chat": {
  1669. name: "DeepSeek Chat",
  1670. },
  1671. },
  1672. },
  1673. },
  1674. }),
  1675. );
  1676. const result = loadFileConfig(path);
  1677. expect(result.loaded).toBe(true);
  1678. expect(result.config.model?.main).toMatchObject({
  1679. provider: "deepseek",
  1680. model: "deepseek-v4-pro",
  1681. kind: "openai-compatible",
  1682. baseURL: "https://api.deepseek.com",
  1683. apiKey: "secret",
  1684. });
  1685. expect(result.config.model?.lite?.model).toBe("deepseek-chat");
  1686. } finally {
  1687. await rm(dir, { force: true, recursive: true });
  1688. }
  1689. });
  1690. it("patches string model selection in file config without dropping other fields", async () => {
  1691. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1692. const path = join(dir, "config.json");
  1693. try {
  1694. await writeFile(
  1695. path,
  1696. JSON.stringify({
  1697. model: "deepseek/deepseek-chat",
  1698. mcp: {
  1699. servers: {
  1700. local: {
  1701. type: "stdio",
  1702. command: "node",
  1703. },
  1704. },
  1705. },
  1706. provider: {
  1707. deepseek: {
  1708. kind: "openai-compatible",
  1709. options: {
  1710. baseURL: "https://api.deepseek.com",
  1711. apiKey: "secret",
  1712. },
  1713. models: {
  1714. "deepseek-chat": {},
  1715. "deepseek-v4-pro": {},
  1716. },
  1717. },
  1718. },
  1719. }),
  1720. );
  1721. await updateModelSelectionInFileConfig(path, "deepseek/deepseek-v4-pro");
  1722. const updated = JSON.parse(await readFile(path, "utf-8"));
  1723. expect(updated.model).toBe("deepseek/deepseek-v4-pro");
  1724. expect(updated.mcp.servers.local.command).toBe("node");
  1725. expect(updated.provider.deepseek.options.apiKey).toBe("secret");
  1726. } finally {
  1727. await rm(dir, { force: true, recursive: true });
  1728. }
  1729. });
  1730. it("patches object model main in file config without changing lite", async () => {
  1731. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1732. const path = join(dir, "config.json");
  1733. try {
  1734. await writeFile(
  1735. path,
  1736. JSON.stringify({
  1737. model: {
  1738. main: "deepseek/deepseek-chat",
  1739. lite: "deepseek/deepseek-lite",
  1740. },
  1741. provider: {
  1742. deepseek: {
  1743. kind: "openai-compatible",
  1744. options: {
  1745. baseURL: "https://api.deepseek.com",
  1746. },
  1747. models: {
  1748. "deepseek-chat": {},
  1749. "deepseek-lite": {},
  1750. "deepseek-v4-pro": {},
  1751. },
  1752. },
  1753. },
  1754. }),
  1755. );
  1756. await updateModelSelectionInFileConfig(path, "deepseek/deepseek-v4-pro");
  1757. const updated = JSON.parse(await readFile(path, "utf-8"));
  1758. expect(updated.model).toEqual({
  1759. main: "deepseek/deepseek-v4-pro",
  1760. lite: "deepseek/deepseek-lite",
  1761. });
  1762. } finally {
  1763. await rm(dir, { force: true, recursive: true });
  1764. }
  1765. });
  1766. it("patches ZAI Coding Plan provider config with an API key", async () => {
  1767. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1768. const path = join(dir, "config.json");
  1769. try {
  1770. await writeFile(
  1771. path,
  1772. JSON.stringify({
  1773. provider: {
  1774. zai: {
  1775. kind: "openai-compatible",
  1776. options: {
  1777. apiKey: "legacy-secret",
  1778. baseURL: "https://legacy.example/v1",
  1779. timeout: 30000,
  1780. },
  1781. models: {
  1782. "glm-5.1": {
  1783. contextWindow: 200000,
  1784. },
  1785. },
  1786. },
  1787. deepseek: {
  1788. kind: "openai-compatible",
  1789. options: {
  1790. apiKey: "deepseek-secret",
  1791. baseURL: "https://api.deepseek.example",
  1792. },
  1793. models: {
  1794. "deepseek-chat": {},
  1795. },
  1796. },
  1797. },
  1798. model: {
  1799. main: "deepseek/deepseek-chat",
  1800. lite: "deepseek/deepseek-chat",
  1801. },
  1802. }),
  1803. );
  1804. const result = await updateCodingPlanProviderInFileConfig({
  1805. apiKey: "zai-coding-key",
  1806. filePath: path,
  1807. providerId: "zai",
  1808. });
  1809. const updated = JSON.parse(await readFile(path, "utf-8"));
  1810. expect(result).toEqual({
  1811. mainModel: "zai/glm-5.1",
  1812. path,
  1813. providerId: "zai",
  1814. });
  1815. expect(updated.provider.zai.kind).toBe("anthropic");
  1816. expect(updated.provider.zai.options).toEqual({
  1817. apiKey: "zai-coding-key",
  1818. apiKeyRequired: true,
  1819. baseURL: "https://api.z.ai/api/anthropic",
  1820. timeout: 30000,
  1821. });
  1822. expect(updated.provider.zai.models["glm-5.1"]).toEqual({
  1823. contextWindow: 200000,
  1824. name: "GLM-5.1",
  1825. });
  1826. expect(updated.provider.deepseek.options.apiKey).toBe("deepseek-secret");
  1827. expect(updated.model).toEqual({
  1828. main: "zai/glm-5.1",
  1829. lite: "deepseek/deepseek-chat",
  1830. });
  1831. } finally {
  1832. await rm(dir, { force: true, recursive: true });
  1833. }
  1834. });
  1835. it("creates default Coding Plan configs when the file is missing", async () => {
  1836. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1837. const path = join(dir, "config.json");
  1838. try {
  1839. await updateCodingPlanProviderInFileConfig({
  1840. apiKey: "bigmodel-coding-key",
  1841. filePath: path,
  1842. providerId: "bigmodel",
  1843. });
  1844. const updated = JSON.parse(await readFile(path, "utf-8"));
  1845. expect(updated.model).toEqual({
  1846. main: "bigmodel/glm-5.1",
  1847. lite: "bigmodel/glm-4.7",
  1848. });
  1849. expect(updated.provider.bigmodel).toMatchObject({
  1850. kind: "anthropic",
  1851. name: "BigModel Coding Plan",
  1852. options: {
  1853. apiKey: "bigmodel-coding-key",
  1854. apiKeyRequired: true,
  1855. baseURL: "https://open.bigmodel.cn/api/anthropic",
  1856. },
  1857. });
  1858. } finally {
  1859. await rm(dir, { force: true, recursive: true });
  1860. }
  1861. });
  1862. it("loads provider map config with model metadata", async () => {
  1863. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  1864. const path = join(dir, "config.json");
  1865. try {
  1866. await writeFile(
  1867. path,
  1868. JSON.stringify({
  1869. $schema: "https://zcode.ai/schema/config-v2.json",
  1870. provider: {
  1871. "provider-auth-zai": {
  1872. kind: "openai-compatible",
  1873. name: "Z.AI",
  1874. options: {
  1875. baseURL: "https://api.z.ai/api/coding/v1",
  1876. apiKey: "secret",
  1877. includeUsage: false,
  1878. },
  1879. models: {
  1880. "glm-5": {
  1881. name: "GLM-5",
  1882. reasoning: {
  1883. enabled: true,
  1884. levels: ["low", "medium", "high"],
  1885. defaultLevel: "medium",
  1886. providerOptionsByLevel: {
  1887. high: {
  1888. reasoningEffort: "high",
  1889. },
  1890. },
  1891. },
  1892. modalities: {
  1893. input: ["text", "image", "pdf", "video"],
  1894. output: ["text"],
  1895. },
  1896. limit: {
  1897. context: 200000,
  1898. output: 8192,
  1899. },
  1900. tool_call: true,
  1901. options: {
  1902. reasoningSummary: "auto",
  1903. },
  1904. },
  1905. "glm-4.7": {
  1906. name: "GLM-4.7",
  1907. attachment: false,
  1908. reasoning: false,
  1909. limit: {
  1910. context: 128000,
  1911. output: 4096,
  1912. },
  1913. },
  1914. },
  1915. },
  1916. },
  1917. model: {
  1918. main: "provider-auth-zai/glm-5",
  1919. lite: "provider-auth-zai/glm-4.7",
  1920. },
  1921. }),
  1922. );
  1923. const result = loadFileConfig(path);
  1924. expect(result.loaded).toBe(true);
  1925. expect(result.config.model?.main).toEqual({
  1926. provider: "provider-auth-zai",
  1927. model: "glm-5",
  1928. kind: "openai-compatible",
  1929. providerName: "Z.AI",
  1930. baseURL: "https://api.z.ai/api/coding/v1",
  1931. apiKey: "secret",
  1932. includeUsage: false,
  1933. providerOptions: {
  1934. openaiCompatible: {
  1935. reasoningSummary: "auto",
  1936. },
  1937. },
  1938. });
  1939. expect(result.config.model?.lite?.model).toBe("glm-4.7");
  1940. expect(result.config.model?.lite?.baseURL).toBe("https://api.z.ai/api/coding/v1");
  1941. expect(
  1942. result.config.model?.available?.map((target) => `${target.provider}/${target.model}`),
  1943. ).toEqual(["provider-auth-zai/glm-5", "provider-auth-zai/glm-4.7"]);
  1944. expect(result.config.modelCatalog?.overrides?.["provider-auth-zai/glm-5"]).toEqual({
  1945. name: "GLM-5",
  1946. contextWindow: 200000,
  1947. maxOutputTokens: 8192,
  1948. supportsImages: true,
  1949. supportsPdf: true,
  1950. supportsVideo: true,
  1951. supportsReasoning: true,
  1952. supportsToolCall: true,
  1953. reasoning: {
  1954. enabled: true,
  1955. levels: ["low", "medium", "high"],
  1956. defaultLevel: "medium",
  1957. providerOptionsByLevel: {
  1958. high: {
  1959. openaiCompatible: {
  1960. reasoningEffort: "high",
  1961. },
  1962. },
  1963. },
  1964. },
  1965. });
  1966. expect(result.config.modelCatalog?.overrides?.["provider-auth-zai/glm-4.7"]).toEqual({
  1967. name: "GLM-4.7",
  1968. contextWindow: 128000,
  1969. maxOutputTokens: 4096,
  1970. supportsImages: false,
  1971. supportsReasoning: false,
  1972. reasoning: {
  1973. enabled: false,
  1974. },
  1975. });
  1976. } finally {
  1977. await rm(dir, { force: true, recursive: true });
  1978. }
  1979. });
  1980. it("maps top-level model maxOutputTokens into the existing model capability override", async () => {
  1981. const dir = await mkdtemp(join(tmpdir(), "zcode-config-model-output-"));
  1982. const path = join(dir, "config.json");
  1983. try {
  1984. await writeFile(
  1985. path,
  1986. JSON.stringify({
  1987. provider: {
  1988. "axec-proxy": {
  1989. kind: "anthropic",
  1990. name: "AxecProxy",
  1991. options: {
  1992. apiKey: "test-key",
  1993. baseURL: "https://api.infra.example.invalid/v1",
  1994. },
  1995. models: {
  1996. "tob-glm-5.2-cc": {
  1997. contextWindow: 1_000_000,
  1998. maxOutputTokens: 64_000,
  1999. name: "tob-glm-5.2-cc",
  2000. },
  2001. },
  2002. },
  2003. },
  2004. model: "axec-proxy/tob-glm-5.2-cc",
  2005. }),
  2006. );
  2007. const result = loadFileConfig(path);
  2008. expect(result.loaded).toBe(true);
  2009. expect(result.config.modelCatalog.overrides["axec-proxy/tob-glm-5.2-cc"]).toMatchObject({
  2010. contextWindow: 1_000_000,
  2011. maxOutputTokens: 64_000,
  2012. });
  2013. expect(result.config.model?.main).toMatchObject({
  2014. model: "tob-glm-5.2-cc",
  2015. provider: "axec-proxy",
  2016. });
  2017. } finally {
  2018. await rm(dir, { force: true, recursive: true });
  2019. }
  2020. });
  2021. it("resolves output budget with limit before options.max_tokens before top-level", async () => {
  2022. const dir = await mkdtemp(join(tmpdir(), "zcode-config-model-output-precedence-"));
  2023. const path = join(dir, "config.json");
  2024. try {
  2025. await writeFile(
  2026. path,
  2027. JSON.stringify({
  2028. provider: {
  2029. compatible: {
  2030. kind: "openai-compatible",
  2031. models: {
  2032. "limit-wins": {
  2033. limit: { output: 64_000 },
  2034. maxOutputTokens: 16_000,
  2035. options: { max_tokens: 32_000 },
  2036. },
  2037. "options-wins": {
  2038. maxOutputTokens: 16_000,
  2039. options: { max_tokens: 48_000 },
  2040. },
  2041. },
  2042. },
  2043. },
  2044. model: "compatible/limit-wins",
  2045. }),
  2046. );
  2047. const result = loadFileConfig(path);
  2048. expect(result.config.modelCatalog.overrides["compatible/limit-wins"]?.maxOutputTokens).toBe(
  2049. 64_000,
  2050. );
  2051. expect(result.config.modelCatalog.overrides["compatible/options-wins"]?.maxOutputTokens).toBe(
  2052. 48_000,
  2053. );
  2054. } finally {
  2055. await rm(dir, { force: true, recursive: true });
  2056. }
  2057. });
  2058. it("uses kind-aware Anthropic defaults for configured thinking models", async () => {
  2059. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  2060. const path = join(dir, "config.json");
  2061. try {
  2062. await writeFile(
  2063. path,
  2064. JSON.stringify({
  2065. provider: {
  2066. zai: {
  2067. kind: "anthropic",
  2068. options: {
  2069. baseURL: "https://api.z.ai/api/anthropic/v1",
  2070. apiKey: "secret",
  2071. },
  2072. models: {
  2073. "deepseek-v4-pro": {},
  2074. "glm-5.1": {},
  2075. "glm-5.2": {},
  2076. "deepseek-v4-toB-glm-5.3-route": {},
  2077. "glm-0531[1m]": {},
  2078. "glm-0606[1m]": {},
  2079. "kimi-k3": {},
  2080. k3: {},
  2081. "k3-256k": {},
  2082. },
  2083. },
  2084. },
  2085. model: "zai/glm-5.1",
  2086. }),
  2087. );
  2088. const result = loadFileConfig(path);
  2089. expect(result.loaded).toBe(true);
  2090. expect(result.config.modelCatalog?.overrides?.["zai/glm-5.1"]).toEqual({
  2091. supportsReasoning: true,
  2092. reasoning: {
  2093. enabled: true,
  2094. levels: ["enabled", "disabled"],
  2095. defaultLevel: "enabled",
  2096. providerOptionsByLevel: {
  2097. enabled: {
  2098. anthropic: {
  2099. thinking: {
  2100. budgetTokens: 1024,
  2101. type: "enabled",
  2102. },
  2103. },
  2104. },
  2105. disabled: {
  2106. anthropic: {
  2107. thinking: {
  2108. type: "disabled",
  2109. },
  2110. },
  2111. },
  2112. },
  2113. },
  2114. });
  2115. expect(result.config.modelCatalog?.overrides?.["zai/deepseek-v4-pro"]).toEqual({
  2116. supportsReasoning: true,
  2117. reasoning: {
  2118. enabled: true,
  2119. levels: ["high", "max"],
  2120. defaultLevel: "max",
  2121. providerOptionsByLevel: {
  2122. high: {
  2123. anthropic: {
  2124. effort: "high",
  2125. thinking: {
  2126. budgetTokens: 1024,
  2127. type: "enabled",
  2128. },
  2129. },
  2130. },
  2131. max: {
  2132. anthropic: {
  2133. effort: "max",
  2134. thinking: {
  2135. budgetTokens: 1024,
  2136. type: "enabled",
  2137. },
  2138. },
  2139. },
  2140. },
  2141. },
  2142. });
  2143. expect(result.config.modelCatalog?.overrides?.["zai/glm-5.2"]).toEqual({
  2144. supportsReasoning: true,
  2145. reasoning: {
  2146. enabled: true,
  2147. levels: ["max", "high", "nothink"],
  2148. defaultLevel: "max",
  2149. providerOptionsByLevel: {
  2150. high: {
  2151. anthropic: {
  2152. effort: "high",
  2153. thinking: {
  2154. budgetTokens: 16_000,
  2155. type: "enabled",
  2156. },
  2157. },
  2158. },
  2159. max: {
  2160. anthropic: {
  2161. effort: "max",
  2162. thinking: {
  2163. budgetTokens: 32_000,
  2164. type: "enabled",
  2165. },
  2166. },
  2167. },
  2168. nothink: {
  2169. anthropic: {
  2170. thinking: {
  2171. type: "disabled",
  2172. },
  2173. },
  2174. },
  2175. },
  2176. },
  2177. });
  2178. expect(result.config.modelCatalog?.overrides?.["zai/deepseek-v4-toB-glm-5.3-route"]).toEqual({
  2179. supportsReasoning: true,
  2180. reasoning: {
  2181. enabled: true,
  2182. levels: ["low", "high", "max"],
  2183. defaultLevel: "max",
  2184. providerOptionsByLevel: {
  2185. low: {
  2186. anthropic: {
  2187. effort: "low",
  2188. thinking: { budgetTokens: 8_000, type: "enabled" },
  2189. },
  2190. },
  2191. high: {
  2192. anthropic: {
  2193. effort: "high",
  2194. thinking: { budgetTokens: 16_000, type: "enabled" },
  2195. },
  2196. },
  2197. max: {
  2198. anthropic: {
  2199. effort: "max",
  2200. thinking: { budgetTokens: 32_000, type: "enabled" },
  2201. },
  2202. },
  2203. },
  2204. },
  2205. });
  2206. expect(result.config.modelCatalog?.overrides?.["zai/glm-0531[1m]"]?.reasoning).toEqual(
  2207. result.config.modelCatalog?.overrides?.["zai/glm-5.2"]?.reasoning,
  2208. );
  2209. expect(result.config.modelCatalog?.overrides?.["zai/glm-0606[1m]"]?.reasoning).toEqual(
  2210. result.config.modelCatalog?.overrides?.["zai/glm-5.2"]?.reasoning,
  2211. );
  2212. expect(result.config.modelCatalog?.overrides?.["zai/kimi-k3"]).toEqual({
  2213. supportsReasoning: true,
  2214. reasoning: {
  2215. enabled: true,
  2216. levels: ["low", "high", "max"],
  2217. defaultLevel: "max",
  2218. providerOptionsByLevel: {
  2219. low: { anthropic: { effort: "low" } },
  2220. high: { anthropic: { effort: "high" } },
  2221. max: { anthropic: { effort: "max" } },
  2222. },
  2223. },
  2224. });
  2225. expect(result.config.modelCatalog?.overrides?.["zai/k3"]).toEqual(
  2226. result.config.modelCatalog?.overrides?.["zai/kimi-k3"],
  2227. );
  2228. expect(result.config.modelCatalog?.overrides?.["zai/k3-256k"]).toEqual(
  2229. result.config.modelCatalog?.overrides?.["zai/kimi-k3"],
  2230. );
  2231. } finally {
  2232. await rm(dir, { force: true, recursive: true });
  2233. }
  2234. });
  2235. it("maps ox-alpha reasoning by configured transport without injecting unrelated defaults", async () => {
  2236. const dir = await mkdtemp(join(tmpdir(), "zcode-config-ox-alpha-"));
  2237. const path = join(dir, "config.json");
  2238. try {
  2239. await writeFile(
  2240. path,
  2241. JSON.stringify({
  2242. provider: {
  2243. "openrouter-chat": {
  2244. kind: "openai-compatible",
  2245. models: {
  2246. "vendor/ox-alpha-preview": {},
  2247. "GLM-X-PREVIEW-F": {},
  2248. "X-pReViEw-F-fReE": {},
  2249. },
  2250. },
  2251. "openrouter-anthropic": {
  2252. kind: "anthropic",
  2253. models: {
  2254. "ox-alpha": {},
  2255. "gLm-X-pReViEw-F": {},
  2256. "x-PrEvIeW-f-FrEe": {},
  2257. },
  2258. },
  2259. "openrouter-responses": {
  2260. kind: "openai",
  2261. models: { "ox-alpha": {}, "GlM-x-PrEvIeW-f": {} },
  2262. },
  2263. },
  2264. model: "openrouter-chat/vendor/ox-alpha-preview",
  2265. }),
  2266. );
  2267. const result = loadFileConfig(path);
  2268. expect(
  2269. result.config.modelCatalog.overrides["openrouter-chat/vendor/ox-alpha-preview"],
  2270. ).toEqual({
  2271. reasoning: {
  2272. defaultLevel: "max",
  2273. enabled: true,
  2274. levels: ["low", "high", "max"],
  2275. providerOptionsByLevel: {
  2276. low: { openaiCompatible: { reasoningEffort: "low" } },
  2277. high: { openaiCompatible: { reasoningEffort: "high" } },
  2278. max: { openaiCompatible: { reasoningEffort: "max" } },
  2279. },
  2280. },
  2281. supportsReasoning: true,
  2282. });
  2283. expect(result.config.modelCatalog.overrides["openrouter-anthropic/ox-alpha"]).toEqual({
  2284. reasoning: {
  2285. defaultLevel: "max",
  2286. enabled: true,
  2287. levels: ["low", "high", "max"],
  2288. providerOptionsByLevel: {
  2289. low: { anthropic: { effort: "low", thinking: { type: "adaptive" } } },
  2290. high: { anthropic: { effort: "high", thinking: { type: "adaptive" } } },
  2291. max: { anthropic: { effort: "max", thinking: { type: "adaptive" } } },
  2292. },
  2293. },
  2294. supportsReasoning: true,
  2295. });
  2296. expect(result.config.modelCatalog.overrides["openrouter-responses/ox-alpha"]).toEqual({
  2297. supportsReasoning: false,
  2298. });
  2299. expect(result.config.modelCatalog.overrides["openrouter-chat/GLM-X-PREVIEW-F"]).toEqual(
  2300. result.config.modelCatalog.overrides["openrouter-chat/vendor/ox-alpha-preview"],
  2301. );
  2302. expect(result.config.modelCatalog.overrides["openrouter-anthropic/gLm-X-pReViEw-F"]).toEqual(
  2303. result.config.modelCatalog.overrides["openrouter-anthropic/ox-alpha"],
  2304. );
  2305. expect(result.config.modelCatalog.overrides["openrouter-chat/X-pReViEw-F-fReE"]).toEqual(
  2306. result.config.modelCatalog.overrides["openrouter-chat/vendor/ox-alpha-preview"],
  2307. );
  2308. expect(result.config.modelCatalog.overrides["openrouter-anthropic/x-PrEvIeW-f-FrEe"]).toEqual(
  2309. result.config.modelCatalog.overrides["openrouter-anthropic/ox-alpha"],
  2310. );
  2311. expect(result.config.modelCatalog.overrides["openrouter-responses/GlM-x-PrEvIeW-f"]).toEqual(
  2312. result.config.modelCatalog.overrides["openrouter-responses/ox-alpha"],
  2313. );
  2314. const catalog = new ModelCatalogService({
  2315. modelsDev: false,
  2316. overrides: result.config.modelCatalog.overrides,
  2317. });
  2318. expect(catalog.getCapability("openrouter-responses", "ox-alpha")).toMatchObject({
  2319. reasoning: { enabled: false, levels: [] },
  2320. supportsReasoning: false,
  2321. });
  2322. } finally {
  2323. await rm(dir, { force: true, recursive: true });
  2324. }
  2325. });
  2326. it("rejects legacy small model config keys", async () => {
  2327. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  2328. const smallModelPath = join(dir, "small-model.json");
  2329. const modelSmallPath = join(dir, "model-small.json");
  2330. try {
  2331. await writeFile(
  2332. smallModelPath,
  2333. JSON.stringify({
  2334. provider: {
  2335. openai: {
  2336. kind: "openai",
  2337. },
  2338. },
  2339. model: "openai/gpt-4o",
  2340. small_model: "openai/gpt-4o-mini",
  2341. }),
  2342. );
  2343. await writeFile(
  2344. modelSmallPath,
  2345. JSON.stringify({
  2346. provider: {
  2347. openai: {
  2348. kind: "openai",
  2349. },
  2350. },
  2351. model: {
  2352. main: "openai/gpt-4o",
  2353. small: "openai/gpt-4o-mini",
  2354. },
  2355. }),
  2356. );
  2357. expect(loadFileConfig(smallModelPath).loaded).toBe(false);
  2358. expect(loadFileConfig(modelSmallPath).loaded).toBe(false);
  2359. } finally {
  2360. await rm(dir, { force: true, recursive: true });
  2361. }
  2362. });
  2363. it("loads selectable models from multiple configured providers", async () => {
  2364. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  2365. const path = join(dir, "config.json");
  2366. try {
  2367. await writeFile(
  2368. path,
  2369. JSON.stringify({
  2370. provider: {
  2371. zai: {
  2372. kind: "openai-compatible",
  2373. name: "Z.AI",
  2374. options: {
  2375. baseURL: "https://api.z.ai/api/coding/v1",
  2376. apiKey: "zai-secret",
  2377. },
  2378. models: {
  2379. "glm-5.1": {
  2380. name: "GLM-5.1",
  2381. },
  2382. "glm-4.7": {
  2383. name: "GLM-4.7",
  2384. },
  2385. },
  2386. },
  2387. deepseek: {
  2388. kind: "openai-compatible",
  2389. name: "DeepSeek",
  2390. options: {
  2391. apiKey: "deepseek-secret",
  2392. baseURL: "https://api.deepseek.example",
  2393. },
  2394. models: {
  2395. "deepseek-v4-pro": {
  2396. name: "DeepSeek V4 Pro",
  2397. },
  2398. },
  2399. },
  2400. },
  2401. model: "zai/glm-5.1",
  2402. }),
  2403. );
  2404. const result = loadFileConfig(path);
  2405. expect(result.loaded).toBe(true);
  2406. expect(
  2407. result.config.model?.available?.map((target) => `${target.provider}/${target.model}`),
  2408. ).toEqual(["zai/glm-5.1", "zai/glm-4.7", "deepseek/deepseek-v4-pro"]);
  2409. expect(
  2410. result.config.model?.available?.find((target) => target.provider === "deepseek"),
  2411. ).toMatchObject({
  2412. kind: "openai-compatible",
  2413. apiKey: "deepseek-secret",
  2414. baseURL: "https://api.deepseek.example",
  2415. });
  2416. } finally {
  2417. await rm(dir, { force: true, recursive: true });
  2418. }
  2419. });
  2420. it("rejects file config that fails the zod schema", async () => {
  2421. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  2422. const path = join(dir, "config.json");
  2423. try {
  2424. await writeFile(
  2425. path,
  2426. JSON.stringify({
  2427. provider: {
  2428. local: {
  2429. models: {
  2430. tiny: {
  2431. limit: {
  2432. context: "large",
  2433. },
  2434. },
  2435. },
  2436. },
  2437. },
  2438. model: "local/tiny",
  2439. }),
  2440. );
  2441. const result = loadFileConfig(path);
  2442. expect(result.loaded).toBe(false);
  2443. expect(result.config).toEqual({});
  2444. } finally {
  2445. await rm(dir, { force: true, recursive: true });
  2446. }
  2447. });
  2448. it("rejects unsupported provider kinds and npm-based provider config", async () => {
  2449. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  2450. const unsupportedKindPath = join(dir, "unsupported-kind.json");
  2451. const npmPath = join(dir, "npm-provider.json");
  2452. try {
  2453. await writeFile(
  2454. unsupportedKindPath,
  2455. JSON.stringify({
  2456. provider: {
  2457. local: {
  2458. kind: "gateway",
  2459. models: {
  2460. tiny: { name: "Tiny" },
  2461. },
  2462. },
  2463. },
  2464. model: "local/tiny",
  2465. }),
  2466. );
  2467. await writeFile(
  2468. npmPath,
  2469. JSON.stringify({
  2470. provider: {
  2471. local: {
  2472. npm: "@ai-sdk/openai-compatible",
  2473. models: {
  2474. tiny: { name: "Tiny" },
  2475. },
  2476. },
  2477. },
  2478. model: "local/tiny",
  2479. }),
  2480. );
  2481. expect(loadFileConfig(unsupportedKindPath).loaded).toBe(false);
  2482. expect(loadFileConfig(npmPath).loaded).toBe(false);
  2483. } finally {
  2484. await rm(dir, { force: true, recursive: true });
  2485. }
  2486. });
  2487. it("loads model catalog reasoning overrides from file config", async () => {
  2488. const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
  2489. const path = join(dir, "config.json");
  2490. try {
  2491. await writeFile(
  2492. path,
  2493. JSON.stringify({
  2494. modelCatalog: {
  2495. overrides: {
  2496. "openai/glm-4.7": {
  2497. supportsReasoning: true,
  2498. reasoning: {
  2499. enabled: true,
  2500. levels: ["low", "medium", "high"],
  2501. defaultLevel: "medium",
  2502. providerOptionsByLevel: {
  2503. high: {
  2504. reasoningEffort: "high",
  2505. },
  2506. },
  2507. },
  2508. },
  2509. },
  2510. },
  2511. }),
  2512. );
  2513. const result = loadFileConfig(path);
  2514. expect(result.loaded).toBe(true);
  2515. expect(result.config.modelCatalog?.overrides?.["openai/glm-4.7"]).toEqual({
  2516. supportsReasoning: true,
  2517. reasoning: {
  2518. enabled: true,
  2519. levels: ["low", "medium", "high"],
  2520. defaultLevel: "medium",
  2521. providerOptionsByLevel: {
  2522. high: {
  2523. reasoningEffort: "high",
  2524. },
  2525. },
  2526. },
  2527. });
  2528. } finally {
  2529. await rm(dir, { force: true, recursive: true });
  2530. }
  2531. });
  2532. it("removes a plugin's enabled flag and options on uninstall while preserving others", async () => {
  2533. const dir = await mkdtemp(join(tmpdir(), "zcode-config-plugin-remove-"));
  2534. const configPath = join(dir, "config.json");
  2535. try {
  2536. await writeFile(
  2537. configPath,
  2538. JSON.stringify({
  2539. plugins: {
  2540. enabledPlugins: {
  2541. "hello@market": true,
  2542. "keep@market": true,
  2543. },
  2544. options: {
  2545. "hello@market": { default_device: "iPhone 16" },
  2546. "keep@market": { foo: "bar" },
  2547. },
  2548. },
  2549. }),
  2550. );
  2551. const result = await removePluginFromFileConfig(configPath, "hello@market");
  2552. expect(result.removedEnabled).toBe(true);
  2553. expect(result.removedOptions).toBe(true);
  2554. const parsed = JSON.parse(await readFile(configPath, "utf8")) as {
  2555. plugins: {
  2556. enabledPlugins: Record<string, boolean>;
  2557. options: Record<string, unknown>;
  2558. };
  2559. };
  2560. expect(parsed.plugins.enabledPlugins).toEqual({ "keep@market": true });
  2561. expect(parsed.plugins.options).toEqual({ "keep@market": { foo: "bar" } });
  2562. } finally {
  2563. await rm(dir, { force: true, recursive: true });
  2564. }
  2565. });
  2566. it("is a no-op when removing a plugin that has no config footprint", async () => {
  2567. const dir = await mkdtemp(join(tmpdir(), "zcode-config-plugin-remove-missing-"));
  2568. const configPath = join(dir, "config.json");
  2569. try {
  2570. await writeFile(
  2571. configPath,
  2572. JSON.stringify({ plugins: { enabledPlugins: { "keep@market": true } } }),
  2573. );
  2574. const before = await readFile(configPath, "utf8");
  2575. const result = await removePluginFromFileConfig(configPath, "missing@market");
  2576. expect(result.removedEnabled).toBe(false);
  2577. expect(result.removedOptions).toBe(false);
  2578. // 未命中任何键时不应改写文件内容。
  2579. expect(await readFile(configPath, "utf8")).toBe(before);
  2580. } finally {
  2581. await rm(dir, { force: true, recursive: true });
  2582. }
  2583. });
  2584. it("adds and removes a suppressed builtin id atomically", async () => {
  2585. const dir = await mkdtemp(join(tmpdir(), "zcode-config-suppress-patch-"));
  2586. const path = join(dir, "config.json");
  2587. const id = "skill-creator@zcode-plugins-official";
  2588. const added = await addSuppressedBuiltinInFileConfig(path, id);
  2589. expect(added.suppressed).toBe(true);
  2590. expect(JSON.parse(await readFile(path, "utf8")).plugins.suppressedBuiltins).toEqual([id]);
  2591. // idempotent add
  2592. await addSuppressedBuiltinInFileConfig(path, id);
  2593. expect(JSON.parse(await readFile(path, "utf8")).plugins.suppressedBuiltins).toEqual([id]);
  2594. const removed = await removeSuppressedBuiltinInFileConfig(path, id);
  2595. expect(removed.suppressed).toBe(false);
  2596. expect(JSON.parse(await readFile(path, "utf8")).plugins.suppressedBuiltins).toEqual([]);
  2597. });
  2598. });
  2599. async function readLogEntries(logDir: string): Promise<Array<Record<string, unknown>>> {
  2600. const files = await readdir(logDir);
  2601. expect(files).toHaveLength(1);
  2602. const content = await readFile(join(logDir, files[0]!), "utf8");
  2603. return content.trim().split("\n").map(JSON.parse) as Array<Record<string, unknown>>;
  2604. }