Follow the Brass

spec/press_recovery_spec.rb

61 lines · ruby

Project files
# frozen_string_literal: true

require "cartridge_case"
require "fact_station"
require "press_recovery"
require "work_in_progress"

RSpec.describe PressRecovery do
  subject(:recovery) { described_class.new(stations: stations) }

  let(:stations) do
    [
      FactStation.new(name: "prime", fact: :primed),
      FactStation.new(name: "charge", fact: :charged),
      FactStation.new(name: "seat", fact: :seated),
      FactStation.new(name: "crimp", fact: :crimped)
    ]
  end

  it "resumes a piece that is missing only its current station's work" do
    at_charge = snapshot(station: stations[1], facts: { primed: true })

    assessment = recovery.assess([at_charge]).first

    expect(assessment.disposition).to eq(:resume)
    expect(assessment.missing).to be_empty
  end

  it "quarantines a piece missing a fact it should already carry" do
    at_seat = snapshot(station: stations[2], facts: { primed: true })

    assessment = recovery.assess([at_seat]).first

    expect(assessment.disposition).to eq(:quarantine)
    expect(assessment.missing).to eq([:charged])
  end

  it "assesses from facts alone, so asking twice gives the same answer" do
    positions = [snapshot(station: stations[2], facts: { primed: true })]

    expect(recovery.assess(positions)).to eq(recovery.assess(positions))
  end

  it "re-striking an already established fact is harmless" do
    primed_work = WorkInProgress.new(
      cartridge_case: CartridgeCase.new(caliber: "5.56"), facts: { primed: true }
    )

    struck_again = stations[0].apply(primed_work).work

    expect(struck_again.fact?(:primed)).to be(true)
    expect(struck_again.facts).to eq(primed_work.facts)
  end

  def snapshot(station:, facts:)
    work = WorkInProgress.new(
      cartridge_case: CartridgeCase.new(caliber: "5.56"), facts: facts
    )
    PositionSnapshot.new(station: station, work: work)
  end
end

Read along in the manual: The Reloading Lab · Download the project (zip)