/
test.js
102 lines (88 loc) · 3.02 KB
/
test.js
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
/* global browser */
const crypto = require('crypto')
const { selectFakeFile } = require('../utils')
const testURL = 'http://localhost:4567/chaos-monkey'
describe('Chaos monkey', function test () {
this.timeout(5 * 60 * 1000) // 5 minutes
beforeEach(async () => {
await browser.url(testURL)
})
it('Add and cancel a bunch', async () => {
await browser.execute(() => {
window.currentUppy = window.setup({ limit: 3 })
window.onerror = (message) => {
window.anyError = message
}
})
const types = ['application/octet-stream', 'text/plain']
const generate = {
'application/octet-stream' () {
const len = Math.round(Math.random() * 5_000_000)
return crypto.randomBytes(len)
},
'text/plain' () {
const len = Math.round(Math.random() * 5_000_000 / 'Lorem ipsum'.length)
return Buffer.from('Lorem ipsum'.repeat(len))
},
}
async function addFile () {
await browser.execute(() => {
window.addLogMessage('Adding a file')
})
const type = types[Math.floor(Math.random() * types.length)]
const data = generate[type]().toString('base64')
const name = `${Math.random().toString(32).slice(2)}-file`
await browser.execute(selectFakeFile, 'currentUppy', name, type, data)
}
function cancelFile () {
return browser.execute(() => {
window.addLogMessage('Cancelling a file')
// prefer deleting a file that is uploading right now
const selector = Math.random() <= 0.7
? '.is-inprogress .uppy-Dashboard-Item-action--remove'
: '.uppy-Dashboard-Item-action--remove'
const buttons = document.querySelectorAll(selector)
const del = buttons[Math.floor(Math.random() * buttons.length)]
if (del) del.click()
})
}
function startUploadIfAnyWaitingFiles () {
return browser.execute(() => {
window.addLogMessage('Starting upload')
const start = document.querySelector('.uppy-StatusBar-actionBtn--upload')
if (start) start.click()
})
}
function cancelAll () {
return browser.execute(() => {
window.addLogMessage('Cancelling everything')
const button = document.querySelector('.uppy-DashboardContent-back')
if (button) button.click()
})
}
await addFile()
await addFile()
await addFile()
for (let i = 0; i < 300; i++) {
await browser.pause(50 + Math.floor(Math.random() * 300))
const v = Math.floor(Math.random() * 100)
if (v < 45) {
await addFile()
} else if (v < 55) {
await cancelFile()
} else if (v === 55) {
await cancelAll()
} else if (v < 75) {
await startUploadIfAnyWaitingFiles()
} else {
// wait
}
}
await cancelAll()
const errorMessage = await browser.execute(() => {
return window.anyError
})
// yikes chai, why can this not be a function call
expect(errorMessage).to.not.exist // eslint-disable-line no-unused-expressions
})
})