resourceManagerNetwork.test.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { beforeEach, expect, it, vi } from "vitest";
  2. import { channel } from "node:diagnostics_channel";
  3. import type { UtilityProcess } from "electron";
  4. const state = vi.hoisted(() => ({
  5. handlers: new Map<string, (...args: any[]) => any>(),
  6. webRequest: { onBeforeRequest: vi.fn() },
  7. partitions: vi.fn(),
  8. }));
  9. vi.mock("electron", () => ({
  10. ipcMain: {
  11. handle: (name: string, handler: (...args: any[]) => any) => state.handlers.set(name, handler),
  12. },
  13. session: { defaultSession: { webRequest: state.webRequest }, fromPartition: state.partitions },
  14. webContents: { fromId: () => null },
  15. }));
  16. beforeEach(() => {
  17. vi.resetModules();
  18. state.webRequest.onBeforeRequest.mockReset();
  19. state.partitions.mockReset().mockReturnValue({ webRequest: state.webRequest });
  20. });
  21. it("默认无捕获;窗口拥有权限及启停,新 Host 继承,关窗清理并拒绝旧批次", async () => {
  22. const before = channel("undici:request:create").hasSubscribers;
  23. const capture = await import("../src/main/resourceManagerNetwork.js");
  24. const host = { postMessage: vi.fn() } as unknown as UtilityProcess;
  25. capture.registerResourceManagerNetworkIpc();
  26. const get = state.handlers.get("zcode:get-network-capture-snapshot")!;
  27. capture.configureNetworkCaptureHost(host);
  28. expect(host.postMessage).not.toHaveBeenCalled();
  29. expect(channel("undici:request:create").hasSubscribers).toBe(before);
  30. expect(() => get({ sender: { id: 7 } })).toThrow();
  31. capture.startResourceManagerNetwork(7, () => [host]);
  32. try {
  33. expect(() => get({ sender: { id: 99 } })).toThrow();
  34. const id = get({ sender: { id: 7 } }).captureId;
  35. expect(host.postMessage).toHaveBeenLastCalledWith({
  36. type: "network-capture",
  37. control: { captureId: id },
  38. });
  39. expect(state.partitions.mock.calls.flat()).not.toContain("persist:zcode-embedded-browser");
  40. const late = { postMessage: vi.fn() } as unknown as UtilityProcess;
  41. capture.configureNetworkCaptureHost(late);
  42. expect(late.postMessage).toHaveBeenCalledWith({
  43. type: "network-capture",
  44. control: { captureId: id },
  45. });
  46. capture.ingestHostNetworkCapture(
  47. {
  48. captureId: id,
  49. records: [
  50. {
  51. timestamp: 1,
  52. pid: 99,
  53. processType: "host",
  54. method: "GET",
  55. url: "https://example.test/",
  56. },
  57. ],
  58. dropped: 0,
  59. },
  60. 42,
  61. );
  62. expect(get({ sender: { id: 7 } }).records[0].pid).toBe(42);
  63. state.handlers.get("zcode:clear-network-capture")!({ sender: { id: 7 } });
  64. const clearedId = get({ sender: { id: 7 } }).captureId;
  65. expect(clearedId).not.toBe(id);
  66. capture.ingestHostNetworkCapture({ captureId: id, records: [], dropped: 50 }, 42);
  67. expect(get({ sender: { id: 7 } })).toMatchObject({ records: [], dropped: 0 });
  68. expect(host.postMessage).toHaveBeenLastCalledWith({
  69. type: "network-capture",
  70. control: { captureId: clearedId },
  71. });
  72. capture.stopResourceManagerNetwork([host, late]);
  73. expect(host.postMessage).toHaveBeenLastCalledWith({
  74. type: "network-capture",
  75. control: { captureId: null },
  76. });
  77. expect(state.webRequest.onBeforeRequest).toHaveBeenLastCalledWith(null);
  78. expect(channel("undici:request:create").hasSubscribers).toBe(before);
  79. capture.startResourceManagerNetwork(8, () => [host]);
  80. capture.ingestHostNetworkCapture({ captureId: id, records: [], dropped: 100 }, 42);
  81. expect(get({ sender: { id: 8 } })).toMatchObject({ records: [], dropped: 0 });
  82. } finally {
  83. capture.stopResourceManagerNetwork([host]);
  84. }
  85. });