-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathtest.js
More file actions
171 lines (142 loc) · 4.55 KB
/
test.js
File metadata and controls
171 lines (142 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {test} from 'node:test';
import assert from 'node:assert/strict';
import process from 'node:process';
import {subscribe, unsubscribe} from 'node:diagnostics_channel';
import {createTestServer} from './test-server.js';
import isOnline from './index.js';
test('v4', async () => {
assert.equal(await isOnline(), true);
});
test('v4 with timeout', async () => {
assert.equal(await isOnline({timeout: 10_000}), true);
});
test('v4 with impossible timeout', async () => {
assert.equal(await isOnline({timeout: 1}), false);
});
test('v4 with abort signal', async () => {
const controller = new AbortController();
const promise = isOnline({signal: controller.signal});
controller.abort();
assert.equal(await promise, false);
});
test('v4 with pre-aborted signal', async () => {
const controller = new AbortController();
controller.abort();
assert.equal(await isOnline({signal: controller.signal}), false);
});
test('invalid ipVersion throws error', async () => {
await assert.rejects(isOnline({ipVersion: 5}), {message: '`ipVersion` must be 4 or 6'});
});
test('fallbackUrls when main checks fail', async () => {
// Create a test server
const testServer = await createTestServer();
try {
// Mock the main checks to fail by using invalid network
// Use longer timeout to allow fallback URL check to work, but main checks should still fail due to network issues
const result = await isOnline({
timeout: 1000, // Give enough time for fallback URL check to succeed
fallbackUrls: [testServer.url],
});
// Should return true because fallback URL check succeeds
assert.equal(result, true);
} finally {
testServer.close();
}
});
test('fallbackUrls with multiple URLs', async () => {
const testServer1 = await createTestServer();
const testServer2 = await createTestServer();
try {
const result = await isOnline({
timeout: 100, // Short timeout to make main checks fail, but enough for fallback URL check
fallbackUrls: [
'http://this-should-not-exist-12345.com',
testServer1.url,
testServer2.url,
],
});
assert.equal(result, true);
} finally {
testServer1.close();
testServer2.close();
}
});
test('fallbackUrls all fail', async () => {
const result = await isOnline({
timeout: 1, // Very short timeout to ensure main checks fail
fallbackUrls: [
'http://this-should-not-exist-12345.com',
'http://another-non-existent-host-67890.com',
],
});
assert.equal(result, false);
});
test('fallbackUrls with abort signal', async () => {
const testServer = await createTestServer();
try {
const controller = new AbortController();
const promise = isOnline({
timeout: 100, // Short timeout to make main checks fail
fallbackUrls: [testServer.url],
signal: controller.signal,
});
controller.abort();
assert.equal(await promise, false);
} finally {
testServer.close();
}
});
test('invalid fallbackUrls should not crash', async () => {
// Use pre-aborted signal to ensure main checks fail immediately
const controller = new AbortController();
controller.abort();
const result = await isOnline({
signal: controller.signal,
fallbackUrls: [
'not-a-valid-url',
'ftp://invalid-protocol.com',
],
});
assert.equal(result, false);
});
if (!process.env.CI) {
test('v6', async () => {
assert.equal(await isOnline({ipVersion: 6}), true);
});
test('v6 with timeout', async () => {
assert.equal(await isOnline({ipVersion: 6, timeout: 10_000}), true);
});
test('v6 with abort signal', async () => {
const controller = new AbortController();
const promise = isOnline({ipVersion: 6, signal: controller.signal});
controller.abort();
assert.equal(await promise, false);
});
test('v6 with pre-aborted signal', async () => {
const controller = new AbortController();
controller.abort();
assert.equal(await isOnline({ipVersion: 6, signal: controller.signal}), false);
});
}
test('diagnostics channel publishes failure events', async () => {
const diagnostics = [];
const listener = message => {
diagnostics.push(message);
};
subscribe('is-online:connectivity-check', listener);
try {
// Success case might publish some events in CI due to network differences
// What matters is that failure case publishes diagnostic events
await isOnline();
// Clear diagnostics for failure test
diagnostics.length = 0;
// Failure case should publish diagnostic info
await isOnline({timeout: 1});
assert.ok(diagnostics.length > 0);
assert.ok(diagnostics[0].url);
assert.ok(diagnostics[0].error);
assert.ok(diagnostics[0].timestamp);
} finally {
unsubscribe('is-online:connectivity-check', listener);
}
});