asyx 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,164 +0,0 @@
1
- /**
2
- * CLI-REAUTH: Unit tests for WebSocket control frame helper
3
- */
4
-
5
- import { describe, test, expect, jest, beforeEach, afterEach } from '@jest/globals';
6
- import { sendControlAndWaitAck, dispatchControlFrame, clearAllWaiters } from '../ws-control.js';
7
-
8
- describe('ws-control', () => {
9
- let mockWs;
10
- let mockSend;
11
- let mockReadyState;
12
-
13
- beforeEach(() => {
14
- jest.useFakeTimers();
15
- mockSend = jest.fn();
16
- mockReadyState = 1; // WebSocket.OPEN
17
- mockWs = {
18
- send: mockSend,
19
- get readyState() { return mockReadyState; }
20
- };
21
- clearAllWaiters();
22
- });
23
-
24
- afterEach(async () => {
25
- // Run any pending timers before switching back
26
- try {
27
- jest.runOnlyPendingTimers();
28
- jest.clearAllTimers();
29
- } catch {}
30
-
31
- // Clear all waiters (rejects pending promises) and await cleanup
32
- const cleanupPromise = clearAllWaiters();
33
- if (cleanupPromise) {
34
- await cleanupPromise.catch(() => {}); // Suppress rejections
35
- }
36
-
37
- // Switch back to real timers
38
- jest.useRealTimers();
39
-
40
- // Give event loop a tick to process any pending microtasks
41
- await new Promise(resolve => setImmediate(resolve));
42
- });
43
-
44
- describe('sendControlAndWaitAck', () => {
45
- test('resolves with control frame when matching op received', async () => {
46
- const payload = JSON.stringify({ op: 'reauth', leaseId: 'test-lease', jwt: 'test-jwt' });
47
- const promise = sendControlAndWaitAck(mockWs, payload, 'reauth_ack', 3000);
48
-
49
- // Advance time slightly to ensure send is called
50
- await jest.advanceTimersByTimeAsync(10);
51
-
52
- expect(mockSend).toHaveBeenCalledWith(payload);
53
-
54
- // Simulate receiving reauth_ack
55
- const ackFrame = { op: 'reauth_ack', status: 'ok', active: { jti: 'new-jti', exp: 1234567890 } };
56
- dispatchControlFrame(ackFrame);
57
-
58
- const result = await promise;
59
- expect(result).toEqual(ackFrame);
60
- });
61
-
62
- test('resolves with timeout error when no ack received', async () => {
63
- const payload = JSON.stringify({ op: 'reauth', leaseId: 'test-lease', jwt: 'test-jwt' });
64
- const promise = sendControlAndWaitAck(mockWs, payload, 'reauth_ack', 3000);
65
-
66
- await jest.advanceTimersByTimeAsync(10);
67
- expect(mockSend).toHaveBeenCalledWith(payload);
68
-
69
- // Advance time past timeout
70
- await jest.advanceTimersByTimeAsync(3000);
71
-
72
- const result = await promise;
73
- expect(result).toEqual({ status: 'error', error: 'timeout' });
74
- });
75
-
76
- test('resolves with error when socket not open', async () => {
77
- mockReadyState = 3; // WebSocket.CLOSED
78
- const payload = JSON.stringify({ op: 'reauth', leaseId: 'test-lease', jwt: 'test-jwt' });
79
- const result = await sendControlAndWaitAck(mockWs, payload, 'reauth_ack', 3000);
80
-
81
- expect(result).toEqual({ status: 'error', error: 'socket_not_open' });
82
- expect(mockSend).not.toHaveBeenCalled();
83
- });
84
-
85
- test('resolves with error when send fails', async () => {
86
- mockSend.mockImplementation(() => {
87
- throw new Error('Send failed');
88
- });
89
-
90
- const payload = JSON.stringify({ op: 'reauth', leaseId: 'test-lease', jwt: 'test-jwt' });
91
- const result = await sendControlAndWaitAck(mockWs, payload, 'reauth_ack', 3000);
92
-
93
- expect(result).toEqual({ status: 'error', error: 'send_failed', message: 'Send failed' });
94
- });
95
-
96
- test('cleans up listener when resolved', async () => {
97
- const payload = JSON.stringify({ op: 'reauth', leaseId: 'test-lease', jwt: 'test-jwt' });
98
- const promise = sendControlAndWaitAck(mockWs, payload, 'reauth_ack', 3000);
99
-
100
- await jest.advanceTimersByTimeAsync(10);
101
- expect(mockSend).toHaveBeenCalled();
102
-
103
- // Resolve
104
- const ackFrame = { op: 'reauth_ack', status: 'ok' };
105
- dispatchControlFrame(ackFrame);
106
-
107
- await promise;
108
-
109
- // Try to dispatch again - should return false (no waiters)
110
- expect(dispatchControlFrame(ackFrame)).toBe(false);
111
- });
112
- });
113
-
114
- describe('dispatchControlFrame', () => {
115
- test('returns false for non-control frames', () => {
116
- expect(dispatchControlFrame({ type: 'ping' })).toBe(false);
117
- expect(dispatchControlFrame({ t: 'pong' })).toBe(false);
118
- expect(dispatchControlFrame(null)).toBe(false);
119
- expect(dispatchControlFrame({})).toBe(false);
120
- });
121
-
122
- test('dispatches to correct waiter and resolves promise', async () => {
123
- const payload = JSON.stringify({ op: 'reauth', leaseId: 'test' });
124
- const promise1 = sendControlAndWaitAck(mockWs, payload, 'reauth_ack', 3000);
125
- const promise2 = sendControlAndWaitAck(mockWs, payload, 'other_ack', 3000);
126
-
127
- await jest.advanceTimersByTimeAsync(10);
128
-
129
- // Dispatch reauth_ack - should resolve promise1 only
130
- const ackFrame = { op: 'reauth_ack', status: 'ok' };
131
- const dispatched = dispatchControlFrame(ackFrame);
132
-
133
- expect(dispatched).toBe(true);
134
- const result1 = await promise1;
135
- expect(result1).toEqual(ackFrame);
136
-
137
- // promise2 should still be waiting
138
- jest.advanceTimersByTime(1000);
139
- // Still waiting, advance more to timeout
140
- await jest.advanceTimersByTimeAsync(2000);
141
- const result2 = await promise2;
142
- expect(result2).toEqual({ status: 'error', error: 'timeout' });
143
- });
144
- });
145
-
146
- describe('clearAllWaiters', () => {
147
- test('clears all waiters and rejects them', async () => {
148
- const payload = JSON.stringify({ op: 'reauth', leaseId: 'test' });
149
- const promise = sendControlAndWaitAck(mockWs, payload, 'reauth_ack', 3000);
150
-
151
- await jest.advanceTimersByTimeAsync(10);
152
-
153
- // Clear waiters
154
- clearAllWaiters();
155
-
156
- // Promise should reject
157
- await expect(promise).rejects.toThrow('WebSocket closed before control frame received');
158
-
159
- // Dispatch should return false
160
- expect(dispatchControlFrame({ op: 'reauth_ack', status: 'ok' })).toBe(false);
161
- });
162
- });
163
- });
164
-