-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimers.spec.ts
100 lines (83 loc) · 3.11 KB
/
timers.spec.ts
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
/*
* Copyright (c) 2022-2024 The Peacock Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, Timer } from "../src"
import assert from "assert"
import callSpy from "call-spy"
const data = {
After1: [
{
$after: 5,
},
{},
],
After2: [
{
$after: 8,
},
{},
],
}
describe("$after", () => {
it("returns false with no timer array specified", () => {
const [sm, vars] = data.After1
assert.strictEqual(test(sm, vars), false)
})
it("won't try to evaluate if no timestamp is specified", () => {
const [sm, vars] = data.After1
const [logger, loggerCallDetails] = callSpy((category, message) => {
if (category === "validation") {
assert.strictEqual(
message,
"No event timestamp found when timer is supposed to be active",
)
}
})
assert.strictEqual(test(sm, vars, { timers: [], logger }), false)
assert.strictEqual(loggerCallDetails.called, true)
})
it("supports basic timers", () => {
const [sm, vars] = data.After2
const timers: Timer[] = []
const result = test(sm, vars, { timers, eventTimestamp: 0 })
assert.strictEqual(timers.length, 1, "incorrect timer count")
assert.strictEqual(timers[0].startTime, 0, "start time not correct")
assert.strictEqual(timers[0].endTime, 8, "end time not correct")
assert.strictEqual(result, false, "timer returned true")
// now, let's try again, pretending 8 seconds have passed
const result2 = test(sm, vars, {
timers,
eventTimestamp: 8,
})
assert.strictEqual(timers.length, 0, "incorrect timer count")
assert.strictEqual(result2, true, "timer returned false")
// Without a timer in effect, a new timer should be created.
const result3 = test(sm, vars, {
timers,
eventTimestamp: 9,
})
assert.strictEqual(timers.length, 1, "incorrect timer count")
assert.strictEqual(timers[0].startTime, 9, "start time not correct")
assert.strictEqual(timers[0].endTime, 17, "end time not correct")
assert.strictEqual(result3, false, "timer returned true")
// The second timer is up.
const result4 = test(sm, vars, {
timers,
eventTimestamp: 18,
})
assert.strictEqual(timers.length, 0, "incorrect timer count")
assert.strictEqual(result4, true, "timer returned false")
})
})