| 1 | use std::{cell::RefCell, collections::VecDeque, rc::Rc}; |
| 2 | use zipelo_ui_memory_app::archive_ui::*; |
| 3 | |
| 4 | #[derive(Default)] |
| 5 | pub struct State { |
| 6 | pub requests: Vec<(u64, &'static str)>, |
| 7 | pub starts: Vec<Start>, |
| 8 | pub password_seen: bool, |
| 9 | pub job: u64, |
| 10 | sequence: u64, |
| 11 | pub queue: VecDeque<HostEvent>, |
| 12 | pub manual: bool, |
| 13 | pause: bool, |
| 14 | } |
| 15 | #[derive(Default, Clone)] |
| 16 | pub struct FakeFactory { |
| 17 | pub state: Rc<RefCell<State>>, |
| 18 | } |
| 19 | impl FakeFactory { |
| 20 | pub fn manual() -> Self { |
| 21 | let f = Self::default(); |
| 22 | f.state.borrow_mut().manual = true; |
| 23 | f |
| 24 | } |
| 25 | } |
| 26 | impl State { |
| 27 | pub fn frame(&mut self, event: Event) { |
| 28 | let sequence = self.sequence; |
| 29 | self.sequence += 1; |
| 30 | self.queue.push_back(HostEvent::Frame { |
| 31 | job_id: self.job, |
| 32 | sequence, |
| 33 | event, |
| 34 | }); |
| 35 | } |
| 36 | pub fn finish(&mut self, outcome: Outcome, publication: Publication) { |
| 37 | self.frame(Event::Terminal(Terminal { |
| 38 | outcome, |
| 39 | error: None, |
| 40 | publication, |
| 41 | created_count: if publication == Publication::Complete { |
| 42 | 1 |
| 43 | } else { |
| 44 | 0 |
| 45 | }, |
| 46 | retained_partial_count: 0, |
| 47 | durability: Durability::Synced, |
| 48 | cleanup: Cleanup::Complete, |
| 49 | })); |
| 50 | self.queue.push_back(HostEvent::StdoutEof); |
| 51 | self.queue.push_back(HostEvent::StderrEof); |
| 52 | self.queue.push_back(HostEvent::Exited { success: true }); |
| 53 | } |
| 54 | fn complete_action(&mut self) { |
| 55 | match self.starts.last().cloned().unwrap() { |
| 56 | Start::List { archive } => { |
| 57 | self.frame(Event::Entry(Entry { |
| 58 | id: 0, |
| 59 | name: "읽어주세요.txt".into(), |
| 60 | kind: Kind::File, |
| 61 | bytes: Some(31), |
| 62 | packed: Some(54), |
| 63 | encrypted: false, |
| 64 | })); |
| 65 | let format = if archive.extension().is_some_and(|x| x == "gz") { |
| 66 | Format::Gzip |
| 67 | } else { |
| 68 | Format::Zip |
| 69 | }; |
| 70 | self.frame(Event::ListEnd { |
| 71 | entry_count: 1, |
| 72 | list_id: ListId { |
| 73 | catalog_version: 1, |
| 74 | format, |
| 75 | source_object: SourceObject::Unix { |
| 76 | device: 1, |
| 77 | inode: 1, |
| 78 | }, |
| 79 | archive_bytes: 54, |
| 80 | archive_sha256: [7; 32], |
| 81 | }, |
| 82 | }); |
| 83 | self.finish(Outcome::Completed, Publication::None); |
| 84 | } |
| 85 | _ => { |
| 86 | self.frame(Event::Progress { |
| 87 | stage: Stage::Write, |
| 88 | processed: 31, |
| 89 | total: Some(31), |
| 90 | completed_entries: 1, |
| 91 | }); |
| 92 | self.frame(Event::Output { |
| 93 | entry_id: Some(0), |
| 94 | relative_name: "읽어주세요.txt".into(), |
| 95 | disposition: Disposition::Created, |
| 96 | }); |
| 97 | self.finish(Outcome::Completed, Publication::Complete); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | struct FakeConnection { |
| 103 | state: Rc<RefCell<State>>, |
| 104 | } |
| 105 | impl Factory for FakeFactory { |
| 106 | fn connect(&mut self, job: u64) -> Result<Box<dyn Connection>, String> { |
| 107 | let mut s = self.state.borrow_mut(); |
| 108 | s.job = job; |
| 109 | s.sequence = 0; |
| 110 | s.queue.clear(); |
| 111 | s.pause = false; |
| 112 | Ok(Box::new(FakeConnection { |
| 113 | state: self.state.clone(), |
| 114 | })) |
| 115 | } |
| 116 | } |
| 117 | impl Connection for FakeConnection { |
| 118 | fn send(&mut self, request: Request) -> Result<(), String> { |
| 119 | let mut s = self.state.borrow_mut(); |
| 120 | assert_eq!(s.job, request.job_id); |
| 121 | match request.command { |
| 122 | Command::Start(start) => { |
| 123 | s.requests.push((request.sequence, "start")); |
| 124 | s.starts.push(start.clone()); |
| 125 | if !s.manual { |
| 126 | s.frame(Event::Accepted { encryption: false }); |
| 127 | s.frame(Event::Progress { |
| 128 | stage: Stage::Scan, |
| 129 | processed: 0, |
| 130 | total: None, |
| 131 | completed_entries: 0, |
| 132 | }); |
| 133 | if matches!(&start,Start::List {archive} if archive.file_name().is_some_and(|n|n.to_string_lossy().contains("locked"))) |
| 134 | { |
| 135 | s.frame(Event::PasswordRequired { |
| 136 | challenge_id: 17, |
| 137 | attempt: 1, |
| 138 | encrypt: false, |
| 139 | }); |
| 140 | } else { |
| 141 | s.complete_action(); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | Command::Password { |
| 146 | challenge_id, |
| 147 | secret, |
| 148 | } => { |
| 149 | assert_eq!(challenge_id, 17); |
| 150 | assert!(!secret.as_bytes().is_empty()); |
| 151 | s.password_seen = true; |
| 152 | s.requests.push((request.sequence, "password")); |
| 153 | if !s.manual { |
| 154 | s.complete_action(); |
| 155 | } |
| 156 | } |
| 157 | Command::Cancel => { |
| 158 | s.requests.push((request.sequence, "cancel")); |
| 159 | if !s.manual { |
| 160 | let consumed = s |
| 161 | .queue |
| 162 | .iter() |
| 163 | .filter(|e| matches!(e, HostEvent::Frame { .. })) |
| 164 | .count() as u64; |
| 165 | s.sequence -= consumed; |
| 166 | s.queue.clear(); |
| 167 | s.frame(Event::CancelAck { |
| 168 | request_sequence: request.sequence, |
| 169 | status: CancelStatus::Requested, |
| 170 | }); |
| 171 | s.finish(Outcome::Cancelled, Publication::None); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | Ok(()) |
| 176 | } |
| 177 | fn poll(&mut self) -> Option<HostEvent> { |
| 178 | let mut s = self.state.borrow_mut(); |
| 179 | if s.pause { |
| 180 | s.pause = false; |
| 181 | return None; |
| 182 | } |
| 183 | s.pause = true; |
| 184 | s.queue.pop_front() |
| 185 | } |
| 186 | } |