| 1 | use super::*; |
| 2 | |
| 3 | fn finish_preview_catalog(model: &mut Model, allow: bool) -> usize { |
| 4 | let deadline = Instant::now() + Duration::from_secs(30); |
| 5 | let mut questions = 0; |
| 6 | while model.busy() { |
| 7 | model.poll(); |
| 8 | if let Some(question) = model.resource.clone() { |
| 9 | assert!(question.scope.entries); |
| 10 | assert!(!question.scope.ratio); |
| 11 | assert_eq!(question.action, ActionKind::Preview); |
| 12 | assert_eq!(model.estimated_usage().estimated_entries, Some(10_001)); |
| 13 | assert!(model.resource_reply(model.job_id().unwrap(), question.id, allow)); |
| 14 | questions += 1; |
| 15 | } |
| 16 | assert!(Instant::now() < deadline, "{:?}", model.error); |
| 17 | std::thread::sleep(Duration::from_millis(1)); |
| 18 | } |
| 19 | assert!(model.error.is_none(), "{:?}", model.error); |
| 20 | questions |
| 21 | } |
| 22 | |
| 23 | fn preview_catalog(count: usize, bytes: usize, allow: bool, image: bool) { |
| 24 | let name = if image { "small.png" } else { "small.txt" }; |
| 25 | let scratch = Scratch::new(); |
| 26 | let archive = scratch.path("many.zip"); |
| 27 | let generated = std::process::Command::new("python3") |
| 28 | .arg("-c") |
| 29 | .arg("import sys,zipfile; z=zipfile.ZipFile(sys.argv[1], 'w'); z.writestr(sys.argv[4], b'x'*int(sys.argv[3])); [z.writestr(f'{i}.bin', b'') for i in range(int(sys.argv[2])-1)]; z.close()") |
| 30 | .arg(&archive) |
| 31 | .arg(count.to_string()) |
| 32 | .arg(bytes.to_string()) |
| 33 | .arg(name) |
| 34 | .output() |
| 35 | .unwrap(); |
| 36 | assert!(generated.status.success()); |
| 37 | let mut factory = ProcessFactory::new(binary("zipelo-archive-worker")); |
| 38 | let mut model = Model::default(); |
| 39 | assert!(model.begin(Start::List { archive }, &mut factory)); |
| 40 | wait(&mut model); |
| 41 | if count > 10_000 { |
| 42 | assert_eq!( |
| 43 | model.terminal.as_ref().unwrap().error, |
| 44 | Some(ErrorCode::Limit) |
| 45 | ); |
| 46 | assert!(model.retry_expanded(&mut factory)); |
| 47 | let deadline = Instant::now() + Duration::from_secs(30); |
| 48 | while model.busy() { |
| 49 | model.poll(); |
| 50 | if let Some(question) = model.resource.clone() { |
| 51 | assert!(model.resource_reply(model.job_id().unwrap(), question.id, true)); |
| 52 | } |
| 53 | assert!(Instant::now() < deadline); |
| 54 | std::thread::sleep(Duration::from_millis(1)); |
| 55 | } |
| 56 | } |
| 57 | completed(&model); |
| 58 | assert_eq!(model.entries.len(), count); |
| 59 | let id = model |
| 60 | .entries |
| 61 | .iter() |
| 62 | .find(|entry| entry.name == name) |
| 63 | .unwrap() |
| 64 | .id; |
| 65 | model.selected.insert(id); |
| 66 | assert!(model.begin_preview(&mut factory)); |
| 67 | let questions = finish_preview_catalog(&mut model, allow); |
| 68 | assert_eq!(questions, usize::from(count > 10_000)); |
| 69 | if !allow { |
| 70 | assert!(model.preview.is_none()); |
| 71 | assert_ne!(model.terminal.as_ref().unwrap().outcome, Outcome::Completed); |
| 72 | } else if bytes > if image { 8 * 1024 * 1024 } else { 1024 * 1024 } { |
| 73 | assert_eq!( |
| 74 | model.terminal.as_ref().unwrap().error, |
| 75 | Some(ErrorCode::Limit) |
| 76 | ); |
| 77 | assert!(model.preview.is_none()); |
| 78 | assert!(!model.can_retry_expanded()); |
| 79 | } else { |
| 80 | completed(&model); |
| 81 | assert_eq!(model.preview.as_ref().unwrap().data, vec![b'x'; bytes]); |
| 82 | } |
| 83 | assert!(model.outputs.is_empty()); |
| 84 | assert_eq!(model.selected, [id].into_iter().collect()); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn preview_catalog_at_default_entry_limit_needs_no_approval() { |
| 89 | preview_catalog(10_000, 7, true, false); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn preview_catalog_over_default_entry_limit_requires_new_approval() { |
| 94 | preview_catalog(10_001, 7, true, false); |
| 95 | } |
| 96 | |
| 97 | #[test] |
| 98 | fn preview_catalog_approval_does_not_raise_text_size_limit() { |
| 99 | preview_catalog(10_001, 1024 * 1024 + 1, true, false); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn preview_catalog_denial_does_not_show_data() { |
| 104 | preview_catalog(10_001, 7, false, false); |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn preview_catalog_approval_does_not_raise_image_size_limit() { |
| 109 | preview_catalog(10_001, 8 * 1024 * 1024 + 1, true, true); |
| 110 | } |