1use gen_core::{HashId, traits::Capnp};
2use serde::{Deserialize, Serialize};
3
4use crate::{
5 annotations::{AnnotationFile, AnnotationFileInfo},
6 db::OperationsConnection,
7 errors::FileAdditionError,
8 gen_models_capnp::{
9 manifest, manifest_annotation_file_addition, manifest_diff, manifest_operation,
10 manifest_operation_file_addition,
11 },
12 operations::{FileAddition, Operation, OperationFile, OperationSummary},
13 traits::Query,
14};
15
16#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
17pub struct ManifestOperationFileAddition {
18 pub file_addition: FileAddition,
19 pub filename: String,
20 pub file_path: String,
21}
22
23#[derive(Debug, thiserror::Error)]
24pub enum ManifestOperationFileAdditionError {
25 #[error("Database error: {0}")]
26 DatabaseError(#[from] rusqlite::Error),
27
28 #[error(transparent)]
29 FileAdditionError(#[from] FileAdditionError),
30}
31
32impl<'a> Capnp<'a> for ManifestOperationFileAddition {
33 type Builder = manifest_operation_file_addition::Builder<'a>;
34 type Reader = manifest_operation_file_addition::Reader<'a>;
35
36 fn write_capnp(&self, builder: &mut Self::Builder) {
37 let mut file_addition_builder = builder.reborrow().init_file_addition();
38 self.file_addition.write_capnp(&mut file_addition_builder);
39 builder.set_filename(&self.filename);
40 builder.set_file_path(&self.file_path);
41 }
42
43 fn read_capnp(reader: Self::Reader) -> Self {
44 Self {
45 file_addition: FileAddition::read_capnp(reader.get_file_addition().unwrap()),
46 filename: reader.get_filename().unwrap().to_string().unwrap(),
47 file_path: reader.get_file_path().unwrap().to_string().unwrap(),
48 }
49 }
50}
51
52impl ManifestOperationFileAddition {
53 pub fn get_files_for_operation(
54 conn: &OperationsConnection,
55 operation_hash: &HashId,
56 ) -> Result<Vec<Self>, ManifestOperationFileAdditionError> {
57 Ok(
58 OperationFile::get_files_for_operation(conn, operation_hash)?
59 .into_iter()
60 .map(|file| {
61 let file_addition = FileAddition {
62 id: file.id,
63 asset_uri: file.asset_uri,
64 file_type: file.file_type,
65 checksum: file.checksum,
66 };
67 Self {
68 file_path: file.file_path,
69 file_addition,
70 filename: file.filename,
71 }
72 })
73 .collect(),
74 )
75 }
76}
77
78#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
79pub struct ManifestAnnotationFileAddition {
80 pub file_addition: FileAddition,
81 pub index_file_addition: Option<FileAddition>,
82 pub name: Option<String>,
83}
84
85impl<'a> Capnp<'a> for ManifestAnnotationFileAddition {
86 type Builder = manifest_annotation_file_addition::Builder<'a>;
87 type Reader = manifest_annotation_file_addition::Reader<'a>;
88
89 fn write_capnp(&self, builder: &mut Self::Builder) {
90 let mut file_addition_builder = builder.reborrow().init_file_addition();
91 self.file_addition.write_capnp(&mut file_addition_builder);
92 match &self.name {
93 Some(name) => builder.reborrow().get_name().set_some(name),
94 None => builder.reborrow().get_name().set_none(()),
95 }
96 match &self.index_file_addition {
97 Some(index_file_addition) => {
98 let mut index_file_builder =
99 builder.reborrow().get_index_file_addition().init_some();
100 index_file_addition.write_capnp(&mut index_file_builder);
101 }
102 None => builder.reborrow().get_index_file_addition().set_none(()),
103 }
104 }
105
106 fn read_capnp(reader: Self::Reader) -> Self {
107 let file_addition = FileAddition::read_capnp(reader.get_file_addition().unwrap());
108 let name = match reader.get_name().which().unwrap() {
109 manifest_annotation_file_addition::name::None(()) => None,
110 manifest_annotation_file_addition::name::Some(name_reader) => {
111 Some(name_reader.unwrap().to_string().unwrap())
112 }
113 };
114 let index_file_addition = match reader.get_index_file_addition().which().unwrap() {
115 manifest_annotation_file_addition::index_file_addition::None(()) => None,
116 manifest_annotation_file_addition::index_file_addition::Some(file_reader) => {
117 Some(FileAddition::read_capnp(file_reader.unwrap()))
118 }
119 };
120 ManifestAnnotationFileAddition {
121 file_addition,
122 index_file_addition,
123 name,
124 }
125 }
126}
127
128#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
129pub struct ManifestOperation {
130 pub operation: Operation,
131 pub file_additions: Vec<ManifestOperationFileAddition>,
132 pub annotation_file_additions: Vec<ManifestAnnotationFileAddition>,
133 pub operation_summary: Option<OperationSummary>,
134}
135
136impl<'a> Capnp<'a> for ManifestOperation {
137 type Builder = manifest_operation::Builder<'a>;
138 type Reader = manifest_operation::Reader<'a>;
139
140 fn write_capnp(&self, builder: &mut Self::Builder) {
141 let mut operation_builder = builder.reborrow().init_operation();
142 self.operation.write_capnp(&mut operation_builder);
143
144 let mut operation_file_additions_builder = builder
145 .reborrow()
146 .init_operation_file_additions(self.file_additions.len() as u32);
147 for (i, file_addition) in self.file_additions.iter().enumerate() {
148 let mut file_addition_builder =
149 operation_file_additions_builder.reborrow().get(i as u32);
150 file_addition.write_capnp(&mut file_addition_builder);
151 }
152
153 let mut annotation_file_additions_builder = builder
154 .reborrow()
155 .init_annotation_file_additions(self.annotation_file_additions.len() as u32);
156 for (i, file_addition) in self.annotation_file_additions.iter().enumerate() {
157 let mut file_addition_builder =
158 annotation_file_additions_builder.reborrow().get(i as u32);
159 file_addition
160 .file_addition
161 .write_capnp(&mut file_addition_builder);
162 }
163
164 let mut annotation_file_details_builder = builder
165 .reborrow()
166 .init_annotation_file_details(self.annotation_file_additions.len() as u32);
167 for (i, file_addition) in self.annotation_file_additions.iter().enumerate() {
168 let mut detail_builder = annotation_file_details_builder.reborrow().get(i as u32);
169 file_addition.write_capnp(&mut detail_builder);
170 }
171
172 match &self.operation_summary {
173 None => {
174 builder.reborrow().get_operation_summary().set_none(());
175 }
176 Some(summary) => {
177 let mut summary_builder = builder.reborrow().get_operation_summary().init_some();
178 summary.write_capnp(&mut summary_builder);
179 }
180 }
181 }
182
183 fn read_capnp(reader: Self::Reader) -> Self {
184 let operation = Operation::read_capnp(reader.get_operation().unwrap());
185 let file_additions_reader = reader.get_operation_file_additions().unwrap();
186 let file_additions = file_additions_reader
187 .iter()
188 .map(ManifestOperationFileAddition::read_capnp)
189 .collect();
190
191 let annotation_file_additions = if reader.has_annotation_file_details() {
192 let annotation_file_details_reader = reader.get_annotation_file_details().unwrap();
193 annotation_file_details_reader
194 .iter()
195 .map(ManifestAnnotationFileAddition::read_capnp)
196 .collect()
197 } else {
198 let annotation_file_additions_reader = reader.get_annotation_file_additions().unwrap();
199 annotation_file_additions_reader
200 .iter()
201 .map(|file_addition_reader| ManifestAnnotationFileAddition {
202 file_addition: FileAddition::read_capnp(file_addition_reader),
203 index_file_addition: None,
204 name: None,
205 })
206 .collect()
207 };
208
209 let operation_summary = match reader.get_operation_summary().which().unwrap() {
210 manifest_operation::operation_summary::None(()) => None,
211 manifest_operation::operation_summary::Some(summary_reader) => {
212 Some(OperationSummary::read_capnp(summary_reader.unwrap()))
213 }
214 };
215
216 ManifestOperation {
217 operation,
218 file_additions,
219 annotation_file_additions,
220 operation_summary,
221 }
222 }
223}
224
225#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
226pub struct Manifest {
227 pub manifest_version: String,
228 pub branch_name: String,
229 pub end_hash: Option<HashId>,
230 pub operations: Vec<ManifestOperation>,
231}
232
233impl<'a> Capnp<'a> for Manifest {
234 type Builder = manifest::Builder<'a>;
235 type Reader = manifest::Reader<'a>;
236
237 fn write_capnp(&self, builder: &mut Self::Builder) {
238 builder.set_manifest_version(&self.manifest_version);
239 builder.set_branch_name(&self.branch_name);
240 let mut end_hash_builder = builder.reborrow().get_end_hash();
241 match &self.end_hash {
242 Some(hash) => end_hash_builder.set_some(&hash.0).unwrap(),
243 None => end_hash_builder.set_none(()),
244 }
245
246 let mut operations_builder = builder
247 .reborrow()
248 .init_operations(self.operations.len() as u32);
249 for (i, operation) in self.operations.iter().enumerate() {
250 let mut operation_builder = operations_builder.reborrow().get(i as u32);
251 operation.write_capnp(&mut operation_builder);
252 }
253 }
254
255 fn read_capnp(reader: Self::Reader) -> Self {
256 let manifest_version = reader.get_manifest_version().unwrap().to_string().unwrap();
257 let branch_name = reader.get_branch_name().unwrap().to_string().unwrap();
258
259 let operations_reader = reader.get_operations().unwrap();
260 let mut operations = Vec::new();
261 for operation_reader in operations_reader.iter() {
262 operations.push(ManifestOperation::read_capnp(operation_reader));
263 }
264
265 let end_hash = match reader.get_end_hash().which().unwrap() {
266 manifest::end_hash::None(()) => None,
267 manifest::end_hash::Some(hash_reader) => {
268 let hash_reader = hash_reader.unwrap();
269 let slice = hash_reader.as_slice().unwrap();
270 Some(slice.try_into().unwrap())
271 }
272 };
273
274 Manifest {
275 manifest_version,
276 branch_name,
277 end_hash,
278 operations,
279 }
280 }
281}
282
283#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
284pub struct ManifestDiff {
285 pub missing_in_manifest2: Vec<ManifestOperation>,
286 pub missing_in_manifest1: Vec<ManifestOperation>,
287}
288
289impl<'a> Capnp<'a> for ManifestDiff {
290 type Builder = manifest_diff::Builder<'a>;
291 type Reader = manifest_diff::Reader<'a>;
292
293 fn write_capnp(&self, builder: &mut Self::Builder) {
294 let mut missing_in_manifest2_builder = builder
295 .reborrow()
296 .init_missing_in_manifest2(self.missing_in_manifest2.len() as u32);
297 for (i, operation) in self.missing_in_manifest2.iter().enumerate() {
298 let mut operation_builder = missing_in_manifest2_builder.reborrow().get(i as u32);
299 operation.write_capnp(&mut operation_builder);
300 }
301
302 let mut missing_in_manifest1_builder = builder
303 .reborrow()
304 .init_missing_in_manifest1(self.missing_in_manifest1.len() as u32);
305 for (i, operation) in self.missing_in_manifest1.iter().enumerate() {
306 let mut operation_builder = missing_in_manifest1_builder.reborrow().get(i as u32);
307 operation.write_capnp(&mut operation_builder);
308 }
309 }
310
311 fn read_capnp(reader: Self::Reader) -> Self {
312 let missing_in_manifest2_reader = reader.get_missing_in_manifest2().unwrap();
313 let mut missing_in_manifest2 = Vec::new();
314 for operation_reader in missing_in_manifest2_reader.iter() {
315 missing_in_manifest2.push(ManifestOperation::read_capnp(operation_reader));
316 }
317
318 let missing_in_manifest1_reader = reader.get_missing_in_manifest1().unwrap();
319 let mut missing_in_manifest1 = Vec::new();
320 for operation_reader in missing_in_manifest1_reader.iter() {
321 missing_in_manifest1.push(ManifestOperation::read_capnp(operation_reader));
322 }
323
324 ManifestDiff {
325 missing_in_manifest2,
326 missing_in_manifest1,
327 }
328 }
329}
330
331pub struct ManifestGenerator<'a> {
332 conn: &'a OperationsConnection,
333}
334
335impl<'a> ManifestGenerator<'a> {
336 pub fn new(conn: &'a OperationsConnection) -> Self {
337 Self { conn }
338 }
339
340 pub fn generate_manifest(
341 &self,
342 branch_name: &str,
343 end_hash: Option<&HashId>,
344 ) -> Result<Manifest, ManifestError> {
345 let mut manifest_operations = vec![];
346
347 if let Some(target_hash) = end_hash {
348 let hashes = Operation::get_upstream(self.conn, target_hash);
349 let mut operations_map = std::collections::HashMap::new();
350 for op in Operation::query_by_ids(self.conn, &hashes) {
351 operations_map.insert(op.hash, op.clone());
352 }
353
354 for hash in hashes.iter() {
355 if let Some(op) = operations_map.get(hash) {
356 let file_additions = ManifestOperationFileAddition::get_files_for_operation(
357 self.conn, &op.hash,
358 )?;
359 let annotation_file_additions =
360 AnnotationFile::get_files_for_operation(self.conn, &op.hash)
361 .into_iter()
362 .map(|entry: AnnotationFileInfo| ManifestAnnotationFileAddition {
363 file_addition: entry.file_addition,
364 index_file_addition: entry.index_file_addition,
365 name: entry.name,
366 })
367 .collect();
368 let operation_summary = OperationSummary::query(
369 self.conn,
370 "select * from operation_summaries where operation_hash = ?1",
371 rusqlite::params![op.hash],
372 )
373 .into_iter()
374 .next();
375
376 manifest_operations.push(ManifestOperation {
377 operation: op.clone(),
378 file_additions,
379 annotation_file_additions,
380 operation_summary,
381 });
382 }
383 }
384 }
385
386 Ok(Manifest {
387 manifest_version: "1.0".to_string(),
388 branch_name: branch_name.to_string(),
389 end_hash: end_hash.copied(),
390 operations: manifest_operations,
391 })
392 }
393}
394
395#[derive(Debug, thiserror::Error)]
396pub enum ManifestError {
397 #[error("Database error: {0}")]
398 DatabaseError(#[from] rusqlite::Error),
399 #[error("Serialization error: {0}")]
400 SerializationError(#[from] serde_json::Error),
401 #[error("Operation not found")]
402 OperationNotFound(String),
403 #[error("Branch not found")]
404 BranchNotFound,
405 #[error("Manifest operation file addition error: {0}")]
406 ManifestOperationFileAdditionError(#[from] ManifestOperationFileAdditionError),
407}
408
409pub struct ManifestComparer;
410
411impl ManifestComparer {
412 pub fn diff_manifests(
413 manifest1: &Manifest,
414 manifest2: &Manifest,
415 ) -> Result<ManifestDiff, ManifestDiffError> {
416 if manifest1.manifest_version != manifest2.manifest_version {
417 return Err(ManifestDiffError::IncompatibleVersions);
418 }
419
420 let ops1_hashes: std::collections::HashSet<_> = manifest1
421 .operations
422 .iter()
423 .map(|op| &op.operation.hash)
424 .collect();
425
426 let ops2_hashes: std::collections::HashSet<_> = manifest2
427 .operations
428 .iter()
429 .map(|op| &op.operation.hash)
430 .collect();
431
432 let missing_in_manifest2: Vec<ManifestOperation> = manifest1
433 .operations
434 .iter()
435 .filter(|op| !ops2_hashes.contains(&op.operation.hash))
436 .cloned()
437 .collect();
438
439 let missing_in_manifest1: Vec<ManifestOperation> = manifest2
440 .operations
441 .iter()
442 .filter(|op| !ops1_hashes.contains(&op.operation.hash))
443 .cloned()
444 .collect();
445
446 Ok(ManifestDiff {
447 missing_in_manifest2,
448 missing_in_manifest1,
449 })
450 }
451}
452
453#[derive(Debug, thiserror::Error)]
454pub enum ManifestDiffError {
455 #[error("Incompatible manifest versions")]
456 IncompatibleVersions,
457}
458
459#[cfg(test)]
460mod tests {
461 use std::fs;
462
463 use capnp::message::TypedBuilder;
464
465 use super::*;
466 use crate::{
467 annotations::{AnnotationFile, AnnotationFileAdditionInput},
468 file_types::FileTypes,
469 operations::{OperationFile, OperationInfo},
470 session_operations::{end_operation, start_operation},
471 test_helpers::setup_gen,
472 };
473
474 #[test]
475 fn test_manifest_operation_capnp_serialization() {
476 let context = setup_gen();
477 let conn = context.graph().conn();
478 let op_conn = context.operations().conn();
479
480 let db_uuid = crate::metadata::get_db_uuid(conn);
481 crate::files::GenDatabase::create(op_conn, &db_uuid, "test_db", "test_db_path").unwrap();
482
483 let mut session = start_operation(conn);
484 crate::sequence::Sequence::new()
485 .sequence("ACGT")
486 .sequence_type("DNA")
487 .save(conn)
488 .unwrap();
489 let op_info = OperationInfo {
490 files: vec![],
491 description: "test op".to_string(),
492 };
493 let operation = end_operation(&context, &mut session, &op_info, "test", None).unwrap();
494
495 let manifest_operation = ManifestOperation {
496 operation: operation.clone(),
497 file_additions: vec![ManifestOperationFileAddition {
498 file_addition: FileAddition {
499 id: HashId([1u8; 32]),
500 asset_uri: "file:///path/to/file.fa".to_string(),
501 file_type: FileTypes::Fasta,
502 checksum: HashId([2u8; 32]),
503 },
504 filename: "file.fa".to_string(),
505 file_path: "fixtures/file.fa".to_string(),
506 }],
507 annotation_file_additions: vec![ManifestAnnotationFileAddition {
508 file_addition: FileAddition {
509 id: HashId([3u8; 32]),
510 asset_uri: "file:///path/to/annotation.gff3".to_string(),
511 file_type: FileTypes::Gff3,
512 checksum: HashId([4u8; 32]),
513 },
514 index_file_addition: None,
515 name: Some("track-a".to_string()),
516 }],
517 operation_summary: Some(OperationSummary {
518 id: 1,
519 operation_hash: operation.hash,
520 summary: "Test operation summary".to_string(),
521 }),
522 };
523
524 let mut message = TypedBuilder::<manifest_operation::Owned>::new_default();
525 let mut root = message.init_root();
526 manifest_operation.write_capnp(&mut root);
527
528 let deserialized = ManifestOperation::read_capnp(root.into_reader());
529 assert_eq!(manifest_operation, deserialized);
530 }
531
532 #[test]
533 fn test_manifest_capnp_serialization() {
534 let context = setup_gen();
535 let conn = context.graph().conn();
536 let op_conn = context.operations().conn();
537
538 let db_uuid = crate::metadata::get_db_uuid(conn);
539 crate::files::GenDatabase::create(op_conn, &db_uuid, "test_db", "test_db_path").unwrap();
540
541 let mut session = start_operation(conn);
542 crate::sequence::Sequence::new()
543 .sequence("ACGT")
544 .sequence_type("DNA")
545 .save(conn)
546 .unwrap();
547 let op_info = OperationInfo {
548 files: vec![],
549 description: "test op".to_string(),
550 };
551 let operation = end_operation(&context, &mut session, &op_info, "test", None).unwrap();
552
553 let manifest = Manifest {
554 manifest_version: "1.0".to_string(),
555 branch_name: "main".to_string(),
556 end_hash: Some(operation.hash),
557 operations: vec![ManifestOperation {
558 operation,
559 file_additions: vec![],
560 annotation_file_additions: vec![],
561 operation_summary: None,
562 }],
563 };
564
565 let mut message = TypedBuilder::<manifest::Owned>::new_default();
566 let mut root = message.init_root();
567 manifest.write_capnp(&mut root);
568
569 let deserialized = Manifest::read_capnp(root.into_reader());
570 assert_eq!(manifest, deserialized);
571 }
572
573 #[test]
574 fn test_manifest_diff_capnp_serialization() {
575 let context = setup_gen();
576 let conn = context.graph().conn();
577 let op_conn = context.operations().conn();
578
579 let db_uuid = crate::metadata::get_db_uuid(conn);
580 crate::files::GenDatabase::create(op_conn, &db_uuid, "test_db", "test_db_path").unwrap();
581
582 let mut session = start_operation(conn);
583 crate::sequence::Sequence::new()
584 .sequence("ACGT")
585 .sequence_type("DNA")
586 .save(conn)
587 .unwrap();
588 let op_info = OperationInfo {
589 files: vec![],
590 description: "test op".to_string(),
591 };
592 let operation = end_operation(&context, &mut session, &op_info, "test", None).unwrap();
593
594 let manifest_operation = ManifestOperation {
595 operation,
596 file_additions: vec![],
597 annotation_file_additions: vec![],
598 operation_summary: None,
599 };
600
601 let manifest_diff = ManifestDiff {
602 missing_in_manifest2: vec![manifest_operation.clone()],
603 missing_in_manifest1: vec![manifest_operation],
604 };
605
606 let mut message = TypedBuilder::<manifest_diff::Owned>::new_default();
607 let mut root = message.init_root();
608 manifest_diff.write_capnp(&mut root);
609
610 let deserialized = ManifestDiff::read_capnp(root.into_reader());
611 assert_eq!(manifest_diff, deserialized);
612 }
613
614 #[test]
615 fn test_manifest_generator() {
616 let context = setup_gen();
617 let conn = context.graph().conn();
618 let op_conn = context.operations().conn();
619
620 let db_uuid = crate::metadata::get_db_uuid(conn);
621 crate::files::GenDatabase::create(op_conn, &db_uuid, "test_db", "test_db_path").unwrap();
622
623 let mut session = start_operation(conn);
624 crate::sequence::Sequence::new()
625 .sequence("ACGT")
626 .sequence_type("DNA")
627 .save(conn)
628 .unwrap();
629 let op_info = OperationInfo {
630 files: vec![],
631 description: "first op".to_string(),
632 };
633 let op1 = end_operation(&context, &mut session, &op_info, "test", None).unwrap();
634
635 let mut session = start_operation(conn);
636 crate::sequence::Sequence::new()
637 .sequence("TGCA")
638 .sequence_type("DNA")
639 .save(conn)
640 .unwrap();
641 let op_info = OperationInfo {
642 files: vec![],
643 description: "second op".to_string(),
644 };
645 let op2 = end_operation(&context, &mut session, &op_info, "test", None).unwrap();
646
647 let generator = ManifestGenerator::new(op_conn);
648 let manifest = generator
649 .generate_manifest("main", Some(&op2.hash))
650 .unwrap();
651
652 assert_eq!(manifest.branch_name, "main");
653 assert_eq!(manifest.operations.len(), 2);
654 assert_eq!(manifest.operations[0].operation.hash, op1.hash);
655 assert_eq!(manifest.operations[1].operation.hash, op2.hash);
656
657 let manifest = generator
658 .generate_manifest("main", Some(&op1.hash))
659 .unwrap();
660 assert_eq!(manifest.operations.len(), 1);
661 assert_eq!(manifest.operations[0].operation.hash, op1.hash);
662 }
663
664 #[test]
665 fn test_manifest_generator_preserves_operation_file_path() {
666 let context = setup_gen();
667 let conn = context.graph().conn();
668 let op_conn = context.operations().conn();
669
670 let db_uuid = crate::metadata::get_db_uuid(conn);
671 crate::files::GenDatabase::create(op_conn, &db_uuid, "test_db", "test_db_path").unwrap();
672
673 let repo_root = context.workspace().repo_root().unwrap();
674 let input_path = repo_root.join("fastas").join("input.fa");
675 fs::create_dir_all(input_path.parent().unwrap()).unwrap();
676 fs::write(&input_path, ">seq\nACGT\n").unwrap();
677
678 let mut session = start_operation(conn);
679 crate::sequence::Sequence::new()
680 .sequence("ACGT")
681 .sequence_type("DNA")
682 .save(conn)
683 .unwrap();
684 let operation = end_operation(
685 &context,
686 &mut session,
687 &OperationInfo {
688 files: vec![OperationFile::new("fastas/input.fa")],
689 description: "file op".to_string(),
690 },
691 "test",
692 None,
693 )
694 .unwrap();
695
696 let generator = ManifestGenerator::new(op_conn);
697 let manifest = generator
698 .generate_manifest("main", Some(&operation.hash))
699 .unwrap();
700 let file = &manifest.operations[0].file_additions[0];
701
702 assert_eq!(file.filename, "input.fa");
703 assert_eq!(file.file_path, "fastas/input.fa");
704 assert!(file.file_addition.file_path().starts_with(".gen/assets/"));
705 }
706
707 #[test]
708 fn test_manifest_generator_includes_annotation_files() {
709 let context = setup_gen();
710 let conn = context.graph().conn();
711 let op_conn = context.operations().conn();
712
713 let db_uuid = crate::metadata::get_db_uuid(conn);
714 crate::files::GenDatabase::create(op_conn, &db_uuid, "test_db", "test_db_path").unwrap();
715
716 let mut session = start_operation(conn);
717 crate::sequence::Sequence::new()
718 .sequence("ACGT")
719 .sequence_type("DNA")
720 .save(conn)
721 .unwrap();
722 let op_info = OperationInfo {
723 files: vec![],
724 description: "annotation op".to_string(),
725 };
726 let operation = end_operation(&context, &mut session, &op_info, "test", None).unwrap();
727
728 let repo_root = context.workspace().repo_root().unwrap();
729 let annotation_path = repo_root.join("fixtures").join("manifest_annotation.gff3");
730 fs::create_dir_all(annotation_path.parent().unwrap()).unwrap();
731 fs::write(&annotation_path, "##gff-version 3\n").unwrap();
732
733 let file_addition = AnnotationFile::add_to_operation(
734 context.workspace(),
735 op_conn,
736 &operation.hash,
737 &AnnotationFileAdditionInput {
738 file_path: "fixtures/manifest_annotation.gff3".to_string(),
739 file_type: FileTypes::Gff3,
740 checksum_override: None,
741 name: Some("manifest-track".to_string()),
742 index_file_path: None,
743 },
744 )
745 .unwrap();
746
747 let generator = ManifestGenerator::new(op_conn);
748 let manifest = generator
749 .generate_manifest("main", Some(&operation.hash))
750 .unwrap();
751
752 assert_eq!(manifest.operations.len(), 1);
753 assert_eq!(
754 manifest.operations[0].annotation_file_additions,
755 vec![ManifestAnnotationFileAddition {
756 file_addition,
757 index_file_addition: None,
758 name: Some("manifest-track".to_string()),
759 }]
760 );
761 }
762
763 #[test]
764 fn test_manifest_comparer() {
765 let context = setup_gen();
766 let conn = context.graph().conn();
767 let op_conn = context.operations().conn();
768
769 let db_uuid = crate::metadata::get_db_uuid(conn);
770 crate::files::GenDatabase::create(op_conn, &db_uuid, "test_db", "test_db_path").unwrap();
771
772 let mut session = start_operation(conn);
773 crate::sequence::Sequence::new()
774 .sequence("ACGT")
775 .sequence_type("DNA")
776 .save(conn)
777 .unwrap();
778 let op_info = OperationInfo {
779 files: vec![],
780 description: "first op".to_string(),
781 };
782 let op1 = end_operation(&context, &mut session, &op_info, "test", None).unwrap();
783
784 let mut session = start_operation(conn);
785 crate::sequence::Sequence::new()
786 .sequence("TTTT")
787 .sequence_type("DNA")
788 .save(conn)
789 .unwrap();
790 let op_info = OperationInfo {
791 files: vec![],
792 description: "second op".to_string(),
793 };
794 let op2 = end_operation(&context, &mut session, &op_info, "test 2", None).unwrap();
795
796 let mut session = start_operation(conn);
797 crate::sequence::Sequence::new()
798 .sequence("AAAA")
799 .sequence_type("DNA")
800 .save(conn)
801 .unwrap();
802 let op_info = OperationInfo {
803 files: vec![],
804 description: "third op".to_string(),
805 };
806 let op3 = end_operation(&context, &mut session, &op_info, "test 3", None).unwrap();
807
808 let manifest1 = Manifest {
809 manifest_version: "1.0".to_string(),
810 branch_name: "main".to_string(),
811 end_hash: Some(op2.hash),
812 operations: vec![
813 ManifestOperation {
814 operation: op1.clone(),
815 file_additions: vec![],
816 annotation_file_additions: vec![],
817 operation_summary: None,
818 },
819 ManifestOperation {
820 operation: op2.clone(),
821 file_additions: vec![],
822 annotation_file_additions: vec![],
823 operation_summary: None,
824 },
825 ],
826 };
827
828 let manifest2 = Manifest {
829 manifest_version: "1.0".to_string(),
830 branch_name: "main".to_string(),
831 end_hash: Some(op3.hash),
832 operations: vec![
833 ManifestOperation {
834 operation: op2.clone(),
835 file_additions: vec![],
836 annotation_file_additions: vec![],
837 operation_summary: None,
838 },
839 ManifestOperation {
840 operation: op3.clone(),
841 file_additions: vec![],
842 annotation_file_additions: vec![],
843 operation_summary: None,
844 },
845 ],
846 };
847
848 let diff = ManifestComparer::diff_manifests(&manifest1, &manifest2).unwrap();
849
850 assert_eq!(diff.missing_in_manifest2.len(), 1);
851 assert_eq!(diff.missing_in_manifest2[0].operation.hash, op1.hash);
852
853 assert_eq!(diff.missing_in_manifest1.len(), 1);
854 assert_eq!(diff.missing_in_manifest1[0].operation.hash, op3.hash);
855 }
856
857 #[test]
858 fn test_manifest_generator_operation_not_found() {
859 let context = setup_gen();
860 let conn = context.graph().conn();
861 let op_conn = context.operations().conn();
862
863 let db_uuid = crate::metadata::get_db_uuid(conn);
864 crate::files::GenDatabase::create(op_conn, &db_uuid, "test_db", "test_db_path").unwrap();
865
866 let mut session = start_operation(conn);
867 crate::sequence::Sequence::new()
868 .sequence("ACGT")
869 .sequence_type("DNA")
870 .save(conn)
871 .unwrap();
872 let op_info = OperationInfo {
873 files: vec![],
874 description: "first op".to_string(),
875 };
876 end_operation(&context, &mut session, &op_info, "test", None).unwrap();
877
878 let generator = ManifestGenerator::new(op_conn);
879 let manifest = generator
880 .generate_manifest("main", Some(&HashId::convert_str("non_existent_op")))
881 .unwrap();
882 assert!(manifest.operations.is_empty());
883 }
884}