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:
rickyandClaude Opus 5.5 committed 2026-11-06 21:05:19 -05:00
1 parent 2d068e1a63
commit 1c6c2f88c0
54 files changed
+2794 -286

No files matched your search

@@ -0,0 +1,93 @@
require "test_helper"
class AircraftArchiveFlowTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
setup do
@owner = create_user("owner@example.com")
@pilot = create_user("pilot@example.com")
@a = create_aircraft(owner: @owner, pilot: @pilot, tail_number: "N12345", make: "Cessna")
engine = create_component(@a)
@a.squawks.create!(priority: 1, issue_reported: "Cracked fairing", component: engine,
attachments: [ { io: StringIO.new("see photo"), filename: "notes.txt", content_type: "text/plain" } ])
@a.logbook_entries.create!(date: Date.new(2026, 1, 5), text: "Oil changed")
end
def download_export
get aircraft_export_path(@a)
assert_response :success
assert_equal "application/zip", response.media_type
path = File.join(Dir.mktmpdir, "export.sam.zip")
File.binwrite(path, response.body)
path
end
test "owner downloads an export; pilots and outsiders can't" do
sign_in @pilot
get aircraft_export_path(@a)
assert_redirected_to aircraft_path(@a)
sign_in create_user
get aircraft_export_path(@a)
assert_response :not_found
sign_in @owner
get aircraft_aircraft_roles_path(@a)
assert_select "a[href='#{aircraft_export_path(@a)}']", text: "Download .sam.zip"
path = download_export
assert_match(/attachment; filename="n12345-\d{4}-\d\d-\d\d\.sam\.zip"/, response.headers["Content-Disposition"])
assert_equal "N12345", Zip::File.open(path) { JSON.parse(_1.read("manifest.json")) }["aircraft"]["tail_number"]
assert_equal "Aircraft exported (.sam.zip)", @a.events.last.event_name
end
test "importing an archive: tail clash asks for a new tail, then the job builds the aircraft" do
sign_in @owner
path = download_export
archive = -> { Rack::Test::UploadedFile.new(path, "application/zip", original_filename: "n12345.sam.zip") }
sign_in @pilot
get root_path
assert_select "a[href='#{new_aircraft_import_path}']"
post aircraft_imports_path, params: { archive: archive.() }
assert_response :unprocessable_entity
assert_includes response.body, "An aircraft N12345 already exists"
assert_equal 0, AircraftImport.count
assert_enqueued_jobs 1, only: AircraftImportJob do
post aircraft_imports_path, params: { archive: archive.(), tail_number: "n54321" }
end
import = AircraftImport.last
assert_redirected_to aircraft_import_path(import)
assert_equal [ "N54321", @pilot, "queued" ], [ import.tail_number, import.user, import.status ]
follow_redirect!
assert_includes response.body, "Waiting to start"
assert_select "meta[http-equiv=refresh]"
perform_enqueued_jobs(only: AircraftImportJob)
import.reload
assert_equal "done", import.status, import.error
copy = import.aircraft
assert_equal [ "N54321", "Cessna" ], [ copy.tail_number, copy.make ]
assert_equal [ "owner" ], copy.roles.where(user: @pilot).pluck(:role) # the importer owns the copy
assert_equal "notes.txt", copy.squawks.sole.attachments.sole.filename.to_s
assert_equal 1, import.counts["logbook_entries"]
get aircraft_import_path(import)
assert_includes response.body, "Imported."
assert_select "meta[http-equiv=refresh]", 0
assert_select "a[href='#{aircraft_path(copy)}']"
perform_enqueued_jobs # purge the staged archive
refute import.reload.archive.attached?
sign_in create_user
get aircraft_import_path(import)
assert_response :not_found # only the importer (or an admin) sees it
end
test "a bad file is refused up front" do
sign_in @owner
post aircraft_imports_path, params: { archive: fixture_file_upload("flights.csv", "application/zip") }
assert_redirected_to new_aircraft_import_path
assert_match "That archive can't be imported: Not a valid .sam.zip archive", flash[:alert]
end
end
@@ -0,0 +1,152 @@
require "test_helper"
class LogbookImportFlowTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
# Stands in for Anthropic::Client: records each request and answers from a queue.
class FakeClaude
Message = Struct.new(:stop_reason, :content, :model)
Text = Struct.new(:type, :text)
attr_reader :requests
def initialize(*answers)
@answers = answers
@requests = []
end
def beta = self
def messages = self
def stream(**params)
@requests << params
answer = @answers.shift or raise "unexpected request"
message = answer.is_a?(Symbol) ? Message.new(answer, [], "claude-opus-5-5") : Message.new(:end_turn, [ Text.new(:text, answer.to_json) ], "claude-opus-5-5")
Struct.new(:accumulated_message).new(message)
end
end
JPEG = "\xFF\xD8\xFF\xE0fake-jpeg".b
def entry(text, page_start, page_end = page_start, **extra)
{ date: "2026-01-05", log_type: "ENG", entry_type: "MAINTENANCE", text: text, aircraft_hours: 1990.5,
signoff_person: "J. Mechanic A&P 1234567", signoff_location: "KOQU", page_start: page_start, page_end: page_end,
confidence: "high" }.merge(extra)
end
def answer(*entries, forms: []) = { entries: entries, non_logbook_pages: forms, unparseable_pages: [] }
def page(name) = Rack::Test::UploadedFile.new(StringIO.new(JPEG), "image/jpeg", original_filename: name)
setup do
@owner = create_user("owner@example.com")
@pilot = create_user("pilot@example.com")
@a = create_aircraft(owner: @owner, pilot: @pilot)
@previous_key = ENV["ANTHROPIC_API_KEY"]
ENV["ANTHROPIC_API_KEY"] = "sk-ant-test"
LogbookTranscriptionJob.batch_pages = 2
end
teardown do
ENV["ANTHROPIC_API_KEY"] = @previous_key
LogbookTranscriber.client = nil
LogbookTranscriptionJob.batch_pages = 6
end
test "pages are saved in name order and transcribed in overlapping batches" do
claude = FakeClaude.new(
# pages 1–2: A on page 1; B starts on page 2 and may continue
answer(entry("Changed oil", 0), entry("Annual inspection, found", 1, 1, entry_type: "INSPECTION")),
# pages 2–3: B completed from the carried copy, C on page 3 (no readable date, hard to read), page 3 also has a form
answer(entry("Annual inspection, found airworthy", 0, 1, entry_type: "INSPECTION"), entry("Replaced [?] gasket", 1, date: nil, confidence: "low"))
)
LogbookTranscriber.client = claude
sign_in @owner
get aircraft_logbook_entries_path(@a)
assert_select "a[href='#{new_aircraft_logbook_import_path(@a)}']", text: "Import scans"
get new_aircraft_logbook_import_path(@a)
assert_select "input[type=checkbox][name=transcribe]:not([disabled])"
post aircraft_logbook_imports_path(@a), params: { pages: [ page("page-10.jpg"), page("page-2.jpg"), page("page-1.jpg") ],
collection_id: "new", collection_name: "Engine Log #1", transcribe: "1", log_type: "" }
import = @a.logbook_imports.last
assert_redirected_to aircraft_logbook_import_path(@a, import)
document = import.document
assert_equal %w[page-1.jpg page-2.jpg page-10.jpg], document.pages.sort_by(&:id).map { _1.filename.to_s } # natural order
assert_equal "Engine Log #1", document.document_collection.name
assert_equal "LOG", document.doc_type
follow_redirect!
assert_select "meta[http-equiv=refresh]"
perform_enqueued_jobs(only: LogbookTranscriptionJob)
import.reload
assert_equal [ "done", 3, 3, 3 ], [ import.status, import.pages_total, import.pages_done, import.entries_created ], import.error
first, second = claude.requests
assert_equal "claude-opus-5-5", first[:model]
assert_equal :default, first[:fallbacks]
assert_equal [ "server-side-fallback-2026-07-01" ], first[:betas]
assert_equal :json_schema, first[:output_config][:format_][:type]
assert_equal 2, first[:messages][0][:content].count { _1[:type] == "image" }
assert_match "Entries already read from page 0", second[:messages][0][:content].map { _1[:text] }.compact.join
assert_match "Annual inspection, found", second[:messages][0][:content].map { _1[:text] }.compact.join
entries = @a.logbook_entries.order(:id)
assert_equal [ "Changed oil", "Annual inspection, found airworthy", "[date unreadable] Replaced [?] gasket" ], entries.map(&:text)
assert_equal [ 1, 2, 3 ], entries.map(&:page_number)
assert entries.all? { _1.document == document && _1.log_type == "ENG" }
assert_equal [ "MAINTENANCE", "INSPECTION", "MAINTENANCE" ], entries.map(&:entry_type)
assert_equal 1990.5, entries.first.aircraft_hours_at_entry.to_f
assert_equal Date.new(2026, 1, 5), entries.last.date # borrowed from the entry before it
assert_equal 2, import.warnings.size
assert_match "had no readable date", import.warnings.first
assert_match "hard to read", import.warnings.last
get aircraft_logbook_import_path(@a, import)
assert_includes response.body, "Done: 3 logbook entries added."
assert_select "meta[http-equiv=refresh]", 0
assert_equal "Logbook pages transcribed (3 entries)", @a.events.where(category: "logbook").last.event_name
end
test "a PDF goes up whole, the log type can be forced, and a refusal becomes a note" do
LogbookTranscriber.client = FakeClaude.new(answer(entry("Prop overhauled", 0), entry("Prop balanced", 2, log_type: "AC"), forms: [ 1 ]), :refusal)
sign_in @owner
pdf = Rack::Test::UploadedFile.new(StringIO.new("%PDF-1.4 fake"), "application/pdf", original_filename: "prop-log.pdf")
post aircraft_logbook_imports_path(@a), params: { pages: [ page("z-loose-page.jpg"), pdf ], transcribe: "1", log_type: "PROP",
collection_id: "new", document_name: "Prop log" }
perform_enqueued_jobs(only: LogbookTranscriptionJob)
import = @a.logbook_imports.last
assert_equal "done", import.status
request = LogbookTranscriber.client.requests.first
assert_equal [ "text", "document", "text" ], request[:messages][0][:content].map { _1[:type] }
assert_equal %w[PROP PROP], @a.logbook_entries.pluck(:log_type)
assert_equal [ 1, 1 ], @a.logbook_entries.pluck(:page_number) # both from the PDF, page 1 of the document
assert_match "Not logbook entries (forms, tags…), so nothing was transcribed from: prop-log.pdf page 2", import.warnings.first
assert_equal "Page 2 wasn't transcribed: Claude declined to read these pages.", import.warnings.last
end
test "without an API key, pages are only uploaded" do
ENV["ANTHROPIC_API_KEY"] = nil
LogbookTranscriber.client = FakeClaude.new # any request would raise
sign_in @owner
get new_aircraft_logbook_import_path(@a)
assert_select "input[type=checkbox][name=transcribe][disabled]"
assert_includes response.body, "ANTHROPIC_API_KEY"
post aircraft_logbook_imports_path(@a), params: { pages: [ page("p1.jpg") ], transcribe: "1", collection_id: "new" }
perform_enqueued_jobs(only: LogbookTranscriptionJob)
import = @a.logbook_imports.last
assert_equal [ "done", false, 0 ], [ import.status, import.transcribe, import.entries_created ]
assert_equal 1, import.document.pages.count
end
test "rejects files that aren't scans, and pilots can't import" do
sign_in @owner
post aircraft_logbook_imports_path(@a), params: { pages: [ fixture_file_upload("engine_data/cgr30p.csv", "application/x-msdownload") ], collection_id: "new" }
assert_response :unprocessable_entity
assert_equal 0, @a.documents.count
sign_in @pilot
get new_aircraft_logbook_import_path(@a)
assert_redirected_to aircraft_path(@a)
end
end
+54 -11
View File
@@ -8,10 +8,11 @@ class OilAnalysisFlowTest < ActionDispatch::IntegrationTest
@engine = create_component(@aircraft, hours_since_overhaul: 120.5)
end
def results(**overrides)
def results(elements: {}, **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)
engine_hours: 120.5, oil_hours: 40,
elements: { iron: 30, copper: 6, lead: 2500, silicon: 22 }.merge(elements),
element_averages: { iron: 35, copper: 9, lead: 2400, silicon: 12 } }.merge(overrides)
end
test "pilot adds samples; trends, flags and comparison render" do
@@ -20,7 +21,7 @@ class OilAnalysisFlowTest < ActionDispatch::IntegrationTest
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(sample_date: "2026-03-01", elements: { 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)
@@ -42,19 +43,44 @@ class OilAnalysisFlowTest < ActionDispatch::IntegrationTest
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" } }
patch aircraft_oil_analysis_path(@aircraft, analysis), params: { oil_analysis: { status: "action_required" } }
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
assert_select "input[name='oil_analysis[elements][iron]'][value='30.0']"
assert_select "input[name='oil_analysis[elements][zinc]']" # the extra elements are there too
patch aircraft_oil_analysis_path(@aircraft, analysis), params: { oil_analysis: { status: "monitor", elements: { iron: "", copper: "6", lead: "2500", silicon: "22", zinc: "4" } } }
analysis.reload
assert_equal "monitor", analysis.status
assert_nil analysis.value("iron")
assert_equal 4.0, analysis.value("zinc")
delete aircraft_oil_analysis_path(@aircraft, analysis)
assert_equal 0, @aircraft.oil_analyses.count
end
test "oil type, properties and leaving a sample out of trends" do
sign_in @owner
post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(sample_date: "2026-03-01", elements: { iron: 25 }) }
post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(sample_date: "2026-05-01", elements: { iron: 90 }, excluded_from_averages: "1") }
post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(oil_type: "Aeroshell W100 Plus", status: "",
oil_properties: { flashpoint_f: "445", fuel_percent: "0.5" }) }
latest = @aircraft.oil_analyses.recent_first.first
assert_nil latest.status
get aircraft_oil_analysis_path(@aircraft, latest)
assert_includes response.body, "Aeroshell W100 Plus"
assert_includes response.body, "445 °F"
assert_includes response.body, "Not rated"
get aircraft_oil_analyses_path(@aircraft)
assert_includes response.body, "left out of trends"
iron = response.body[/"name":"Iron","data":\[(.*?)\]\]/, 1]
assert_includes iron, "25.0"
refute_includes iron, "90.0" # the excluded sample isn't charted
end
test "an engine from another aircraft is ignored" do
other = create_component(create_aircraft)
sign_in @owner
@@ -85,8 +111,8 @@ class OilAnalysisImportTest < ActionDispatch::IntegrationTest
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 44.0, analyses.first.value("iron")
assert_equal 13.7, analyses.first.average("silicon")
assert_equal 3, @aircraft.events.where(category: "oil_analysis").count
post import_aircraft_oil_analyses_path(@aircraft), params: { report: report }
@@ -98,7 +124,7 @@ class OilAnalysisImportTest < ActionDispatch::IntegrationTest
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)
manual = @aircraft.oil_analyses.create!(sample_date: Date.new(2026, 8, 20), elements: { iron: 44 })
sign_in @owner
post import_aircraft_oil_analyses_path(@aircraft), params: { report: report }
assert_equal 3, @aircraft.oil_analyses.count
@@ -116,4 +142,21 @@ class OilAnalysisImportTest < ActionDispatch::IntegrationTest
assert_match(/Couldn't import that report/, flash[:alert])
assert_equal 0, @aircraft.oil_analyses.count
end
test "imports a Blackstone report: 20 elements, properties, oil type, unrated" do
sign_in @owner
post import_aircraft_oil_analyses_path(@aircraft), params: { report: fixture_file_upload("oil_analysis/blackstone_report.pdf", "application/pdf") }
assert_equal "Imported 5 samples from the Blackstone report.", flash[:notice]
latest = @aircraft.oil_analyses.recent_first.first
assert_equal [ Date.new(2024, 1, 25), "Blackstone", "Phillips XC (A/C) 20W/50", nil, @in_service ],
[ latest.sample_date, latest.lab, latest.oil_type, latest.status, latest.component ] # no engine S/N: the engine in service
assert_equal 20, latest.elements.size
assert_equal 445.0, latest.property("flashpoint_f")
assert latest.report.attached?
follow_redirect!
assert_select "td", text: "Zinc"
assert_includes response.body, "Viscosity @ 100°C"
assert_match(/Iron, Copper, .* above average/, response.body) # 2019: iron 818 ppm vs the 35 ppm universal average
end
end
+1 -1
View File
@@ -24,7 +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)
@oa = @a.oil_analyses.create!(sample_date: Date.current, elements: { iron: 30 }, element_averages: { iron: 35 })
@token = @a.share_tokens.create!(privilege: "maintenance", label: "Mechanic")
@a.log_event!("aircraft", "Created", user: @owner)
sign_in @owner