dev-workflow.integration.mjs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import assert from "node:assert/strict";
  2. import { execFile } from "node:child_process";
  3. import { mkdtemp, readFile, writeFile, rm, realpath } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join, resolve } from "node:path";
  6. import { promisify } from "node:util";
  7. import { createPlugin } from "../skills/plugin-creator/scripts/create-basic-plugin.mjs";
  8. import { upsertDevMarketplace } from "../skills/plugin-creator/scripts/upsert-dev-marketplace.mjs";
  9. // 明确传入真实 CLI 入口;隔离全部注册与安装写入,不能修改开发者的个人插件库。
  10. if (!process.argv[2])
  11. throw new Error("Usage: node dev-workflow.integration.mjs <built-zcode-cli-js>");
  12. const cli = resolve(process.argv[2]);
  13. const root = await mkdtemp(join(tmpdir(), "zcode-dev-workflow-"));
  14. const execute = promisify(execFile);
  15. const env = { ...process.env, ZCODE_STORAGE_DIR: join(root, "storage") };
  16. async function command(...args) {
  17. const { stdout } = await execute(process.execPath, [cli, "plugins", ...args, "--json"], {
  18. cwd: root,
  19. env,
  20. });
  21. return JSON.parse(stdout);
  22. }
  23. try {
  24. const source = await createPlugin({
  25. name: "dev-demo",
  26. parentPath: join(root, "plugins"),
  27. components: ["skills"],
  28. });
  29. const skillPath = join("skills", "dev-demo", "SKILL.md");
  30. await writeFile(
  31. join(source, skillPath),
  32. "---\nname: dev-demo\ndescription: Show the development workflow marker\n---\nReturn DEV_WORKFLOW_ONE.\n",
  33. );
  34. const dev = await upsertDevMarketplace({
  35. pluginPath: source,
  36. displayName: "Development Demo",
  37. nameZh: "开发演示",
  38. descriptionZh: "开发闭环测试",
  39. });
  40. await command("validate", source);
  41. await command("validate", dev.marketplacePath);
  42. const added = await command("marketplace", "add", dev.marketplaceRoot);
  43. assert.equal(added.id, dev.marketplaceId);
  44. const markets = await command("marketplace", "list");
  45. const registered = markets.find((m) => m.id === dev.marketplaceId);
  46. assert.equal(registered.source.source, "directory");
  47. assert.equal(await realpath(registered.source.path), dev.marketplaceRoot);
  48. assert.equal(
  49. (await command("list", "--available")).available.find((p) => p.id === dev.pluginId)?.version,
  50. "0.1.0",
  51. );
  52. assert.equal((await command("install", dev.pluginId)).ok, true);
  53. let installed = (await command("list")).find((p) => p.id === dev.pluginId);
  54. assert.equal(installed.enabled, true);
  55. assert.equal(installed.skillCount, 1);
  56. assert.notEqual(await realpath(installed.rootPath), await realpath(source));
  57. assert.match(await readFile(join(installed.rootPath, skillPath), "utf8"), /DEV_WORKFLOW_ONE/);
  58. // 编辑源码不会直接修改已安装副本,随后通过原生刷新/更新完成第二轮交付。
  59. const manifestPath = join(source, ".zcode-plugin", "plugin.json");
  60. const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
  61. manifest.version = "0.1.1";
  62. await writeFile(manifestPath, JSON.stringify(manifest));
  63. await writeFile(
  64. join(source, skillPath),
  65. "---\nname: dev-demo\ndescription: Show the updated development workflow marker\n---\nReturn DEV_WORKFLOW_TWO.\n",
  66. );
  67. assert.match(await readFile(join(installed.rootPath, skillPath), "utf8"), /DEV_WORKFLOW_ONE/);
  68. await command("disable", dev.pluginId);
  69. assert.equal((await upsertDevMarketplace({ pluginPath: source })).pluginId, dev.pluginId);
  70. assert.equal((await command("marketplace", "update", dev.marketplaceId)).ok, true);
  71. assert.equal((await command("update", dev.pluginId)).ok, true);
  72. installed = (await command("list")).find((p) => p.id === dev.pluginId);
  73. assert.equal(installed.version, "0.1.1");
  74. assert.equal(installed.enabled, false);
  75. assert.match(await readFile(join(installed.rootPath, skillPath), "utf8"), /DEV_WORKFLOW_TWO/);
  76. await command("validate", installed.rootPath);
  77. // 包装器的附加检查只读;避免通过默认 CLI 环境污染用户目录。
  78. const { preflightPlugin } = await import("../skills/plugin-creator/scripts/validate-plugin.mjs");
  79. assert.deepEqual(await preflightPlugin(installed.rootPath), []);
  80. console.log(
  81. JSON.stringify({
  82. result: "passed",
  83. marketplace: dev.marketplaceId,
  84. created: "0.1.0",
  85. updated: installed.version,
  86. preservesDisabled: true,
  87. sourceAndInstallSeparated: true,
  88. }),
  89. );
  90. } finally {
  91. await rm(root, { recursive: true, force: true });
  92. }