From ba3fa24f9ac74f30f5314d58607f4cbbfef729a5 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Mon, 11 Jun 2018 00:15:15 -0700 Subject: [PATCH] Tests for modifying projects and using /subscribe --- server/src/partition_watcher.rs | 4 +- server/tests/web.rs | 200 +++++++++++++++++++++++++++++--- 2 files changed, 190 insertions(+), 14 deletions(-) diff --git a/server/src/partition_watcher.rs b/server/src/partition_watcher.rs index 2f9785e7..5885bb74 100644 --- a/server/src/partition_watcher.rs +++ b/server/src/partition_watcher.rs @@ -8,6 +8,8 @@ use partition::Partition; use vfs_session::FileChange; use file_route::FileRoute; +const WATCH_TIMEOUT_MS: u64 = 100; + pub struct PartitionWatcher { pub watcher: RecommendedWatcher, } @@ -16,7 +18,7 @@ impl PartitionWatcher { pub fn start_new(partition: Partition, tx: Sender) -> PartitionWatcher { let (watch_tx, watch_rx) = channel(); - let mut watcher = watcher(watch_tx, Duration::from_millis(100)).unwrap(); + let mut watcher = watcher(watch_tx, Duration::from_millis(WATCH_TIMEOUT_MS)).unwrap(); watcher.watch(&partition.path, RecursiveMode::Recursive).unwrap(); diff --git a/server/tests/web.rs b/server/tests/web.rs index 00303011..b21f4220 100644 --- a/server/tests/web.rs +++ b/server/tests/web.rs @@ -7,14 +7,16 @@ extern crate librojo; mod test_util; use test_util::*; -use std::collections::HashMap; -use std::path::PathBuf; use std::borrow::Cow; +use std::collections::HashMap; +use std::fs::{File, remove_file}; +use std::io::Write; +use std::path::PathBuf; use librojo::{ session::Session, project::Project, - web::{Server, WebConfig, ServerInfoResponse, ReadResponse, ReadAllResponse}, + web::{Server, WebConfig, ServerInfoResponse, ReadResponse, ReadAllResponse, SubscribeResponse}, }; #[test] @@ -87,16 +89,16 @@ fn one_partition() { assert_eq!(response.partitions, partitions); } - let check_base_read_all = || { - let body = server.get_string("/api/read_all"); - let response = serde_json::from_str::(&body).unwrap(); - - let partition_id = *response.partition_instances.get("lib").unwrap(); + let initial_body = server.get_string("/api/read_all"); + let initial_response = { + let response = serde_json::from_str::(&initial_body).unwrap(); assert_eq!(response.server_id, "0"); assert_eq!(response.message_cursor, -1); assert_eq!(response.instances.len(), 4); + let partition_id = *response.partition_instances.get("lib").unwrap(); + let mut root_id = None; let mut module_id = None; let mut client_id = None; @@ -150,7 +152,6 @@ fn one_partition() { let mut properties = HashMap::new(); properties.insert("Source".to_string(), "-- b.client.lua".to_string()); - assert_eq!(instance.name, "b"); assert_eq!(instance.properties, properties); assert_eq!(instance.parent, Some(partition_id)); assert_eq!(instance.children.len(), 0); @@ -169,7 +170,6 @@ fn one_partition() { let mut properties = HashMap::new(); properties.insert("Source".to_string(), "-- a.server.lua".to_string()); - assert_eq!(instance.name, "a"); assert_eq!(instance.properties, properties); assert_eq!(instance.parent, Some(partition_id)); assert_eq!(instance.children.len(), 0); @@ -190,14 +190,188 @@ fn one_partition() { let client_id = client_id.unwrap(); let server_id = server_id.unwrap(); + { + let root_instance = response.instances.get(&root_id).unwrap(); + + assert!(root_instance.children.contains(&module_id)); + assert!(root_instance.children.contains(&client_id)); + assert!(root_instance.children.contains(&server_id)); + } + + response + }; + + { + let temp_name = { + let mut path = project_path.clone(); + path.push("lib/c.client.lua"); + path + }; + + { + let mut file = File::create(&temp_name).unwrap(); + file.write_all(b"-- c.client.lua").unwrap(); + } + + { + // Block until Rojo detects the addition of our temp file + let body = server.get_string("/api/subscribe/-1"); + let response = serde_json::from_str::(&body).unwrap(); + + assert_eq!(response.server_id, "0"); + assert_eq!(response.message_cursor, 0); + assert_eq!(response.messages.len(), 1); + } + + let body = server.get_string("/api/read_all"); + let response = serde_json::from_str::(&body).unwrap(); + + assert_eq!(response.server_id, "0"); + assert_eq!(response.message_cursor, 0); + assert_eq!(response.instances.len(), 5); + + let partition_id = *response.partition_instances.get("lib").unwrap(); + + let mut root_id = None; + let mut module_id = None; + let mut client_id = None; + let mut server_id = None; + let mut new_id = None; + + for (id, instance) in response.instances.iter() { + match (instance.name.as_str(), instance.class_name.as_str()) { + // TOOD: Should partition roots (and other directories) be some + // magical object instead of Folder? + // TODO: Should this name actually equal the last part of the + // partition's target? + ("OnePartition", "Folder") => { + assert!(root_id.is_none()); + root_id = Some(*id); + + assert_eq!(*id, partition_id); + + assert_eq!(instance.properties.len(), 0); + assert_eq!(instance.parent, None); + assert_eq!(instance.children.len(), 4); + + let single_body = server.get_string(&format!("/api/read/{}", id)); + let single_response = serde_json::from_str::(&single_body).unwrap(); + + let single_instance = single_response.instances.get(id).unwrap(); + + assert_eq!(single_instance, &Cow::Borrowed(instance)); + }, + ("a", "ModuleScript") => { + assert!(module_id.is_none()); + module_id = Some(*id); + + let mut properties = HashMap::new(); + properties.insert("Source".to_string(), "-- a.lua".to_string()); + + assert_eq!(instance.properties, properties); + assert_eq!(instance.parent, Some(partition_id)); + assert_eq!(instance.children.len(), 0); + + let single_body = server.get_string(&format!("/api/read/{}", id)); + let single_response = serde_json::from_str::(&single_body).unwrap(); + + let single_instance = single_response.instances.get(id).unwrap(); + + assert_eq!(single_instance, &Cow::Borrowed(instance)); + }, + ("b", "LocalScript") => { + assert!(client_id.is_none()); + client_id = Some(*id); + + let mut properties = HashMap::new(); + properties.insert("Source".to_string(), "-- b.client.lua".to_string()); + + assert_eq!(instance.properties, properties); + assert_eq!(instance.parent, Some(partition_id)); + assert_eq!(instance.children.len(), 0); + + let single_body = server.get_string(&format!("/api/read/{}", id)); + let single_response = serde_json::from_str::(&single_body).unwrap(); + + let single_instance = single_response.instances.get(id).unwrap(); + + assert_eq!(single_instance, &Cow::Borrowed(instance)); + }, + ("a", "Script") => { + assert!(server_id.is_none()); + server_id = Some(*id); + + let mut properties = HashMap::new(); + properties.insert("Source".to_string(), "-- a.server.lua".to_string()); + + assert_eq!(instance.properties, properties); + assert_eq!(instance.parent, Some(partition_id)); + assert_eq!(instance.children.len(), 0); + + let single_body = server.get_string(&format!("/api/read/{}", id)); + let single_response = serde_json::from_str::(&single_body).unwrap(); + + let single_instance = single_response.instances.get(id).unwrap(); + + assert_eq!(single_instance, &Cow::Borrowed(instance)); + }, + ("c", "LocalScript") => { + assert!(new_id.is_none()); + new_id = Some(*id); + + let mut properties = HashMap::new(); + properties.insert("Source".to_string(), "-- c.client.lua".to_string()); + + assert_eq!(instance.properties, properties); + assert_eq!(instance.parent, Some(partition_id)); + assert_eq!(instance.children.len(), 0); + + let single_body = server.get_string(&format!("/api/read/{}", id)); + let single_response = serde_json::from_str::(&single_body).unwrap(); + + let single_instance = single_response.instances.get(id).unwrap(); + + assert_eq!(single_instance, &Cow::Borrowed(instance)); + }, + _ => {}, + } + } + + let root_id = root_id.unwrap(); + let module_id = module_id.unwrap(); + let client_id = client_id.unwrap(); + let server_id = server_id.unwrap(); + let new_id = new_id.unwrap(); + let root_instance = response.instances.get(&root_id).unwrap(); assert!(root_instance.children.contains(&module_id)); assert!(root_instance.children.contains(&client_id)); assert!(root_instance.children.contains(&server_id)); - }; + assert!(root_instance.children.contains(&new_id)); - check_base_read_all(); + remove_file(&temp_name).unwrap(); + } - // TODO: Test /subscribe + { + // Block until Rojo detects the removal of our temp file + let body = server.get_string("/api/subscribe/0"); + let response = serde_json::from_str::(&body).unwrap(); + + assert_eq!(response.server_id, "0"); + assert_eq!(response.message_cursor, 1); + assert_eq!(response.messages.len(), 1); + } + + { + // Everything should be back to the initial state! + let body = server.get_string("/api/read_all"); + let response = serde_json::from_str::(&body).unwrap(); + + assert_eq!(response.server_id, "0"); + assert_eq!(response.message_cursor, 1); + assert_eq!(response.instances.len(), 4); + + assert_eq!(response.instances, initial_response.instances); + } } \ No newline at end of file