-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
154 lines (125 loc) · 4.69 KB
/
Copy pathtest.js
File metadata and controls
154 lines (125 loc) · 4.69 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
const timeSync = require('./index.js');
console.log('🧪 TimeSync Tests\n');
async function runTests() {
let passed = 0;
let total = 0;
function test(name, condition) {
total++;
if (condition) {
console.log(`✅ ${name}`);
passed++;
} else {
console.log(`❌ ${name}`);
}
}
try {
// Test 1: API exports
test('Correct API exports',
typeof timeSync.sync === 'function' &&
typeof timeSync.now === 'function' &&
typeof timeSync.timestamp === 'function'
);
// Test 2: Initial state
test('Initial state not synchronized',
!timeSync.isSynchronized()
);
// Test 3: Synchronization
console.log('\n⏳ Testing synchronization...');
const result = await timeSync.sync();
test('Synchronization successful',
result && result.server && typeof result.offset === 'number'
);
test('Synchronized state after sync',
timeSync.isSynchronized()
);
// Test 4: Get time
const now = timeSync.now();
const timestamp = timeSync.timestamp();
const offset = timeSync.offset();
test('now() returns a Date',
now instanceof Date
);
test('timestamp() returns an ISO string',
typeof timestamp === 'string' && timestamp.includes('T')
);
test('offset() returns a number',
typeof offset === 'number'
);
// Test 5: Formatting
const formatted = timeSync.format(null, 'locale');
test('format() works',
typeof formatted === 'string' && formatted.length > 0
);
// Test 6: Statistics
const stats = timeSync.stats();
test('stats() returns data',
stats && stats.synchronized === true
);
// Test 7: Auto-sync (quick test)
timeSync.startAutoSync(60000);
test('Auto-sync starts', true);
setTimeout(() => {
timeSync.stopAutoSync();
test('Auto-sync stops', true);
}, 100);
// Test 8: Time difference
const diff = timeSync.diff(new Date(), new Date(Date.now() + 1000));
test('diff() calculates correctly',
Math.abs(diff - 1000) < 100
);
// Test 9: Server coherence validation
console.log('\n⏳ Testing server coherence validation...');
const coherenceResult = await timeSync.sync({ coherenceValidation: true });
test('Coherence validation works',
coherenceResult && typeof coherenceResult.coherenceVariance === 'number'
);
// Test 10: Smooth correction configuration
timeSync.setSmoothCorrection(true, {
maxCorrectionJump: 1000,
correctionRate: 0.1,
maxOffsetThreshold: 5000
});
test('Smooth correction configuration', true);
// Test 11: Stats include new fields
const detailedStats = timeSync.stats();
test('Stats include correction fields',
typeof detailedStats.correctedOffset === 'number' &&
typeof detailedStats.targetOffset === 'number' &&
typeof detailedStats.correctionInProgress === 'boolean' &&
detailedStats.config &&
typeof detailedStats.config.smoothCorrection === 'boolean'
);
// Test 12: Force correction method
timeSync.forceCorrection();
test('Force correction method exists', typeof timeSync.forceCorrection === 'function');
// Test 13: Event handling
let eventReceived = false;
timeSync.on('sync', () => { eventReceived = true; });
await timeSync.sync();
test('Events are emitted', eventReceived);
console.log('\n⏳ Testing WebSocket server...');
// Test 14: WebSocket server
try {
const port = timeSync.startWebSocketServer(8081);
test('WebSocket server starts', typeof port === 'number');
setTimeout(() => {
timeSync.stopWebSocketServer();
test('WebSocket server stops', true);
}, 100);
} catch {
test('WebSocket server (optional)', true); // Skip if port is busy
}
} catch (error) {
console.log(`❌ Error during tests: ${error.message}`);
}
// Results
console.log(`\n📊 Results: ${passed}/${total} tests passed`);
if (passed === total) {
console.log('🎉 All tests passed!');
process.exit(0);
} else {
console.log('💥 Some tests failed');
process.exit(1);
}
}
runTests().catch(console.error);