import { expect } from 'chai'
import migrations from '@/lib/storedInquiries/_migrations'

describe('_migrations.js', () => {
  it('migrates from version 1 to the current', () => {
    const oldInquiries = [
      {
        id: '123',
        name: 'foo',
        query: 'SELECT * FROM foo',
        chart: { here_are: 'foo chart settings' },
        createdAt: '2021-05-06T11:05:50.877Z'
      },
      {
        id: '456',
        name: 'bar',
        query: 'SELECT * FROM bar',
        chart: { here_are: 'bar chart settings' },
        createdAt: '2021-05-07T11:05:50.877Z'
      }
    ]

    expect(migrations._migrate(1, oldInquiries)).to.eql([
      {
        id: '123',
        name: 'foo',
        query: 'SELECT * FROM foo',
        viewType: 'chart',
        viewOptions: { here_are: 'foo chart settings' },
        createdAt: '2021-05-06T11:05:50.877Z'
      },
      {
        id: '456',
        name: 'bar',
        query: 'SELECT * FROM bar',
        viewType: 'chart',
        viewOptions: { here_are: 'bar chart settings' },
        createdAt: '2021-05-07T11:05:50.877Z'
      }
    ])
  })

  it('migrates from version 2 to the current', () => {
    const oldInquiries = [
      {
        id: '123',
        name: 'foo',
        query: 'SELECT * FROM foo',
        viewType: 'chart',
        viewOptions: { here_are: 'foo chart settings' },
        createdAt: '2021-05-06T11:05:50.877Z'
      },
      {
        id: '456',
        name: 'bar',
        query: 'SELECT * FROM bar',
        viewType: 'graph',
        viewOptions: {
          structure: {
            nodeId: 'node_id',
            objectType: 'object_type',
            edgeSource: 'source',
            edgeTarget: 'target'
          },
          style: {
            backgroundColor: 'white',
            nodes: {
              size: { type: 'constant', value: 10 },
              color: {
                type: 'calculated',
                method: 'degree',
                colorscale: null,
                mode: 'continious',
                colorscaleDirection: 'reversed'
              },
              label: { source: 'label', color: '#444444' }
            },
            edges: {
              showDirection: true,
              size: { type: 'constant', value: 2 },
              color: { type: 'constant', value: '#a2b1c6' },
              label: { source: null, color: '#a2b1c6' }
            }
          },
          layout: {
            type: 'forceAtlas2',
            options: {
              initialIterationsAmount: 50,
              adjustSizes: false,
              barnesHutOptimize: false,
              barnesHutTheta: 0.5,
              edgeWeightInfluence: 0,
              gravity: 1,
              linLogMode: false,
              outboundAttractionDistribution: false,
              scalingRatio: 1,
              slowDown: 1,
              strongGravityMode: false
            }
          }
        },
        createdAt: '2021-05-07T11:05:50.877Z'
      }
    ]
    expect(migrations._migrate(2, oldInquiries)).to.eql([
      {
        id: '123',
        name: 'foo',
        query: 'SELECT * FROM foo',
        viewType: 'chart',
        viewOptions: { here_are: 'foo chart settings' },
        createdAt: '2021-05-06T11:05:50.877Z'
      },
      {
        id: '456',
        name: 'bar',
        query: 'SELECT * FROM bar',
        viewType: 'graph',
        viewOptions: {
          structure: {
            nodeId: 'node_id',
            objectType: 'object_type',
            edgeSource: 'source',
            edgeTarget: 'target'
          },
          style: {
            backgroundColor: 'white',
            highlightMode: 'node_and_neighbors',
            nodes: {
              size: { type: 'constant', value: 10 },
              color: {
                type: 'calculated',
                method: 'degree',
                colorscale: null,
                mode: 'continious',
                colorscaleDirection: 'reversed',
                opacity: 100
              },
              label: { source: 'label', color: '#444444' }
            },
            edges: {
              showDirection: true,
              size: { type: 'constant', value: 2 },
              color: { type: 'constant', value: '#a2b1c6' },
              label: { source: null, color: '#a2b1c6' }
            }
          },
          layout: {
            type: 'forceAtlas2',
            options: {
              initialAlgorithm: 'circular',
              initialIterationsAmount: 50,
              adjustSizes: false,
              barnesHutOptimize: false,
              barnesHutTheta: 0.5,
              edgeWeightInfluence: 0,
              gravity: 1,
              linLogMode: false,
              outboundAttractionDistribution: false,
              scalingRatio: 1,
              slowDown: 1,
              strongGravityMode: false
            }
          }
        },
        createdAt: '2021-05-07T11:05:50.877Z'
      }
    ])
  })

  it('migrates from version 3 to the current', () => {
    const oldInquiries = [
      {
        id: '123',
        name: 'foo',
        query: 'SELECT * FROM foo',
        viewType: 'chart',
        viewOptions: { here_are: 'foo chart settings' },
        createdAt: '2021-05-06T11:05:50.877Z'
      },
      {
        id: '456',
        name: 'bar',
        query: 'SELECT * FROM bar',
        viewType: 'graph',
        viewOptions: {
          structure: {
            nodeId: 'node_id',
            objectType: 'object_type',
            edgeSource: 'source',
            edgeTarget: 'target'
          },
          style: {
            backgroundColor: 'white',
            highlightMode: 'node_and_neighbors',
            nodes: {
              size: { type: 'constant', value: 10 },
              color: {
                type: 'calculated',
                method: 'degree',
                colorscale: null,
                mode: 'continious',
                colorscaleDirection: 'reversed',
                opacity: 100
              },
              label: { source: 'label', color: '#444444' }
            },
            edges: {
              showDirection: true,
              size: { type: 'constant', value: 2 },
              color: { type: 'constant', value: '#a2b1c6' },
              label: { source: null, color: '#a2b1c6' }
            }
          },
          layout: {
            type: 'forceAtlas2',
            options: {
              initialIterationsAmount: 50,
              adjustSizes: false,
              barnesHutOptimize: false,
              barnesHutTheta: 0.5,
              edgeWeightInfluence: 0,
              gravity: 1,
              linLogMode: false,
              outboundAttractionDistribution: false,
              scalingRatio: 1,
              slowDown: 1,
              strongGravityMode: false
            }
          }
        },
        createdAt: '2021-05-07T11:05:50.877Z'
      }
    ]
    expect(migrations._migrate(3, oldInquiries)).to.eql([
      {
        id: '123',
        name: 'foo',
        query: 'SELECT * FROM foo',
        viewType: 'chart',
        viewOptions: { here_are: 'foo chart settings' },
        createdAt: '2021-05-06T11:05:50.877Z'
      },
      {
        id: '456',
        name: 'bar',
        query: 'SELECT * FROM bar',
        viewType: 'graph',
        viewOptions: {
          structure: {
            nodeId: 'node_id',
            objectType: 'object_type',
            edgeSource: 'source',
            edgeTarget: 'target'
          },
          style: {
            backgroundColor: 'white',
            highlightMode: 'node_and_neighbors',
            nodes: {
              size: { type: 'constant', value: 10 },
              color: {
                type: 'calculated',
                method: 'degree',
                colorscale: null,
                mode: 'continious',
                colorscaleDirection: 'reversed',
                opacity: 100
              },
              label: { source: 'label', color: '#444444' }
            },
            edges: {
              showDirection: true,
              size: { type: 'constant', value: 2 },
              color: { type: 'constant', value: '#a2b1c6' },
              label: { source: null, color: '#a2b1c6' }
            }
          },
          layout: {
            type: 'forceAtlas2',
            options: {
              initialAlgorithm: 'circular',
              initialIterationsAmount: 50,
              adjustSizes: false,
              barnesHutOptimize: false,
              barnesHutTheta: 0.5,
              edgeWeightInfluence: 0,
              gravity: 1,
              linLogMode: false,
              outboundAttractionDistribution: false,
              scalingRatio: 1,
              slowDown: 1,
              strongGravityMode: false
            }
          }
        },
        createdAt: '2021-05-07T11:05:50.877Z'
      }
    ])
  })
})
