serviceCollectionConnectionOverride.test.ts 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import type { IServerChannel } from "@zcode/rpc";
  2. import { describe, expect, it, vi } from "vitest";
  3. import { ServiceCollection } from "../src/collection.js";
  4. import { createServiceDescriptor } from "../src/descriptors.js";
  5. describe("ServiceCollection connection override", () => {
  6. it("单个 ChannelServer 可覆盖 service,不修改共享 collection", async () => {
  7. const descriptor = createServiceDescriptor<{ read(): string }>("scoped-test");
  8. const collection = new ServiceCollection().register(descriptor, {
  9. read: () => "base",
  10. });
  11. const channels = new Map<string, IServerChannel<unknown>>();
  12. const server = {
  13. registerChannel: vi.fn(
  14. (name: string, channel: IServerChannel<unknown>) => {
  15. channels.set(name, channel);
  16. },
  17. ),
  18. };
  19. collection.exposeOnChannelServer(
  20. server as never,
  21. new Map([[descriptor.channelName, { read: () => "connection-a" }]]),
  22. );
  23. expect(
  24. await channels.get(descriptor.channelName)?.call({}, "read", []),
  25. ).toBe("connection-a");
  26. expect(collection.get(descriptor).read()).toBe("base");
  27. });
  28. });