# frozen_string_literal: true

require_relative '../spec_helper'

RSpec.describe(TestController) do
  # GET /test - redirects to /test/list
  describe 'GET /test' do
    before { get '/test' }

    it 'Test base route is a redirect' do
      expect(last_response).to(be_redirect)
    end

    it 'Test base route is an HTML response' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it 'Test base route Location header points to /test/list' do
      expect(last_response['Location']).to(eq("#{BASE_URL}/test/list"))
    end
  end

  # GET /test/list - displays a table of tests
  describe 'GET /test/list' do
    before { get '/test/list' }

    it 'Test list page returns 200.' do
      expect(last_response).to(be_ok)
    end

    it 'Test list page is an HTML response' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it "Test list page contains 'List of tests' on page." do
      expect(last_response.body).to(include('List of tests'))
    end
  end

  # GET /test/add - form for adding test
  describe 'GET /test/add' do
    before { get '/test/add' }

    it 'Test add page returns 200.' do
      expect(last_response).to(be_ok)
    end

    it 'Test add page is an HTML response' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it "Test add page contains 'Add new test' on page." do
      expect(last_response.body).to(include('Add new test'))
    end
  end

  # POST /test/add - backend for adding a test
  describe 'POST /test/add' do
    before do
      @hardware = Hardware.create(name: 'Test Hardware', type: 'gpu')
      @benchmark = Benchmark.create(name: 'Test Benchmark', scoring: 'fps')
      request_data = {
        test_name: 'Test Test',
        test_hardware: @hardware.id,
        'test_benchmarks[]': [@benchmark.id],
        test_description: 'Test for testing'
      }
      post '/test/add', request_data
    end

    it 'Test add POST route is a redirect.' do
      expect(last_response).to(be_redirect)
    end

    it 'Test add POST route is an HTML response.' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it 'Test add POST route Location header points to /test/1' do
      expect(last_response['Location']).to(eq("#{BASE_URL}/test/1"))
    end

    it 'Test add POST route creates new Test.' do
      expect(Test.count).to(eq(1))
    end

    it 'Test add POST route created test has name.' do
      expect(Test.first.name).to(eq('Test Test'))
    end

    it 'Test add POST route created test has hardware.' do
      expect(Test.first.hardware.id).to(eq(@hardware.id))
    end

    it 'Test add POST route created test has benchmarks.' do
      expect(Test.first.benchmarks.length).to(eq(1))
    end

    it 'Test add POST route created test\'s benchmark can be read.' do
      expect(Test.first.benchmarks[0].id).to(eq(@benchmark.id))
    end

    it 'Test add POST route created test has description.' do
      expect(Test.first.description).to(eq('Test for testing'))
    end
  end

  # GET /test/:test_id - page for viewing a test model
  describe 'GET /test/:test_id' do
    before do
      @hardware = Hardware.create(name: 'Test Hardware', type: 'gpu')
      @benchmark = Benchmark.create(name: 'Test Benchmark', scoring: 'fps')
      @test = Test.create(
        name: 'Test Test',
        hardware_id: @hardware.id,
        description: 'Test for testing'
      )
      @test.add_benchmark(@benchmark)
      get "/test/#{@test.id}"
    end

    it 'Test view page returns 200.' do
      expect(last_response).to(be_ok)
    end

    it 'Test view page is an HTML response' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it 'Test view page contains test name on page.' do
      expect(last_response.body).to(include("#{@test.name}"))
    end

    it 'Test view page contains hardware name on page.' do
      expect(last_response.body).to(include("#{@hardware.name}"))
    end
  end

  # GET /test/:test_id/edit - page for editing a test model
  describe 'GET /test/:test_id/edit' do
    before do
      @hardware = Hardware.create(name: 'Test Hardware', type: 'gpu')
      @benchmark = Benchmark.create(name: 'Test Benchmark', scoring: 'fps')
      @test = Test.create(
        name: 'Test Test',
        hardware_id: @hardware.id,
        description: 'Test for testing'
      )
      @test.add_benchmark(@benchmark)
      get "/test/#{@test.id}/edit"
    end

    it 'Test edit page returns 200.' do
      expect(last_response).to(be_ok)
    end

    it 'Test edit page is an HTML response' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it 'Test edit page contains "Editing: <test name>" on page.' do
      expect(last_response.body).to(include("Editing: #{@test.name}"))
    end
  end
end
