|
| 1 | +mod utils; |
| 2 | + |
| 3 | +use couchbase_lite::*; |
| 4 | +use std::path::Path; |
| 5 | +use utils::*; |
| 6 | + |
| 7 | +#[allow(deprecated)] |
| 8 | +fn main() { |
| 9 | + println!("=== Test with Reporting Infrastructure ===\n"); |
| 10 | + |
| 11 | + // STEP 0: Check git status |
| 12 | + println!("Step 0: Checking git status..."); |
| 13 | + let git_info = match check_git_status() { |
| 14 | + Ok(info) => { |
| 15 | + println!("✓ Git status clean"); |
| 16 | + println!(" - Commit: {}", info.commit_short_sha); |
| 17 | + println!(" - Branch: {}\n", info.branch); |
| 18 | + info |
| 19 | + } |
| 20 | + Err(e) => { |
| 21 | + eprintln!("✗ Git check failed:"); |
| 22 | + eprintln!("{}", e); |
| 23 | + std::process::exit(1); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + // STEP 1: Ensure clean Docker environment |
| 28 | + println!("Step 1: Setting up Docker environment..."); |
| 29 | + if let Err(e) = ensure_clean_environment() { |
| 30 | + eprintln!("✗ Docker setup failed: {}", e); |
| 31 | + std::process::exit(1); |
| 32 | + } |
| 33 | + |
| 34 | + // STEP 2: Initialize test reporter |
| 35 | + let mut reporter = match TestReporter::new("test_with_reporting", git_info) { |
| 36 | + Ok(r) => r, |
| 37 | + Err(e) => { |
| 38 | + eprintln!("✗ Failed to initialize reporter: {}", e); |
| 39 | + std::process::exit(1); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + // STEP 3: Run actual test |
| 44 | + reporter.log("=== Starting test ==="); |
| 45 | + |
| 46 | + let mut db = Database::open( |
| 47 | + "test_reporting", |
| 48 | + Some(DatabaseConfiguration { |
| 49 | + directory: Path::new("./"), |
| 50 | + #[cfg(feature = "enterprise")] |
| 51 | + encryption_key: None, |
| 52 | + }), |
| 53 | + ) |
| 54 | + .unwrap(); |
| 55 | + |
| 56 | + add_or_update_user("report_test_user", vec!["channel1".into()]); |
| 57 | + let session_token = get_session("report_test_user"); |
| 58 | + |
| 59 | + let mut repl = setup_replicator(db.clone(), session_token).add_document_listener(Box::new( |
| 60 | + |_dir, docs| { |
| 61 | + for doc in docs { |
| 62 | + println!(" 📡 Replicated: {} (flags={})", doc.id, doc.flags); |
| 63 | + } |
| 64 | + }, |
| 65 | + )); |
| 66 | + |
| 67 | + repl.start(false); |
| 68 | + std::thread::sleep(std::time::Duration::from_secs(3)); |
| 69 | + |
| 70 | + // Create document |
| 71 | + reporter.log("\nSTEP 1: Creating document..."); |
| 72 | + create_doc(&mut db, "test_doc", "channel1"); |
| 73 | + std::thread::sleep(std::time::Duration::from_secs(3)); |
| 74 | + |
| 75 | + let state1 = get_sync_xattr("test_doc"); |
| 76 | + reporter.checkpoint( |
| 77 | + "CREATED", |
| 78 | + state1.clone(), |
| 79 | + vec!["Document created in channel1".to_string()], |
| 80 | + ); |
| 81 | + reporter.log("✓ Document created and replicated"); |
| 82 | + |
| 83 | + // Delete document |
| 84 | + reporter.log("\nSTEP 2: Deleting document..."); |
| 85 | + let mut doc = db.get_document("test_doc").unwrap(); |
| 86 | + db.delete_document(&mut doc).unwrap(); |
| 87 | + std::thread::sleep(std::time::Duration::from_secs(3)); |
| 88 | + |
| 89 | + let state2 = get_sync_xattr("test_doc"); |
| 90 | + reporter.checkpoint( |
| 91 | + "DELETED", |
| 92 | + state2.clone(), |
| 93 | + vec!["Document deleted, should be tombstone".to_string()], |
| 94 | + ); |
| 95 | + reporter.log("✓ Document deleted"); |
| 96 | + |
| 97 | + // Re-create document |
| 98 | + reporter.log("\nSTEP 3: Re-creating document..."); |
| 99 | + create_doc(&mut db, "test_doc", "channel1"); |
| 100 | + std::thread::sleep(std::time::Duration::from_secs(3)); |
| 101 | + |
| 102 | + let state3 = get_sync_xattr("test_doc"); |
| 103 | + reporter.checkpoint( |
| 104 | + "RECREATED", |
| 105 | + state3.clone(), |
| 106 | + vec!["Document re-created, should be live".to_string()], |
| 107 | + ); |
| 108 | + reporter.log("✓ Document re-created"); |
| 109 | + |
| 110 | + repl.stop(None); |
| 111 | + |
| 112 | + reporter.log("\n=== Test complete ==="); |
| 113 | + |
| 114 | + // Finalize report |
| 115 | + if let Err(e) = reporter.finalize() { |
| 116 | + eprintln!("⚠ Failed to generate report: {}", e); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +#[allow(deprecated)] |
| 121 | +fn create_doc(db: &mut Database, id: &str, channel: &str) { |
| 122 | + let mut doc = Document::new_with_id(id); |
| 123 | + doc.set_properties_as_json( |
| 124 | + &serde_json::json!({ |
| 125 | + "channels": channel, |
| 126 | + "test_data": "reporting test" |
| 127 | + }) |
| 128 | + .to_string(), |
| 129 | + ) |
| 130 | + .unwrap(); |
| 131 | + db.save_document(&mut doc).unwrap(); |
| 132 | +} |
| 133 | + |
| 134 | +fn setup_replicator(db: Database, session_token: String) -> Replicator { |
| 135 | + let repl_conf = ReplicatorConfiguration { |
| 136 | + database: Some(db.clone()), |
| 137 | + endpoint: Endpoint::new_with_url(SYNC_GW_URL).unwrap(), |
| 138 | + replicator_type: ReplicatorType::PushAndPull, |
| 139 | + continuous: true, |
| 140 | + disable_auto_purge: false, |
| 141 | + max_attempts: 3, |
| 142 | + max_attempt_wait_time: 1, |
| 143 | + heartbeat: 60, |
| 144 | + authenticator: None, |
| 145 | + proxy: None, |
| 146 | + headers: vec![( |
| 147 | + "Cookie".to_string(), |
| 148 | + format!("SyncGatewaySession={session_token}"), |
| 149 | + )] |
| 150 | + .into_iter() |
| 151 | + .collect(), |
| 152 | + pinned_server_certificate: None, |
| 153 | + trusted_root_certificates: None, |
| 154 | + channels: MutableArray::default(), |
| 155 | + document_ids: MutableArray::default(), |
| 156 | + collections: None, |
| 157 | + accept_parent_domain_cookies: false, |
| 158 | + #[cfg(feature = "enterprise")] |
| 159 | + accept_only_self_signed_server_certificate: false, |
| 160 | + }; |
| 161 | + let repl_context = ReplicationConfigurationContext::default(); |
| 162 | + Replicator::new(repl_conf, Box::new(repl_context)).unwrap() |
| 163 | +} |
0 commit comments