@@ -22,32 +22,32 @@ describe('isNewOsSession', () => {
2222 } )
2323
2424 it ( 'unix-like: returns true when expected' , async ( ) => {
25- const uptimeStub = sandbox . stub ( )
25+ const uptimeMillisStub = sandbox . stub ( )
2626 const now = sandbox . stub ( )
27- // We started our computer at 2 minutes since epoch (time - pc uptime)
28- // and the comptuer has been on for 1 minute. So the OS started 1 minute since epoch .
29- now . returns ( 60_000 + 60_000 )
30- uptimeStub . returns ( 1 )
27+ // We started our computer at 1 minutes since epoch and the comptuer uptime has been 1 minute.
28+ // So the OS started at the epoch (time - uptime) .
29+ now . returns ( 0 ) // the epoch time
30+ uptimeMillisStub . returns ( 0 ) // this session has just started
3131
3232 // On a brand new session the first caller will get true
33- assert . strictEqual ( await isNewOsSession ( now , uptimeStub ) , true )
33+ assert . strictEqual ( await isNewOsSession ( now , uptimeMillisStub ) , true )
3434 // Subsequent callers will get false
35- assert . strictEqual ( await isNewOsSession ( now , uptimeStub ) , false )
36-
37- // Start a computer session 10 minutes from epoch
38- uptimeStub . returns ( 0 )
39- now . returns ( 60_000 * 10 )
40- assert . strictEqual ( await isNewOsSession ( now , uptimeStub ) , true )
41- // Anything that is within a 5 second threshold of the last session time, is considered the same session
42- now . returns ( 60_000 * 10 + 5000 )
43- assert . strictEqual ( await isNewOsSession ( now , uptimeStub ) , false )
44- now . returns ( 60_000 * 10 + 5000 + 1 )
45- assert . strictEqual ( await isNewOsSession ( now , uptimeStub ) , true )
46-
47- // A non-zero uptime
48- uptimeStub . returns ( 5 ) // The computer has been running for 5 minutes already, so the start time is relative to this.
49- now . returns ( 60_000 * 10 + 5000 + 60_000 * 10 ) // 5 minutes since last session
50- // Nothing changes since the diff between uptime and the last start has not changed
51- assert . strictEqual ( await isNewOsSession ( now , uptimeStub ) , true )
35+ assert . strictEqual ( await isNewOsSession ( now , uptimeMillisStub ) , false )
36+
37+ // 10 minutes later, same session
38+ now . returns ( 1000 * 60 * 10 )
39+ uptimeMillisStub . returns ( 1000 * 60 * 10 ) // This scales proportionately with the current time
40+ // This is still the same session, so we get false
41+ assert . strictEqual ( await isNewOsSession ( now , uptimeMillisStub ) , false )
42+
43+ // Test the lowerbound of what is considered a new session
44+ // Pretend we started a new computer session 5 seconds after the initial session
45+ uptimeMillisStub . returns ( 0 )
46+ now . returns ( 5000 )
47+ // Anything that is within a 5 second threshold of the last session time, is considered the SAME session
48+ assert . strictEqual ( await isNewOsSession ( now , uptimeMillisStub ) , false )
49+ // This is 1 millisecond after the threshold, it is considered a NEW session
50+ now . returns ( 5000 + 1 )
51+ assert . strictEqual ( await isNewOsSession ( now , uptimeMillisStub ) , true )
5252 } )
5353} )
0 commit comments