| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895 |
- import { describe, expect, it } from "vitest";
- import { createConfig, updateUiLocaleInFileConfig } from "../src/config/index.js";
- import { parseEnvConfig } from "../src/config/env-config.adapter.js";
- import {
- addSuppressedBuiltinInFileConfig,
- loadFileConfig,
- removePluginFromFileConfig,
- removeSuppressedBuiltinInFileConfig,
- updatePluginEnabledInFileConfig,
- updatePluginOptionsInFileConfig,
- } from "../src/config/file-config.adapter.js";
- import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- describe("config adapters", () => {
- it("migrates the legacy official CUA plugin id to the canonical id", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-cua-identity-"));
- const configPath = join(dir, "config.json");
- try {
- await writeFile(
- configPath,
- JSON.stringify({
- plugins: {
- enabledPlugins: {
- "zcode-cua@zcode-plugins-official": true,
- "computer-use@zcode-plugins-official": false,
- },
- suppressedBuiltins: [
- "zcode-cua@zcode-plugins-official",
- "computer-use@zcode-plugins-official",
- ],
- options: {
- "zcode-cua@zcode-plugins-official": { apiKey: "legacy-key" },
- "computer-use@zcode-plugins-official": { apiKey: "canonical-key" },
- },
- },
- }),
- );
- const loaded = loadFileConfig(configPath);
- expect(loaded.config.plugins?.enabledPlugins).toEqual({
- "computer-use@zcode-plugins-official": false,
- });
- expect(loaded.config.plugins?.suppressedBuiltins).toEqual([
- "computer-use@zcode-plugins-official",
- ]);
- expect(loaded.config.plugins?.options).toEqual({
- "computer-use@zcode-plugins-official": { apiKey: "canonical-key" },
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("persists the canonical CUA plugin id while loading legacy config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-cua-writeback-"));
- const path = join(dir, "config.json");
- await writeFile(
- path,
- JSON.stringify({
- plugins: {
- enabledPlugins: { "zcode-cua@zcode-plugins-official": true },
- suppressedBuiltins: ["zcode-cua@zcode-plugins-official"],
- options: {
- "zcode-cua@zcode-plugins-official": { apiKey: "legacy-key" },
- },
- },
- }),
- );
- loadFileConfig(path);
- expect(JSON.parse(await readFile(path, "utf8"))).toEqual({
- plugins: {
- enabledPlugins: { "computer-use@zcode-plugins-official": true },
- suppressedBuiltins: ["computer-use@zcode-plugins-official"],
- options: {
- "computer-use@zcode-plugins-official": { apiKey: "legacy-key" },
- },
- },
- });
- await rm(dir, { recursive: true, force: true });
- });
- it("writes canonical CUA ids when removing legacy config entries", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-cua-write-migration-"));
- const configPath = join(dir, "config.json");
- try {
- await writeFile(
- configPath,
- JSON.stringify({
- plugins: {
- enabledPlugins: { "zcode-cua@zcode-plugins-official": true },
- suppressedBuiltins: ["zcode-cua@zcode-plugins-official"],
- },
- }),
- );
- expect(
- (
- await removeSuppressedBuiltinInFileConfig(
- configPath,
- "computer-use@zcode-plugins-official",
- )
- ).suppressed,
- ).toBe(false);
- expect(
- (await removePluginFromFileConfig(configPath, "computer-use@zcode-plugins-official"))
- .removedEnabled,
- ).toBe(true);
- expect(JSON.parse(await readFile(configPath, "utf8"))).toEqual({
- plugins: { enabledPlugins: {}, suppressedBuiltins: [], options: {} },
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("clears only explicitly selected Plugin option keys", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-plugin-option-clear-"));
- const configPath = join(dir, "config.json");
- const pluginId = "secret-plugin@fixture";
- try {
- await writeFile(
- configPath,
- JSON.stringify({
- plugins: {
- enabledPlugins: { [pluginId]: true },
- options: {
- [pluginId]: {
- token: "secret",
- region: "us-east-1",
- },
- },
- },
- }),
- );
- await updatePluginOptionsInFileConfig(configPath, pluginId, { region: "eu-west-1" }, [
- "token",
- ]);
- expect(JSON.parse(await readFile(configPath, "utf8"))).toEqual({
- plugins: {
- enabledPlugins: { [pluginId]: true },
- options: {
- [pluginId]: {
- region: "eu-west-1",
- },
- },
- },
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("deep merges Workspace Plugin enablement and options over User config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-workspace-plugins-"));
- const userPath = join(dir, "user.json");
- const workspacePath = join(dir, ".zcode", "config.json");
- try {
- await mkdir(join(dir, ".zcode"), { recursive: true });
- await writeFile(
- userPath,
- JSON.stringify({
- plugins: {
- dirs: [join(dir, "user-plugin")],
- enabledPlugins: {
- "workspace-plugin@fixture": true,
- "user-only@fixture": true,
- },
- options: {
- "workspace-plugin@fixture": {
- inherited: "user",
- overridden: "user",
- },
- },
- },
- }),
- );
- await writeFile(
- workspacePath,
- JSON.stringify({
- plugins: {
- enabledPlugins: {
- "workspace-plugin@fixture": false,
- },
- options: {
- "workspace-plugin@fixture": {
- overridden: "workspace",
- workspaceOnly: true,
- },
- },
- },
- }),
- );
- const result = createConfig({
- env: {},
- userConfigPath: userPath,
- projectConfigPath: workspacePath,
- workingDirectory: dir,
- });
- expect(result.config.plugins.enabledPlugins).toEqual({
- "workspace-plugin@fixture": false,
- "user-only@fixture": true,
- });
- expect(result.config.plugins.options).toEqual({
- "workspace-plugin@fixture": {
- inherited: "user",
- overridden: "workspace",
- workspaceOnly: true,
- },
- });
- expect(result.sources.project.paths).toEqual([workspacePath]);
- expect(result.sources.plugins.enabled).toEqual({
- "user-only@fixture": "user",
- "workspace-plugin@fixture": "workspace",
- });
- expect(result.sources.plugins.dirs).toEqual({
- user: [join(dir, "user-plugin")],
- workspace: [],
- });
- expect(result.config.plugins.dirs).toEqual([join(dir, "user-plugin")]);
- expect(result.sources.plugins.options).toEqual({
- "workspace-plugin@fixture": {
- inherited: "user",
- overridden: "workspace",
- workspaceOnly: "workspace",
- },
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("keeps Marketplace declarations in the User config layer", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-workspace-marketplaces-"));
- const userPath = join(dir, "user.json");
- const workspacePath = join(dir, "workspace.json");
- try {
- await writeFile(
- userPath,
- JSON.stringify({
- plugins: {
- extraKnownMarketplaces: {
- "shared-market": {
- source: { source: "github", repo: "example/user-market" },
- },
- "user-market": {
- source: { source: "url", url: "https://example.test/user-market.json" },
- },
- },
- },
- }),
- );
- await writeFile(
- workspacePath,
- JSON.stringify({
- plugins: {
- extraKnownMarketplaces: {
- "shared-market": {
- source: { source: "directory", path: "./workspace-market" },
- },
- "workspace-market": {
- source: { source: "file", path: "./workspace-market.json" },
- },
- },
- },
- }),
- );
- const result = createConfig({
- env: {},
- userConfigPath: userPath,
- projectConfigPath: workspacePath,
- });
- expect(result.config.plugins.extraKnownMarketplaces).toEqual({
- "shared-market": {
- source: { source: "github", repo: "example/user-market" },
- },
- "user-market": {
- source: { source: "url", url: "https://example.test/user-market.json" },
- },
- });
- expect(result.sources.plugins.marketplaces).toEqual({
- "shared-market": "user",
- "user-market": "user",
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("parses the supported ZCODE environment surface", () => {
- const config = parseEnvConfig({
- ZCODE_STORAGE_DIR: "/tmp/zcode",
- ZCODE_HTTP_PROXY: "http://127.0.0.1:7890",
- ZCODE_NO_PROXY: "localhost,127.0.0.1",
- ZCODE_AGENT_CA_CERT: "/tmp/zcode-ca.pem",
- ZCODE_HTTP_TIMEOUT: "1234",
- ZCODE_LOCALE: "zh-CN",
- ZCODE_LOG_FORMAT: "json",
- });
- expect(config.model).toBeUndefined();
- expect(config.storage?.dir).toBe("/tmp/zcode");
- expect(config.network?.httpProxy).toBe("http://127.0.0.1:7890");
- expect(config.network?.noProxy).toBe("localhost,127.0.0.1");
- expect(config.network?.caCertFile).toBe("/tmp/zcode-ca.pem");
- expect(config.network?.timeout).toBe(1234);
- expect(config.ui).toBeUndefined();
- expect(config.logging?.format).toBe("json");
- expect(config.logging?.level).toBeUndefined();
- });
- it("loads the supported memory config fields", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-memory-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- memory: {
- use: false,
- },
- }),
- );
- const loaded = loadFileConfig(path);
- const merged = createConfig({ env: {}, userConfigPath: path });
- expect(loaded.loaded).toBe(true);
- expect(loaded.config.memory).toEqual({
- use: false,
- });
- expect(merged.config.memory).toEqual({
- use: false,
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("parses session database path environment overrides", () => {
- expect(
- parseEnvConfig({
- ZCODE_SESSION_DB_PATH: "/tmp/zcode/db/db.sqlite",
- }).storage?.sessionDbPath,
- ).toBe("/tmp/zcode/db/db.sqlite");
- expect(
- parseEnvConfig({
- ZCODE_SESSION_DB: "/tmp/zcode/legacy.sqlite",
- }).storage?.sessionDbPath,
- ).toBe("/tmp/zcode/legacy.sqlite");
- });
- it("loads no-proxy rules from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- network: {
- httpProxy: "http://127.0.0.1:7890",
- noProxy: "localhost,127.0.0.1",
- caCertFile: "/tmp/zcode-ca.pem",
- },
- }),
- );
- const merged = createConfig({ env: {}, userConfigPath: path });
- expect(merged.config.network.httpProxy).toBe("http://127.0.0.1:7890");
- expect(merged.config.network.noProxy).toBe("localhost,127.0.0.1");
- expect(merged.config.network.caCertFile).toBe("/tmp/zcode-ca.pem");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("ignores undeclared ZCODE environment switches", () => {
- const config = parseEnvConfig({
- ZCODE_LOG_LEVEL: "debug",
- ZCODE_UNDECLARED_FLAG: "1",
- });
- expect(config).toEqual({});
- });
- it("loads yolo mode from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(path, JSON.stringify({ permission: { mode: "yolo" } }));
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(true);
- expect(result.config.permission?.mode).toBe("yolo");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("loads edit mode from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(path, JSON.stringify({ permission: { mode: "edit" } }));
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(true);
- expect(result.config.permission?.mode).toBe("edit");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("loads skill/command enable overrides from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- skill: { "/abs/skills/demo/SKILL.md": { enable: false } },
- command: { "/abs/commands/demo.md": { enable: false } },
- }),
- );
- const result = loadFileConfig(path);
- const merged = createConfig({ env: {}, userConfigPath: path });
- expect(result.loaded).toBe(true);
- // JSON 的 skill/command 字段映射到 runtime 的 skillOverrides/commandOverrides
- expect(result.config.skillOverrides?.["/abs/skills/demo/SKILL.md"]?.enable).toBe(false);
- expect(result.config.commandOverrides?.["/abs/commands/demo.md"]?.enable).toBe(false);
- expect(merged.config.skillOverrides["/abs/skills/demo/SKILL.md"]?.enable).toBe(false);
- expect(merged.config.commandOverrides["/abs/commands/demo.md"]?.enable).toBe(false);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("parses plugins.suppressedBuiltins as a string array", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-suppressed-"));
- const path = join(dir, "config.json");
- await writeFile(
- path,
- JSON.stringify({ plugins: { suppressedBuiltins: ["skill-creator@zcode-plugins-official"] } }),
- );
- const loaded = loadFileConfig(path);
- expect(loaded.config.plugins.suppressedBuiltins).toEqual([
- "skill-creator@zcode-plugins-official",
- ]);
- });
- it("maps skills path enable entries to skill overrides", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- skills: {
- enabled: true,
- roots: ["/abs/skills"],
- "/abs/skills/disabled/SKILL.md": { enable: false },
- "/abs/skills/enabled/SKILL.md": { enable: true },
- },
- }),
- );
- const result = loadFileConfig(path);
- const merged = createConfig({ env: {}, userConfigPath: path });
- expect(result.loaded).toBe(true);
- expect(result.config.skills).toEqual({
- enabled: true,
- roots: ["/abs/skills"],
- });
- expect(result.config.skillOverrides?.["/abs/skills/disabled/SKILL.md"]?.enable).toBe(false);
- expect(result.config.skillOverrides?.["/abs/skills/enabled/SKILL.md"]?.enable).toBe(true);
- expect(merged.config.skillOverrides["/abs/skills/disabled/SKILL.md"]?.enable).toBe(false);
- expect(merged.config.skillOverrides["/abs/skills/enabled/SKILL.md"]?.enable).toBe(true);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("loads top-level skills config from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- skills: {
- enabled: false,
- includeInstructions: false,
- metadataBudget: 1024,
- roots: ["/abs/skills"],
- },
- }),
- );
- const result = loadFileConfig(path);
- const merged = createConfig({ env: {}, userConfigPath: path });
- expect(result.loaded).toBe(true);
- expect(result.config.skills).toEqual({
- enabled: false,
- includeInstructions: false,
- metadataBudget: 1024,
- roots: ["/abs/skills"],
- });
- expect(merged.config.skills.enabled).toBe(false);
- expect(merged.config.skills.includeInstructions).toBe(false);
- expect(merged.config.skills.metadataBudget).toBe(1024);
- expect(merged.config.skills.roots).toEqual(["/abs/skills"]);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("loads UI locale from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(path, JSON.stringify({ ui: { locale: "zh-CN" } }));
- const result = loadFileConfig(path);
- const merged = createConfig({ env: {}, userConfigPath: path });
- expect(result.loaded).toBe(true);
- expect(result.config.ui?.locale).toBe("zh-CN");
- expect(merged.config.ui.locale).toBe("zh-CN");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("loads auto UI locale from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(path, JSON.stringify({ ui: { locale: "auto" } }));
- const result = loadFileConfig(path);
- const merged = createConfig({ env: {}, userConfigPath: path });
- expect(result.loaded).toBe(true);
- expect(result.config.ui?.locale).toBe("auto");
- expect(merged.config.ui.locale).toBe("auto");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("loads UI theme from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(path, JSON.stringify({ ui: { theme: "light" } }));
- const result = loadFileConfig(path);
- const merged = createConfig({ env: {}, userConfigPath: path });
- expect(result.loaded).toBe(true);
- expect(result.config.ui?.theme).toBe("light");
- expect(merged.config.ui.theme).toBe("light");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("rejects unsupported UI theme values from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(path, JSON.stringify({ ui: { theme: "sepia" } }));
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(false);
- expect(result.config.ui).toBeUndefined();
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("patches UI locale while preserving unrelated config fields", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- model: {
- main: "openai/gpt-5.1",
- },
- permission: {
- mode: "plan",
- },
- ui: {
- density: "compact",
- locale: "en-US",
- },
- }),
- );
- const result = await updateUiLocaleInFileConfig(path, "zh-CN");
- const written = JSON.parse(await readFile(path, "utf-8"));
- expect(result).toEqual({
- locale: "zh-CN",
- path,
- });
- expect(written).toMatchObject({
- model: {
- main: "openai/gpt-5.1",
- },
- permission: {
- mode: "plan",
- },
- ui: {
- density: "compact",
- locale: "zh-CN",
- },
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("creates config files when patching UI locale", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "nested", "config.json");
- try {
- await updateUiLocaleInFileConfig(path, "auto");
- const written = JSON.parse(await readFile(path, "utf-8"));
- expect(written).toEqual({
- ui: {
- locale: "auto",
- },
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("rejects unsupported UI locale values from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(path, JSON.stringify({ ui: { locale: "fr-FR" } }));
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(false);
- expect(result.config.ui).toBeUndefined();
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("loads MCP server config from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- features: {
- mcp: true,
- },
- mcp: {
- servers: {
- local: {
- type: "stdio",
- command: "node",
- args: ["server.js"],
- cwd: ".",
- env: {
- EXAMPLE: "1",
- },
- },
- remote: {
- type: "http",
- url: "https://example.com/mcp",
- headers: {
- Authorization: "Bearer token",
- },
- oauth: {
- type: "client_credentials",
- clientId: "zcode-client",
- clientSecret: "secret",
- scope: "mcp:tools",
- },
- timeoutMs: 30000,
- },
- events: {
- type: "sse",
- url: "https://example.com/sse",
- oauth: {
- type: "authorization_code",
- clientName: "ZCode",
- redirectPath: "/oauth/callback/mcp/figma",
- scope: "mcp:connect",
- },
- enabled: false,
- },
- },
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(true);
- expect(result.config.features?.mcp).toBe(true);
- expect(result.config.mcp?.servers?.local).toMatchObject({
- type: "stdio",
- command: "node",
- args: ["server.js"],
- });
- expect(result.config.mcp?.servers?.remote).toMatchObject({
- type: "http",
- url: "https://example.com/mcp",
- oauth: {
- type: "client_credentials",
- clientId: "zcode-client",
- clientSecret: "secret",
- scope: "mcp:tools",
- },
- timeoutMs: 30000,
- });
- expect(result.config.mcp?.servers?.events).toMatchObject({
- type: "sse",
- oauth: {
- type: "authorization_code",
- clientName: "ZCode",
- redirectPath: "/oauth/callback/mcp/figma",
- scope: "mcp:connect",
- },
- enabled: false,
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("keeps an MCP server disabled when legacy enable:false coexists with enabled:true", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- mcp: {
- servers: {
- // 桌面端「停用」写入 enable:false,而外部导入/手工编辑的同一条配置里
- // 已经带着 enabled:true。禁用意图必须胜出。
- playwright: {
- type: "stdio",
- command: "npx",
- args: ["-y", "@playwright/mcp@latest"],
- enable: false,
- enabled: true,
- },
- },
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.config.mcp?.servers?.playwright?.enabled).toBe(false);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("keeps an MCP server disabled when enabled:false coexists with legacy enable:true", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- mcp: {
- servers: {
- playwright: {
- type: "stdio",
- command: "npx",
- args: ["-y", "@playwright/mcp@latest"],
- enable: true,
- enabled: false,
- },
- },
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.config.mcp?.servers?.playwright?.enabled).toBe(false);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("keeps an MCP server enabled when both enable and enabled are true", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- mcp: {
- servers: {
- playwright: {
- type: "stdio",
- command: "npx",
- args: ["-y", "@playwright/mcp@latest"],
- enable: true,
- enabled: true,
- },
- },
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.config.mcp?.servers?.playwright?.enabled).toBe(true);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("preserves fresh HTTP MCP config without writing implicit OAuth", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- mcp: {
- servers: {
- notion: {
- type: "http",
- url: "https://mcp.notion.com/mcp",
- timeoutMs: 30000,
- },
- },
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(true);
- expect(result.config.mcp?.servers?.notion).toMatchObject({
- type: "http",
- url: "https://mcp.notion.com/mcp",
- timeoutMs: 30000,
- });
- expect(result.config.mcp?.servers?.notion?.oauth).toBeUndefined();
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("skips invalid MCP OAuth config without dropping unrelated config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- mcp: {
- servers: {
- broken: {
- type: "http",
- url: "https://example.com/mcp",
- oauth: {
- type: "client_credentials",
- clientId: "zcode-client",
- },
- },
- },
- },
- plugins: {
- enabled: true,
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(true);
- expect(result.config.mcp?.servers).not.toHaveProperty("broken");
- expect(result.config.plugins?.enabled).toBe(true);
- expect(result.diagnostics).toEqual([
- expect.objectContaining({
- code: "config_mcp_server_invalid",
- path: "mcp.servers.broken",
- severity: "warning",
- }),
- ]);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("normalizes app-managed MCP config aliases from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- mcp: {
- servers: {
- "figma-dev-mode-mcp-server": {
- type: "stdio",
- command: "npx",
- args: ["-y", "figma-developer-mcp"],
- enable: false,
- },
- "chrome-devtools": {
- command: "npx",
- args: ["-y", "chrome-devtools-mcp@latest"],
- },
- node_repl: {
- command: "/opt/tools/node_repl",
- args: [],
- timeout: 30,
- startup_timeout_sec: 120,
- },
- },
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(true);
- expect(result.config.mcp?.servers?.["figma-dev-mode-mcp-server"]).toMatchObject({
- type: "stdio",
- command: "npx",
- enabled: false,
- });
- expect(result.config.mcp?.servers?.["figma-dev-mode-mcp-server"]).not.toHaveProperty(
- "enable",
- );
- expect(result.config.mcp?.servers?.["chrome-devtools"]).toMatchObject({
- type: "stdio",
- command: "npx",
- args: ["-y", "chrome-devtools-mcp@latest"],
- });
- expect(result.config.mcp?.servers?.node_repl).toMatchObject({
- type: "stdio",
- command: "/opt/tools/node_repl",
- });
- expect(result.config.mcp?.servers?.node_repl).not.toHaveProperty("timeout");
- expect(result.config.mcp?.servers?.node_repl).not.toHaveProperty("startup_timeout_sec");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("normalizes legacy MCP http_headers to runtime headers", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- mcp: {
- servers: {
- web_search_prime: {
- type: "http",
- url: "https://bigmodel.example.test/mcp",
- http_headers: {
- Authorization: "Bearer token",
- },
- },
- },
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(true);
- expect(result.config.mcp?.servers?.web_search_prime).toMatchObject({
- type: "http",
- headers: {
- Authorization: "Bearer token",
- },
- });
- expect(result.config.mcp?.servers?.web_search_prime).not.toHaveProperty("http_headers");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("normalizes legacy MCP remote/environment fields without dropping plugins config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- mcp: {
- servers: {
- "GLM-Image-MCP": {
- enabled: true,
- environment: {
- Z_AI_API_KEY: "test-key",
- Z_AI_MODE: "ZHIPU",
- },
- command: "npx",
- args: ["-y", "@z_ai/mcp-server"],
- type: "stdio",
- },
- "GLM-WebReader-MCP": {
- enabled: true,
- headers: {
- Authorization: "Bearer token",
- },
- type: "remote",
- url: "https://open.bigmodel.cn/api/mcp/web_reader/mcp",
- },
- },
- },
- plugins: {
- enabledPlugins: {
- "superpowers@zcode-plugins-official": true,
- "skill-creator@zcode-plugins-official": false,
- },
- },
- }),
- );
- const result = loadFileConfig(path);
- const merged = createConfig({ env: {}, userConfigPath: path });
- expect(result.loaded).toBe(true);
- expect(result.diagnostics).toEqual([]);
- expect(result.config.mcp?.servers?.["GLM-Image-MCP"]).toMatchObject({
- type: "stdio",
- env: {
- Z_AI_API_KEY: "test-key",
- Z_AI_MODE: "ZHIPU",
- },
- });
- expect(result.config.mcp?.servers?.["GLM-Image-MCP"]).not.toHaveProperty("environment");
- expect(result.config.mcp?.servers?.["GLM-WebReader-MCP"]).toMatchObject({
- headers: {
- Authorization: "Bearer token",
- },
- type: "http",
- url: "https://open.bigmodel.cn/api/mcp/web_reader/mcp",
- });
- expect(merged.config.plugins.enabledPlugins).toMatchObject({
- "skill-creator@zcode-plugins-official": false,
- "superpowers@zcode-plugins-official": true,
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("keeps non-MCP config when one MCP server is invalid", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const logDir = join(dir, "log");
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- mcp: {
- servers: {
- valid: {
- type: "remote",
- url: "https://example.com/mcp",
- },
- broken: {
- type: "stdio",
- args: ["server.js"],
- },
- },
- },
- plugins: {
- enabledPlugins: {
- "android-emulator@zcode-plugins-official": true,
- },
- },
- }),
- );
- const result = loadFileConfig(path);
- const merged = createConfig({
- env: { ZCODE_LOG_DIR: logDir },
- userConfigPath: path,
- });
- expect(result.loaded).toBe(true);
- expect(result.config.mcp?.servers?.valid).toMatchObject({
- type: "http",
- url: "https://example.com/mcp",
- });
- expect(result.config.mcp?.servers).not.toHaveProperty("broken");
- expect(
- result.config.plugins?.enabledPlugins?.["android-emulator@zcode-plugins-official"],
- ).toBe(true);
- expect(result.diagnostics).toEqual([
- expect.objectContaining({
- code: "config_mcp_server_invalid",
- path: "mcp.servers.broken",
- severity: "warning",
- }),
- ]);
- expect(merged.sources.user.diagnostics).toEqual(result.diagnostics);
- expect(merged.config.plugins.enabledPlugins["android-emulator@zcode-plugins-official"]).toBe(
- true,
- );
- await expect(readLogEntries(logDir)).resolves.toEqual([
- expect.objectContaining({
- event: "config.mcp_server.skipped",
- level: "warn",
- message: "MCP server config skipped",
- module: "adapters.config",
- context: expect.objectContaining({
- configPath: path,
- configScope: "user",
- diagnosticCode: "config_mcp_server_invalid",
- diagnosticPath: "mcp.servers.broken",
- }),
- }),
- ]);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("reports invalid discovered project config diagnostics", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
- const logDir = join(root, "log");
- const projectConfigPath = join(root, "zcode.json");
- try {
- await mkdir(join(root, ".git"));
- await writeFile(
- projectConfigPath,
- JSON.stringify({
- ui: {
- theme: "sepia",
- },
- }),
- );
- const result = createConfig({
- env: { ZCODE_LOG_DIR: logDir },
- skipUserConfig: true,
- workingDirectory: root,
- });
- expect(result.sources.project.loaded).toBe(false);
- expect(result.sources.project.diagnostics).toEqual([
- expect.objectContaining({
- code: "config_file_invalid",
- message: expect.stringContaining("ui.theme: Invalid option: expected one of"),
- severity: "error",
- }),
- ]);
- await expect(readLogEntries(logDir)).resolves.toEqual([
- expect.objectContaining({
- event: "config.file.invalid",
- level: "warn",
- message: "Config file failed to load",
- module: "adapters.config",
- context: expect.objectContaining({
- configPath: projectConfigPath,
- configScope: "project",
- diagnosticCode: "config_file_invalid",
- }),
- }),
- ]);
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("discovers project MCP config from the worktree root", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
- const cwd = join(root, "packages", "app");
- try {
- await mkdir(join(root, ".git"));
- await mkdir(cwd, { recursive: true });
- await writeFile(
- join(root, "zcode.json"),
- JSON.stringify({
- mcp: {
- servers: {
- project: {
- type: "stdio",
- command: "node",
- args: ["server.js"],
- cwd: "tools",
- },
- },
- },
- }),
- );
- const result = createConfig({
- env: {},
- skipUserConfig: true,
- workingDirectory: cwd,
- });
- expect(result.sources.project.loaded).toBe(true);
- expect(result.sources.project.paths).toEqual([join(root, "zcode.json")]);
- expect(result.sources.project.mcpServerNames).toEqual(["project"]);
- expect(result.config.mcp.servers.project).toMatchObject({
- type: "stdio",
- command: "node",
- cwd: join(root, "tools"),
- });
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("merges project configs from root to cwd with nearest config winning", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
- const cwd = join(root, "packages", "app");
- const cwdConfigDir = join(cwd, ".zcode");
- try {
- await mkdir(join(root, ".git"));
- await mkdir(cwdConfigDir, { recursive: true });
- await writeFile(
- join(root, "zcode.json"),
- JSON.stringify({
- mcp: {
- servers: {
- project: {
- type: "stdio",
- command: "root-server",
- },
- },
- },
- }),
- );
- await writeFile(
- join(cwdConfigDir, "config.json"),
- JSON.stringify({
- mcp: {
- servers: {
- project: {
- type: "stdio",
- command: "nearest-server",
- },
- },
- },
- }),
- );
- const result = createConfig({
- env: {},
- skipUserConfig: true,
- workingDirectory: cwd,
- });
- expect(result.sources.project.paths).toEqual([
- join(root, "zcode.json"),
- join(cwdConfigDir, "config.json"),
- ]);
- expect(result.config.mcp.servers.project).toMatchObject({
- type: "stdio",
- command: "nearest-server",
- cwd,
- });
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("lets user MCP servers override same-name project MCP servers", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
- const userConfigPath = join(root, "home", "config.json");
- try {
- await mkdir(join(root, ".git"));
- await mkdir(join(root, "home"), { recursive: true });
- await writeFile(
- join(root, "zcode.json"),
- JSON.stringify({
- mcp: {
- servers: {
- projectOnly: {
- type: "stdio",
- command: "project-only-server",
- },
- shared: {
- type: "stdio",
- command: "project-server",
- },
- },
- },
- }),
- );
- await writeFile(
- userConfigPath,
- JSON.stringify({
- mcp: {
- servers: {
- shared: {
- type: "stdio",
- command: "user-server",
- },
- userOnly: {
- type: "stdio",
- command: "user-only-server",
- },
- },
- },
- }),
- );
- const result = createConfig({
- env: {},
- userConfigPath,
- workingDirectory: root,
- });
- expect(result.config.mcp.servers.shared).toMatchObject({
- type: "stdio",
- command: "user-server",
- });
- expect(result.config.mcp.servers.projectOnly).toMatchObject({
- command: "project-only-server",
- });
- expect(result.config.mcp.servers.userOnly).toMatchObject({
- command: "user-only-server",
- });
- expect(result.sources.user.mcpServerNames).toEqual(["shared", "userOnly"]);
- expect(result.sources.project.mcpServerNames.toSorted()).toEqual(["projectOnly", "shared"]);
- expect(result.sources.mcp.serverSources).toMatchObject({
- projectOnly: "project",
- shared: "user",
- userOnly: "user",
- });
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("does not scan project config above the worktree root", async () => {
- const parent = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
- const root = join(parent, "repo");
- const cwd = join(root, "packages", "app");
- try {
- await mkdir(join(root, ".git"), { recursive: true });
- await mkdir(cwd, { recursive: true });
- await writeFile(
- join(parent, "zcode.json"),
- JSON.stringify({
- mcp: {
- servers: {
- parent: {
- type: "stdio",
- command: "parent-server",
- },
- },
- },
- }),
- );
- const result = createConfig({
- env: {},
- skipUserConfig: true,
- workingDirectory: cwd,
- });
- expect(result.sources.project.loaded).toBe(false);
- expect(result.config.mcp.servers.parent).toBeUndefined();
- } finally {
- await rm(parent, { force: true, recursive: true });
- }
- });
- it("keeps CLI feature overrides above project MCP config", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-project-config-"));
- try {
- await mkdir(join(root, ".git"));
- await writeFile(
- join(root, "zcode.json"),
- JSON.stringify({
- features: {
- mcp: true,
- },
- }),
- );
- const result = createConfig({
- cliOverrides: {
- features: {
- mcp: false,
- },
- },
- env: {},
- skipUserConfig: true,
- workingDirectory: root,
- });
- expect(result.config.features.mcp).toBe(false);
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("loads process hook config from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- hooks: {
- enabled: true,
- timeoutMs: 5000,
- maxOutputBytes: 8192,
- events: {
- PreToolUse: [
- {
- matcher: "Bash",
- hooks: [
- {
- type: "process",
- command: "node",
- args: ["hooks/pre-tool.js"],
- enabled: false,
- customFlag: "keep",
- },
- ],
- },
- ],
- },
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(true);
- expect(result.config.hooks).toMatchObject({
- enabled: true,
- timeoutMs: 5000,
- maxOutputBytes: 8192,
- });
- expect(result.config.hooks?.events?.PreToolUse?.[0]?.hooks[0]).toMatchObject({
- type: "process",
- command: "node",
- args: ["hooks/pre-tool.js"],
- enabled: false,
- customFlag: "keep",
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("drops hooks from discovered project config files because project hooks are untrusted code", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-project-hooks-"));
- try {
- await mkdir(join(root, ".git"));
- await mkdir(join(root, ".zcode"), { recursive: true });
- await writeFile(
- join(root, "zcode.json"),
- JSON.stringify({
- hooks: {
- enabled: true,
- events: {
- SessionStart: [
- {
- matcher: "startup",
- hooks: [{ type: "command", command: "echo root-project-hook" }],
- },
- ],
- },
- },
- }),
- );
- await writeFile(
- join(root, ".zcode", "config.json"),
- JSON.stringify({
- hooks: {
- enabled: true,
- events: {
- SessionStart: [
- {
- matcher: "startup",
- hooks: [{ type: "command", command: "echo project-hook" }],
- },
- ],
- },
- },
- }),
- );
- const result = createConfig({
- env: {},
- skipUserConfig: true,
- workingDirectory: root,
- });
- expect(result.sources.project.loaded).toBe(true);
- expect(
- result.sources.project.diagnostics.filter(
- (diagnostic) => diagnostic.code === "config_project_hooks_pending_trust",
- ),
- ).toHaveLength(2);
- for (const configPath of [join(root, "zcode.json"), join(root, ".zcode", "config.json")]) {
- expect(result.sources.project.diagnostics).toContainEqual(
- expect.objectContaining({
- code: "config_project_hooks_pending_trust",
- filePath: configPath,
- message: "Project hooks are pending workspace trust and remain blocked",
- path: "hooks",
- severity: "warning",
- }),
- );
- }
- expect(result.config.hooks.events.SessionStart).toBeUndefined();
- expect(result.config.hooks.enabled).toBe(false);
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("keeps user hooks while dropping untrusted project hooks", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-user-project-hooks-"));
- const userConfigPath = join(root, "user-config.json");
- try {
- await mkdir(join(root, ".git"));
- await mkdir(join(root, ".zcode"), { recursive: true });
- await writeFile(
- userConfigPath,
- JSON.stringify({
- hooks: {
- enabled: true,
- events: {
- SessionStart: [
- {
- matcher: "startup",
- hooks: [{ type: "command", command: "echo user-hook" }],
- },
- ],
- },
- },
- }),
- );
- await writeFile(
- join(root, ".zcode", "config.json"),
- JSON.stringify({
- hooks: {
- enabled: true,
- events: {
- UserPromptSubmit: [
- {
- matcher: "*",
- hooks: [{ type: "command", command: "echo project-hook" }],
- },
- ],
- },
- },
- }),
- );
- const result = createConfig({
- env: {},
- userConfigPath,
- workingDirectory: root,
- });
- expect(result.sources.user.loaded).toBe(true);
- expect(result.sources.project.loaded).toBe(true);
- expect(result.sources.project.diagnostics).toContainEqual(
- expect.objectContaining({
- code: "config_project_hooks_pending_trust",
- path: "hooks",
- severity: "warning",
- }),
- );
- expect(result.config.hooks.enabled).toBe(true);
- expect(result.config.hooks.events.SessionStart?.[0]?.hooks[0]).toMatchObject({
- type: "command",
- command: "echo user-hook",
- source: { kind: "user", path: userConfigPath },
- });
- expect(result.config.hooks.events.UserPromptSubmit).toBeUndefined();
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("loads model stream idle timeout config from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- modelStream: {
- idleTimeoutMs: 120000,
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(true);
- expect(result.config.modelStream).toMatchObject({
- idleTimeoutMs: 120000,
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("merges model stream idle timeout into runtime config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- modelStream: {
- idleTimeoutMs: 240000,
- },
- }),
- );
- const result = createConfig({ env: {}, userConfigPath: path });
- expect(result.config.modelStream.idleTimeoutMs).toBe(240000);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("loads model anomaly guard config from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- modelAnomalyGuard: {
- repeatedToolCallWarningThreshold: 4,
- maxBudgetWarningsPerTurn: 2,
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(true);
- expect(result.config.modelAnomalyGuard).toMatchObject({
- repeatedToolCallWarningThreshold: 4,
- maxBudgetWarningsPerTurn: 2,
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("rejects unknown hook events from file config", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-"));
- const path = join(dir, "config.json");
- try {
- await writeFile(
- path,
- JSON.stringify({
- hooks: {
- enabled: true,
- events: {
- BeforeEverything: [
- {
- hooks: [
- {
- type: "process",
- command: "node",
- },
- ],
- },
- ],
- },
- },
- }),
- );
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(false);
- expect(result.config).toEqual({});
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- // Provider/Model 配置已归 Registry 管理;通用配置忽略退场字段,不得重新推导模型选择。
- it.each([
- { model: { provider: "example", model: "example-model", apiKey: "fixture" } },
- { model: { main: "example/main", lite: "example/lite" } },
- { model: "example/main", modelSmall: "example/small", smallModel: "example/small" },
- {
- provider: {
- example: {
- kind: "unsupported",
- npm: "fixture",
- models: { tiny: { limit: { context: "invalid" } } },
- },
- },
- },
- { modelCatalog: { overrides: { "example/model": { supportsReasoning: true } } } },
- ])(
- "ignores retired provider config while preserving active configuration: %j",
- async (legacy) => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-retired-model-"));
- try {
- const path = join(dir, "config.json");
- await writeFile(path, JSON.stringify({ ...legacy, features: { memory: false } }));
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(true);
- expect(result.config).toMatchObject({ features: { memory: false } });
- for (const key of ["model", "provider", "modelCatalog", "modelSmall", "smallModel"])
- expect(result.config).not.toHaveProperty(key);
- } finally {
- await rm(dir, { recursive: true, force: true });
- }
- },
- );
- it("rejects invalid active configuration", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-invalid-"));
- try {
- const path = join(dir, "config.json");
- await writeFile(path, JSON.stringify({ network: { timeout: "invalid" } }));
- const result = loadFileConfig(path);
- expect(result.loaded).toBe(false);
- expect(result.config).toEqual({});
- } finally {
- await rm(dir, { recursive: true, force: true });
- }
- });
- it("removes a plugin's enabled flag and options on uninstall while preserving others", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-plugin-remove-"));
- const configPath = join(dir, "config.json");
- try {
- await writeFile(
- configPath,
- JSON.stringify({
- plugins: {
- enabledPlugins: {
- "hello@market": true,
- "keep@market": true,
- },
- options: {
- "hello@market": { default_device: "iPhone 16" },
- "keep@market": { foo: "bar" },
- },
- },
- }),
- );
- const result = await removePluginFromFileConfig(configPath, "hello@market");
- expect(result.removedEnabled).toBe(true);
- expect(result.removedOptions).toBe(true);
- const parsed = JSON.parse(await readFile(configPath, "utf8")) as {
- plugins: {
- enabledPlugins: Record<string, boolean>;
- options: Record<string, unknown>;
- };
- };
- expect(parsed.plugins.enabledPlugins).toEqual({ "keep@market": true });
- expect(parsed.plugins.options).toEqual({ "keep@market": { foo: "bar" } });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("is a no-op when removing a plugin that has no config footprint", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-plugin-remove-missing-"));
- const configPath = join(dir, "config.json");
- try {
- await writeFile(
- configPath,
- JSON.stringify({ plugins: { enabledPlugins: { "keep@market": true } } }),
- );
- const before = await readFile(configPath, "utf8");
- const result = await removePluginFromFileConfig(configPath, "missing@market");
- expect(result.removedEnabled).toBe(false);
- expect(result.removedOptions).toBe(false);
- // 未命中任何键时不应改写文件内容。
- expect(await readFile(configPath, "utf8")).toBe(before);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("adds and removes a suppressed builtin id atomically", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-config-suppress-patch-"));
- const path = join(dir, "config.json");
- const id = "skill-creator@zcode-plugins-official";
- const added = await addSuppressedBuiltinInFileConfig(path, id);
- expect(added.suppressed).toBe(true);
- expect(JSON.parse(await readFile(path, "utf8")).plugins.suppressedBuiltins).toEqual([id]);
- // idempotent add
- await addSuppressedBuiltinInFileConfig(path, id);
- expect(JSON.parse(await readFile(path, "utf8")).plugins.suppressedBuiltins).toEqual([id]);
- const removed = await removeSuppressedBuiltinInFileConfig(path, id);
- expect(removed.suppressed).toBe(false);
- expect(JSON.parse(await readFile(path, "utf8")).plugins.suppressedBuiltins).toEqual([]);
- });
- });
- async function readLogEntries(logDir: string): Promise<Array<Record<string, unknown>>> {
- const files = await readdir(logDir);
- expect(files).toHaveLength(1);
- const content = await readFile(join(logDir, files[0]!), "utf8");
- return content.trim().split("\n").map(JSON.parse) as Array<Record<string, unknown>>;
- }
|