51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
// test_ws_logic.js
|
|
class FakeWS {
|
|
constructor() {
|
|
this.msgLog = [];
|
|
this.terminated = false;
|
|
}
|
|
send(msg) {
|
|
this.msgLog.push(msg);
|
|
}
|
|
terminate() {
|
|
this.terminated = true;
|
|
}
|
|
close() {
|
|
this.terminated = true;
|
|
}
|
|
}
|
|
|
|
// SIMULATE _startHeartbeat() logic from ws-client.ts v0.5.12
|
|
function testLogic(isNodeWs, serverSendsPong) {
|
|
let ws = new FakeWS();
|
|
let connected = true;
|
|
let lastPongTime = Date.now();
|
|
let forceHeartbeatTimeoutIfNoPong = serverSendsPong;
|
|
let checkCounter = 0;
|
|
|
|
// Fast forward 61 seconds in time
|
|
let timeElapsed = 61000;
|
|
let currentNow = Date.now() + timeElapsed;
|
|
|
|
// Simulate heartbeat timeout logic
|
|
let conditionMet = false;
|
|
if ((isNodeWs || forceHeartbeatTimeoutIfNoPong) && currentNow - lastPongTime > 60000) {
|
|
conditionMet = true;
|
|
ws.terminate();
|
|
}
|
|
|
|
return {
|
|
conditionMet: conditionMet,
|
|
terminated: ws.terminated
|
|
};
|
|
}
|
|
|
|
console.log("Scenario 1: Node WS (native ping/pong) MUST enforce 60s timeout:");
|
|
console.log(testLogic(true, false)); // expect true, true
|
|
|
|
console.log("\nScenario 2: Browser WS (fallback) + NO JSON PONG FROM SERVER MUST NOT enforce 60s timeout:");
|
|
console.log(testLogic(false, false)); // expect false, false (PREVENTS FALSE POSITIVE)
|
|
|
|
console.log("\nScenario 3: Browser WS (fallback) + JSON PONG FROM SERVER MUST enforce 60s timeout:");
|
|
console.log(testLogic(false, true)); // expect true, true (DETECTS ZOMBIE)
|