<commit_before>/* eslint-disable no-unused-expressions */

'use strict'

const chai = require('chai')
const each = require('async/each')
const eachSeries = require('async/eachSeries')
const expect = chai.expect
const parallel = require('async/parallel')
const series = require('async/series')
const WebTorrent = require('webtorrent')
const webtorrent = new WebTorrent()

const loginUtils = require('../utils/login')
const miscsUtils = require('../utils/miscs')
const podsUtils = require('../utils/pods')
const serversUtils = require('../utils/servers')
const videosUtils = require('../utils/videos')

describe('Test multiple pods', function () {
  let servers = []
  const toRemove = []

  before(function (done) {
    this.timeout(30000)

    series([
      // Run servers
      function (next) {
        serversUtils.flushAndRunMultipleServers(3, function (serversRun) {
          servers = serversRun
          next()
        })
      },
      // Get the access tokens
      function (next) {
        each(servers, function (server, callbackEach) {
          loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
            if (err) return callbackEach(err)

            server.accessToken = accessToken
            callbackEach()
          })
        }, next)
      },
      // The second pod make friend with the third
      function (next) {
        const server = servers[1]
        podsUtils.makeFriends(server.url, server.accessToken, next)
      },
      // Wait for the request between pods
      function (next) {
        setTimeout(next, 10000)
      },
      // Pod 1 make friends too
      function (next) {
        const server = servers[0]
        podsUtils.makeFriends(server.url, server.accessToken, next)
      }
    ], done)
  })

  it('Should not have videos for all pods', function (done) {
    each(servers, function (server, callback) {
      videosUtils.getVideosList(server.url, function (err, res) {
        if (err) throw err

        const videos = res.body.data
        expect(videos).to.be.an('array')
        expect(videos.length).to.equal(0)

        callback()
      })
    }, done)
  })

  describe('Should upload the video and propagate on each pod', function () {
    it('Should upload the video on pod 1 and propagate on each pod', function (done) {
      // Pod 1 has video transcoding activated
      this.timeout(15000)

      series([
        function (next) {
          const videoAttributes = {
            name: 'my super name for pod 1',
            category: 5,
            licence: 4,
            language: 9,
            nsfw: true,
            description: 'my super description for pod 1',
            tags: [ 'tag1p1', 'tag2p1' ],
            fixture: 'video_short1.webm'
          }
          videosUtils.uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes, next)
        },
        function (next) {
          setTimeout(next, 11000)
        }],
        // All pods should have this video
        function (err) {
          if (err) throw err

          each(servers, function (server, callback) {
            let baseMagnet = null

            videosUtils.getVideosList(server.url, function (err, res) {
              if (err) throw err

              const videos = res.body.data
              expect(videos).to.be.an('array')
              expect(videos.length).to.equal(1)
              const video = videos[0]
              expect(video.name).to.equal('my super name for pod 1')
              expect(video.category).to.equal(5)
              expect(video.categoryLabel).to.equal('Sports')
              expect(video.licence).to.equal(4)
              expect(video.licenceLabel).to.equal('Attribution - Non Commercial')
              expect(video.language).to.equal(9)
              expect(video.languageLabel).to.equal('Japanese')
              expect(video.nsfw).to.be.truthy
              expect(video.description).to.equal('my super description for pod 1')
              expect(video.podHost).to.equal('localhost:9001')
              expect(video.magnetUri).to.exist
              expect(video.duration).to.equal(10)
              expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ])
              expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
              expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
              expect(video.author).to.equal('root')

              if (server.url !== 'http://localhost:9001') {
                expect(video.isLocal).to.be.false
              } else {
                expect(video.isLocal).to.be.true
              }

              // All pods should have the same magnet Uri
              if (baseMagnet === null) {
                baseMagnet = video.magnetUri
              } else {
                expect(video.magnetUri).to.equal.magnetUri
              }

              videosUtils.testVideoImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) {
                if (err) throw err
                expect(test).to.equal(true)

                callback()
              })
            })
          }, done)
        }
      )
    })

    it('Should upload the video on pod 2 and propagate on each pod', function (done) {
      this.timeout(30000)

      series([
        function (next) {
          const videoAttributes = {
            name: 'my super name for pod 2',
            category: 4,
            licence: 3,
            language: 11,
            nsfw: true,
            description: 'my super description for pod 2',
            tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
            fixture: 'video_short2.webm'
          }
          videosUtils.uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes, next)
        },
        function (next) {
          // Transcoding, so wait more that 22 seconds
          setTimeout(next, 42000)
        }],
        // All pods should have this video<commit_msg>Oup's, forgot to increate timeout test<commit_after>/* eslint-disable no-unused-expressions */

'use strict'

const chai = require('chai')
const each = require('async/each')
const eachSeries = require('async/eachSeries')
const expect = chai.expect
const parallel = require('async/parallel')
const series = require('async/series')
const WebTorrent = require('webtorrent')
const webtorrent = new WebTorrent()

const loginUtils = require('../utils/login')
const miscsUtils = require('../utils/miscs')
const podsUtils = require('../utils/pods')
const serversUtils = require('../utils/servers')
const videosUtils = require('../utils/videos')

describe('Test multiple pods', function () {
  let servers = []
  const toRemove = []

  before(function (done) {
    this.timeout(30000)

    series([
      // Run servers
      function (next) {
        serversUtils.flushAndRunMultipleServers(3, function (serversRun) {
          servers = serversRun
          next()
        })
      },
      // Get the access tokens
      function (next) {
        each(servers, function (server, callbackEach) {
          loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
            if (err) return callbackEach(err)

            server.accessToken = accessToken
            callbackEach()
          })
        }, next)
      },
      // The second pod make friend with the third
      function (next) {
        const server = servers[1]
        podsUtils.makeFriends(server.url, server.accessToken, next)
      },
      // Wait for the request between pods
      function (next) {
        setTimeout(next, 10000)
      },
      // Pod 1 make friends too
      function (next) {
        const server = servers[0]
        podsUtils.makeFriends(server.url, server.accessToken, next)
      }
    ], done)
  })

  it('Should not have videos for all pods', function (done) {
    each(servers, function (server, callback) {
      videosUtils.getVideosList(server.url, function (err, res) {
        if (err) throw err

        const videos = res.body.data
        expect(videos).to.be.an('array')
        expect(videos.length).to.equal(0)

        callback()
      })
    }, done)
  })

  describe('Should upload the video and propagate on each pod', function () {
    it('Should upload the video on pod 1 and propagate on each pod', function (done) {
      // Pod 1 has video transcoding activated
      this.timeout(15000)

      series([
        function (next) {
          const videoAttributes = {
            name: 'my super name for pod 1',
            category: 5,
            licence: 4,
            language: 9,
            nsfw: true,
            description: 'my super description for pod 1',
            tags: [ 'tag1p1', 'tag2p1' ],
            fixture: 'video_short1.webm'
          }
          videosUtils.uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes, next)
        },
        function (next) {
          setTimeout(next, 11000)
        }],
        // All pods should have this video
        function (err) {
          if (err) throw err

          each(servers, function (server, callback) {
            let baseMagnet = null

            videosUtils.getVideosList(server.url, function (err, res) {
              if (err) throw err

              const videos = res.body.data
              expect(videos).to.be.an('array')
              expect(videos.length).to.equal(1)
              const video = videos[0]
              expect(video.name).to.equal('my super name for pod 1')
              expect(video.category).to.equal(5)
              expect(video.categoryLabel).to.equal('Sports')
              expect(video.licence).to.equal(4)
              expect(video.licenceLabel).to.equal('Attribution - Non Commercial')
              expect(video.language).to.equal(9)
              expect(video.languageLabel).to.equal('Japanese')
              expect(video.nsfw).to.be.truthy
              expect(video.description).to.equal('my super description for pod 1')
              expect(video.podHost).to.equal('localhost:9001')
              expect(video.magnetUri).to.exist
              expect(video.duration).to.equal(10)
              expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ])
              expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
              expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
              expect(video.author).to.equal('root')

              if (server.url !== 'http://localhost:9001') {
                expect(video.isLocal).to.be.false
              } else {
                expect(video.isLocal).to.be.true
              }

              // All pods should have the same magnet Uri
              if (baseMagnet === null) {
                baseMagnet = video.magnetUri
              } else {
                expect(video.magnetUri).to.equal.magnetUri
              }

              videosUtils.testVideoImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) {
                if (err) throw err
                expect(test).to.equal(true)

                callback()
              })
            })
          }, done)
        }
      )
    })

    it('Should upload the video on pod 2 and propagate on each pod', function (done) {
      this.timeout(60000)

      series([
        function (next) {
          const videoAttributes = {
            name: 'my super name for pod 2',
            category: 4,
            licence: 3,
            language: 11,
            nsfw: true,
            description: 'my super description for pod 2',
            tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
            fixture: 'video_short2.webm'
          }
          videosUtils.uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes, next)
        },
        function (next) {
          // Transcoding, so wait more that 22 seconds
          setTimeout(next, 42000)
        }],
        // All pods should have this video
{'fraction_non_alphanumeric': 0.11653596424656379, 'fraction_numerical': 0.015009697276330213, 'mean_word_length': 1.9040156709108718, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4803090', 'n_tokens_mistral': 3437, 'n_tokens_neox': 3277, 'n_words': 796}