From a0d9d8d57662f9f166783b4bca64e3292d34cef4 Mon Sep 17 00:00:00 2001
From: Earl Warren <contact@earl-warren.org>
Date: Tue, 2 Jul 2024 19:29:04 +0200
Subject: [PATCH] test(js): false negative in sleep test
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

It is entirely possible that the difference between the specified
sleep time and the actual sleep time is greater than 15 seconds.

https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#Notes

> Note that in either case, the actual delay may be longer than
> intended; see Reasons for delays longer than specified below.

It is however an error for the delay to be shorter.

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
 FAIL  web_src/js/utils.test.js > sleep
AssertionError: expected false to be truthy
 ❯ testSleep web_src/js/utils.test.js:192:48
    190|   const endTime = Date.now();    // Record the end time
    191|   const actualSleepTime = endTime - startTime;
    192|   expect(Math.abs(actualSleepTime - ms) <= 15).toBeTruthy();
       |                                                ^
    193| }
    194|
 ❯ web_src/js/utils.test.js:184:3
---
 web_src/js/utils.test.js | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/web_src/js/utils.test.js b/web_src/js/utils.test.js
index b7691b8d31..c49bb2a110 100644
--- a/web_src/js/utils.test.js
+++ b/web_src/js/utils.test.js
@@ -177,10 +177,6 @@ test('serializeXml', () => {
 });
 
 test('sleep', async () => {
-  // Test 500 ms sleep
-  await testSleep(500);
-
-  // Test 2000 ms sleep
   await testSleep(2000);
 });
 
@@ -189,5 +185,5 @@ async function testSleep(ms) {
   await sleep(ms);
   const endTime = Date.now();    // Record the end time
   const actualSleepTime = endTime - startTime;
-  expect(Math.abs(actualSleepTime - ms) <= 15).toBeTruthy();
+  expect(actualSleepTime >= ms).toBeTruthy();
 }