mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Track oil analysis reports; import AvLab PDFs
New Oil Analysis tab: one record per oil sample with dates, hours, the lab's verdict and comments, ppm results for 12 wear metals with the lab's averages, and the report PDF. Shows wear-metal trends, a side-by-side history table, and flags readings 1.5x or more above the lab average. AvLab report PDFs can be imported: every sample on the report (current and previous) is added unless already recorded, the engine is matched by serial number, and reports for another tail number are refused. The parser reads positioned text and drops the hidden color-code layer that overlaps results. Adds the pdf-reader gem. Test fixture is a rebuilt report with a dummy header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6c6e7af8e6
commit
d519aa9aae
24 files changed
+3456
-2
No files matched your search
+2548
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,119 @@
|
||||
require "test_helper"
|
||||
|
||||
class OilAnalysisFlowTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@owner = create_user
|
||||
@pilot = create_user
|
||||
@aircraft = create_aircraft(owner: @owner, pilot: @pilot)
|
||||
@engine = create_component(@aircraft, hours_since_overhaul: 120.5)
|
||||
end
|
||||
|
||||
def results(**overrides)
|
||||
{ sample_date: "2026-08-01", status: "normal", component_id: @engine.id, lab: "Example Lab",
|
||||
engine_hours: 120.5, oil_hours: 40, iron: 30, iron_avg: 35, copper: 6, copper_avg: 9,
|
||||
lead: 2500, lead_avg: 2400, silicon: 22, silicon_avg: 12 }.merge(overrides)
|
||||
end
|
||||
|
||||
test "pilot adds samples; trends, flags and comparison render" do
|
||||
sign_in @pilot
|
||||
get new_aircraft_oil_analysis_path(@aircraft)
|
||||
assert_response :success
|
||||
assert_includes response.body, 'value="120.5"' # engine hours default from the engine
|
||||
|
||||
post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(sample_date: "2026-03-01", iron: 25, silicon: 10) }
|
||||
post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(report: fixture_file_upload("engine_data/cgr30p.csv", "application/pdf")) }
|
||||
latest = @aircraft.oil_analyses.recent_first.first
|
||||
assert_redirected_to aircraft_oil_analysis_path(@aircraft, latest)
|
||||
assert latest.report.attached?
|
||||
assert_equal [ "Oil analysis added (sampled 2026-08-01)" ], @aircraft.events.where(category: "oil_analysis").limit(1).pluck(:event_name)
|
||||
|
||||
get aircraft_oil_analyses_path(@aircraft)
|
||||
assert_response :success
|
||||
assert_includes response.body, "Silicon above average"
|
||||
assert_includes response.body, "Chartkick"
|
||||
|
||||
get aircraft_oil_analysis_path(@aircraft, latest)
|
||||
assert_response :success
|
||||
assert_includes response.body, "Compared with"
|
||||
assert_includes response.body, "+5" # iron 25 → 30
|
||||
refute_includes response.body, ">Edit<" # pilots can't edit
|
||||
end
|
||||
|
||||
test "only owners edit and delete" do
|
||||
analysis = @aircraft.oil_analyses.create!(results.except(:component_id))
|
||||
sign_in @pilot
|
||||
patch aircraft_oil_analysis_path(@aircraft, analysis), params: { oil_analysis: { status: "high" } }
|
||||
assert_equal "normal", analysis.reload.status
|
||||
|
||||
sign_in @owner
|
||||
get edit_aircraft_oil_analysis_path(@aircraft, analysis)
|
||||
assert_response :success
|
||||
patch aircraft_oil_analysis_path(@aircraft, analysis), params: { oil_analysis: { status: "elevated", iron: "" } }
|
||||
assert_equal "elevated", analysis.reload.status
|
||||
assert_nil analysis.iron
|
||||
delete aircraft_oil_analysis_path(@aircraft, analysis)
|
||||
assert_equal 0, @aircraft.oil_analyses.count
|
||||
end
|
||||
|
||||
test "an engine from another aircraft is ignored" do
|
||||
other = create_component(create_aircraft)
|
||||
sign_in @owner
|
||||
post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(component_id: other.id) }
|
||||
assert_nil @aircraft.oil_analyses.last.component_id
|
||||
end
|
||||
end
|
||||
|
||||
class OilAnalysisImportTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@owner = create_user
|
||||
@pilot = create_user
|
||||
@aircraft = create_aircraft(owner: @owner, pilot: @pilot, tail_number: "N12345")
|
||||
@in_service = create_component(@aircraft)
|
||||
@engine = create_component(@aircraft, serial_number: "900001", status: "SPARE")
|
||||
end
|
||||
|
||||
def report = fixture_file_upload("oil_analysis/avlab_report.pdf", "application/pdf")
|
||||
|
||||
test "pilot imports every sample on the report; reimporting adds nothing" do
|
||||
sign_in @pilot
|
||||
post import_aircraft_oil_analyses_path(@aircraft), params: { report: report }
|
||||
assert_redirected_to aircraft_oil_analyses_path(@aircraft)
|
||||
assert_equal "Imported 3 samples from the AvLab report.", flash[:notice]
|
||||
|
||||
analyses = @aircraft.oil_analyses.recent_first.to_a
|
||||
assert_equal %w[P26 P47 P69], analyses.map(&:sample_number)
|
||||
assert analyses.all? { _1.component == @engine && _1.lab == "AvLab" } # matched by engine serial
|
||||
assert analyses.first.report.attached?
|
||||
refute analyses.last.report.attached?
|
||||
assert_equal 44.0, analyses.first.iron.to_f
|
||||
assert_equal 13.7, analyses.first.silicon_avg.to_f
|
||||
assert_equal 3, @aircraft.events.where(category: "oil_analysis").count
|
||||
|
||||
post import_aircraft_oil_analyses_path(@aircraft), params: { report: report }
|
||||
assert_equal "Imported 0 samples from the AvLab report. 3 samples were already recorded.", flash[:notice]
|
||||
assert_equal 3, @aircraft.oil_analyses.count
|
||||
|
||||
follow_redirect!
|
||||
assert_includes response.body, "Silicon above average"
|
||||
end
|
||||
|
||||
test "attaches the PDF to a matching sample entered by hand" do
|
||||
manual = @aircraft.oil_analyses.create!(sample_date: Date.new(2026, 8, 20), iron: 44)
|
||||
sign_in @owner
|
||||
post import_aircraft_oil_analyses_path(@aircraft), params: { report: report }
|
||||
assert_equal 3, @aircraft.oil_analyses.count
|
||||
assert manual.reload.report.attached?
|
||||
end
|
||||
|
||||
test "refuses a report for another aircraft, or a file that isn't one" do
|
||||
@aircraft.update!(tail_number: "N54321")
|
||||
sign_in @owner
|
||||
post import_aircraft_oil_analyses_path(@aircraft), params: { report: report }
|
||||
assert_redirected_to new_aircraft_oil_analysis_path(@aircraft)
|
||||
assert_equal "That report is for N12345, not N54321.", flash[:alert]
|
||||
|
||||
post import_aircraft_oil_analyses_path(@aircraft), params: { report: fixture_file_upload("flights.csv", "application/pdf") }
|
||||
assert_match(/Couldn't import that report/, flash[:alert])
|
||||
assert_equal 0, @aircraft.oil_analyses.count
|
||||
end
|
||||
end
|
||||
@@ -24,6 +24,7 @@ class PagesTest < ActionDispatch::IntegrationTest
|
||||
@major = @a.major_records.create!(record_type: "alteration", title: "LED lights", date_performed: Date.current)
|
||||
@flight = @a.flight_logs.create!(date: Date.current, tach_time: 1.2)
|
||||
@cr = @a.consumable_records.create!(record_type: "oil", date: Date.current, quantity_added: 1, flight_hours: 1000)
|
||||
@oa = @a.oil_analyses.create!(sample_date: Date.current, iron: 30, iron_avg: 35)
|
||||
@token = @a.share_tokens.create!(privilege: "maintenance", label: "Mechanic")
|
||||
@a.log_event!("aircraft", "Created", user: @owner)
|
||||
sign_in @owner
|
||||
@@ -39,6 +40,7 @@ class PagesTest < ActionDispatch::IntegrationTest
|
||||
aircraft_notes_path(a), new_aircraft_note_path(a), edit_aircraft_note_path(a, @note),
|
||||
aircraft_flight_logs_path(a), new_aircraft_flight_log_path(a), aircraft_flight_log_path(a, @flight),
|
||||
aircraft_consumable_records_path(a), new_aircraft_consumable_record_path(a, type: "fuel"), edit_aircraft_consumable_record_path(a, @cr),
|
||||
aircraft_oil_analyses_path(a), new_aircraft_oil_analysis_path(a), aircraft_oil_analysis_path(a, @oa), edit_aircraft_oil_analysis_path(a, @oa),
|
||||
aircraft_logbook_entries_path(a), aircraft_logbook_entries_path(a, q: "oil", log_type: "AC"),
|
||||
new_aircraft_logbook_entry_path(a), aircraft_logbook_entry_path(a, @entry), edit_aircraft_logbook_entry_path(a, @entry),
|
||||
aircraft_documents_path(a), new_aircraft_document_path(a), aircraft_document_path(a, @doc), edit_aircraft_document_path(a, @doc),
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
require "test_helper"
|
||||
|
||||
class OilAnalysisTest < ActiveSupport::TestCase
|
||||
setup { @a = create_aircraft }
|
||||
|
||||
def analysis(**attrs) = @a.oil_analyses.new({ sample_date: Date.current }.merge(attrs))
|
||||
|
||||
test "flags readings well above the lab average" do
|
||||
a = analysis(iron: 60, iron_avg: 35, copper: 10, copper_avg: 20, silicon: 20, silicon_avg: 13.3, tin: 5)
|
||||
assert a.flagged?("iron")
|
||||
refute a.flagged?("copper")
|
||||
assert a.flagged?("silicon") # exactly 1.5×
|
||||
refute a.flagged?("tin") # no average to compare
|
||||
assert_equal %w[iron silicon], a.flagged_elements
|
||||
end
|
||||
|
||||
test "validates status, non-negative results, and engine on this aircraft" do
|
||||
assert analysis.valid?
|
||||
refute analysis(status: "bad").valid?
|
||||
refute analysis(iron: -1).valid?
|
||||
other_engine = create_component(create_aircraft)
|
||||
a = analysis(component: other_engine)
|
||||
refute a.valid?
|
||||
assert_includes a.errors[:component], "must be on this aircraft"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,43 @@
|
||||
require "test_helper"
|
||||
|
||||
class OilReportParserTest < ActiveSupport::TestCase
|
||||
# A real AvLab report rebuilt with glyph positions intact (including the hidden
|
||||
# color-code layer that overlaps the results) and a dummy header.
|
||||
def report = @report ||= OilReportParser.from_pdf(file_fixture("oil_analysis/avlab_report.pdf").open)
|
||||
|
||||
test "reads the header" do
|
||||
assert_equal "AvLab", report.lab
|
||||
assert_equal "N12345", report.tail_number
|
||||
assert_equal "900001", report.engine_serial
|
||||
assert_equal "CONT. 200", report.engine_model
|
||||
end
|
||||
|
||||
test "reads the current sample and both previous ones" do
|
||||
assert_equal [ true, false, false ], report.samples.map { _1[:current] }
|
||||
current = report.samples.first
|
||||
assert_equal Date.new(2026, 8, 20), current[:sample_date]
|
||||
assert_equal Date.new(2026, 9, 3), current[:analysis_date]
|
||||
assert_equal "P26", current[:sample_number]
|
||||
assert_equal [ 94.7, 43.7, 43.7, 1 ], current.values_at(:engine_hours, :oil_hours, :filter_hours, :oil_added).map(&:to_f)
|
||||
assert_equal "normal", current[:status]
|
||||
assert_match(/ALL OIL VALUES SEEM FINE/, current[:lab_comments])
|
||||
end
|
||||
|
||||
test "reads results and lab averages through the color-code layer" do
|
||||
current, previous1, previous2 = report.samples
|
||||
assert_equal({ iron: 44.0, copper: 8.1, nickel: 2.5, chromium: 3.9, aluminum: 6.7, lead: 2650.0, silicon: 25.3, tin: 1.6 },
|
||||
current.slice(:iron, :copper, :nickel, :chromium, :aluminum, :lead, :silicon, :tin).transform_values(&:to_f))
|
||||
assert_equal({ iron_avg: 40.5, lead_avg: 2654.0, tin_avg: 3.2 }, current.slice(:iron_avg, :lead_avg, :tin_avg).transform_values(&:to_f))
|
||||
assert_nil current[:silver] # N/A
|
||||
assert_nil current[:silver_avg]
|
||||
assert_equal 0.3, previous1[:nickel].to_f # a real "0" hidden under a color-code "0"
|
||||
assert_equal 0, previous1[:tin] # "< 0.1"
|
||||
assert_equal 2024.0, previous2[:lead].to_f
|
||||
assert_equal 58.7, previous2[:iron].to_f # not overwritten by "Cylinder Type: steel"
|
||||
end
|
||||
|
||||
test "rejects other PDFs and non-PDFs" do
|
||||
assert_raises(OilReportParser::Error) { OilReportParser.from_pdf(StringIO.new("not a pdf")) }
|
||||
assert_raises(OilReportParser::Error) { OilReportParser.new([]).parse }
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user