require "rails_helper" RSpec.describe MaintenanceEntry do it "has a valid factory" do expect(build(:maintenance_entry)).to be_valid end it "requires all fields" do entry = build( :maintenance_entry, vehicle: nil, updated_by_user: nil, name: nil, notes: nil, odometer: nil, cost: nil, date: nil, ensure_matching_schedule: false ) expect(entry).not_to be_valid expect(entry.errors.attribute_names).to include(:vehicle, :updated_by_user, :name, :notes, :odometer, :cost, :date) end it "requires valid numeric values and allows zero cost" do invalid_entry = build(:maintenance_entry, odometer: -1, cost: -1) no_cost_entry = build(:maintenance_entry, cost: 0) expect(invalid_entry).not_to be_valid expect(invalid_entry.errors[:odometer]).to be_present expect(invalid_entry.errors[:cost]).to be_present expect(no_cost_entry).to be_valid end it "rejects future dates" do entry = build(:maintenance_entry, date: Date.current + 1.day) expect(entry).not_to be_valid expect(entry.errors[:date]).to be_present end it "requires name to match a vehicle maintenance schedule case-insensitively" do vehicle = create(:vehicle) create(:maintenance_schedule, vehicle: vehicle, maintenance_type: "Oil Change") matching_entry = build(:maintenance_entry, vehicle: vehicle, name: "oil change") unknown_entry = build(:maintenance_entry, vehicle: vehicle, name: "Tire Rotation", ensure_matching_schedule: false) expect(matching_entry).to be_valid expect(unknown_entry).not_to be_valid expect(unknown_entry.errors[:name]).to be_present end end