mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
Blackstone oil analysis, .sam.zip export/import, AI logbook transcription
- Oil analysis: Blackstone PDF import (20 elements, oil properties, universal averages), results stored per element, oil type, "leave out of trends", the original app's statuses; AvLab fixes (multi-line comments, unrelated labels no longer steal values). Existing records migrate. - Aircraft archives in the original app's .sam.zip format (schema v2): export from Access & Sharing, background import from the Fleet page, rake tasks. Archives move both ways between this app and the original. - Scanned logbook pages (images or PDFs) upload as a document; with ANTHROPIC_API_KEY set, Claude transcribes them into logbook entries. - Fix: deleting a component or aircraft failed when AD compliances or inspection records pointed at a component. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2d068e1a63
commit
1c6c2f88c0
54 files changed
+2794
-286
No files matched your search
@@ -0,0 +1,213 @@
|
||||
require "test_helper"
|
||||
|
||||
class AircraftArchiveTest < ActiveSupport::TestCase
|
||||
include ActiveJob::TestHelper
|
||||
PNG = "\x89PNG\r\n\x1A\n".b + "rest-of-image".b
|
||||
|
||||
setup do
|
||||
@owner = create_user("owner@example.com")
|
||||
@dir = Dir.mktmpdir
|
||||
end
|
||||
|
||||
teardown { FileUtils.rm_rf(@dir) }
|
||||
|
||||
def upload(name, body, type) = { io: StringIO.new(body), filename: name, content_type: type }
|
||||
|
||||
# An aircraft with something in every section.
|
||||
def full_aircraft
|
||||
a = create_aircraft(owner: @owner, tail_number: "N12345", make: "Cessna", model: "150", icao_type: "C150", tach_time: 2000)
|
||||
a.picture.attach(upload("plane.png", PNG, "image/png"))
|
||||
engine = create_component(a, serial_number: "900001", component_type: component_type("Engine"))
|
||||
oil = create_component(a, parent_component: engine, component_type: component_type("Oil"), replacement_critical: true, replacement_hours: 50)
|
||||
collection = a.document_collections.create!(name: "Logbooks", visibility: "maintenance")
|
||||
doc = a.documents.create!(name: "Engine log", document_collection: collection, doc_type: "LOG")
|
||||
doc.pages.attach(upload("page1.pdf", "%PDF-1.4 page one", "application/pdf"), upload("page2.png", PNG, "image/png"))
|
||||
entry = a.logbook_entries.create!(date: Date.new(2026, 1, 5), text: "Oil changed", log_type: "ENG", document: doc, aircraft_hours_at_entry: 1990)
|
||||
a.squawks.create!(priority: 1, issue_reported: "Cracked fairing", component: engine, reported_by: @owner,
|
||||
attachments: [ upload("crack.png", PNG, "image/png"), upload("notes.txt", "see photo", "text/plain") ])
|
||||
a.squawks.create!(priority: 3, issue_reported: "Old one", resolved: true, resolved_at: 1.day.ago)
|
||||
annual = InspectionType.create!(name: "Annual", recurring: true, recurring_months: 12)
|
||||
annual.aircraft << a
|
||||
record = a.inspection_records.create!(inspection_type: annual, date: Date.new(2026, 2, 1), aircraft_hours: 1995, logbook_entry: entry)
|
||||
ad = AirworthinessDirective.create!(name: "2020-01-01", short_description: "Check muffler", recurring: true, recurring_hours: 100, docket_number: "FAA-2020-1")
|
||||
ad.components << engine
|
||||
a.ad_compliances.create!(airworthiness_directive: ad, date_complied: Date.new(2026, 2, 1), next_due_at_time: 2095, compliance_notes: "Inspected",
|
||||
component: engine, inspection_record: record)
|
||||
flight = a.flight_logs.create!(date: Date.new(2025, 7, 14), tach_time: 1.2, departure_location: "KOQU", destination_location: "KBID",
|
||||
user: @owner, track_log: { io: file_fixture("tracks/flight.gpx").open, filename: "flight.gpx" },
|
||||
engine_data: { io: file_fixture("engine_data/cgr30p.csv").open, filename: "cgr30p.csv" })
|
||||
a.consumable_records.create!(record_type: "oil", date: Date.new(2026, 1, 5), quantity_added: 1, flight_hours: 1990, flight_log: flight)
|
||||
a.major_records.create!(record_type: "alteration", title: "LED lights", date_performed: Date.new(2025, 5, 1), form_337_document: doc)
|
||||
a.notes.create!(text: "Hangar 4", public: true, user: @owner)
|
||||
a.oil_analyses.create!(sample_date: Date.new(2026, 1, 5), lab: "Blackstone", sample_number: "S1", component: engine, oil_type: "W100",
|
||||
elements: { iron: 30, zinc: 4 }, element_averages: { iron: 35 }, oil_properties: { flashpoint_f: 445 },
|
||||
excluded_from_averages: true, report: upload("report.pdf", "%PDF-1.4 lab", "application/pdf"))
|
||||
a
|
||||
end
|
||||
|
||||
def export(aircraft)
|
||||
path = File.join(@dir, "export.sam.zip")
|
||||
AircraftArchive::Exporter.new(aircraft, source: "sam.example.com").write_to(path)
|
||||
path
|
||||
end
|
||||
|
||||
def manifest(path) = Zip::File.open(path) { JSON.parse(_1.read("manifest.json")) }
|
||||
|
||||
test "export writes a manifest the original app accepts, plus every file" do
|
||||
path = export(full_aircraft)
|
||||
data = manifest(path)
|
||||
assert_equal AircraftArchive::TOP_LEVEL_KEYS.sort, data.keys.sort # the original rejects unknown or missing top-level keys
|
||||
assert_equal 2, data["schema_version"]
|
||||
assert_equal "N12345", data["aircraft"]["tail_number"]
|
||||
assert_equal 2, data["document_images"].size
|
||||
squawk = data["squawks"].find { _1["issue_reported"] == "Cracked fairing" }
|
||||
assert_equal 2, squawk["attachments"].size
|
||||
assert_equal squawk["attachments"].first, squawk["attachment"] # what the original reads
|
||||
oil = data["oil_analysis_reports"].first
|
||||
assert_equal({ "iron" => 30.0, "zinc" => 4.0 }, oil["elements_ppm"])
|
||||
assert_equal "S1", oil["lab_number"]
|
||||
|
||||
files = Zip::File.open(path) { |zip| zip.entries.map(&:name) - [ "manifest.json" ] }
|
||||
assert_equal 8, files.size # picture, 2 document pages, 2 squawk files, track log, engine data, oil report
|
||||
assert files.all? { _1.start_with?("attachments/") }
|
||||
assert files.any? { _1.end_with?("/crack.png") }, "original filenames are kept"
|
||||
end
|
||||
|
||||
test "round trip: importing an export recreates the aircraft with new IDs" do
|
||||
original = full_aircraft
|
||||
path = export(original)
|
||||
other = create_user("other@example.com")
|
||||
result = AircraftArchive::Importer.new(path, owner: other, tail_number: "N54321").run
|
||||
a = result[:aircraft]
|
||||
|
||||
assert_equal [ "N54321", "Cessna", "150", "C150", 2000 ], [ a.tail_number, a.make, a.model, a.icao_type, a.tach_time.to_i ]
|
||||
refute_equal original.id, a.id
|
||||
assert a.picture.attached?
|
||||
assert_equal [ "owner" ], a.roles.where(user: other).pluck(:role)
|
||||
assert_equal 2, a.components.count
|
||||
oil = a.components.find_by(component_type: component_type("Oil"))
|
||||
assert_equal a.components.find_by(serial_number: "900001"), oil.parent_component
|
||||
doc = a.documents.sole
|
||||
assert_equal %w[page1.pdf page2.png], doc.pages.map { _1.filename.to_s }
|
||||
assert_equal "%PDF-1.4 page one", doc.pages.first.download
|
||||
assert_equal doc, a.logbook_entries.sole.document
|
||||
squawk = a.squawks.find_by(issue_reported: "Cracked fairing")
|
||||
assert_equal %w[crack.png notes.txt], squawk.attachments.map { _1.filename.to_s }.sort
|
||||
assert_equal "IN-USE", squawk.component.status
|
||||
assert a.squawks.find_by(issue_reported: "Old one").resolved?
|
||||
assert_includes a.inspection_types.pluck(:name), "Annual"
|
||||
assert_equal 1, InspectionType.where(name: "Annual").count, "existing inspection types are reused"
|
||||
compliance = a.ad_compliances.sole
|
||||
assert_equal [ "2020-01-01", 2095 ], [ compliance.airworthiness_directive.name, compliance.next_due_at_time.to_i ]
|
||||
assert_equal a.inspection_records.sole, compliance.inspection_record
|
||||
assert_equal 1, AirworthinessDirective.where(name: "2020-01-01").count
|
||||
assert_includes compliance.airworthiness_directive.components, a.components.find_by(serial_number: "900001")
|
||||
flight = a.flight_logs.sole
|
||||
assert flight.track_log.attached?
|
||||
assert flight.engine_data.attached?
|
||||
assert_equal flight, a.consumable_records.sole.flight_log
|
||||
assert_equal doc, a.major_records.sole.form_337_document
|
||||
assert_match "Hangar 4", a.notes.sole.text
|
||||
analysis = a.oil_analyses.sole
|
||||
assert_equal [ "Blackstone", "S1", "W100", true ], [ analysis.lab, analysis.sample_number, analysis.oil_type, analysis.excluded_from_averages ]
|
||||
assert_equal({ "iron" => 30.0, "zinc" => 4.0 }, analysis.elements)
|
||||
assert_equal({ "iron" => 35.0 }, analysis.element_averages)
|
||||
assert analysis.report.attached?
|
||||
assert_equal "aircraft", a.events.last.category
|
||||
assert_empty result[:warnings]
|
||||
end
|
||||
|
||||
test "imports an archive from the original app (UUIDs, its field names, links we don't keep)" do
|
||||
path = File.join(@dir, "original.sam.zip")
|
||||
ids = Hash.new { |h, k| h[k] = SecureRandom.uuid }
|
||||
data = AircraftArchive::TOP_LEVEL_KEYS.to_h { [ _1, [] ] }.merge(
|
||||
"schema_version" => 2, "exported_at" => "2026-10-01T12:00:00+00:00", "source_instance" => "sam.example.org",
|
||||
"aircraft" => { "id" => ids[:a], "tail_number" => "N0000X", "make" => "Piper", "model" => "PA-28", "status" => "AVAILABLE",
|
||||
"tach_time" => "1234.5", "tach_time_offset" => "0.0", "hobbs_time" => "0.0", "hobbs_time_offset" => "0.0", "picture" => nil },
|
||||
"component_types" => [ { "id" => ids[:ct], "name" => "engine", "consumable" => false } ],
|
||||
"components" => [ { "id" => ids[:c], "aircraft_id" => ids[:a], "parent_component_id" => nil, "component_type_id" => ids[:ct],
|
||||
"manufacturer" => "Lycoming", "model" => "O-360", "status" => "IN-USE", "date_in_service" => "2020-01-01",
|
||||
"hours_in_service" => "500.0", "hours_since_overhaul" => "500.0", "tbo_hours" => 2000, "tbo_critical" => true } ],
|
||||
"logbook_entries" => [ { "id" => ids[:le], "aircraft_id" => ids[:a], "log_type" => "ENG", "date" => "2026-01-01", "text" => "Annual",
|
||||
"components" => [ ids[:c] ], "component_hours" => { ids[:c] => 500.0 }, "entry_type" => "INSPECTION" } ],
|
||||
"squawks" => [ { "id" => ids[:s], "aircraft_id" => ids[:a], "priority" => 0, "issue_reported" => "Mag drop",
|
||||
"attachment" => "attachments/health/squawks/abc123.png", "resolved" => false, "logbook_entries" => [] } ],
|
||||
"oil_analysis_reports" => [ { "id" => ids[:o], "aircraft_id" => ids[:a], "component_id" => ids[:c], "sample_date" => "2026-01-01",
|
||||
"lab" => "AVLab", "lab_number" => "", "oil_hours" => "40.0", "elements_ppm" => { "iron" => 12.5 },
|
||||
"oil_properties" => nil, "status" => "monitor", "excluded_from_averages" => false } ],
|
||||
"flight_logs" => [ { "id" => ids[:f], "aircraft_id" => ids[:a], "date" => "2026-02-01", "tach_time" => "1.3",
|
||||
"track_log" => "attachments/track_logs/xyz.kml" } ],
|
||||
"features" => [ { "feature" => "airworthiness_enforcement", "enabled" => true }, { "feature" => "oil_analysis", "enabled" => true } ]
|
||||
)
|
||||
Zip::OutputStream.open(path) do |zip|
|
||||
zip.put_next_entry("manifest.json")
|
||||
zip.write(data.to_json)
|
||||
zip.put_next_entry("attachments/health/squawks/abc123.png")
|
||||
zip.write(PNG)
|
||||
zip.put_next_entry("attachments/track_logs/xyz.kml")
|
||||
zip.write(file_fixture("tracks/flight.kml").read)
|
||||
end
|
||||
|
||||
engine_type = component_type("Engine")
|
||||
preview = AircraftArchive::Importer.check(path)
|
||||
assert_equal [ "N0000X", "sam.example.org", 1 ], [ preview.tail_number, preview.source, preview.counts["components"] ]
|
||||
|
||||
result = AircraftArchive::Importer.new(path, owner: @owner).run
|
||||
a = result[:aircraft]
|
||||
assert_equal [ "N0000X", "Piper", 1234.5 ], [ a.tail_number, a.make, a.tach_time.to_f ]
|
||||
assert_equal engine_type, a.components.sole.component_type, "matched case-insensitively"
|
||||
assert_equal 2000, a.components.sole.tbo_hours
|
||||
assert_equal "INSPECTION", a.logbook_entries.sole.entry_type
|
||||
squawk = a.squawks.sole
|
||||
assert_equal [ 0, "abc123.png" ], [ squawk.priority, squawk.attachments.sole.filename.to_s ]
|
||||
analysis = a.oil_analyses.sole
|
||||
assert_equal [ "monitor", 12.5, nil, a.components.sole ], [ analysis.status, analysis.value("iron"), analysis.sample_number, analysis.component ]
|
||||
assert a.flight_logs.sole.track_log.attached?
|
||||
assert_equal [ "1 logbook entry was linked to components; those links aren't kept here." ], result[:warnings]
|
||||
end
|
||||
|
||||
test "a file whose contents don't match its name is skipped with a note" do
|
||||
path = export(full_aircraft)
|
||||
data = manifest(path)
|
||||
fake = data["squawks"].find { _1["attachments"].any? }["attachments"].first
|
||||
Zip::File.open(path) { |zip| zip.get_output_stream(fake) { _1.write("MZ not a png") } }
|
||||
result = AircraftArchive::Importer.new(path, owner: @owner, tail_number: "N777").run
|
||||
assert_equal 1, result[:aircraft].squawks.find_by(issue_reported: "Cracked fairing").attachments.count
|
||||
assert_match "doesn't contain what its name says (.png)", result[:warnings].sole
|
||||
end
|
||||
|
||||
test "rejects unsafe or unrecognized archives before importing anything" do
|
||||
write = lambda do |entries|
|
||||
path = File.join(@dir, "#{SecureRandom.hex(4)}.zip")
|
||||
Zip::OutputStream.open(path) { |zip| entries.each { |name, body| zip.put_next_entry(name); zip.write(body) } }
|
||||
path
|
||||
end
|
||||
good = AircraftArchive::TOP_LEVEL_KEYS.to_h { [ _1, [] ] }.merge("schema_version" => 2, "aircraft" => { "tail_number" => "N1" })
|
||||
|
||||
{
|
||||
write.({ "notes.txt" => "hi" }) => "no manifest.json",
|
||||
write.({ "manifest.json" => good.merge("schema_version" => 3).to_json }) => "Unsupported archive version 3",
|
||||
write.({ "manifest.json" => good.except("squawks").to_json }) => "missing squawks",
|
||||
write.({ "manifest.json" => good.to_json, "../../evil.txt" => "x" }) => "unsafe path",
|
||||
write.({ "manifest.json" => good.merge("aircraft" => {}).to_json }) => "no aircraft tail number",
|
||||
write.({ "manifest.json" => "{not json" }) => "couldn't be read",
|
||||
File.join(@dir, "missing.zip").tap { File.write(_1, "not a zip") } => "Not a valid .sam.zip"
|
||||
}.each do |path, message|
|
||||
error = assert_raises(AircraftArchive::Error) { AircraftArchive::Importer.check(path) }
|
||||
assert_match message, error.message
|
||||
end
|
||||
assert_equal 0, Aircraft.where(tail_number: "N1").count
|
||||
end
|
||||
|
||||
test "a failure part-way removes everything the import created" do
|
||||
path = export(full_aircraft)
|
||||
before = [ Aircraft.count, Component.count, ActiveStorage::Blob.count ]
|
||||
importer = AircraftArchive::Importer.new(path, owner: @owner, tail_number: "N999")
|
||||
importer.define_singleton_method(:import_notes) { raise "disk on fire" }
|
||||
assert_raises(RuntimeError) { importer.run }
|
||||
assert_equal before[0, 2], [ Aircraft.count, Component.count ]
|
||||
assert_equal 0, Aircraft.where(tail_number: "N999").count
|
||||
perform_enqueued_jobs
|
||||
assert_equal before[2], ActiveStorage::Blob.count
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,32 @@
|
||||
require "test_helper"
|
||||
|
||||
class LogbookTranscriberTest < ActiveSupport::TestCase
|
||||
Message = Struct.new(:stop_reason, :content, :model)
|
||||
Text = Struct.new(:type, :text)
|
||||
|
||||
def client_returning(message)
|
||||
Class.new do
|
||||
define_method(:beta) { self }
|
||||
define_method(:messages) { self }
|
||||
define_method(:stream) { |**| Struct.new(:accumulated_message).new(message) }
|
||||
end.new
|
||||
end
|
||||
|
||||
def transcribe(message) = LogbookTranscriber.new(client: client_returning(message)).transcribe([ { data: "x", media_type: "image/png" } ])
|
||||
|
||||
test "reads the structured answer" do
|
||||
json = { entries: [ { "text" => "Annual" } ], non_logbook_pages: [ 2 ], unparseable_pages: [] }.to_json
|
||||
result = transcribe(Message.new(:end_turn, [ Text.new(:thinking, ""), Text.new(:text, json) ], "claude-opus-5-5"))
|
||||
assert_equal [ { "text" => "Annual" } ], result.entries
|
||||
assert_equal [ 2 ], result.non_logbook_pages
|
||||
end
|
||||
|
||||
test "turns refusals, truncation and bad JSON into clear errors" do
|
||||
{ refusal: "declined", max_tokens: "too long to finish" }.each do |reason, message|
|
||||
error = assert_raises(LogbookTranscriber::Error) { transcribe(Message.new(reason, [], "m")) }
|
||||
assert_match message, error.message
|
||||
end
|
||||
error = assert_raises(LogbookTranscriber::Error) { transcribe(Message.new(:end_turn, [ Text.new(:text, "{nope") ], "m")) }
|
||||
assert_match "wasn't valid JSON", error.message
|
||||
end
|
||||
end
|
||||
@@ -1,43 +1,112 @@
|
||||
require "test_helper"
|
||||
|
||||
class OilReportParserTest < ActiveSupport::TestCase
|
||||
def parse(name) = OilReportParser.from_pdf(file_fixture("oil_analysis/#{name}").open)
|
||||
|
||||
# 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)
|
||||
def avlab = @avlab ||= parse("avlab_report.pdf")
|
||||
|
||||
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
|
||||
# Anonymized sample reports from the original Simple Aircraft Manager project's
|
||||
# test suite (public domain): an older AvLab layout and a Blackstone report.
|
||||
def older_avlab = @older_avlab ||= parse("avlab_older_report.pdf")
|
||||
def blackstone = @blackstone ||= parse("blackstone_report.pdf")
|
||||
|
||||
test "AvLab: reads the header" do
|
||||
assert_equal "AvLab", avlab.lab
|
||||
assert_equal "N12345", avlab.tail_number
|
||||
assert_equal "900001", avlab.engine_serial
|
||||
assert_equal "CONT. 200", avlab.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
|
||||
test "AvLab: reads the current sample and both previous ones" do
|
||||
assert_equal [ true, false, false ], avlab.samples.map { _1[:current] }
|
||||
current = avlab.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])
|
||||
assert_equal "ALL OIL VALUES SEEM FINE. WE WILL CONTINUE TO MONITOR THIS ENGINE WITH YOUR NEXT SAMPLE.", 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"
|
||||
test "AvLab: reads results and lab averages through the color-code layer" do
|
||||
current, previous1, previous2 = avlab.samples.map { |s| [ s[:elements].transform_values(&:to_f), s[:element_averages].transform_values(&:to_f) ] }
|
||||
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[0].slice("iron", "copper", "nickel", "chromium", "aluminum", "lead", "silicon", "tin"))
|
||||
assert_equal({ "iron" => 40.5, "lead" => 2654.0, "tin" => 3.2 }, current[1].slice("iron", "lead", "tin"))
|
||||
refute current[0].key?("silver") # N/A
|
||||
refute current[1].key?("silver")
|
||||
assert_equal 0.3, previous1[0]["nickel"] # a real "0" hidden under a color-code "0"
|
||||
assert_equal 0, previous1[0]["tin"] # "< 0.1"
|
||||
assert_equal 2024.0, previous2[0]["lead"]
|
||||
assert_equal 58.7, previous2[0]["iron"] # not overwritten by "Cylinder Type: steel"
|
||||
end
|
||||
|
||||
test "AvLab, older layout: unused labels don't steal values; comments span lines" do
|
||||
current, previous = older_avlab.samples
|
||||
assert_nil current[:oil_added] # the "Filter Wt. (mgs): 121" beside it isn't oil added
|
||||
assert_equal "normal", current[:status]
|
||||
assert_match(/\AALL METALLIC DEBRIS .* WITH YOUR NEXT SAMPLE\.\z/, current[:lab_comments])
|
||||
assert_equal "monitor", previous[:status] # "** SEE LAB COMMENTS **"
|
||||
assert_match "CLOSEST MATCH AMS# 6274, WITH THE LARGEST PIECE", previous[:lab_comments]
|
||||
assert_equal [ 518.1, 43, 5 ], previous.values_at(:engine_hours, :oil_hours, :oil_added).map(&:to_f)
|
||||
assert_equal({ "aluminum" => 6.8, "chromium" => 4.1, "copper" => 13.0, "iron" => 30.2, "lead" => 1510.0, "nickel" => 0.5, "silicon" => 3.7, "tin" => 0.0 },
|
||||
previous[:elements].transform_values(&:to_f).sort.to_h)
|
||||
end
|
||||
|
||||
test "Blackstone: header, five samples newest first, no rating" do
|
||||
assert_equal "Blackstone", blackstone.lab
|
||||
assert_equal "Phillips XC (A/C) 20W/50", blackstone.oil_type
|
||||
assert_equal "Generic Engine", blackstone.engine_model
|
||||
assert_nil blackstone.tail_number # "N0000X" isn't a real registration
|
||||
assert_equal %w[2024-01-25 2022-03-31 2021-08-12 2020-06-08 2019-12-30], blackstone.samples.map { _1[:sample_date].iso8601 }
|
||||
assert_equal [ true, false, false, false, false ], blackstone.samples.map { _1[:current] }
|
||||
assert blackstone.samples.all? { _1[:status].nil? }
|
||||
current = blackstone.samples.first
|
||||
assert_equal "S000000", current[:sample_number]
|
||||
assert_match(/\ALorem ipsum dolor sit amet.* id est laborum\.\z/, current[:lab_comments])
|
||||
assert_nil blackstone.samples.second[:lab_comments]
|
||||
end
|
||||
|
||||
test "Blackstone: hours, all 20 elements, universal averages and oil properties per column" do
|
||||
current, second, _, fourth, oldest = blackstone.samples
|
||||
assert_equal [ 43, 518, 5 ], current.values_at(:oil_hours, :engine_hours, :oil_added).map(&:to_f)
|
||||
assert_equal 1903, fourth[:engine_hours].to_i # "1,903"
|
||||
assert_nil blackstone.samples.third[:oil_added]
|
||||
assert_equal 20, current[:elements].size
|
||||
assert_equal({ "iron" => 36.0, "lead" => 1841.0, "aluminum" => 7.0, "zinc" => 4.0, "barium" => 0.0 },
|
||||
current[:elements].slice("iron", "lead", "aluminum", "zinc", "barium").transform_values(&:to_f))
|
||||
assert_equal 807.0, second[:elements]["lead"].to_f # skips the unit/location average column (1458)
|
||||
assert_equal 818.0, oldest[:elements]["iron"].to_f
|
||||
assert_equal({ "iron" => 35.0, "lead" => 2354.0, "phosphorus" => 415.0 },
|
||||
current[:element_averages].slice("iron", "lead", "phosphorus").transform_values(&:to_f))
|
||||
assert_equal({ "viscosity_sus_210f" => 95.3, "viscosity_cst_100c" => 19.24, "flashpoint_f" => 445.0, "fuel_percent" => 0.5, "water_percent" => 0.0, "insolubles_percent" => 0.3 },
|
||||
current[:oil_properties].transform_values(&:to_f))
|
||||
assert_equal 490.0, oldest[:oil_properties]["flashpoint_f"].to_f
|
||||
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 }
|
||||
assert_raises(OilReportParser::Error) { OilReportParser::Avlab.new([]).parse }
|
||||
error = assert_raises(OilReportParser::Error) { parse_pdf_text("Just some other document") }
|
||||
assert_match "AvLab and Blackstone", error.message
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# A minimal one-page PDF with the given text.
|
||||
def parse_pdf_text(text)
|
||||
objects = [ "<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
|
||||
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>",
|
||||
nil, "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>" ]
|
||||
stream = "BT /F1 12 Tf 72 720 Td (#{text}) Tj ET"
|
||||
objects[3] = "<< /Length #{stream.bytesize} >>\nstream\n#{stream}\nendstream"
|
||||
pdf = +"%PDF-1.4\n"
|
||||
offsets = objects.each_with_index.map { |body, i| pdf.bytesize.tap { pdf << "#{i + 1} 0 obj\n#{body}\nendobj\n" } }
|
||||
xref = pdf.bytesize
|
||||
pdf << "xref\n0 #{objects.size + 1}\n0000000000 65535 f \n" << offsets.map { format("%010d 00000 n \n", _1) }.join
|
||||
pdf << "trailer\n<< /Size #{objects.size + 1} /Root 1 0 R >>\nstartxref\n#{xref}\n%%EOF\n"
|
||||
OilReportParser.from_pdf(StringIO.new(pdf))
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user