# frozen_string_literal: true

require 'spec_helper'
require_migration!

RSpec.describe ScheduleRepopulateHistoricalVulnerabilityStatistics do
  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:project_settings) { table(:project_settings) }

  let(:namespace) { namespaces.create!(name: 'gitlab', path: 'gitlab-org') }
  let!(:project_1) { projects.create!(namespace_id: namespace.id, name: 'foo_1') }
  let!(:project_2) { projects.create!(namespace_id: namespace.id, name: 'foo_2') }
  let!(:project_3) { projects.create!(namespace_id: namespace.id, name: 'foo_3') }
  let!(:project_4) { projects.create!(namespace_id: namespace.id, name: 'foo_4') }

  around do |example|
    freeze_time { Sidekiq::Testing.fake! { example.run } }
  end

  before do
    stub_const("#{described_class.name}::BATCH_SIZE", 1)

    project_settings.create!(project_id: project_1.id, has_vulnerabilities: true)
    project_settings.create!(project_id: project_2.id, has_vulnerabilities: false)
    project_settings.create!(project_id: project_4.id, has_vulnerabilities: true)
  end

  it 'schedules the background jobs', :aggregate_failures do
    migrate!

    expect(BackgroundMigrationWorker.jobs.size).to be(2)
    expect(described_class::MIGRATION_CLASS).to be_scheduled_delayed_migration(described_class::DELAY_INTERVAL, [project_1.id], described_class::DAY_COUNT)
    expect(described_class::MIGRATION_CLASS).to be_scheduled_delayed_migration(2 * described_class::DELAY_INTERVAL, [project_4.id], described_class::DAY_COUNT)
  end
end
