import test from "ava"

import { fake, mock } from "sinon"

import { identity } from "ramda"

import Factory from "@/factory"

import Socket from "~/app/models/socket"

const universe = mock()

test("constructor sets a uuid", t => {
  const socket = new Socket(universe, { on: identity })
  t.truthy(socket.uuid)
})

test("send", async t => {
  const send = fake()
  const socket = new Socket(universe, { send, on: identity })

  await socket.send({ foo: "bar" })

  t.is('{"foo":"bar"}', send.lastCall.lastArg)
})

test("send when throws an error", t => {
  const send = () => { throw new Error() }
  const socket = new Socket(universe, { send, on: identity })

  t.throws(() => { socket.send({ foo: "bar" }) })
})

test("connected", async t => {
  const addSocket = fake()
  const universe = { addSocket }

  const socket = new Socket(universe, { send: identity, on: identity })

  await socket.connected()

  t.true(addSocket.calledOnce)
})

test("close", t => {
  const removeSocket = fake()
  const universe = { removeSocket }

  const socket = new Socket(universe, { on: identity })

  socket.close()

  t.pass()
})

test("message", async t => {
  const play = fake()
  const universe = { play }

  const user = await Factory.user()
  const send = fake()

  const socket = new Socket(universe, { send, on: identity }, user)

  await socket.message(JSON.stringify({ action: "play" }))

  t.pass()
})

import test from "ava"

import { fake, mock } from "sinon"

import { identity } from "ramda"

import Factory from "@/factory"

import Socket from "~/app/models/socket"

const universe = mock()

test("constructor sets a uuid", t => {
  const socket = new Socket(universe, { on: identity })
  t.truthy(socket.uuid)
})

test("send", async t => {
  const send = fake()
  const socket = new Socket(universe, { send, on: identity })

  await socket.send({ foo: "bar" })

  t.is('{"foo":"bar"}', send.lastCall.lastArg)
})

test("send when throws an error", t => {
  const send = () => { throw new Error() }
  const socket = new Socket(universe, { send, on: identity })

  t.throws(() => { socket.send({ foo: "bar" }) })
})

test("connected", async t => {
  const games = { length: () => { return 0 } }

  const addSocket = fake()
  const universe = { addSocket, games}

  const socket = new Socket(universe, { send: identity, on: identity })

  await socket.connected()

  t.true(addSocket.calledOnce)
})

test("close", t => {
  const removeSocket = fake()
  const universe = { removeSocket }

  const socket = new Socket(universe, { on: identity })

  socket.close()

  t.pass()
})

test("message", async t => {
  const play = fake()
  const universe = { play }

  const user = await Factory.user()
  const send = fake()

  const socket = new Socket(universe, { send, on: identity }, user)

  await socket.message(JSON.stringify({ action: "play" }))

  t.pass()
})

fix test