# frozen_string_literal: true

require_relative '../spec_helper'
require_relative '../../src/models/benchmark'

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

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

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

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

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

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

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

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

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

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

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

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

  # POST /benchmark/add - backend for adding a benchmark
  describe 'POST /benchmark/add' do
    before do
      request_data = {
        benchmark_name: 'Test Benchmark',
        benchmark_scoring: 'fps',
        benchmark_description: 'Benchmark for testing'
      }
      post '/benchmark/add', request_data
    end

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

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

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

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

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

    it 'Benchmark add POST route created benchmark has scoring type.' do
      expect(Benchmark.first.scoring).to(eq('fps'))
    end

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

  # GET /benchmark/:benchmark_id - page for viewing a benchmark model
  describe 'GET /benchmark/:benchmark_id' do
    before do
      @benchmark = Benchmark.create(name: 'Test Benchmark', scoring: 'fps')
      get "/benchmark/#{@benchmark.id}"
    end

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

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

    it 'Benchmark view page contains "Add new benchmark" on page.' do
      expect(last_response.body).to(include("#{@benchmark.name}"))
    end
  end

  # GET /benchmark/:benchmark_id/edit - page for editing a benchmark model
  describe 'GET /benchmark/:benchmark_id/edit' do
    before do
      @benchmark = Benchmark.create(name: 'Test Benchmark', scoring: 'fps')
      get "/benchmark/#{@benchmark.id}/edit"
    end

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

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

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