feat: Enhance WebSocket server with UUID client mapping and connection handling

This commit is contained in:
StarAppeal
2025-09-30 06:07:43 +02:00
parent 6364cece5b
commit 8b8302ba36
2 changed files with 24 additions and 11 deletions
+16 -5
View File
@@ -71,7 +71,9 @@ describe("ExtendedWebSocketServer", () => {
mockUserService = createMockUserService();
extendedWss = new ExtendedWebSocketServer(mockHttpServer, mockUserService, mockSpotifyPollingService, mockWeatherPollingService, createMockJwtAuthenticator() as any);
extendedWss = new ExtendedWebSocketServer(mockHttpServer,
mockUserService, mockSpotifyPollingService, mockWeatherPollingService,
createMockJwtAuthenticator() as any);
});
describe("Constructor and Setup", () => {
@@ -108,9 +110,11 @@ describe("ExtendedWebSocketServer", () => {
describe("sendMessageToUser", () => {
it("should send a message to a specific user by their UUID", () => {
const client1 = {readyState: WebSocket.OPEN, payload: {uuid: "uuid-1"}, send: vi.fn()};
const client2 = {readyState: WebSocket.OPEN, payload: {uuid: "uuid-2"}, send: vi.fn()};
mockWssInstance.clients.add(client1 as any).add(client2 as any);
const client1 = {readyState: WebSocket.OPEN, payload: {uuid: "uuid-1"}, send: vi.fn(), emit: vi.fn()};
const client2 = {readyState: WebSocket.OPEN, payload: {uuid: "uuid-2"}, send: vi.fn(), emit: vi.fn()};
const connectionHandler = vi.mocked(mockServerEventHandler.enableConnectionEvent).mock.calls[0][0];
connectionHandler(client1 as any, {} as any);
connectionHandler(client2 as any, {} as any);
extendedWss.sendMessageToUser("uuid-1", "private");
expect(client1.send).toHaveBeenCalledWith("private", {binary: false});
expect(client2.send).not.toHaveBeenCalled();
@@ -166,13 +170,17 @@ describe("ExtendedWebSocketServer", () => {
send: vi.fn(),
emit: vi.fn(),
};
mockWssInstance.clients.add(mockClient);
const connectionHandler =
vi.mocked(mockServerEventHandler.enableConnectionEvent).mock.calls[0][0];
connectionHandler(mockClient, {} as any);
});
it("should listen for USER_UPDATED_EVENT and emit to the correct client", () => {
const userUpdateListener = eventBusListeners.get(USER_UPDATED_EVENT);
expect(userUpdateListener).toBeDefined();
vi.mocked(mockClient.emit).mockClear();
const updatedUserPayload = {uuid: "user-123", name: "Neuer Name"};
userUpdateListener!(updatedUserPayload);
@@ -205,6 +213,9 @@ describe("ExtendedWebSocketServer", () => {
const userUpdateListener = eventBusListeners.get(USER_UPDATED_EVENT);
const spotifyStateListener = eventBusListeners.get(SPOTIFY_STATE_UPDATED_EVENT);
vi.mocked(mockClient.send).mockClear();
vi.mocked(mockClient.emit).mockClear();
const eventPayload = {uuid: "user-unknown", name: "some data"};
userUpdateListener!(eventPayload);