{"size": 4640, "ext": "js", "max_stars_repo_path": "test/browser/data-size.js", "max_stars_repo_name": "mstdokumaci/stx", "max_stars_repo_stars_event_min_datetime": "2018-06-28T11:42:20.000Z", "max_stars_repo_stars_event_max_datetime": "2020-09-08T08:40:58.000Z", "max_issues_repo_path": "test/browser/data-size.js", "max_issues_repo_name": "mstdokumaci/stx", "max_issues_repo_issues_event_min_datetime": "2020-03-13T21:06:55.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-04T19:44:17.000Z", "max_forks_repo_path": "test/browser/data-size.js", "max_forks_repo_name": "mstdokumaci/stx", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 33, "max_issues_count": 7, "max_forks_count": null, "avg_line_length": 40.0, "max_line_length": 97, "alphanum_fraction": 0.663362069}
const test = require('tape')
const { create } = require('../../dist')

const handler = require('serve-handler')
const http = require('http')
const puppeteer = require('puppeteer')

test('browser - data size', { timeout: 5000 }, async t => {
  const master = create()
  const server = master.listen(7070)

  const bigData = { here: 'it is' }
  const val = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor ' +
    'incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud ' +
    'exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure ' +
    'dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ' +
    'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit' +
    'anim id est laborum. Blandit inimicus pri ex, no placerat invenire vim. In ignota noster ' +
    'dicunt mel. Odio omnis everti duo ea. Nulla detraxit deserunt et nec, ne tempor pericula ' +
    'his. Per epicurei interesset an, cum te menandri ullamcorper, ut mea velit inciderint. ' +
    'Justo facer nam ea, ius scripta alienum delectus no. Usu in periculis mediocritatem, quo ' +
    'inermis epicuri delectus ut, mei cu nominavi rationibus voluptatibus. At diam facilisis ' +
    'eos, nam ad copiosae insolens. Vix ei ipsum dicam interpretaris. Augue impetus suscipit ' +
    'in has. Eum id tale augue denique, euripidis persecuti et vis. At recteque persequeris ' +
    'vel. Ludus partem maluisset duo ne. Eum an idque eligendi. Ei utroque voluptaria eos. ' +
    'Et omnis tincidunt ius, et cetero sapientem facilisis ius. Pro te lobortis pertinacia. ' +
    'Nobis vidisse detraxit sit ut. Cu splendide complectitur sea, mei ea decore nusquam ' +
    'invenire. Vix no mollis tamquam eligendi, id cum feugait nominavi, ut regione delenit ' +
    'sapientem est. Vis an disputando repudiandae, qui illud incorrupte te, sed maluisset ' +
    'consetetur posidonium te. Amet placerat eum ne, qui sumo partiendo salutandi ea. Mel ' +
    'case civibus eligendi ei, ei causae cetero eum, stet impetus ea est. Urbanitas ' +
    'signiferumque ex per. An dico habeo disputationi eam, vim te meis antiopam sententiae, ' +
    'propriae periculis adversarium eos ex. Insolens percipitur efficiantur qui at, dicam ' +
    'qualisque appellantur ne per. In saepe delenit incorrupte eam, antiopam elaboraret id ' +
    'eum. Mazim noluisse definitiones has ad, vel id erat equidem. Ex qui inani iusto delenit.'
  let i = 7e2
  while (i--) {
    const d = 1e11 + Math.round(Math.random() * 1e9) + i
    bigData[`key-${d}-longer-string-${d}`] = {
      keyOne: { subKeyOne: val, subKeyTwo: val, subKeyThree: val },
      keyTwo: { subKeyOne: val, subKeyTwo: val, subKeyThree: val },
      keyThree: { subKeyOne: val, subKeyTwo: val, subKeyThree: val },
      keyFour: { subKeyOne: val, subKeyTwo: val, subKeyThree: val },
      keyFive: { subKeyOne: val, subKeyTwo: val, subKeyThree: val }
    }
  }

  master.set({ bigData })

  const httpServer = http.createServer(
    (request, response) => handler(
      request,
      response,
      {
        public: './dist/',
        renderSingle: true
      }
    )
  )

  httpServer.listen(8888)

  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('http://localhost:8888')

  const fired = []
  page.on('console', msg => fired.push(msg.text()))

  await page.evaluate(
    window => new Promise(resolve => {
      window.master = window.stx.create()

      window.master.get('bigData', {}).subscribe(bigData => {
        if (bigData.get('here')) {
          console.log('bigData.here', bigData.get('here').compute())
          resolve()
        }
      })

      window.client = window.master.connect('ws://localhost:7070')
    }).catch(e => console.log(e.message, e.stack)),
    await page.evaluateHandle('window')
  )

  master.set({
    otherData: {
      here: 'it is'
    }
  })

  await page.evaluate(
    window => new Promise(resolve => {
      window.master.get('otherData', {}).subscribe(otherData => {
        if (otherData.get('here')) {
          console.log('otherData.here', otherData.get('here').compute())
          window.client.socket.close()
          resolve()
        }
      })
    }).catch(e => console.log(e.message, e.stack)),
    await page.evaluateHandle('window')
  )

  t.same(
    fired,
    ['bigData.here it is', 'otherData.here it is'],
    'fired = [ bigData.here it is, otherData.here it is ]'
  )

  await browser.close()
  await server.close()
  await httpServer.close()
  t.end()
})
