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