require 'spec_helper'
require 'debci/experiment_test'

describe Debci::ExperimentTest do
  let(:user) do
    Debci::User.create!(username: 'theuser')
  end
  let(:experiment) do
    Debci::Experiment.create!(
      package_selector: { "name" => "foobar" },
      suite: 'testing',
      arch: 'arm64',
      test_parameters: { requested_backend: "incus-lxc" },
      owner: user,
    )
  end
  let(:package) do
    Debci::Package.create!(name: "foobar")
  end
  let(:control_job) do
    Debci::Job.create!(
      package: package,
      suite: 'testing',
      arch: 'arm64',
      requestor: user,
    )
  end
  let(:test_job) do
    Debci::Job.create!(
      package: package,
      suite: 'testing',
      arch: 'arm64',
      requestor: user,
      requested_backend: "incus-lxc",
    )
  end

  let(:test) do
    Debci::ExperimentTest.new(
      experiment_id: experiment.id,
      control_job_id: control_job.id,
      test_job_id: test_job.id,
    )
  end

  it 'points to experiment' do
    expect(test.experiment).to eq(experiment)
  end

  it 'points to control job' do
    expect(test.control_job).to eq(control_job)
  end

  it 'points to test job' do
    expect(test.test_job).to eq(test_job)
  end

  it 'does not signal regression for two passes' do
    control_job.update(status: 'pass')
    test_job.update(status: 'pass')
    expect(test).to_not be_a_regression
  end

  it 'signals regressions' do
    control_job.update(status: 'pass')
    test_job.update(status: 'fail')
    expect(test).to be_a_regression
  end

  it 'queries regressions' do
    control_job.update(status: 'pass')
    test_job.update(status: 'fail')
    test.save!
    expect(Debci::ExperimentTest.regressions).to include(test)
  end

  it 'signals an improment' do
    control_job.update(status: 'fail')
    test_job.update(status: 'pass')
    expect(test).to be_an_improvement
  end
end
