remoteServiceAccess.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { IChannel, IChannelClient } from "@zcode/rpc";
  3. import { ISourceServerManagementService } from "@zcode/services";
  4. import { RemoteServiceAccess } from "../src/remoteServiceAccess.js";
  5. const channel: IChannel = {
  6. call: vi.fn(),
  7. listen: vi.fn(),
  8. };
  9. describe("RemoteServiceAccess", () => {
  10. it("公开配置经独立频道 RPC 读取", async () => {
  11. const call = vi.fn(async () => ({ pluginStoreOrder: null }));
  12. const requested: string[] = [];
  13. const services = new RemoteServiceAccess({
  14. getChannel: (name) => {
  15. requested.push(name);
  16. return { call, listen: vi.fn() } as IChannel;
  17. },
  18. });
  19. expect(await services.clientConfigService.getSnapshot({ forceRefresh: true })).toEqual({
  20. pluginStoreOrder: null,
  21. });
  22. expect(requested).toContain("client-config");
  23. expect(call.mock.calls[0]?.slice(0, 2)).toEqual(["getSnapshot", [{ forceRefresh: true }]]);
  24. });
  25. });
  26. describe("RemoteServiceAccess optional channels", () => {
  27. it("does not construct a Source management proxy for scopes that do not publish it", () => {
  28. const getChannel = vi.fn(() => channel);
  29. const services = new RemoteServiceAccess({ getChannel } as IChannelClient, {
  30. sourceServerManagementService: false,
  31. });
  32. expect(services.sourceServerManagementService).toBeUndefined();
  33. expect(getChannel).not.toHaveBeenCalledWith(ISourceServerManagementService.channelName);
  34. });
  35. });
  36. it("MRO02:窗口目录只由显式能力创建,原工作区 project 频道保留", async () => {
  37. const calls: string[] = [];
  38. const getChannel = vi.fn((name: string) => ({
  39. call: async () => {
  40. calls.push(name);
  41. return name === "window-project-changes" ? { version: 1 } : { projects: [], tasks: [] };
  42. },
  43. listen: vi.fn(),
  44. }));
  45. const legacy = new RemoteServiceAccess({ getChannel });
  46. expect(legacy.windowProjectDirectory).toBeUndefined();
  47. expect(getChannel).not.toHaveBeenCalledWith("window-project");
  48. const services = new RemoteServiceAccess({ getChannel }, { windowProjectDirectory: true });
  49. expect(services.windowProjectDirectory).toBeDefined();
  50. await services.windowProjectDirectory!.changesService.getCapabilities();
  51. await services.windowProjectDirectory!.projectService.readTree();
  52. await services.projectService.readTree();
  53. expect(calls).toEqual(["window-project-changes", "window-project", "project"]);
  54. });