pluginConfigPatch.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodePluginInfo } from "@zcode/shared";
  3. import { buildPluginConfigPatch } from "../src/settings/pluginConfigPatch.js";
  4. const plugin = {
  5. id: "demo@marketplace",
  6. name: "demo",
  7. enabled: true,
  8. userConfig: {
  9. region: { type: "string" },
  10. endpoint: { type: "string" },
  11. enabled: { type: "boolean" },
  12. retries: { type: "number" },
  13. },
  14. configuredOptions: {
  15. region: "us-east-1",
  16. endpoint: "https://example.test",
  17. enabled: true,
  18. retries: 3,
  19. },
  20. } as ZCodePluginInfo;
  21. describe("plugin config patch", () => {
  22. it("没有显式 draft 时返回空 patch", () => {
  23. expect(buildPluginConfigPatch(plugin, {})).toEqual({
  24. options: {},
  25. clearOptionKeys: [],
  26. });
  27. });
  28. it("只提交显式修改的 option,不固化其他 effective 值", () => {
  29. expect(
  30. buildPluginConfigPatch(plugin, {
  31. region: "eu-west-1",
  32. }),
  33. ).toEqual({
  34. options: { region: "eu-west-1" },
  35. clearOptionKeys: [],
  36. });
  37. });
  38. it("保留显式的 false、0 和空字符串值", () => {
  39. expect(
  40. buildPluginConfigPatch(plugin, {
  41. enabled: false,
  42. retries: 0,
  43. endpoint: "",
  44. }),
  45. ).toEqual({
  46. options: { enabled: false, endpoint: "", retries: 0 },
  47. clearOptionKeys: [],
  48. });
  49. });
  50. it("恢复继承时只清除指定 option,不把 effective 值写回", () => {
  51. expect(
  52. buildPluginConfigPatch(plugin, {
  53. region: null,
  54. }),
  55. ).toEqual({
  56. options: {},
  57. clearOptionKeys: ["region"],
  58. });
  59. });
  60. });