skill-reference-catalog.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // S08:skills/referenceCatalog 的 workspace/session authority 契约。
  2. // session 直读 runtime 冻结目录;draft 重新扫描 workspace;未知 session fail closed。
  3. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. import { describe, expect, it } from "vitest";
  7. import { zcodeSkillsReferenceCatalogResultSchema } from "@zcode/shared";
  8. import type { SkillLoadOutcome } from "@zcode/contracts";
  9. import { getSkillReferenceCatalog } from "../src/zcode-protocol/skill-reference-catalog.js";
  10. import type {
  11. ZCodeProtocolAgentServerContext,
  12. ZCodeProtocolSessionRecord,
  13. } from "../src/zcode-protocol/server-types.js";
  14. const SESSION_CATALOG: SkillLoadOutcome = {
  15. skills: [
  16. {
  17. name: "frozen",
  18. description: "Frozen session skill",
  19. path: "/workspace/.zcode/skills/frozen/SKILL.md",
  20. directory: "/workspace/.zcode/skills/frozen",
  21. rootPath: "/workspace/.zcode/skills",
  22. scope: "project",
  23. source: "zcode",
  24. safeToAutoLoad: true,
  25. frontmatterKeys: ["name", "description"],
  26. },
  27. ],
  28. diagnostics: [],
  29. totalDiscovered: 1,
  30. };
  31. function contextWithSession(sessionId: string): ZCodeProtocolAgentServerContext {
  32. const record = {
  33. app: {
  34. getSkillCatalog: async () => SESSION_CATALOG,
  35. },
  36. } as unknown as ZCodeProtocolSessionRecord;
  37. return {
  38. deps: { env: {} },
  39. sessions: new Map([[sessionId, record]]),
  40. } as unknown as ZCodeProtocolAgentServerContext;
  41. }
  42. describe("skills/referenceCatalog protocol", () => {
  43. it("serves the Session runtime frozen catalog", async () => {
  44. const result = await getSkillReferenceCatalog(contextWithSession("session-a"), {
  45. workspace: { workspacePath: "/workspace", workspaceKey: "/workspace" },
  46. sessionId: "session-a",
  47. });
  48. expect(result).toEqual({
  49. authority: "session",
  50. skills: [
  51. {
  52. id: "glm:workspace:/workspace/.zcode/skills/frozen/SKILL.md",
  53. name: "frozen",
  54. description: "Frozen session skill",
  55. path: "/workspace/.zcode/skills/frozen/SKILL.md",
  56. scope: "workspace",
  57. enabled: true,
  58. },
  59. ],
  60. });
  61. expect(() => zcodeSkillsReferenceCatalogResultSchema.parse(result)).not.toThrow();
  62. });
  63. it("fails closed for an unknown Session instead of falling back to workspace discovery", async () => {
  64. await expect(
  65. getSkillReferenceCatalog(contextWithSession("session-a"), {
  66. workspace: { workspacePath: "/workspace", workspaceKey: "/workspace" },
  67. sessionId: "missing",
  68. }),
  69. ).rejects.toThrow(/Session is not active/);
  70. });
  71. it("rescans the current workspace catalog for a draft request", async () => {
  72. const root = await mkdtemp(join(tmpdir(), "zcode-skill-reference-workspace-"));
  73. const skillDirectory = join(root, ".zcode", "skills", "manual-added");
  74. await mkdir(skillDirectory, { recursive: true });
  75. await writeFile(
  76. join(skillDirectory, "SKILL.md"),
  77. ["---", "name: manual-added", "description: Added outside the app.", "---"].join("\n"),
  78. );
  79. try {
  80. const result = await getSkillReferenceCatalog(
  81. {
  82. deps: {
  83. env: {
  84. HOME: root,
  85. ZCODE_STORAGE_DIR: join(root, "cli"),
  86. },
  87. },
  88. sessions: new Map(),
  89. } as unknown as ZCodeProtocolAgentServerContext,
  90. { workspace: { workspacePath: root, workspaceKey: root } },
  91. );
  92. expect(result.authority).toBe("workspace");
  93. expect(result.skills).toContainEqual(
  94. expect.objectContaining({
  95. name: "manual-added",
  96. description: "Added outside the app.",
  97. scope: "workspace",
  98. enabled: true,
  99. }),
  100. );
  101. expect(() => zcodeSkillsReferenceCatalogResultSchema.parse(result)).not.toThrow();
  102. } finally {
  103. await rm(root, { force: true, recursive: true });
  104. }
  105. });
  106. });