skillStoreRefresh.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import type { ISkillsService } from "@zcode/services";
  3. import {
  4. ZCODE_AGENT_PROVIDER,
  5. type SkillSummary,
  6. type SkillsListResult,
  7. } from "@zcode/shared";
  8. import { refreshSharedSkillStoreForWorkspace } from "@/lib/skillStoreRefresh.js";
  9. import { useSkillStore } from "@/store/skillStore.js";
  10. function createSkill(name: string): SkillSummary {
  11. return {
  12. id: `user:${name}`,
  13. name,
  14. description: `${name} description`,
  15. body: "",
  16. path: `/root/.zcode/skills/${name}/SKILL.md`,
  17. scope: "user",
  18. enabled: true,
  19. };
  20. }
  21. function createSkillsService(result: SkillsListResult): ISkillsService {
  22. return {
  23. list: vi.fn(async () => result),
  24. setEnabled: vi.fn(async () => undefined),
  25. buildPromptContext: vi.fn(async () => ({
  26. prompt: "",
  27. activatedSkillNames: [],
  28. })),
  29. copyToCommon: vi.fn(async () => ({ newPath: "" })),
  30. removeFromCommon: vi.fn(async () => undefined),
  31. deleteSkill: vi.fn(async () => undefined),
  32. };
  33. }
  34. function setLoadedSkillStore(params: {
  35. workspacePath: string;
  36. workspaceIdentity: string | null;
  37. skills?: SkillSummary[];
  38. }): void {
  39. useSkillStore.setState({
  40. workspacePath: params.workspacePath,
  41. workspaceIdentity: params.workspaceIdentity,
  42. loadedWorkspacePath: params.workspacePath,
  43. loadedWorkspaceIdentity: params.workspaceIdentity,
  44. provider: ZCODE_AGENT_PROVIDER,
  45. loadedProvider: ZCODE_AGENT_PROVIDER,
  46. skills: params.skills ?? [createSkill("old-skill")],
  47. capability: { userScopeAvailable: true },
  48. loading: false,
  49. error: null,
  50. });
  51. }
  52. describe("refreshSharedSkillStoreForWorkspace", () => {
  53. afterEach(() => {
  54. useSkillStore.setState({
  55. workspacePath: null,
  56. workspaceIdentity: null,
  57. loadedWorkspacePath: null,
  58. loadedWorkspaceIdentity: null,
  59. provider: ZCODE_AGENT_PROVIDER,
  60. loadedProvider: null,
  61. skills: [],
  62. capability: null,
  63. loading: false,
  64. error: null,
  65. });
  66. });
  67. it("refreshes the shared skill cache for the loaded remote workspace identity", async () => {
  68. const workspacePath = "/root/project";
  69. const workspaceIdentity = "ssh://root@localhost:2223/root/project";
  70. const newSkill = createSkill("synced-skill");
  71. const skillsService = createSkillsService({
  72. skills: [newSkill],
  73. capability: { userScopeAvailable: true },
  74. diagnostics: [],
  75. });
  76. setLoadedSkillStore({
  77. workspacePath,
  78. workspaceIdentity,
  79. });
  80. await refreshSharedSkillStoreForWorkspace({
  81. workspacePath,
  82. workspaceIdentity: ` ${workspaceIdentity} `,
  83. skillsService,
  84. });
  85. expect(skillsService.list).toHaveBeenCalledWith({
  86. workspacePath,
  87. workspaceIdentity,
  88. provider: ZCODE_AGENT_PROVIDER,
  89. });
  90. expect(useSkillStore.getState().skills).toEqual([newSkill]);
  91. });
  92. it("does not refresh when the loaded workspace identity belongs to another remote", async () => {
  93. const workspacePath = "/root/project";
  94. const oldSkill = createSkill("old-skill");
  95. const skillsService = createSkillsService({
  96. skills: [createSkill("other-remote-skill")],
  97. capability: { userScopeAvailable: true },
  98. diagnostics: [],
  99. });
  100. setLoadedSkillStore({
  101. workspacePath,
  102. workspaceIdentity: "ssh://root@host-a/root/project",
  103. skills: [oldSkill],
  104. });
  105. await refreshSharedSkillStoreForWorkspace({
  106. workspacePath,
  107. workspaceIdentity: "ssh://root@host-b/root/project",
  108. skillsService,
  109. });
  110. expect(skillsService.list).not.toHaveBeenCalled();
  111. expect(useSkillStore.getState().skills).toEqual([oldSkill]);
  112. });
  113. });