|
| 1 | +import { EventHub } from "../source/event_hub"; |
| 2 | +import { vi, describe, expect } from "vitest"; |
| 3 | + |
| 4 | +describe("EventHub", () => { |
| 5 | + let eventHub; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + eventHub = new EventHub("", "", ""); |
| 9 | + eventHub._socketIo = { |
| 10 | + on: vi.fn(), |
| 11 | + emit: vi.fn(), |
| 12 | + socket: { connected: true }, |
| 13 | + }; |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + vi.clearAllMocks(); |
| 18 | + }); |
| 19 | + |
| 20 | + test("should correctly add subscribers", () => { |
| 21 | + const topicSubscriberCallback = vi.fn(); |
| 22 | + const wildcardSubscriberCallback = vi.fn(); |
| 23 | + |
| 24 | + const topicSubscriberId = eventHub.subscribe( |
| 25 | + "topic=ftrack.update", |
| 26 | + topicSubscriberCallback |
| 27 | + ); |
| 28 | + const wildcardSubscriberId = eventHub.subscribe( |
| 29 | + "topic=ftrack.*", |
| 30 | + wildcardSubscriberCallback |
| 31 | + ); |
| 32 | + |
| 33 | + expect(typeof topicSubscriberId).toBe("string"); |
| 34 | + expect(eventHub.getSubscriberByIdentifier(topicSubscriberId).callback).toBe( |
| 35 | + topicSubscriberCallback |
| 36 | + ); |
| 37 | + expect(typeof wildcardSubscriberId).toBe("string"); |
| 38 | + expect( |
| 39 | + eventHub.getSubscriberByIdentifier(wildcardSubscriberId).callback |
| 40 | + ).toBe(wildcardSubscriberCallback); |
| 41 | + }); |
| 42 | + |
| 43 | + test("should not subscribe without a valid topic", () => { |
| 44 | + const callback = vi.fn(); |
| 45 | + expect(() => eventHub.subscribe("", callback)).toThrow(); |
| 46 | + expect(() => eventHub.subscribe(null, callback)).toThrow(); |
| 47 | + expect(() => eventHub.subscribe(undefined, callback)).toThrow(); |
| 48 | + expect(() => eventHub.subscribe("*", callback)).toThrow(); |
| 49 | + expect(() => |
| 50 | + eventHub.subscribe("anything-except-topic", callback) |
| 51 | + ).toThrow(); |
| 52 | + }); |
| 53 | + |
| 54 | + test("should not subscribe without a valid callback", () => { |
| 55 | + expect(() => eventHub.subscribe("topic=ftrack.update", null)).toThrow(); |
| 56 | + expect(() => |
| 57 | + eventHub.subscribe("topic=ftrack.update", "not a function") |
| 58 | + ).toThrow(); |
| 59 | + expect(() => eventHub.subscribe("topic=ftrack.update", {})).toThrow(); |
| 60 | + }); |
| 61 | + |
| 62 | + test("should correctly unsubscribe", () => { |
| 63 | + const topicSubscriberCallback = vi.fn(); |
| 64 | + const wildcardSubscriberCallback = vi.fn(); |
| 65 | + const topicSubscriberId = eventHub.subscribe( |
| 66 | + "topic=ftrack.update", |
| 67 | + topicSubscriberCallback |
| 68 | + ); |
| 69 | + const wildcardSubscriberId = eventHub.subscribe( |
| 70 | + "topic=ftrack.*", |
| 71 | + wildcardSubscriberCallback |
| 72 | + ); |
| 73 | + |
| 74 | + const topicUnsubscribeSuccess = eventHub.unsubscribe(topicSubscriberId); |
| 75 | + const wildcardUnsubscribeSuccess = |
| 76 | + eventHub.unsubscribe(wildcardSubscriberId); |
| 77 | + |
| 78 | + expect(topicUnsubscribeSuccess).toBe(true); |
| 79 | + expect(wildcardUnsubscribeSuccess).toBe(true); |
| 80 | + expect(eventHub.getSubscriberByIdentifier(topicSubscriberId)).toBe(null); |
| 81 | + expect(eventHub.getSubscriberByIdentifier(wildcardSubscriberId)).toBe(null); |
| 82 | + }); |
| 83 | + |
| 84 | + test("should not unsubscribe with an invalid ID", () => { |
| 85 | + const invalid1 = eventHub.unsubscribe("invalid ID"); |
| 86 | + const invalid2 = eventHub.unsubscribe(null); |
| 87 | + const invalid3 = eventHub.unsubscribe(undefined); |
| 88 | + |
| 89 | + expect(invalid1).toBe(false); |
| 90 | + expect(invalid2).toBe(false); |
| 91 | + expect(invalid3).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + test("should handle topic events", () => { |
| 95 | + const callback = vi.fn(); |
| 96 | + const testEvent = { topic: "ftrack.test", data: {} }; |
| 97 | + eventHub.subscribe("topic=ftrack.*", callback); |
| 98 | + |
| 99 | + eventHub._handle(testEvent); |
| 100 | + |
| 101 | + expect(callback).toHaveBeenCalledWith(testEvent); |
| 102 | + }); |
| 103 | + |
| 104 | + test("should handle wildcard events", () => { |
| 105 | + const callback = vi.fn(); |
| 106 | + const testEvent = { topic: "ftrack.test", data: {} }; |
| 107 | + eventHub.subscribe("topic=ftrack.test", callback); |
| 108 | + |
| 109 | + eventHub._handle(testEvent); |
| 110 | + |
| 111 | + expect(callback).toHaveBeenCalledWith(testEvent); |
| 112 | + }); |
| 113 | + |
| 114 | + test("should handle events with unexpected topics", () => { |
| 115 | + const callback = vi.fn(); |
| 116 | + eventHub.subscribe("topic=ftrack.update", callback); |
| 117 | + const testEvent = { topic: null, data: {} }; |
| 118 | + eventHub._handle(testEvent); |
| 119 | + expect(callback).not.toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + |
| 122 | + test("should handle events with unexpected topics", () => { |
| 123 | + const callback = vi.fn(); |
| 124 | + eventHub.subscribe("topic=*", callback); |
| 125 | + const testEvent = { topic: null, data: {} }; |
| 126 | + const testEvent2 = { topic: { topic: "topic" }, data: {} }; |
| 127 | + const testEvent3 = { topic: ["topic"], data: {} }; |
| 128 | + |
| 129 | + eventHub._handle(testEvent); |
| 130 | + eventHub._handle(testEvent2); |
| 131 | + eventHub._handle(testEvent3); |
| 132 | + |
| 133 | + expect(callback).not.toHaveBeenCalled(); |
| 134 | + }); |
| 135 | + |
| 136 | + test("should handle events without data", () => { |
| 137 | + const callback = vi.fn(); |
| 138 | + eventHub.subscribe("topic=ftrack.update", callback); |
| 139 | + const testEvent = { topic: "ftrack.update" }; |
| 140 | + eventHub._handle(testEvent); |
| 141 | + expect(callback).toHaveBeenCalledWith(testEvent); |
| 142 | + }); |
| 143 | + |
| 144 | + test("Should not handle non subscribed events", () => { |
| 145 | + const callback = vi.fn(); |
| 146 | + const testEvent = { topic: "test.topic", data: {} }; |
| 147 | + eventHub.subscribe("topic=ftrack.*", callback); |
| 148 | + eventHub.subscribe("topic=ftrack.update", callback); |
| 149 | + eventHub.subscribe("topic=test.topic.*", callback); |
| 150 | + |
| 151 | + eventHub._handle(testEvent); |
| 152 | + |
| 153 | + expect(callback).not.toHaveBeenCalledWith(testEvent); |
| 154 | + }); |
| 155 | +}); |
0 commit comments