<commit_before>_ = require 'underscore'
Q = require 'q'
SphereClient = require '../../lib/client'
Config = require('../../config').config

uniqueId = (prefix) ->
  _.uniqueId "#{prefix}#{new Date().getTime()}_"

newState = ->
  key: uniqueId 's'
  type: 'LineItemState'

updateState = (version) ->
  version: version
  actions: [
    {action: 'setName', name: {en: 'A State'}}
    {action: 'setDescription', description: {en: 'This is a State'}}
  ]

# Increase timeout
jasmine.getEnv().defaultTimeoutInterval = 10000

describe 'Integration Channels', ->

  beforeEach (done) ->
    @client = new SphereClient
      config: Config
      logConfig:
        levelStream: 'info'
        levelFile: 'error'
    @logger = @client._logger

    @client.states.save(newState())
    .then (result) =>
      expect(result.statusCode).toBe 201
      @state = result.body
      @logger.info @state, 'New state created'
      done()
    .fail (error) =>
      @logger.error error
      done(JSON.stringify(error))

  afterEach (done) ->
    @client.states.byId(@state.id).delete(@state.version)
    .then (result) =>
      @logger.info "State deleted: #{@state.id}"
      expect(result.statusCode).toBe 200
      done()
    .fail (error) =>
      @logger.error error
      done(JSON.stringify(error))

  it 'should update a state', (done) ->
    @client.states.byId(@state.id).update(updateState(@state.version))
    .then (result) =>
      expect(result.statusCode).toBe 200
      @state = result.body
      expect(@state.name).toEqual {en: 'A State'}
      expect(@state.description).toEqual {en: 'This is a State'}
      done()
    .fail (error) =>
      @logger.error error
      done(JSON.stringify(error))

  it 'should create some states and use them as transitions references', (done) ->
    Q.all _.map [1..101], => @client.states.save(newState())
    .then (results) =>
      mainState = _.head results
      otherStates = _.tail results
      transitions = _.map otherStates, (r) ->
        id: r.body.id
        typeId: 'state'
      @client.states.byId(mainState.body.id).update
        version: mainState.body.version
        actions: [
          {action: 'setTransitions', transitions: transitions}
        ]
      .then (result) =>
        expect(result.statusCode).toBe 200
        mainState = result.body
        expect(mainState.transitions.length).toBe 100

        # delete states
        Q.all _.map results, (r) => @client.states.byId(r.body.id).delete(r.body.version)
      .then (results) ->
        done()
    .fail (error) =>
      @logger.error error
      done(JSON.stringify(error))
<commit_msg>Ensure state with transitions is deleted first<commit_after>_ = require 'underscore'
Q = require 'q'
SphereClient = require '../../lib/client'
Config = require('../../config').config

uniqueId = (prefix) ->
  _.uniqueId "#{prefix}#{new Date().getTime()}_"

newState = ->
  key: uniqueId 's'
  type: 'LineItemState'

updateState = (version) ->
  version: version
  actions: [
    {action: 'setName', name: {en: 'A State'}}
    {action: 'setDescription', description: {en: 'This is a State'}}
  ]

# Increase timeout
jasmine.getEnv().defaultTimeoutInterval = 10000

describe 'Integration Channels', ->

  beforeEach (done) ->
    @client = new SphereClient
      config: Config
      logConfig:
        levelStream: 'info'
        levelFile: 'error'
    @logger = @client._logger

    @client.states.save(newState())
    .then (result) =>
      expect(result.statusCode).toBe 201
      @state = result.body
      @logger.info @state, 'New state created'
      done()
    .fail (error) =>
      @logger.error error
      done(JSON.stringify(error))

  afterEach (done) ->
    @client.states.byId(@state.id).delete(@state.version)
    .then (result) =>
      @logger.info "State deleted: #{@state.id}"
      expect(result.statusCode).toBe 200
      done()
    .fail (error) =>
      @logger.error error
      done(JSON.stringify(error))

  it 'should update a state', (done) ->
    @client.states.byId(@state.id).update(updateState(@state.version))
    .then (result) =>
      expect(result.statusCode).toBe 200
      @state = result.body
      expect(@state.name).toEqual {en: 'A State'}
      expect(@state.description).toEqual {en: 'This is a State'}
      done()
    .fail (error) =>
      @logger.error error
      done(JSON.stringify(error))

  it 'should create some states and use them as transitions references', (done) ->
    Q.all _.map [1..51], => @client.states.save(newState())
    .then (results) =>
      mainState = _.head(results).body
      otherStates = _.tail results
      transitions = _.map otherStates, (r) ->
        id: r.body.id
        typeId: 'state'
      @client.states.byId(mainState.id).update
        version: mainState.version
        actions: [
          {action: 'setTransitions', transitions: transitions}
        ]
      .then (result) =>
        expect(result.statusCode).toBe 200
        mainState = result.body
        expect(mainState.transitions.length).toBe 50

        # delete states
        @client.states.byId(mainState.id).delete(mainState.version)
      .then (result) =>
        Q.all _.map otherStates, (r) => @client.states.byId(r.body.id).delete(r.body.version)
      .then (results) ->
        done()
    .fail (error) =>
      @logger.error error
      done(JSON.stringify(error))
{'fraction_non_alphanumeric': 0.15151515151515152, 'fraction_numerical': 0.008604564160119716, 'mean_word_length': 2.6623287671232876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 46, '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': '6331371', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1645, 'n_words': 386}