skills-command.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { run } from "../src/run.js";
  4. import type { RunContext } from "@zcode/shared-types";
  5. type CapturedWriteStream = NodeJS.WriteStream & {
  6. output: () => string;
  7. };
  8. const createWriteStream = (): CapturedWriteStream => {
  9. let output = "";
  10. return {
  11. output: () => output,
  12. write: (
  13. chunk: string | Uint8Array,
  14. encodingOrCallback?: BufferEncoding | ((err?: Error | null) => void),
  15. callback?: (err?: Error | null) => void,
  16. ): boolean => {
  17. output += typeof chunk === "string" ? chunk : chunk.toString();
  18. const writeCallback =
  19. typeof encodingOrCallback === "function" ? encodingOrCallback : callback;
  20. writeCallback?.();
  21. return true;
  22. },
  23. } as CapturedWriteStream;
  24. };
  25. const createContext = (
  26. argv: string[],
  27. ): RunContext & {
  28. stderr: CapturedWriteStream;
  29. stdout: CapturedWriteStream;
  30. } => ({
  31. argv,
  32. stderr: createWriteStream(),
  33. stdin: {
  34. isTTY: false,
  35. } as NodeJS.ReadStream,
  36. stdout: createWriteStream(),
  37. });
  38. test("inspects a skill in human format", async () => {
  39. const ctx = createContext(["skills", "inspect", "demo", "--verbose"]);
  40. const exitCode = await run(ctx, {
  41. cwd: () => "/workspace/project",
  42. inspectSkill: async (options) => {
  43. assert.equal(options.workingDirectory, "/workspace/project");
  44. assert.equal(options.name, "demo");
  45. return createSkillInspection();
  46. },
  47. });
  48. const output = ctx.stdout.output();
  49. assert.equal(exitCode, 0);
  50. assert.match(output, /Skill: demo/);
  51. assert.match(output, /scope\/source: project\/zcode/);
  52. assert.match(output, /description: Use for demo tasks\./);
  53. assert.match(output, /safeToAutoLoad: no/);
  54. assert.match(output, /size: 34\/34 bytes/);
  55. assert.match(output, /# Demo Skill/);
  56. assert.match(output, /Diagnostics \(1\)/);
  57. assert.equal(ctx.stderr.output(), "");
  58. });
  59. test("inspects a skill in JSON format", async () => {
  60. const ctx = createContext(["skills", "inspect", "demo", "--json"]);
  61. const exitCode = await run(ctx, {
  62. cwd: () => "/workspace/project",
  63. inspectSkill: async () => createSkillInspection(),
  64. });
  65. const payload = JSON.parse(ctx.stdout.output()) as {
  66. cwd: string;
  67. skill: {
  68. content: string;
  69. metadata: { name: string; safeToAutoLoad: boolean };
  70. truncated: boolean;
  71. };
  72. };
  73. assert.equal(exitCode, 0);
  74. assert.equal(payload.cwd, "/workspace/project");
  75. assert.equal(payload.skill.metadata.name, "demo");
  76. assert.equal(payload.skill.metadata.safeToAutoLoad, false);
  77. assert.equal(payload.skill.content, "# Demo Skill\nFollow this workflow.");
  78. assert.equal(payload.skill.truncated, false);
  79. assert.equal(ctx.stderr.output(), "");
  80. });
  81. test("reports missing skill inspection errors", async () => {
  82. const ctx = createContext(["skills", "inspect", "missing"]);
  83. const exitCode = await run(ctx, {
  84. inspectSkill: async () => {
  85. throw new Error("Skill not found: missing");
  86. },
  87. });
  88. assert.equal(exitCode, 1);
  89. assert.match(ctx.stderr.output(), /Error: Skill not found: missing/);
  90. assert.equal(ctx.stdout.output(), "");
  91. });
  92. test("rejects inspect without a skill name", async () => {
  93. const ctx = createContext(["skills", "inspect"]);
  94. const exitCode = await run(ctx, {
  95. inspectSkill: async () => {
  96. throw new Error("should not inspect");
  97. },
  98. });
  99. assert.equal(exitCode, 1);
  100. assert.match(ctx.stderr.output(), /Usage: zcode skills \[list\|inspect <name>\]/);
  101. assert.equal(ctx.stdout.output(), "");
  102. });
  103. function createSkillInspection() {
  104. return {
  105. diagnostics: [
  106. {
  107. code: "skill_scan_failed",
  108. message: "Failed to scan skill directory: demo",
  109. path: "/workspace/project/.zcode/skills/demo/SKILL.md",
  110. severity: "warning",
  111. skillName: "demo",
  112. },
  113. ],
  114. skill: {
  115. baseDirectory: "/workspace/project/.zcode/skills/demo",
  116. bytesRead: 34,
  117. content: "# Demo Skill\nFollow this workflow.",
  118. metadata: {
  119. description: "Use for demo tasks.",
  120. directory: "/workspace/project/.zcode/skills/demo",
  121. frontmatterKeys: ["name", "description", "version"],
  122. name: "demo",
  123. path: "/workspace/project/.zcode/skills/demo/SKILL.md",
  124. rootPath: "/workspace/project/.zcode/skills",
  125. safeToAutoLoad: false,
  126. scope: "project",
  127. source: "zcode",
  128. },
  129. sizeBytes: 34,
  130. truncated: false,
  131. },
  132. } as never;
  133. }