use std::fmt::Write as _;
use std::path::Path;
use std::{env, fs};
use osmflow::Location;
use osmflow::nodes::PagedStore;
use osmflow::nodes::prelude::*;
use osmflow::osc::Replication;
use osmflow_sync::{FileStamp, ImportOpts, Mirror, Rebuild, UpdateOpts};
use postgres::{Client, NoTls};
const PBF: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../osmflow/fixtures/osmpbfreader/clipperton.osm.pbf"
);
const STATE: &str = "sequenceNumber=1708\ntimestamp=2025-04-23T20\\:21\\:01Z\n";
const NODE: i64 = 1_886_651_627;
const DIFF_1709: &str = r#"<modify>
<node id="1886651627" version="9" timestamp="2026-11-01T00:00:00Z" lat="10.3" lon="-109.2"/>
</modify>
<create>
<node id="13000000000" version="1" timestamp="2026-11-01T00:00:00Z" lat="10.31" lon="-109.21">
<tag k="amenity" v="bench"/>
</node>
<way id="2000000000" version="1" timestamp="2026-11-01T00:00:00Z">
<nd ref="1724610187"/><nd ref="13000000000"/>
<tag k="highway" v="path"/>
</way>
</create>
<delete>
<way id="265426044" version="5"/>
<relation id="11980" version="30"/>
</delete>"#;
const DIFF_1711: &str = r#"<modify>
<node id="1886651627" version="10" timestamp="2026-11-01T00:02:00Z" lat="10.32" lon="-109.22"/>
</modify>"#;
const DIFF_1712: &str = r#"<modify>
<node id="1886651627" version="11" timestamp="2026-11-01T00:03:00Z" lat="10.33" lon="-109.23"/>
<node id="13000000000" version="2" timestamp="2026-11-01T00:03:00Z" lat="10.4" lon="-109.3">
<tag k="amenity" v="bench"/>
</node>
</modify>"#;
#[test]
fn import_update_rebuild() {
let Ok(url) = env::var("OSMFLOW_SYNC_TEST_DB") else {
return;
};
let dir = tempfile::tempdir().unwrap();
let diffs = dir.path().join("diffs");
write_state(&diffs, 708, STATE);
let replication = Replication::local(&diffs);
let files = dir.path().join("files");
let mirror = Mirror::new(&url, &files);
import(&mirror);
let mut db = Client::connect(&url, NoTls).unwrap();
write_diffs(&diffs, &mut db);
let other = dir.path().join("other");
write_state(&other, 708, &STATE.replace("20\\:21", "20\\:22"));
let err = mirror
.update(&Replication::local(&other), &until(1709, 1))
.unwrap_err();
assert!(err.to_string().contains("not the database's"), "{err}");
let saved = dir.path().join("saved");
copy_dir(&files, &saved);
let stats = mirror.update(&replication, &until(1711, 1)).unwrap();
assert_eq!((stats.batches, stats.diffs, stats.sequence), (3, 3, 1711));
check_rows(&mut db);
assert_eq!(minor_rev(&mut db, 178_300_745), (2, 4));
assert_eq!(minor_rev(&mut db, 210_476_684), (0, 3));
let one_by_one = rows(&mut db);
copy_dir(&saved, &files);
let restored = mirror.status().unwrap();
assert_eq!(
(restored.nodes.unwrap().rev, restored.index.unwrap().rev),
(1, 1)
);
let stats = mirror.update(&replication, &until(1712, 1)).unwrap();
assert_eq!(stats.batches, 1);
assert_eq!(
changes(&mut db, 5),
[
("n", 13_000_000_000, "u"),
("w", 178_300_745, "g"),
("w", 2_000_000_000, "g")
]
.map(owned)
);
check_files(&mirror, 5);
import(&mirror);
let stats = mirror.update(&replication, &until(1711, 60)).unwrap();
assert_eq!((stats.batches, stats.diffs), (1, 3));
assert_eq!(rows(&mut db), one_by_one);
assert_eq!(minor_rev(&mut db, 178_300_745), (1, 2));
mirror.update(&replication, &until(1712, 60)).unwrap();
check_files(&mirror, 3);
let stats = mirror.update(&replication, &until(1712, 60)).unwrap();
assert_eq!(stats.batches, 0);
for from in [
Rebuild::Database,
Rebuild::Pbf {
path: Path::new(PBF),
sequence: None,
replication: &replication,
max_diffs: 2,
},
] {
fs::remove_file(mirror.index_path()).unwrap();
mirror.rebuild(&from).unwrap();
check_files(&mirror, 3);
}
}
fn import(mirror: &Mirror) {
let opts = ImportOpts {
replace: true,
..ImportOpts::default()
};
let stats = mirror.import(PBF, None, &opts).unwrap();
assert_eq!(
(stats.nodes, stats.tagged_nodes, stats.ways, stats.relations),
(3097, 238, 151, 17)
);
}
fn until(sequence: u64, max_diffs: u64) -> UpdateOpts {
UpdateOpts {
max_diffs,
until: Some(sequence),
}
}
fn write_state(dir: &Path, n: u32, state: &str) {
fs::create_dir_all(dir.join("000/001")).unwrap();
fs::write(dir.join(format!("000/001/{n}.state.txt")), state).unwrap();
}
fn write_diffs(dir: &Path, db: &mut Client) {
let nodes: Vec<i64> = db
.query_one("SELECT nodes FROM osm.ways WHERE id = 210476684", &[])
.unwrap()
.get(0);
let refs = nodes
.iter()
.filter(|&&n| n != NODE)
.fold(String::new(), |mut refs, n| {
write!(refs, r#"<nd ref="{n}"/>"#).unwrap();
refs
});
let diff_1710 = format!(
r#"<modify><way id="210476684" version="99" timestamp="2026-11-01T00:01:00Z">{refs}</way></modify>"#
);
for (n, diff) in [
(709, DIFF_1709),
(710, &diff_1710),
(711, DIFF_1711),
(712, DIFF_1712),
] {
let xml = format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<osmChange version=\"0.6\">\n{diff}\n</osmChange>\n"
);
fs::write(dir.join(format!("000/001/{n}.osc.gz")), xml).unwrap();
let state = format!(
"sequenceNumber={}\ntimestamp=2026-11-01T00\\:0{}\\:30Z\n",
1000 + n,
n - 709
);
write_state(dir, n, &state);
}
}
fn copy_dir(from: &Path, to: &Path) {
if to.exists() {
fs::remove_dir_all(to).unwrap();
}
fs::create_dir_all(to).unwrap();
for entry in fs::read_dir(from).unwrap() {
let path = entry.unwrap().path();
if path.is_file() {
fs::copy(&path, to.join(path.file_name().unwrap())).unwrap();
}
}
}
fn minor_rev(db: &mut Client, id: i64) -> (i32, i64) {
let row = db
.query_one("SELECT minor, rev FROM osm.ways WHERE id = $1", &[&id])
.unwrap();
(row.get(0), row.get(1))
}
fn rows(db: &mut Client) -> Vec<String> {
[
"SELECT format('n%s %s %s %s', id, version, tags, ST_AsText(geom)) FROM osm.nodes ORDER BY id",
"SELECT format('w%s %s %s %s %s', id, version, tags, nodes, ST_AsText(geom)) FROM osm.ways ORDER BY id",
"SELECT format('r%s %s %s %s %s', id, version, tags, member_ids, member_roles) FROM osm.relations ORDER BY id",
]
.iter()
.flat_map(|sql| db.query(*sql, &[]).unwrap())
.map(|r| r.get(0))
.collect()
}
fn changes(db: &mut Client, rev: i64) -> Vec<(String, i64, String)> {
db.query(
"SELECT kind::text, id, action::text FROM osm.changes WHERE rev = $1 ORDER BY 1, 2",
&[&rev],
)
.unwrap()
.iter()
.map(|r| (r.get(0), r.get(1), r.get(2)))
.collect()
}
fn owned((kind, id, action): (&str, i64, &str)) -> (String, i64, String) {
(kind.to_owned(), id, action.to_owned())
}
fn check_rows(db: &mut Client) {
let geom = |db: &mut Client, id: i64| -> Option<String> {
db.query_opt("SELECT ST_AsText(geom) FROM osm.ways WHERE id = $1", &[&id])
.unwrap()
.map(|r| r.get(0))
};
assert!(geom(db, 178_300_745).unwrap().contains("-109.22 10.32"));
assert!(!geom(db, 210_476_684).unwrap().contains("-109.2 "));
assert_eq!(geom(db, 265_426_044), None);
assert!(geom(db, 2_000_000_000).unwrap().ends_with("-109.21 10.31)"));
let bench: i64 = db
.query_one(
"SELECT count(*) FROM osm.nodes WHERE tags->>'amenity' = 'bench'",
&[],
)
.unwrap()
.get(0);
assert_eq!(bench, 1);
let expected = [
("n", 13_000_000_000, "u"),
("r", 11980, "d"),
("w", 178_300_745, "g"),
("w", 210_476_684, "g"),
("w", 265_426_044, "d"),
("w", 2_000_000_000, "u"),
];
assert_eq!(changes(db, 2), expected.map(owned));
assert_eq!(changes(db, 3), [("w", 210_476_684, "u")].map(owned));
assert_eq!(changes(db, 4), [("w", 178_300_745, "g")].map(owned));
}
fn check_files(mirror: &Mirror, rev: i64) {
let status = mirror.status().unwrap();
let stamp = FileStamp {
id: Some(status.db.id),
rev,
};
assert_eq!(status.nodes.unwrap(), stamp);
assert_eq!(status.index.unwrap(), stamp);
let store = PagedStore::open_read_only(mirror.node_path()).unwrap();
for (id, lat, lon) in [(NODE, 10.33, -109.23), (13_000_000_000, 10.4, -109.3)] {
let id = id.cast_unsigned();
assert_eq!(
store.get(id),
Some(Location::from_degrees(lat, lon)),
"{id}"
);
}
}