app: add sidecar sync commands (export, detect, import, resolve, full export/import)

This commit is contained in:
2026-05-31 05:29:03 +02:00
parent d1394ce7bb
commit 4b31a0f74b
43 changed files with 1685 additions and 6 deletions

View File

@@ -0,0 +1,76 @@
use std::sync::Arc;
use application::testing::{InMemoryJobBatchRepository, InMemoryJobRepository, StubEventPublisher};
use application::processing::{CompleteJobCommand, CompleteJobHandler};
use domain::entities::{Job, JobBatch, JobStatus, JobType};
use domain::events::DomainEvent;
use domain::ports::{JobBatchRepository, JobRepository};
use domain::value_objects::StructuredData;
#[tokio::test]
async fn completes_job() {
let job_repo = Arc::new(InMemoryJobRepository::new());
let batch_repo = Arc::new(InMemoryJobBatchRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let mut job = Job::new(JobType::ExtractMetadata, 5, StructuredData::new());
job.start().unwrap();
let job_id = job.job_id;
job_repo.save(&job).await.unwrap();
let handler = CompleteJobHandler::new(job_repo.clone(), batch_repo.clone(), event_pub.clone());
let result = handler.execute(CompleteJobCommand {
job_id,
result: StructuredData::new(),
}).await.unwrap();
assert_eq!(result.status, JobStatus::Completed);
assert!(result.result_data.is_some());
}
#[tokio::test]
async fn completes_job_and_updates_batch() {
let job_repo = Arc::new(InMemoryJobRepository::new());
let batch_repo = Arc::new(InMemoryJobBatchRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let batch = JobBatch::new("test-batch", 2);
let batch_id = batch.batch_id;
batch_repo.save(&batch).await.unwrap();
let mut job = Job::new(JobType::ExtractMetadata, 5, StructuredData::new())
.with_batch(batch_id);
job.start().unwrap();
let job_id = job.job_id;
job_repo.save(&job).await.unwrap();
let handler = CompleteJobHandler::new(job_repo.clone(), batch_repo.clone(), event_pub.clone());
handler.execute(CompleteJobCommand {
job_id,
result: StructuredData::new(),
}).await.unwrap();
let updated_batch = batch_repo.find_by_id(&batch_id).await.unwrap().unwrap();
assert_eq!(updated_batch.completed_count, 1);
}
#[tokio::test]
async fn publishes_event() {
let job_repo = Arc::new(InMemoryJobRepository::new());
let batch_repo = Arc::new(InMemoryJobBatchRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let mut job = Job::new(JobType::ExtractMetadata, 5, StructuredData::new());
job.start().unwrap();
let job_id = job.job_id;
job_repo.save(&job).await.unwrap();
let handler = CompleteJobHandler::new(job_repo.clone(), batch_repo.clone(), event_pub.clone());
handler.execute(CompleteJobCommand {
job_id,
result: StructuredData::new(),
}).await.unwrap();
let events = event_pub.published().await;
assert_eq!(events.len(), 1);
assert!(matches!(&events[0], DomainEvent::JobCompleted { job_id: id, .. } if *id == job_id));
}

View File

@@ -0,0 +1,48 @@
use std::sync::Arc;
use application::testing::{InMemoryPipelineRepository, InMemoryPluginRepository};
use application::processing::{ConfigurePipelineCommand, ConfigurePipelineHandler, PipelineStepConfig};
use domain::entities::{Plugin, PluginType};
use domain::errors::DomainError;
use domain::ports::PluginRepository;
use domain::value_objects::{StructuredData, SystemId};
#[tokio::test]
async fn creates_pipeline() {
let pipeline_repo = Arc::new(InMemoryPipelineRepository::new());
let plugin_repo = Arc::new(InMemoryPluginRepository::new());
let p1 = Plugin::new("EXIF", PluginType::MediaProcessor);
let p2 = Plugin::new("Thumb", PluginType::MediaProcessor);
let p1_id = p1.plugin_id;
let p2_id = p2.plugin_id;
plugin_repo.save(&p1).await.unwrap();
plugin_repo.save(&p2).await.unwrap();
let handler = ConfigurePipelineHandler::new(pipeline_repo.clone(), plugin_repo.clone());
let pipeline = handler.execute(ConfigurePipelineCommand {
trigger_event: "asset.ingested".into(),
steps: vec![
PipelineStepConfig { plugin_id: p1_id, config: StructuredData::new() },
PipelineStepConfig { plugin_id: p2_id, config: StructuredData::new() },
],
}).await.unwrap();
assert_eq!(pipeline.trigger_event, "asset.ingested");
assert_eq!(pipeline.steps.len(), 2);
}
#[tokio::test]
async fn rejects_nonexistent_plugin() {
let pipeline_repo = Arc::new(InMemoryPipelineRepository::new());
let plugin_repo = Arc::new(InMemoryPluginRepository::new());
let handler = ConfigurePipelineHandler::new(pipeline_repo.clone(), plugin_repo.clone());
let result = handler.execute(ConfigurePipelineCommand {
trigger_event: "asset.ingested".into(),
steps: vec![
PipelineStepConfig { plugin_id: SystemId::new(), config: StructuredData::new() },
],
}).await;
assert!(matches!(result, Err(DomainError::NotFound(_))));
}

View File

@@ -0,0 +1,65 @@
use std::sync::Arc;
use application::testing::{InMemoryJobRepository, StubEventPublisher};
use application::processing::{EnqueueJobCommand, EnqueueJobHandler};
use domain::entities::{JobStatus, JobType};
use domain::events::DomainEvent;
use domain::value_objects::{StructuredData, SystemId};
#[tokio::test]
async fn enqueues_job() {
let job_repo = Arc::new(InMemoryJobRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let handler = EnqueueJobHandler::new(job_repo.clone(), event_pub.clone());
let job = handler.execute(EnqueueJobCommand {
job_type: JobType::ExtractMetadata,
priority: 5,
payload: StructuredData::new(),
target_asset_id: None,
batch_id: None,
}).await.unwrap();
assert_eq!(job.status, JobStatus::Queued);
assert_eq!(job.priority, 5);
assert!(job.target_asset_id.is_none());
assert!(job.batch_id.is_none());
}
#[tokio::test]
async fn enqueues_with_target_and_batch() {
let job_repo = Arc::new(InMemoryJobRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let handler = EnqueueJobHandler::new(job_repo.clone(), event_pub.clone());
let target = SystemId::new();
let batch = SystemId::new();
let job = handler.execute(EnqueueJobCommand {
job_type: JobType::GenerateDerivative,
priority: 10,
payload: StructuredData::new(),
target_asset_id: Some(target),
batch_id: Some(batch),
}).await.unwrap();
assert_eq!(job.target_asset_id, Some(target));
assert_eq!(job.batch_id, Some(batch));
}
#[tokio::test]
async fn publishes_event() {
let job_repo = Arc::new(InMemoryJobRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let handler = EnqueueJobHandler::new(job_repo.clone(), event_pub.clone());
let job = handler.execute(EnqueueJobCommand {
job_type: JobType::ScanDirectory,
priority: 1,
payload: StructuredData::new(),
target_asset_id: None,
batch_id: None,
}).await.unwrap();
let events = event_pub.published().await;
assert_eq!(events.len(), 1);
assert!(matches!(&events[0], DomainEvent::JobEnqueued { job_id, .. } if *job_id == job.job_id));
}

View File

@@ -0,0 +1,95 @@
use std::sync::Arc;
use application::testing::{InMemoryJobBatchRepository, InMemoryJobRepository, StubEventPublisher};
use application::processing::{FailJobCommand, FailJobHandler};
use domain::entities::{Job, JobBatch, JobStatus, JobType};
use domain::events::DomainEvent;
use domain::ports::{JobBatchRepository, JobRepository};
use domain::value_objects::StructuredData;
fn make_handler(
job_repo: Arc<InMemoryJobRepository>,
batch_repo: Arc<InMemoryJobBatchRepository>,
event_pub: Arc<StubEventPublisher>,
) -> FailJobHandler {
FailJobHandler::new(job_repo, batch_repo, event_pub)
}
#[tokio::test]
async fn retries_on_failure() {
let job_repo = Arc::new(InMemoryJobRepository::new());
let batch_repo = Arc::new(InMemoryJobBatchRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let job = Job::new(JobType::ExtractMetadata, 5, StructuredData::new());
let job_id = job.job_id;
assert_eq!(job.retry_count, 0);
job_repo.save(&job).await.unwrap();
let handler = make_handler(job_repo.clone(), batch_repo.clone(), event_pub.clone());
let result = handler.execute(FailJobCommand {
job_id,
error: "transient error".into(),
}).await.unwrap();
assert_eq!(result.status, JobStatus::Queued);
assert_eq!(result.retry_count, 1);
let events = event_pub.published().await;
assert!(matches!(&events[0], DomainEvent::JobEnqueued { .. }));
}
#[tokio::test]
async fn fails_permanently_after_max_retries() {
let job_repo = Arc::new(InMemoryJobRepository::new());
let batch_repo = Arc::new(InMemoryJobBatchRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let mut job = Job::new(JobType::ExtractMetadata, 5, StructuredData::new());
// Exhaust retries (max_retries=3, so fail 3 times)
job.fail("err1");
job.fail("err2");
assert_eq!(job.retry_count, 2);
assert_eq!(job.status, JobStatus::Queued); // still retryable
let job_id = job.job_id;
job_repo.save(&job).await.unwrap();
let handler = make_handler(job_repo.clone(), batch_repo.clone(), event_pub.clone());
let result = handler.execute(FailJobCommand {
job_id,
error: "fatal".into(),
}).await.unwrap();
assert_eq!(result.status, JobStatus::Failed);
assert_eq!(result.retry_count, 3);
let events = event_pub.published().await;
assert!(matches!(&events[0], DomainEvent::JobFailed { .. }));
}
#[tokio::test]
async fn updates_batch_on_permanent_failure() {
let job_repo = Arc::new(InMemoryJobRepository::new());
let batch_repo = Arc::new(InMemoryJobBatchRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let batch = JobBatch::new("test-batch", 2);
let batch_id = batch.batch_id;
batch_repo.save(&batch).await.unwrap();
let mut job = Job::new(JobType::ExtractMetadata, 5, StructuredData::new())
.with_batch(batch_id);
// Exhaust retries
job.fail("err1");
job.fail("err2");
let job_id = job.job_id;
job_repo.save(&job).await.unwrap();
let handler = make_handler(job_repo.clone(), batch_repo.clone(), event_pub.clone());
handler.execute(FailJobCommand {
job_id,
error: "permanent failure".into(),
}).await.unwrap();
let updated_batch = batch_repo.find_by_id(&batch_id).await.unwrap().unwrap();
assert_eq!(updated_batch.failed_count, 1);
}

View File

@@ -0,0 +1,61 @@
use std::sync::Arc;
use application::testing::InMemoryPluginRepository;
use application::processing::{ManagePluginCommand, ManagePluginHandler, PluginAction};
use domain::entities::{Plugin, PluginType};
use domain::ports::PluginRepository;
use domain::value_objects::StructuredData;
#[tokio::test]
async fn creates_plugin() {
let plugin_repo = Arc::new(InMemoryPluginRepository::new());
let handler = ManagePluginHandler::new(plugin_repo.clone());
let plugin = handler.execute(ManagePluginCommand {
plugin_id: None,
action: PluginAction::Create {
name: "EXIF Extractor".into(),
plugin_type: PluginType::MediaProcessor,
config: StructuredData::new(),
},
}).await.unwrap();
assert_eq!(plugin.name, "EXIF Extractor");
assert_eq!(plugin.plugin_type, PluginType::MediaProcessor);
assert!(plugin.is_enabled);
let saved = plugin_repo.find_by_id(&plugin.plugin_id).await.unwrap();
assert!(saved.is_some());
}
#[tokio::test]
async fn enables_plugin() {
let plugin_repo = Arc::new(InMemoryPluginRepository::new());
let mut plugin = Plugin::new("Test", PluginType::ScheduledTask);
plugin.disable();
let plugin_id = plugin.plugin_id;
plugin_repo.save(&plugin).await.unwrap();
let handler = ManagePluginHandler::new(plugin_repo.clone());
let result = handler.execute(ManagePluginCommand {
plugin_id: Some(plugin_id),
action: PluginAction::Enable,
}).await.unwrap();
assert!(result.is_enabled);
}
#[tokio::test]
async fn disables_plugin() {
let plugin_repo = Arc::new(InMemoryPluginRepository::new());
let plugin = Plugin::new("Test", PluginType::SidecarWriter);
let plugin_id = plugin.plugin_id;
plugin_repo.save(&plugin).await.unwrap();
let handler = ManagePluginHandler::new(plugin_repo.clone());
let result = handler.execute(ManagePluginCommand {
plugin_id: Some(plugin_id),
action: PluginAction::Disable,
}).await.unwrap();
assert!(!result.is_enabled);
}

View File

@@ -0,0 +1,6 @@
mod enqueue_job;
mod start_job;
mod complete_job;
mod fail_job;
mod manage_plugin;
mod configure_pipeline;

View File

@@ -0,0 +1,35 @@
use std::sync::Arc;
use application::testing::InMemoryJobRepository;
use application::processing::{StartJobCommand, StartJobHandler};
use domain::entities::{Job, JobStatus, JobType};
use domain::errors::DomainError;
use domain::ports::JobRepository;
use domain::value_objects::StructuredData;
#[tokio::test]
async fn starts_queued_job() {
let job_repo = Arc::new(InMemoryJobRepository::new());
let job = Job::new(JobType::ExtractMetadata, 5, StructuredData::new());
let job_id = job.job_id;
job_repo.save(&job).await.unwrap();
let handler = StartJobHandler::new(job_repo.clone());
let result = handler.execute(StartJobCommand { job_id }).await.unwrap();
assert_eq!(result.status, JobStatus::Processing);
assert!(result.started_at.is_some());
}
#[tokio::test]
async fn rejects_non_queued_job() {
let job_repo = Arc::new(InMemoryJobRepository::new());
let mut job = Job::new(JobType::ExtractMetadata, 5, StructuredData::new());
job.start().unwrap(); // now Processing
let job_id = job.job_id;
job_repo.save(&job).await.unwrap();
let handler = StartJobHandler::new(job_repo.clone());
let result = handler.execute(StartJobCommand { job_id }).await;
assert!(matches!(result, Err(DomainError::Conflict(_))));
}