This repository has been archived by the owner on Jul 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaits.ts
75 lines (59 loc) · 1.95 KB
/
waits.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
import * as tape from 'tape';
import {
DisplayBuffer,
InputBuffer,
RTTEvent,
WaitAction
} from '../src/index';
const test = tape.test;
const sleep = (time: number) => {
return new Promise<void>(resolve => {
setTimeout(resolve, time);
});
};
const processEvent = async (display: DisplayBuffer, event: RTTEvent) => {
return new Promise<string>(resolve => {
display.onStateChange = ({ text, drained }) => {
if (drained) {
resolve(text);
}
};
display.process(event);
});
};
export default function runTests() {
test('[Wait] Input delays', async t => {
const input = new InputBuffer();
input.start();
input.update('When I say Vol, you say Tron!');
await sleep(200);
input.update('Vol!');
const event = input.diff();
const waitEvent = event!.actions![1] as WaitAction;
t.equal(waitEvent.type, 'wait', 'Wait action present');
t.ok(waitEvent.num >= 200, 'Wait action for 200+ milliseconds: ' + waitEvent.num);
t.equal(event!.actions!.length, 4);
t.end();
});
test('[Wait] Display delays', async t => {
const display = new DisplayBuffer();
const startTime = Date.now();
const text = await processEvent(display, {
actions: [
{ type: 'insert', text: 'Uhhhh', pos: 0 },
{ type: 'wait', num: 200 },
{ type: 'erase', num: 5, pos: 4 },
{ type: 'insert', text: 'Vol', pos: 0 },
{ type: 'wait', num: 200 },
{ type: 'insert', text: 'tron?', pos: 3 }
],
event: 'new',
seq: 1
});
const endTime = Date.now();
const time = endTime - startTime;
t.equal(text, 'Voltron?', 'Rendered "Voltron?"');
t.ok(time >= 400, 'Wait actions were 400+ milliseconds total: ' + time);
t.end();
});
}