workspace-plugin-declaration.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, beforeEach, expect, it } from "vitest";
  5. import { inspectWorkspacePluginRoot } from "../src/plugins/workspace-plugin-declaration/index.js";
  6. // docs/plugins/workspace-plugin-dir-trust.md:声明摘要的覆盖范围与稳定性。
  7. let workspace: string;
  8. let plugin: string;
  9. beforeEach(async () => {
  10. workspace = await mkdtemp(join(tmpdir(), "zcode-workspace-plugin-declaration-"));
  11. plugin = join(workspace, "plugins", "demo");
  12. await mkdir(join(plugin, ".zcode-plugin"), { recursive: true });
  13. await mkdir(join(plugin, "mcp"), { recursive: true });
  14. });
  15. afterEach(() => rm(workspace, { recursive: true, force: true }));
  16. async function digest(): Promise<string | undefined> {
  17. return (await inspectWorkspacePluginRoot({ workspaceRoot: workspace, rootPath: plugin }))
  18. ?.declarationDigest;
  19. }
  20. it("covers MCP declaration files referenced through nested arrays", async () => {
  21. // !3742 CR-01:MCP 加载递归展开嵌套数组;摘要曾只看一层,嵌套引用的文件改了也不回到待信任。
  22. await writeFile(
  23. join(plugin, ".zcode-plugin", "plugin.json"),
  24. JSON.stringify({ name: "demo", mcpServers: [["./mcp/servers.json"]] }),
  25. );
  26. await writeFile(
  27. join(plugin, "mcp", "servers.json"),
  28. JSON.stringify({ mcpServers: { a: { command: "node" } } }),
  29. );
  30. const before = await digest();
  31. await writeFile(
  32. join(plugin, "mcp", "servers.json"),
  33. JSON.stringify({ mcpServers: { a: { command: "python" } } }),
  34. );
  35. expect(await digest()).not.toBe(before);
  36. });
  37. it("keeps the digest independent of the process locale", async () => {
  38. // !3742 SG-03:文件按码点排序,不能依赖 localeCompare 的语言规则。
  39. await writeFile(
  40. join(plugin, ".zcode-plugin", "plugin.json"),
  41. JSON.stringify({ name: "demo", mcpServers: ["./mcp/B.json", "./mcp/a.json"] }),
  42. );
  43. await writeFile(join(plugin, "mcp", "B.json"), "{}");
  44. await writeFile(join(plugin, "mcp", "a.json"), "{}");
  45. const original = String.prototype.localeCompare;
  46. const expected = await digest();
  47. try {
  48. // 模拟另一种 locale 的排序结果:反转比较方向。
  49. String.prototype.localeCompare = function (this: string, other: string) {
  50. return -original.call(this, other);
  51. };
  52. expect(await digest()).toBe(expected);
  53. } finally {
  54. String.prototype.localeCompare = original;
  55. }
  56. });