@gjsify/net 0.3.21 → 0.4.3

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,413 +0,0 @@
1
- // Extended net tests — isIP edge cases, Socket/Server properties, createConnection
2
- // Ported from refs/node-test/parallel/test-net-*.js
3
- // Original: MIT license, Node.js contributors
4
-
5
- import { describe, it, expect } from '@gjsify/unit';
6
- import * as net from 'node:net';
7
-
8
- export default async () => {
9
-
10
- // ===================== isIP comprehensive =====================
11
- await describe('net.isIP comprehensive', async () => {
12
- // Valid IPv4
13
- await it('should return 4 for 0.0.0.0', async () => {
14
- expect(net.isIP('0.0.0.0')).toBe(4);
15
- });
16
- await it('should return 4 for 255.255.255.255', async () => {
17
- expect(net.isIP('255.255.255.255')).toBe(4);
18
- });
19
- await it('should return 4 for 192.168.1.1', async () => {
20
- expect(net.isIP('192.168.1.1')).toBe(4);
21
- });
22
- await it('should return 4 for 10.0.0.1', async () => {
23
- expect(net.isIP('10.0.0.1')).toBe(4);
24
- });
25
- await it('should return 4 for 1.1.1.1', async () => {
26
- expect(net.isIP('1.1.1.1')).toBe(4);
27
- });
28
-
29
- // Invalid IPv4
30
- await it('should return 0 for 256.0.0.1', async () => {
31
- expect(net.isIP('256.0.0.1')).toBe(0);
32
- });
33
- await it('should return 0 for 1.2.3', async () => {
34
- expect(net.isIP('1.2.3')).toBe(0);
35
- });
36
- await it('should return 0 for 1.2.3.4.5', async () => {
37
- expect(net.isIP('1.2.3.4.5')).toBe(0);
38
- });
39
- await it('should return 0 for leading zeros in IPv4', async () => {
40
- expect(net.isIP('01.02.03.04')).toBe(0);
41
- });
42
- await it('should return 0 for negative octets', async () => {
43
- expect(net.isIP('-1.0.0.0')).toBe(0);
44
- });
45
- await it('should return 0 for empty octets', async () => {
46
- expect(net.isIP('1..3.4')).toBe(0);
47
- });
48
-
49
- // Valid IPv6
50
- await it('should return 6 for ::1', async () => {
51
- expect(net.isIP('::1')).toBe(6);
52
- });
53
- await it('should return 6 for ::', async () => {
54
- expect(net.isIP('::')).toBe(6);
55
- });
56
- await it('should return 6 for full IPv6', async () => {
57
- expect(net.isIP('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe(6);
58
- });
59
- await it('should return 6 for compressed IPv6', async () => {
60
- expect(net.isIP('2001:db8::1')).toBe(6);
61
- });
62
- await it('should return 6 for fe80::1', async () => {
63
- expect(net.isIP('fe80::1')).toBe(6);
64
- });
65
- await it('should return 6 for IPv4-mapped IPv6', async () => {
66
- expect(net.isIP('::ffff:192.168.1.1')).toBe(6);
67
- });
68
- await it('should return 6 for loopback full', async () => {
69
- expect(net.isIP('0000:0000:0000:0000:0000:0000:0000:0001')).toBe(6);
70
- });
71
-
72
- // Invalid IPv6
73
- await it('should return 0 for triple colon', async () => {
74
- expect(net.isIP(':::1')).toBe(0);
75
- });
76
- await it('should return 0 for too many groups', async () => {
77
- expect(net.isIP('1:2:3:4:5:6:7:8:9')).toBe(0);
78
- });
79
-
80
- // Non-IP strings
81
- await it('should return 0 for empty string', async () => {
82
- expect(net.isIP('')).toBe(0);
83
- });
84
- await it('should return 0 for hostname', async () => {
85
- expect(net.isIP('example.com')).toBe(0);
86
- });
87
- await it('should return 0 for random text', async () => {
88
- expect(net.isIP('not an ip')).toBe(0);
89
- });
90
- await it('should return 0 for number input', async () => {
91
- expect(net.isIP(123 as any)).toBe(0);
92
- });
93
- await it('should return 0 for null', async () => {
94
- expect(net.isIP(null as any)).toBe(0);
95
- });
96
- await it('should return 0 for undefined', async () => {
97
- expect(net.isIP(undefined as any)).toBe(0);
98
- });
99
- await it('should return 0 for boolean', async () => {
100
- expect(net.isIP(true as any)).toBe(0);
101
- });
102
- });
103
-
104
- // ===================== isIPv4 comprehensive =====================
105
- await describe('net.isIPv4 comprehensive', async () => {
106
- await it('should return true for valid IPv4', async () => {
107
- expect(net.isIPv4('127.0.0.1')).toBe(true);
108
- expect(net.isIPv4('0.0.0.0')).toBe(true);
109
- expect(net.isIPv4('255.255.255.255')).toBe(true);
110
- });
111
- await it('should return false for IPv6', async () => {
112
- expect(net.isIPv4('::1')).toBe(false);
113
- expect(net.isIPv4('::')).toBe(false);
114
- });
115
- await it('should return false for invalid', async () => {
116
- expect(net.isIPv4('256.0.0.1')).toBe(false);
117
- expect(net.isIPv4('')).toBe(false);
118
- expect(net.isIPv4('hello')).toBe(false);
119
- });
120
- await it('should return false for non-string', async () => {
121
- expect(net.isIPv4(null as any)).toBe(false);
122
- expect(net.isIPv4(undefined as any)).toBe(false);
123
- expect(net.isIPv4(42 as any)).toBe(false);
124
- });
125
- });
126
-
127
- // ===================== isIPv6 comprehensive =====================
128
- await describe('net.isIPv6 comprehensive', async () => {
129
- await it('should return true for valid IPv6', async () => {
130
- expect(net.isIPv6('::1')).toBe(true);
131
- expect(net.isIPv6('::')).toBe(true);
132
- expect(net.isIPv6('fe80::1')).toBe(true);
133
- expect(net.isIPv6('2001:db8::1')).toBe(true);
134
- });
135
- await it('should return false for IPv4', async () => {
136
- expect(net.isIPv6('127.0.0.1')).toBe(false);
137
- expect(net.isIPv6('0.0.0.0')).toBe(false);
138
- });
139
- await it('should return false for invalid', async () => {
140
- expect(net.isIPv6(':::1')).toBe(false);
141
- expect(net.isIPv6('')).toBe(false);
142
- expect(net.isIPv6('hello')).toBe(false);
143
- });
144
- await it('should return false for non-string', async () => {
145
- expect(net.isIPv6(null as any)).toBe(false);
146
- expect(net.isIPv6(undefined as any)).toBe(false);
147
- });
148
- });
149
-
150
- // ===================== Socket extended =====================
151
- await describe('net.Socket extended', async () => {
152
- await it('should be constructable without args', async () => {
153
- const socket = new net.Socket();
154
- expect(socket).toBeDefined();
155
- });
156
-
157
- await it('should have Duplex stream methods', async () => {
158
- const socket = new net.Socket();
159
- expect(typeof socket.write).toBe('function');
160
- expect(typeof socket.end).toBe('function');
161
- expect(typeof socket.destroy).toBe('function');
162
- expect(typeof socket.pipe).toBe('function');
163
- });
164
-
165
- await it('should have EventEmitter methods', async () => {
166
- const socket = new net.Socket();
167
- expect(typeof socket.on).toBe('function');
168
- expect(typeof socket.emit).toBe('function');
169
- expect(typeof socket.once).toBe('function');
170
- expect(typeof socket.removeListener).toBe('function');
171
- expect(typeof socket.removeAllListeners).toBe('function');
172
- });
173
-
174
- await it('bytesRead should default to 0', async () => {
175
- const socket = new net.Socket();
176
- expect(socket.bytesRead).toBe(0);
177
- });
178
-
179
- await it('bytesWritten should default to 0', async () => {
180
- const socket = new net.Socket();
181
- expect(socket.bytesWritten).toBe(0);
182
- });
183
-
184
- await it('connecting should be false initially', async () => {
185
- const socket = new net.Socket();
186
- expect(socket.connecting).toBe(false);
187
- });
188
-
189
- await it('pending should be true initially', async () => {
190
- const socket = new net.Socket();
191
- expect(socket.pending).toBe(true);
192
- });
193
-
194
- await it('destroyed should be false initially', async () => {
195
- const socket = new net.Socket();
196
- expect(socket.destroyed).toBe(false);
197
- });
198
-
199
- await it('should have setTimeout method', async () => {
200
- const socket = new net.Socket();
201
- expect(typeof socket.setTimeout).toBe('function');
202
- });
203
-
204
- await it('should have setKeepAlive method', async () => {
205
- const socket = new net.Socket();
206
- expect(typeof socket.setKeepAlive).toBe('function');
207
- });
208
-
209
- await it('should have setNoDelay method', async () => {
210
- const socket = new net.Socket();
211
- expect(typeof socket.setNoDelay).toBe('function');
212
- });
213
-
214
- await it('should have ref/unref methods', async () => {
215
- const socket = new net.Socket();
216
- expect(typeof socket.ref).toBe('function');
217
- expect(typeof socket.unref).toBe('function');
218
- });
219
-
220
- await it('should have connect method', async () => {
221
- const socket = new net.Socket();
222
- expect(typeof socket.connect).toBe('function');
223
- });
224
-
225
- await it('should have address method', async () => {
226
- const socket = new net.Socket();
227
- expect(typeof socket.address).toBe('function');
228
- });
229
-
230
- await it('remoteAddress should be undefined when not connected', async () => {
231
- const socket = new net.Socket();
232
- expect(socket.remoteAddress).toBeUndefined();
233
- });
234
-
235
- await it('remotePort should be undefined when not connected', async () => {
236
- const socket = new net.Socket();
237
- expect(socket.remotePort).toBeUndefined();
238
- });
239
-
240
- await it('remoteFamily should be undefined when not connected', async () => {
241
- const socket = new net.Socket();
242
- expect(socket.remoteFamily).toBeUndefined();
243
- });
244
-
245
- await it('localAddress should be undefined when not connected', async () => {
246
- const socket = new net.Socket();
247
- expect(socket.localAddress).toBeUndefined();
248
- });
249
-
250
- await it('localPort should be undefined when not connected', async () => {
251
- const socket = new net.Socket();
252
- expect(socket.localPort).toBeUndefined();
253
- });
254
-
255
- await it('setTimeout should not throw', async () => {
256
- const socket = new net.Socket();
257
- expect(() => socket.setTimeout(1000)).not.toThrow();
258
- });
259
-
260
- await it('setKeepAlive should not throw', async () => {
261
- const socket = new net.Socket();
262
- expect(() => socket.setKeepAlive(true)).not.toThrow();
263
- });
264
-
265
- await it('setNoDelay should not throw', async () => {
266
- const socket = new net.Socket();
267
- expect(() => socket.setNoDelay(true)).not.toThrow();
268
- });
269
-
270
- await it('ref should return socket (chainable)', async () => {
271
- const socket = new net.Socket();
272
- expect(socket.ref()).toBe(socket);
273
- });
274
-
275
- await it('unref should return socket (chainable)', async () => {
276
- const socket = new net.Socket();
277
- expect(socket.unref()).toBe(socket);
278
- });
279
-
280
- await it('should accept options with fd', async () => {
281
- const socket = new net.Socket({ fd: undefined });
282
- expect(socket).toBeDefined();
283
- });
284
-
285
- await it('should accept allowHalfOpen option', async () => {
286
- const socket = new net.Socket({ allowHalfOpen: true });
287
- expect(socket).toBeDefined();
288
- });
289
-
290
- await it('should accept readable/writable options', async () => {
291
- const socket = new net.Socket({ readable: true, writable: true });
292
- expect(socket).toBeDefined();
293
- });
294
- });
295
-
296
- // ===================== Server extended =====================
297
- await describe('net.Server extended', async () => {
298
- await it('should be constructable', async () => {
299
- const server = new net.Server();
300
- expect(server).toBeDefined();
301
- });
302
-
303
- await it('should accept connectionListener', async () => {
304
- const server = new net.Server((_socket) => {});
305
- expect(server).toBeDefined();
306
- });
307
-
308
- await it('should accept options', async () => {
309
- const server = new net.Server({ allowHalfOpen: true });
310
- expect(server).toBeDefined();
311
- });
312
-
313
- await it('should have listen method', async () => {
314
- const server = new net.Server();
315
- expect(typeof server.listen).toBe('function');
316
- });
317
-
318
- await it('should have close method', async () => {
319
- const server = new net.Server();
320
- expect(typeof server.close).toBe('function');
321
- });
322
-
323
- await it('should have address method', async () => {
324
- const server = new net.Server();
325
- expect(typeof server.address).toBe('function');
326
- });
327
-
328
- await it('should have getConnections method', async () => {
329
- const server = new net.Server();
330
- expect(typeof server.getConnections).toBe('function');
331
- });
332
-
333
- await it('should have ref/unref methods', async () => {
334
- const server = new net.Server();
335
- expect(typeof server.ref).toBe('function');
336
- expect(typeof server.unref).toBe('function');
337
- });
338
-
339
- await it('listening should be false initially', async () => {
340
- const server = new net.Server();
341
- expect(server.listening).toBe(false);
342
- });
343
-
344
- await it('should have maxConnections property', async () => {
345
- const server = new net.Server();
346
- expect(typeof server.maxConnections === 'number' || server.maxConnections === undefined).toBe(true);
347
- });
348
-
349
- await it('should be an EventEmitter', async () => {
350
- const server = new net.Server();
351
- expect(typeof server.on).toBe('function');
352
- expect(typeof server.emit).toBe('function');
353
- expect(typeof server.once).toBe('function');
354
- });
355
-
356
- await it('address should return null when not listening', async () => {
357
- const server = new net.Server();
358
- expect(server.address()).toBeNull();
359
- });
360
- });
361
-
362
- // ===================== Module exports =====================
363
- await describe('net module exports', async () => {
364
- await it('should export Socket', async () => {
365
- expect(typeof net.Socket).toBe('function');
366
- });
367
- await it('should export Server', async () => {
368
- expect(typeof net.Server).toBe('function');
369
- });
370
- await it('should export createServer', async () => {
371
- expect(typeof net.createServer).toBe('function');
372
- });
373
- await it('should export createConnection', async () => {
374
- expect(typeof net.createConnection).toBe('function');
375
- });
376
- await it('should export connect', async () => {
377
- expect(typeof net.connect).toBe('function');
378
- });
379
- await it('should export isIP', async () => {
380
- expect(typeof net.isIP).toBe('function');
381
- });
382
- await it('should export isIPv4', async () => {
383
- expect(typeof net.isIPv4).toBe('function');
384
- });
385
- await it('should export isIPv6', async () => {
386
- expect(typeof net.isIPv6).toBe('function');
387
- });
388
- });
389
-
390
- // ===================== createServer =====================
391
- await describe('net.createServer', async () => {
392
- await it('should return a Server', async () => {
393
- const server = net.createServer();
394
- expect(server).toBeDefined();
395
- expect(server instanceof net.Server).toBe(true);
396
- });
397
-
398
- await it('should accept options', async () => {
399
- const server = net.createServer({ allowHalfOpen: true });
400
- expect(server).toBeDefined();
401
- });
402
-
403
- await it('should accept connectionListener', async () => {
404
- const server = net.createServer((_socket) => {});
405
- expect(server).toBeDefined();
406
- });
407
-
408
- await it('should accept options and connectionListener', async () => {
409
- const server = net.createServer({ allowHalfOpen: false }, (_socket) => {});
410
- expect(server).toBeDefined();
411
- });
412
- });
413
- };