forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-time.test.ts
More file actions
88 lines (71 loc) · 3.18 KB
/
run-time.test.ts
File metadata and controls
88 lines (71 loc) · 3.18 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
import { launchBrowser, MOCK_HOST, MOCK_URL, sleep, type LaunchContext } from "../common/base"
import { parseTime2Sec, readRecordsOfFirstPage } from "../common/record"
import { createWhitelist } from "../common/whitelist"
let context: LaunchContext
async function clickRunTimeChange(siteHost: string): Promise<void> {
const sitePage = await context.openAppPage("/additional/site-manage")
await sitePage.focus('input[placeholder]')
await sitePage.keyboard.type(siteHost)
await sitePage.keyboard.press('Enter')
await sleep(.1)
await sitePage.evaluate(() => {
const runTimeSwitch = document.querySelector<HTMLDivElement>('table > tbody > tr > td.el-table_1_column_7 .el-switch')
runTimeSwitch?.click()
})
}
describe('Run time tracking', () => {
beforeEach(async () => context = await launchBrowser())
afterEach(() => context.close())
test('Basically track', async () => {
await context.newPageAndWaitCsInjected(MOCK_URL)
await sleep(1.1)
let records = await readRecordsOfFirstPage(context)
let record = records[0]
expect(parseTime2Sec(record.time)).toBeGreaterThanOrEqual(1)
expect(record.runTime).toBeFalsy()
// 1. Enable run time tracking
const enableTs = Date.now()
await clickRunTimeChange(MOCK_HOST)
// 2. Sleep
const emptyPage = await context.newPage()
await sleep(1.1)
records = await readRecordsOfFirstPage(context)
const runTime1 = parseTime2Sec(records[0]?.runTime) ?? 0
expect(runTime1).toBeGreaterThanOrEqual(1)
// 3. Add another page sharing the same run time with old page
await context.newPageAndWaitCsInjected(MOCK_URL)
// jump to new page
await emptyPage.bringToFront()
await sleep(1)
records = await readRecordsOfFirstPage(context)
const runTime2 = parseTime2Sec(records[0].runTime)
expect(runTime2).toBeGreaterThanOrEqual(runTime1 + 1)
expect(runTime2).toBeLessThan((Date.now() - enableTs) / 1000)
// 3. Disable run time tracking
await clickRunTimeChange(MOCK_HOST)
const disableTs = Date.now()
await emptyPage.bringToFront()
await sleep(4)
records = await readRecordsOfFirstPage(context)
const runTime3 = parseTime2Sec(records[0].runTime)
expect(runTime3).toBeLessThanOrEqual(Math.round((disableTs - enableTs) / 1000))
}, 60000)
test('white list', async () => {
await context.newPage(MOCK_URL)
// 1. Enable
await clickRunTimeChange(MOCK_HOST)
const enableTs = Date.now()
await sleep(4)
let records = await readRecordsOfFirstPage(context)
const runTime = parseTime2Sec(records[0].runTime)
expect(runTime).toBeTruthy()
expect(runTime).toBeLessThanOrEqual((Date.now() - enableTs) / 1000 + 1)
// 2. Add whitelist
await createWhitelist(context, MOCK_HOST)
const disableTs = Date.now()
await sleep(2)
records = await readRecordsOfFirstPage(context)
const runTime1 = parseTime2Sec(records[0].runTime)
expect(runTime1).toBeLessThan((disableTs - enableTs) / 1000 + 1)
}, 60000)
})