-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_test.js
More file actions
105 lines (78 loc) · 2.53 KB
/
session_test.js
File metadata and controls
105 lines (78 loc) · 2.53 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
// MIT License
// Copyright (c) 2026 Open2b
// See the LICENSE file for full text.
import { assertEquals } from '@std/assert'
import { FakeTime } from '@std/testing/time'
import Storage from './storage.js'
import Session from './session.js'
import Options from './options.js'
const DEBUG = false
const writeKey = 'rq6JJg5ENWK28NHfxSwJZmzeIvDC8GQO'
Deno.test('Session', async (t) => {
const fiveMinutes = 5 * 60 * 1000
const tenMinutes = 2 * fiveMinutes
let session
function expectGet(id) {
assertEquals(session.get(), id)
}
function expectGetFresh(id, start) {
const [actualID, actualStart] = session.getFresh() // autoTrack is false.
assertEquals(actualStart, start)
assertEquals(actualID, id)
}
await t.step('session without auto tracking', () => {
localStorage.clear()
const time = new FakeTime()
const options = new Options()
options.stores = ['localStorage']
const storage = new Storage(writeKey, options)
session = new Session(storage, false, tenMinutes)
session.debug(DEBUG)
// Check that the session is not started.
expectGet(null)
expectGetFresh(null, false)
session.start()
const startedAt = new Date().getTime()
expectGet(startedAt)
expectGetFresh(startedAt, true)
expectGetFresh(startedAt, false)
session.end()
expectGet(null)
const newSessionID = 52089473
session.start(newSessionID)
expectGet(newSessionID)
expectGetFresh(newSessionID, true)
time.tick(tenMinutes * 2)
expectGetFresh(newSessionID, false)
session.end()
expectGet(null)
expectGetFresh(null, false)
time.restore()
})
await t.step('session with auto tracking', () => {
localStorage.clear()
const time = new FakeTime()
const startedAt = new Date().getTime()
const options = new Options()
options.stores = ['localStorage']
const storage = new Storage(writeKey, options)
session = new Session(storage, true, tenMinutes)
session.debug(DEBUG)
assertEquals(session.get(), startedAt)
expectGetFresh(startedAt, true) // start is true because will be the first call to getFresh.
expectGetFresh(startedAt, false)
time.tick(fiveMinutes) // advance 5 minutes in time
expectGet(startedAt)
time.tick(fiveMinutes) // advance another 5 minutes in time
expectGet(startedAt)
time.tick(1) // advance 1ms in time
expectGet(null)
expectGetFresh(startedAt + 2 * fiveMinutes + 1, true)
session.end()
expectGet(null)
time.tick(fiveMinutes) // advance another 5 minutes in time
expectGetFresh(startedAt + 3 * fiveMinutes + 1, true)
expectGet(startedAt + 3 * fiveMinutes + 1)
time.restore()
})
})