test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* global browser, expect */
  2. const crypto = require('crypto')
  3. const lorem = require('@jamen/lorem')
  4. const { selectFakeFile } = require('../utils')
  5. const testURL = 'http://localhost:4567/chaos-monkey'
  6. describe('Chaos monkey', function () {
  7. this.timeout(5 * 60 * 1000) // 5 minutes
  8. beforeEach(async () => {
  9. await browser.url(testURL)
  10. })
  11. it('Add and cancel a bunch', async () => {
  12. await browser.execute(function () {
  13. window.currentUppy = window.setup({ limit: 3 })
  14. window.onerror = function (message) {
  15. window.anyError = message
  16. }
  17. })
  18. const types = ['application/octet-stream', 'text/plain']
  19. const generate = {
  20. 'application/octet-stream' () {
  21. const len = Math.round(Math.random() * 5000000)
  22. return crypto.randomBytes(len)
  23. },
  24. 'text/plain' () {
  25. const len = Math.round(Math.random() * 5000000)
  26. return Buffer.from(lorem(len))
  27. }
  28. }
  29. async function addFile () {
  30. await browser.execute(function () {
  31. window.addLogMessage('Adding a file')
  32. })
  33. const type = types[Math.floor(Math.random() * types.length)]
  34. const data = generate[type]().toString('base64')
  35. const name = `${Math.random().toString(32).slice(2)}-file`
  36. await browser.execute(selectFakeFile, 'currentUppy', name, type, data)
  37. }
  38. function cancelFile () {
  39. return browser.execute(function () {
  40. window.addLogMessage('Cancelling a file')
  41. // prefer deleting a file that is uploading right now
  42. var selector = Math.random() <= 0.7
  43. ? '.is-inprogress .uppy-DashboardItem-action--remove'
  44. : '.uppy-DashboardItem-action--remove'
  45. var buttons = document.querySelectorAll(selector)
  46. var del = buttons[Math.floor(Math.random() * buttons.length)]
  47. if (del) del.click()
  48. })
  49. }
  50. function startUploadIfAnyWaitingFiles () {
  51. return browser.execute(function () {
  52. window.addLogMessage('Starting upload')
  53. var start = document.querySelector('.uppy-StatusBar-actionBtn--upload')
  54. if (start) start.click()
  55. })
  56. }
  57. function cancelAll () {
  58. return browser.execute(function () {
  59. window.addLogMessage('Cancelling everything')
  60. var button = document.querySelector('.uppy-DashboardContent-back')
  61. if (button) button.click()
  62. })
  63. }
  64. await addFile()
  65. await addFile()
  66. await addFile()
  67. for (let i = 0; i < 300; i++) {
  68. await browser.pause(50 + Math.floor(Math.random() * 300))
  69. const v = Math.floor(Math.random() * 100)
  70. if (v < 45) {
  71. await addFile()
  72. } else if (v < 55) {
  73. await cancelFile()
  74. } else if (v === 55) {
  75. await cancelAll()
  76. } else if (v < 75) {
  77. await startUploadIfAnyWaitingFiles()
  78. } else {
  79. // wait
  80. }
  81. }
  82. await cancelAll()
  83. const errorMessage = await browser.execute(function () {
  84. return window.anyError
  85. })
  86. // yikes chai, why can this not be a function call
  87. expect(errorMessage).to.not.exist // eslint-disable-line no-unused-expressions
  88. })
  89. })