/**
 * Created by tushar.mathur on 25/06/16.
 */

'use strict'

import {ReactiveTest, TestScheduler} from 'rx'
import test from 'ava'
import {CreateMTDFile} from '../src/Utils'
import {demux} from 'muxer'
const {onNext, onCompleted} = ReactiveTest
const Hot = (sh, ...args) => () => sh.createHotObservable(...args)
const MockFILE = (sh) => {
  return {
    open: Hot(sh, onNext(210, 19), onCompleted(210)),
    write: Hot(sh, onNext(230, [1000, 'BUFFER-WRITTEN']), onCompleted(230))
  }
}

const MockHTTP = (sh) => {
  return {
    requestHead: Hot(sh,
      onNext(220, {headers: {'content-length': 9000}}),
      onCompleted(220))
  }
}

const pluck = (key, $) => demux($, key)[0][key]
const createParams = (sh, options) => ({
  FILE: MockFILE(sh),
  HTTP: MockHTTP(sh),
  options
})

test('meta$', t => {
  const sh = new TestScheduler()
  const options = {url: '/a/b/c', range: 3}
  const params = createParams(sh, options)
  const {messages} = sh.startScheduler(() => pluck('meta$', CreateMTDFile(params)))
  t.deepEqual(messages, [
    onNext(220, {
      url: '/a/b/c', range: 3, totalBytes: 9000,
      offsets: [0, 3000, 6000],
      threads: [[0, 2999], [3000, 5999], [6000, 9000]]
    }),
    onCompleted(230)
  ])
})

test('written$', t => {
  const sh = new TestScheduler()
  const options = {url: '/a/b/c', range: 3}
  const params = createParams(sh, options)
  const {messages} = sh.startScheduler(() => pluck('written$', CreateMTDFile(params)))
  t.deepEqual(messages, [
    onNext(230, [1000, 'BUFFER-WRITTEN']),
    onCompleted(230)
  ])
})


@@ -593,20 +593,22 @@
 ength': 
+'
 9000
+'
 %7D%7D),%0A   
@@ -1535,28 +1535,351 @@
   onCompleted(230)%0A  %5D)%0A%7D)%0A%0A
+test('remoteFileSize$', t =%3E %7B%0A  const sh = new TestScheduler()%0A  const options = %7Burl: '/a/b/c', range: 3%7D%0A  const params = createParams(sh, options)%0A  const %7Bmessages%7D = sh.startScheduler(() =%3E pluck('remoteFileSize$', CreateMTDFile(params)))%0A  t.deepEqual(messages, %5B%0A    onNext(220, 9000),%0A    onCompleted(230)%0A  %5D)%0A%7D)%0A

add test for CreateMTDFile():remoteFileSize$