<commit_before>import dotenv from 'dotenv'
import 'fetch-everywhere'
import test from 'ava'
import Deskbookers from '../src'
import faker from 'faker'
dotenv.load()

const {
  LOGIN_EMAIL,
  LOGIN_PASSWORD
} = process.env

function client () {
  return new Deskbookers({
    https: process.env.API_HTTPS === 'true',
    host: process.env.API_HOST
  })
}

async function login (t, deskbookers, dbg) {
  const user = await deskbookers.account.login(
    LOGIN_EMAIL,
    LOGIN_PASSWORD
  )

  t.truthy(deskbookers.session)

  return user
}

test('Login', async t => {
  const user = await login(t, client(), 'Login')

  t.truthy(user.id)
})

test('Signup', async t => {
  const signup = await client().account.signup({
    firstName: faker.name.firstName(),
    lastName: faker.name.lastName(),
    email: faker.internet.email(),
    password: '<PASSWORD>'
  })

  t.truthy(signup.id)
})

test('Forgot', async t => {
  const forgot = await client().account.forgot(faker.internet.email())

  t.truthy(forgot)
})

test('Logout', async t => {
  const deskbookers = client()
  await login(t, deskbookers, 'Logout')

  t.truthy(deskbookers.session)

  // Logout
  await deskbookers.account.logout()

  t.is(deskbookers.session, null)
})

test('Retrieve', async t => {
  const deskbookers = client()

  // Should fail whilte logged out
  t.throws(deskbookers.account.retrieve())

  await login(t, deskbookers, 'Retrieve')

  // Should succeed while logged in
  t.notThrows(deskbookers.account.retrieve())
})

test('Set language', async t => {
  // Login
  const deskbookers = client()
  await login(t, deskbookers, 'Set language')

  // Set language test
  for (let language of ['en-gb']) { // 'en-gb', 'nl-nl', 'de-de']) {
    const result = await deskbookers.account.setLanguage(language)
    const user = await deskbookers.account.retrieve()

    t.truthy(result)
    t.is(user.language, language)
  }
})<commit_msg>test: add preference test, refactor others<commit_after>import dotenv from 'dotenv'
import 'fetch-everywhere'
import test from 'ava'
import Deskbookers from '../src'
import faker from 'faker'
dotenv.load()

const {
  LOGIN_EMAIL,
  LOGIN_PASSWORD
} = process.env

async function client (login = false) {
  const deskbookers = new Deskbookers({
    https: process.env.API_HTTPS === 'true',
    host: process.env.API_HOST
  })

  if (login) {
    await deskbookers.account.login(LOGIN_EMAIL, LOGIN_PASSWORD)
  }

  return deskbookers
}

test('Login', async t => {
  const deskbookers = await client(true)
  t.truthy(deskbookers.session)
})

test('Signup', async t => {
  const deskbookers = await client()
  const signup = await deskbookers.account.signup({
    firstName: faker.name.firstName(),
    lastName: faker.name.lastName(),
    email: faker.internet.email(),
    password: '<PASSWORD>'
  })

  t.truthy(signup.id)
})

test('Forgot', async t => {
  const deskbookers = await client()
  const forgot = await deskbookers.account.forgot(faker.internet.email())
  t.truthy(forgot)
})

test('Logout', async t => {
  const deskbookers = await client(true)
  t.truthy(deskbookers.session)

  // Logout
  await deskbookers.account.logout()

  t.is(deskbookers.session, null)
})

test('Retrieve', async t => {
  const deskbookers = await client()

  // Fail when logged out
  t.throws(deskbookers.account.retrieve())


  // Succeed when logged in
  await deskbookers.account.login(LOGIN_EMAIL, LOGIN_PASSWORD)
  t.notThrows(deskbookers.account.retrieve())
})

test('Set language', async t => {
  const deskbookers = await client(true)

  const language = 'en-gb'
  const result = await deskbookers.account.setLanguage(language)
  const user = await deskbookers.account.retrieve()

  t.truthy(result)
  t.is(user.language, language)
})

test('Preferences', async t => {
  const deskbookers = await client(true)

  // Create preference
  const create = await deskbookers.account.preferences.create({
    key: 'foo',
    value: 'bar'
  })

  // Retrieve single
  const retrieve = await deskbookers.account.preferences.retrieve('foo')

  // List all
  const list = await deskbookers.account.preferences.list()

  // Test
  t.is(create.foo, 'bar')
  t.is(retrieve, 'bar')
  t.truthy(list)
})
{'fraction_non_alphanumeric': 0.14064362336114422, 'fraction_numerical': 0.0, 'mean_word_length': 3.8452655889145495, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, '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': '5783184', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1473, 'n_words': 334}