Skip to content

Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)

4.3 KB
Raw Blame
"""CLI monitoring against an existing real upload job, without cancelling it."""
import json
import os
from pathlib import Path
import signal
import subprocess
import sys
import time

import pytest


def test_watch_real_job_and_interrupt_only_the_watcher():
    source = os.environ.get('DOPPLERGEIST_REAL_JOB_ROOT')
    if not source:
        pytest.skip('set DOPPLERGEIST_REAL_JOB_ROOT to a running real upload')
    job = Path(source)
    if json.loads((job / 'result.json').read_text())['status'] != 'running':
        pytest.skip('the real upload is not currently running')
    worker_files = list((job / 'search' / '.tile-workers').glob('*.sqlite*'))
    recent_worker_write = any(time.time() - path.stat().st_mtime < 30 for path in worker_files)
    base = [sys.executable, '-m', 'dopplergeist.cli', 'job', job.name, '--jobs', str(job.parent)]
    once = subprocess.run(base, check=True, capture_output=True, text=True)
    assert json.loads(once.stdout)['job_id'] == job.name
    watcher = subprocess.Popen(base + ['--watch', '--interval', '1'], stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE, text=True)
    try:
        time.sleep(2.5)
        assert watcher.poll() is None
        watcher.send_signal(signal.SIGINT)
        stdout, stderr = watcher.communicate(timeout=15)
    finally:
        if watcher.poll() is None:
            watcher.kill()
            watcher.wait()
    assert watcher.returncode == 130 and stdout == ''
    assert 'search files observed (unverified:' in stderr and 'file-count rate' in stderr
    assert 'newest matching file modified' in stderr
    assert 'fine support records observed (unverified)' in stderr
    assert 'newest search write ' in stderr
    assert 'unverified file activity' in stderr
    if recent_worker_write:
        assert '.tile-workers/' in stderr
    assert 'Stopped watching; the job continues.' in stderr
    assert json.loads((job / 'result.json').read_text())['status'] == 'running'


def test_real_upload_prints_job_id_before_final_json(tmp_path):
    source = os.environ.get('DOPPLERGEIST_REAL_JOB_ROOT')
    if not source:
        pytest.skip('set DOPPLERGEIST_REAL_JOB_ROOT to the original upload')
    audio = Path(source) / 'input.mp3'
    config = tmp_path / 'operator.json'
    config.write_text(json.dumps({'search_workers': 0}))  # Fail before audio discovery.
    jobs = tmp_path / 'jobs'
    result = subprocess.run([sys.executable, '-m', 'dopplergeist.cli', 'analyze', str(audio),
                             '--jobs', str(jobs), '--config', str(config)],
                            check=True, capture_output=True, text=True, timeout=60)
    value = json.loads(result.stdout)
    assert value['status'] == 'failed' and value['reason_code'] == 'invalid_configuration'
    assert f"Job {value['job_id']} started." in result.stderr
    assert f"dopplergeist job {value['job_id']}" in result.stderr
    assert (jobs / value['job_id'] / 'result.json').is_file()


def test_watch_labels_real_search_file_counts_unverified():
    source = os.environ.get('DOPPLERGEIST_REAL_JOB_ROOT')
    if not source:
        pytest.skip('set DOPPLERGEIST_REAL_JOB_ROOT to a completed real upload')
    job = Path(source)
    value = json.loads((job / 'result.json').read_text())
    if value['status'] == 'running':
        pytest.skip('use a completed real upload so its checkpoints stay stable')
    search = job / 'search'
    coarse = [path for path in search.glob('tile-*.json') if not path.name.endswith('.error.json')]
    fine = [path for path in search.glob('fine-tile-*.json') if not path.name.endswith('.error.json')]
    diagnostics = list((search / 'support').glob('fine-*.json'))
    if len(fine) == len(diagnostics):
        pytest.skip('the real upload does not distinguish search payloads from support diagnostics')

    command = [sys.executable, '-m', 'dopplergeist.cli', 'job', job.name,
               '--jobs', str(job.parent), '--watch', '--interval', '1']
    result = subprocess.run(command, check=True, capture_output=True, text=True, timeout=15)

    assert f'{len(coarse) + len(fine)} search files observed (unverified:' in result.stderr
    assert f'(unverified: {len(coarse)} coarse, {len(fine)} fine)' in result.stderr
    assert f', {len(diagnostics)} fine support records observed (unverified)' in result.stderr
    assert 'fine support checkpoints' not in result.stderr