Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
358 changes: 253 additions & 105 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ default-members = [
]

[workspace.package]
version = "3.6.0"
version = "3.6.1"
edition = "2024"
license = "MIT"
authors = ["Dennis Blümer <dennis.bluemer@ducart.de>"]
Expand Down
12 changes: 6 additions & 6 deletions crates/resonance-cli/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ async fn bail_stops_after_the_first_failure_and_skips_the_rest() {
}

#[tokio::test(flavor = "multi_thread")]
async fn a_v1_collection_and_a_plain_directory_are_refused() {
async fn a_directory_without_a_collection_yaml_is_refused() {
let server = Server::start().await;
let legacy = Collection::new("Legacy");
std::fs::remove_file(legacy.path().join("collection.yaml")).unwrap();
legacy.write("collection.json", "{}");
let cli = run(&legacy, &server, &[]);
let unmarked = Collection::new("Unmarked");
std::fs::remove_file(unmarked.path().join("collection.yaml")).unwrap();
unmarked.write("collection.json", "{}");
let cli = run(&unmarked, &server, &[]);
assert_eq!(cli.code(), 2);
assert!(
cli.stderr().contains("open it in Resonance once"),
cli.stderr().contains("no collection.yaml"),
"{}",
cli.stderr()
);
Expand Down
136 changes: 64 additions & 72 deletions crates/resonance-collections/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,13 @@ fn index_requests(loaded: &LoadedCollection) -> HashMap<String, RequestEntry> {
}

/// Indexes an existing tree's folders by id, to keep their directories.
///
/// A directory without `_folder.yaml` is indexed under its derived id, the
/// same one the frontend was given, so a save finds it instead of creating a
/// suffixed sibling.
fn index_folders(node: &FolderNode, out: &mut HashMap<String, FolderNode>) {
for folder in &node.folders {
if let Some(meta) = &folder.meta {
out.insert(meta.id.clone(), folder.clone());
}
out.insert(folder_id(folder), folder.clone());
index_folders(folder, out);
}
}
Expand Down Expand Up @@ -737,7 +739,10 @@ fn folder_from_ipc(
.and_then(|f| f.meta.as_ref())
.and_then(|m| m.auth.clone())
}),
extra: Map::new(),
extra: previous
.and_then(|f| f.meta.as_ref())
.map(|m| m.extra.clone())
.unwrap_or_default(),
}),
source: previous.and_then(|f| f.source.clone()),
requests: Vec::new(),
Expand Down Expand Up @@ -800,12 +805,8 @@ pub fn find_request<'a>(loaded: &'a LoadedCollection, request_id: &str) -> Optio
#[cfg(test)]
mod tests {
use super::*;
use crate::legacy::v1_to_v2;
use crate::model::{FORMAT_VERSION, FolderDoc};
use crate::read::{RequestEntry, read_collection_dir};
use crate::write::write_collection_dir;
use std::collections::HashMap;
use tempfile::TempDir;
use crate::read::RequestEntry;

fn request(id: &str, name: &str) -> RequestEntry {
RequestEntry::new(RequestDoc::new(id.into(), name.into()))
Expand Down Expand Up @@ -1147,69 +1148,6 @@ mod tests {
assert!(request_to_endpoint_data(&doc).scripts.is_none());
}
}

/// The step-9 promise: a v2 directory written by a teammate opens
/// correctly in a build that still writes v1.
#[test]
fn a_v2_directory_on_disk_reads_back_through_the_ipc_shape() {
let v1 = Collection {
id: "c1".into(),
name: "Petstore".into(),
base_url: "https://api.example.com".into(),
endpoints: vec![
json!({"id": "custom_1", "name": "Health", "method": "GET", "path": "/health"}),
json!({"id": "custom_2", "name": "Create Pet", "method": "POST", "path": "/pets"}),
],
folders: vec![json!({
"id": "folder_pets",
"name": "pets",
"endpoints": [{"id": "custom_2", "name": "Create Pet", "method": "POST", "path": "/pets"}]
})],
default_headers: json!({"Accept": "application/json"}),
auth_config: None,
open_api_spec: None,
storage_path: None,
storage_parent_path: None,
linked: false,
can_close: false,
git_branch: None,
};

let mut data = HashMap::new();
data.insert(
"custom_2".to_string(),
EndpointData {
modified_body: Some("{\"name\": \"Rex\"}".into()),
headers: vec![json!({"key": "Accept", "value": "*/*"})],
..Default::default()
},
);

let temp = TempDir::new().unwrap();
let mut converted = v1_to_v2(&v1, &data);
write_collection_dir(temp.path(), &mut converted).unwrap();

let reread = read_collection_dir(temp.path()).unwrap();
let ipc = to_ipc_collection(&reread, &temp.path().to_string_lossy(), false);

assert_eq!(ipc.id, "c1");
assert_eq!(ipc.default_headers["Accept"], "application/json");

// Both requests are reachable, and the foldered one is in both views.
assert_eq!(ipc.endpoints.len(), 2);
assert_eq!(ipc.folders.len(), 1);
assert_eq!(ipc.folders[0]["endpoints"].as_array().unwrap().len(), 1);
assert_eq!(ipc.folders[0]["endpoints"][0]["id"], "custom_2");

// The per-request data survives the trip through the files.
let request = find_request(&reread, "custom_2").unwrap();
let endpoint_data = request_to_endpoint_data(request);
assert_eq!(
endpoint_data.modified_body.as_deref(),
Some("{\"name\": \"Rex\"}")
);
assert_eq!(endpoint_data.headers.len(), 1);
}
}

#[cfg(test)]
Expand Down Expand Up @@ -1569,6 +1507,60 @@ mod save_path {
);
}

#[test]
fn a_folder_without_metadata_keeps_its_directory() {
let mut existing = existing_with_state();
existing.root.folders.push(FolderNode {
meta: None,
source: Some(std::path::PathBuf::from("/c/pets")),
requests: Vec::new(),
folders: Vec::new(),
});
let id = folder_id(&existing.root.folders[0]);

let incoming = ipc_collection(
vec![],
vec![json!({"id": id, "name": "pets", "endpoints": []})],
);
let root = tree_from_ipc(&incoming, Some(&existing));

assert_eq!(
root.folders[0].source.as_ref().unwrap(),
&std::path::PathBuf::from("/c/pets")
);
}

#[test]
fn unknown_folder_keys_survive_a_save() {
let mut existing = existing_with_state();
let mut extra = Map::new();
extra.insert("color".into(), json!("red"));
existing.root.folders.push(FolderNode {
meta: Some(crate::model::FolderDoc {
format: FORMAT_VERSION,
id: "f1".into(),
name: "pets".into(),
seq: 10,
auth: None,
extra,
}),
source: Some(std::path::PathBuf::from("/c/pets")),
requests: Vec::new(),
folders: Vec::new(),
});

let incoming = ipc_collection(
vec![],
vec![json!({"id": "f1", "name": "pets", "endpoints": []})],
);
let root = tree_from_ipc(&incoming, Some(&existing));

assert_eq!(
root.folders[0].meta.as_ref().unwrap().extra["color"],
json!("red")
);
}

mod reordering {
use super::*;

Expand Down
Loading
Loading