sourceSettingsServicePortBridge.test.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // @vitest-environment jsdom
  2. import { InternalChannels } from "@zcode/shared";
  3. import { getSourceSettingsServices } from "@zcode/ui";
  4. import { describe, expect, it, vi } from "vitest";
  5. import { handleSourceSettingsServicePortMessage } from "../src/renderer/src/sourceSettingsServicePortBridge.js";
  6. function createPortEvent(params: {
  7. attachmentId: string;
  8. sourceServerId: string;
  9. port: MessagePort;
  10. }): MessageEvent {
  11. return {
  12. data: {
  13. type: InternalChannels.SourceSettingsServicePort,
  14. attachmentId: params.attachmentId,
  15. sourceServerId: params.sourceServerId,
  16. },
  17. ports: [params.port],
  18. } as unknown as MessageEvent;
  19. }
  20. describe("sourceSettingsServicePortBridge", () => {
  21. it("keeps Port B registered when Port A closes after the replacement", async () => {
  22. const sourceServerId = "source-late-close";
  23. const portA = new MessageChannel();
  24. const portB = new MessageChannel();
  25. handleSourceSettingsServicePortMessage(
  26. createPortEvent({
  27. attachmentId: "attachment-a",
  28. sourceServerId,
  29. port: portA.port1,
  30. }),
  31. );
  32. const servicesA = getSourceSettingsServices(sourceServerId);
  33. handleSourceSettingsServicePortMessage(
  34. createPortEvent({
  35. attachmentId: "attachment-b",
  36. sourceServerId,
  37. port: portB.port1,
  38. }),
  39. );
  40. const servicesB = getSourceSettingsServices(sourceServerId);
  41. portA.port2.close();
  42. await vi.waitFor(() => expect(getSourceSettingsServices(sourceServerId)).toBe(servicesB));
  43. expect(servicesB).toBeDefined();
  44. expect(servicesB).not.toBe(servicesA);
  45. portB.port2.close();
  46. await vi.waitFor(() => expect(getSourceSettingsServices(sourceServerId)).toBeUndefined());
  47. });
  48. });