use std::time::Duration;
use axum::http::StatusCode;
use serde_json::json;
use crate::tests::harness::{author, TestServer};
const TENANT: &str = "/docs/acme";
async fn wait_for_path(path: &std::path::Path) -> bool {
let deadline = std::time::Instant::now() + Duration::from_secs(10);
while std::time::Instant::now() < deadline {
if path.exists() {
return true;
}
tokio::time::sleep(Duration::from_millis(5)).await;
}
false
}
#[tokio::test]
async fn files_are_mirrored_to_disk_by_default() {
let server = TestServer::start().await;
server.write_file(TENANT, "docs/intro.md", "# Hello").await;
let on_disk = server.repo_path("docs", "acme").join("docs/intro.md");
assert!(wait_for_path(&on_disk).await, "file was not mirrored");
assert_eq!(
std::fs::read_to_string(&on_disk).unwrap(),
"# Hello",
"mirrored content differs from what was committed"
);
}
#[tokio::test]
async fn the_mirror_follows_deletes_and_moves() {
let server = TestServer::start().await;
server.write_file(TENANT, "docs/intro.md", "x").await;
let repo = server.repo_path("docs", "acme");
assert!(wait_for_path(&repo.join("docs/intro.md")).await);
server
.post(
&format!("{}/files/docs/intro.md/move", TENANT),
json!({ "author": author(), "destination": "handbook/intro.md" }),
)
.await
.expect_status(StatusCode::OK);
assert!(wait_for_path(&repo.join("handbook/intro.md")).await);
assert!(!repo.join("docs/intro.md").exists());
server
.delete(
&format!("{}/files/handbook/intro.md", TENANT),
json!({ "author": author() }),
)
.await
.expect_status(StatusCode::OK);
assert!(!repo.join("handbook/intro.md").exists());
}
#[tokio::test]
async fn with_checkout_files_off_nothing_is_written_to_disk() {
let server = TestServer::builder().checkout_files(false).start().await;
server.write_file(TENANT, "docs/intro.md", "# Hello").await;
let repo = server.repo_path("docs", "acme");
assert!(repo.join(".git").is_dir());
tokio::time::sleep(Duration::from_millis(100)).await;
assert!(
!repo.join("docs/intro.md").exists(),
"a file was mirrored with checkout_files off"
);
}
#[tokio::test]
async fn every_route_answers_identically_with_no_working_tree() {
let server = TestServer::builder().checkout_files(false).start().await;
server.write_file(TENANT, "docs/intro.md", "# Hello").await;
server.write_file(TENANT, "docs/other.md", "# Other").await;
assert_eq!(
server.read_file(TENANT, "docs/intro.md").await.as_deref(),
Some("# Hello")
);
server
.head(&format!("{}/files/docs/intro.md", TENANT))
.await
.expect_status(StatusCode::OK);
let listing = server.get(&format!("{}/files", TENANT)).await;
listing.expect_status(StatusCode::OK);
assert_eq!(listing.json()["files"][0]["name"], "docs");
server
.post(
&format!("{}/files/docs/intro.md/move", TENANT),
json!({ "author": author(), "destination": "moved.md" }),
)
.await
.expect_status(StatusCode::OK);
assert_eq!(
server.read_file(TENANT, "moved.md").await.as_deref(),
Some("# Hello")
);
server
.delete(
&format!("{}/files/docs/other.md", TENANT),
json!({ "author": author() }),
)
.await
.expect_status(StatusCode::OK);
server.write_file(TENANT, "docs/a.md", "x").await;
server
.delete(
&format!("{}/files/docs", TENANT),
json!({ "author": author(), "allow_prefix_path_recurse": true }),
)
.await
.expect_status(StatusCode::OK);
assert!(server.read_file(TENANT, "docs/a.md").await.is_none());
}
#[tokio::test]
async fn autoheal_is_off_by_default_so_a_restart_changes_nothing() {
let store = {
let writer = TestServer::builder().checkout_files(false).start().await;
writer.write_file(TENANT, "docs/intro.md", "# Hello").await;
writer.store()
};
let server = TestServer::builder()
.reuse_store(store)
.checkout_files(true)
.start()
.await;
tokio::time::sleep(Duration::from_millis(200)).await;
assert!(
!server
.repo_path("docs", "acme")
.join("docs/intro.md")
.exists(),
"a store was healed without being asked"
);
assert_eq!(
server.read_file(TENANT, "docs/intro.md").await.as_deref(),
Some("# Hello")
);
}
#[tokio::test]
async fn autoheal_writes_the_files_a_disabled_node_never_mirrored() {
let store = {
let writer = TestServer::builder().checkout_files(false).start().await;
writer.write_file(TENANT, "docs/intro.md", "# Hello").await;
writer.write_file(TENANT, "README.md", "# Readme").await;
writer.store()
};
let server = TestServer::builder()
.reuse_store(store)
.checkout_files(true)
.checkout_files_autoheal(true)
.start()
.await;
let repo = server.repo_path("docs", "acme");
assert!(
wait_for_path(&repo.join("docs/intro.md")).await,
"the heal pass did not write the missing file"
);
assert!(wait_for_path(&repo.join("README.md")).await);
assert_eq!(
std::fs::read_to_string(repo.join("docs/intro.md")).unwrap(),
"# Hello"
);
}
#[tokio::test]
async fn autoheal_removes_a_file_head_no_longer_names() {
let store = {
let writer = TestServer::start().await;
writer.write_file(TENANT, "docs/intro.md", "x").await;
let stray = writer.repo_path("docs", "acme").join("docs/stray.md");
assert!(wait_for_path(&writer.repo_path("docs", "acme").join("docs/intro.md")).await);
std::fs::write(&stray, "not in HEAD").expect("cannot write stray file");
writer.store()
};
let server = TestServer::builder()
.reuse_store(store)
.checkout_files_autoheal(true)
.start()
.await;
let stray = server.repo_path("docs", "acme").join("docs/stray.md");
let deadline = std::time::Instant::now() + Duration::from_secs(10);
while stray.exists() && std::time::Instant::now() < deadline {
tokio::time::sleep(Duration::from_millis(5)).await;
}
assert!(
!stray.exists(),
"the heal pass left a file HEAD does not name"
);
assert!(server
.repo_path("docs", "acme")
.join("docs/intro.md")
.exists());
}
#[tokio::test]
async fn autoheal_is_inert_when_no_files_are_kept_on_disk() {
let store = {
let writer = TestServer::builder().checkout_files(false).start().await;
writer.write_file(TENANT, "docs/intro.md", "x").await;
writer.store()
};
let server = TestServer::builder()
.reuse_store(store)
.checkout_files(false)
.checkout_files_autoheal(true)
.start()
.await;
tokio::time::sleep(Duration::from_millis(200)).await;
assert!(
!server
.repo_path("docs", "acme")
.join("docs/intro.md")
.exists(),
"autoheal wrote files on a node keeping none"
);
}