remotePluginSyncRefresh.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import type { CommandsListResult, ZCodeCommand } from "@zcode/shared";
  3. import { ZCODE_AGENT_PROVIDER } from "@zcode/shared";
  4. import { refreshWorkspacePluginCapabilitiesAfterRemoteSync } from "../src/lib/remotePluginSyncRefresh.js";
  5. import { useCommandsStore } from "../src/store/commandsStore.js";
  6. import { useSkillStore } from "../src/store/skillStore.js";
  7. import { useZCodeSessionStore } from "../src/store/zcodeSessionStore.js";
  8. function makePluginCommand(
  9. overrides: Partial<Extract<ZCodeCommand, { source: "plugin" }>>,
  10. ): Extract<ZCodeCommand, { source: "plugin" }> {
  11. return {
  12. id: "plugin:team-market:hello-world:hello:/remote/commands/hello.md",
  13. name: "hello",
  14. prompt: "Say hello",
  15. content: "# hello",
  16. filePath: "/remote/commands/hello.md",
  17. source: "plugin",
  18. enabled: true,
  19. pluginName: "hello-world",
  20. pluginMarketplace: "team-market",
  21. pluginEnabled: true,
  22. scope: "global",
  23. ...overrides,
  24. };
  25. }
  26. beforeEach(() => {
  27. useZCodeSessionStore.setState((state) => ({
  28. ...state,
  29. workspaces: {},
  30. }));
  31. useCommandsStore.setState({
  32. workspacePath: null,
  33. workspaceIdentity: null,
  34. loadedWorkspacePath: null,
  35. loadedWorkspaceIdentity: null,
  36. commands: [],
  37. userCommands: [],
  38. pluginCommands: [],
  39. capability: { userScopeAvailable: true },
  40. loading: false,
  41. error: null,
  42. operatingCommandId: null,
  43. });
  44. useSkillStore.setState({
  45. workspacePath: null,
  46. workspaceIdentity: null,
  47. loadedWorkspacePath: null,
  48. loadedWorkspaceIdentity: null,
  49. provider: ZCODE_AGENT_PROVIDER,
  50. loadedProvider: null,
  51. skills: [],
  52. capability: null,
  53. loading: false,
  54. error: null,
  55. });
  56. });
  57. describe("refreshWorkspacePluginCapabilitiesAfterRemoteSync", () => {
  58. it("refreshes slash commands even when commands settings store was not loaded", async () => {
  59. const workspacePath = "/Users/dev";
  60. const workspaceIdentity = "remote:ssh:intranet.example.invalid:22:dev:/Users/dev";
  61. useZCodeSessionStore.getState().setSlashCommands(
  62. workspacePath,
  63. [
  64. {
  65. name: "compact",
  66. description: "Compact",
  67. inputHint: "/compact [instructions]",
  68. source: "builtin",
  69. },
  70. {
  71. name: "old-plugin",
  72. description: "Old plugin command",
  73. inputHint: "/old-plugin",
  74. source: "custom",
  75. },
  76. ],
  77. workspaceIdentity,
  78. );
  79. const helloCommand = makePluginCommand({
  80. name: "hello",
  81. description: "Hello plugin command",
  82. });
  83. const commandsResult: CommandsListResult = {
  84. commands: [helloCommand],
  85. userCommands: [],
  86. pluginCommands: [helloCommand],
  87. capability: { userScopeAvailable: true },
  88. };
  89. const commandsService = {
  90. list: vi.fn(async () => commandsResult),
  91. };
  92. await refreshWorkspacePluginCapabilitiesAfterRemoteSync({
  93. workspacePath,
  94. workspaceIdentity,
  95. reason: "sidebar-remote-plugin-sync",
  96. zcodeAgentService: {} as never,
  97. zcodeSessionService: { closeSession: vi.fn(async () => {}) } as never,
  98. skillsService: { list: vi.fn() } as never,
  99. commandsService: commandsService as never,
  100. mcpSyncService: {
  101. loadMcpFromUserDirectory: vi.fn(async () => ({ servers: [] })),
  102. } as never,
  103. });
  104. expect(commandsService.list).toHaveBeenCalledWith({
  105. workspacePath,
  106. workspaceIdentity,
  107. });
  108. expect(
  109. useZCodeSessionStore
  110. .getState()
  111. .getWorkspaceState(workspacePath, workspaceIdentity)
  112. .slashCommands,
  113. ).toEqual([
  114. {
  115. name: "compact",
  116. description: "Compact",
  117. inputHint: "/compact [instructions]",
  118. source: "builtin",
  119. },
  120. {
  121. name: "hello",
  122. description: "Hello plugin command",
  123. inputHint: "/hello",
  124. source: "custom",
  125. },
  126. ]);
  127. });
  128. });