workspaceCatalogContextStores.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import type { ISubagentsService } from "@zcode/services";
  3. import type { AgentSummary } from "@zcode/shared";
  4. import { getSubagentsContextKey, useSubagentsContextStore } from "@/store/subagentsContextStore.js";
  5. function createAgent(workspacePath: string, suffix = "initial"): AgentSummary {
  6. return {
  7. id: `agent:${workspacePath}:${suffix}`,
  8. name: `agent-${workspacePath.split("/").at(-1)}-${suffix}`,
  9. description: workspacePath,
  10. systemPrompt: suffix,
  11. path: `${workspacePath}/.zcode/agents/${suffix}.md`,
  12. scope: "workspace",
  13. source: "workspace",
  14. enabled: true,
  15. };
  16. }
  17. function createSubagentsService(list: ISubagentsService["list"]): ISubagentsService {
  18. return {
  19. list,
  20. getPrimaryUserAgentsDirectory: vi.fn(async () => ({ path: "/tmp/agents" })),
  21. createAgent: vi.fn(async () => ({ agent: createAgent("/tmp", "new") })),
  22. updateAgent: vi.fn(async () => ({ agent: createAgent("/tmp", "updated") })),
  23. deleteAgent: vi.fn(async () => {}),
  24. setEnabled: vi.fn(async () => {}),
  25. };
  26. }
  27. describe("workspace catalog context stores", () => {
  28. beforeEach(() => {
  29. useSubagentsContextStore.setState({ contexts: {} });
  30. });
  31. it("Subagents 同时按 workspaceIdentity 分桶,不再用全局 latest request 丢掉另一 pane", async () => {
  32. const workspacePath = "/workspace/shared-path";
  33. const identityA = "ssh://host-a/workspace/shared-path";
  34. const identityB = "ssh://host-b/workspace/shared-path";
  35. const list = vi.fn<ISubagentsService["list"]>(async ({ workspaceIdentity }) => {
  36. const suffix = workspaceIdentity === identityA ? "host-a" : "host-b";
  37. const agent = createAgent(workspacePath, suffix);
  38. return {
  39. agents: [agent],
  40. userAgents: [agent],
  41. pluginAgents: [],
  42. capability: { userScopeAvailable: true },
  43. diagnostics: [],
  44. };
  45. });
  46. const service = createSubagentsService(list);
  47. await Promise.all([
  48. useSubagentsContextStore.getState().initialize(workspacePath, "glm", service, identityA),
  49. useSubagentsContextStore.getState().initialize(workspacePath, "glm", service, identityB),
  50. ]);
  51. const keyA = getSubagentsContextKey(workspacePath, "glm", identityA);
  52. const keyB = getSubagentsContextKey(workspacePath, "glm", identityB);
  53. expect(useSubagentsContextStore.getState().contexts[keyA]?.agents[0]?.name).toBe(
  54. "agent-shared-path-host-a",
  55. );
  56. expect(useSubagentsContextStore.getState().contexts[keyB]?.agents[0]?.name).toBe(
  57. "agent-shared-path-host-b",
  58. );
  59. expect(list).toHaveBeenCalledTimes(2);
  60. });
  61. });