basant307/AI_Governance_Project
048
1/**2 * @license3 * Copyright 2026 Qwen Team4 * SPDX-License-Identifier: Apache-2.05 */6import { describe, it, expect, vi } from 'vitest';7import { safeWsSend } from './safe-ws-send.js';8// Minimal WebSocket stand-in: just the readyState + send() the guard touches,9// plus the per-instance OPEN constant it compares against.10function fakeWs(readyState) {11 const send = vi.fn();12 return {13 ws: { readyState, OPEN: 1, send },14 send,15 };16}17describe('safeWsSend', () => {18 it('sends when the socket is OPEN', () => {19 const { ws, send } = fakeWs(1); // OPEN20 safeWsSend(ws, 'hello');21 expect(send).toHaveBeenCalledWith('hello');22 });23 it('does not throw when OPEN send fails', () => {24 const { ws, send } = fakeWs(1); // OPEN25 send.mockImplementationOnce(() => {26 throw new Error('socket write failed');27 });28 expect(() => safeWsSend(ws, 'hello', 'CDP')).not.toThrow();29 });30 it('drops (no send, no throw) when the socket is CLOSING', () => {31 const { ws, send } = fakeWs(2); // CLOSING32 expect(() => safeWsSend(ws, 'late', 'CDP')).not.toThrow();33 expect(send).not.toHaveBeenCalled();34 });35 it('drops (no send, no throw) when the socket is CLOSED', () => {36 const { ws, send } = fakeWs(3); // CLOSED37 expect(() => safeWsSend(ws, 'late')).not.toThrow();38 expect(send).not.toHaveBeenCalled();39 });40});41//# sourceMappingURL=safe-ws-send.test.js.map