use indexmap::IndexMap;
use serde_json::Value;
use std::fmt;
use toha::{
Id, RawAnswer,
interview::SnapshotSeed,
snapshot::{Listed, Project, ProjectError, Snapshot, SnapshotError, SnapshotId},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LikeSelector {
Reference(String),
Latest,
}
#[derive(Debug, Clone)]
pub struct LikeSeed {
pub engine: SnapshotSeed,
pub id: SnapshotId,
pub label: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LikeError {
NoProject,
Missing { source: String },
Unknown { reference: String },
Ambiguous { reference: String },
WrongSource {
want: String,
got: String,
id: SnapshotId,
},
}
impl fmt::Display for LikeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LikeError::NoProject => write!(
f,
"the target is not inside a git repository, so there is no snapshot to seed from"
),
LikeError::Missing { source } => {
write!(f, "no snapshot of {source} to seed from")
}
LikeError::Unknown { reference } => {
write!(f, "no snapshot matches {reference}")
}
LikeError::Ambiguous { reference } => {
write!(
f,
"snapshot prefix {reference} matches more than one snapshot"
)
}
LikeError::WrongSource { want, got, id } => write!(
f,
"snapshot {id} is of source {got}, not the template's source {want}"
),
}
}
}
pub fn candidates(project: &Project, source: &str) -> Result<Vec<Snapshot>, ProjectError> {
Ok(project
.snapshots_all()?
.into_iter()
.filter_map(|entry| match entry {
Listed::Valid(snapshot) => Some(snapshot),
Listed::Invalid { .. } => None,
})
.filter(|snapshot| snapshot.source() == source)
.collect())
}
pub fn latest(candidates: &[Snapshot]) -> Option<&Snapshot> {
candidates.iter().max_by(|a, b| a.id().cmp(b.id()))
}
pub fn find_required(
project: Option<&Project>,
source: &str,
reference: &str,
) -> Result<Snapshot, LikeError> {
let project = project.ok_or(LikeError::NoProject)?;
let snapshot = match project.find(reference) {
Ok(snapshot) => snapshot,
Err(SnapshotError::Ambiguous { .. }) => {
return Err(LikeError::Ambiguous {
reference: reference.to_owned(),
});
}
Err(_) => {
return Err(LikeError::Unknown {
reference: reference.to_owned(),
});
}
};
if snapshot.source() != source {
return Err(LikeError::WrongSource {
want: source.to_owned(),
got: snapshot.source().to_owned(),
id: *snapshot.id(),
});
}
Ok(snapshot)
}
pub fn fold_submissions(submissions: &[IndexMap<String, Value>]) -> IndexMap<Id, RawAnswer> {
let mut out: IndexMap<Id, RawAnswer> = IndexMap::new();
for batch in submissions {
for (key, value) in batch {
if let Ok(id) = Id::parse(key) {
out.insert(id, RawAnswer(value.clone()));
}
}
}
out
}
pub fn seed(snapshot: &Snapshot) -> LikeSeed {
let id = *snapshot.id();
LikeSeed {
engine: SnapshotSeed {
defaults: fold_submissions(snapshot.submissions()),
from: id.to_string(),
},
id,
label: format!("{id} ({}, {})", snapshot.source(), snapshot.target()),
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use std::collections::BTreeSet;
use toha::snapshot::{
CommitId, FrozenNow, Origin, PathOwnership, ProjectPoint, RepoPath, Revision, SnapshotDoc,
Timestamp,
};
fn snapshot(
id_text: &str,
template: &str,
target: &str,
submissions: Vec<IndexMap<String, Value>>,
) -> Snapshot {
let id: SnapshotId = id_text.parse().unwrap();
let doc = SnapshotDoc::new(
id,
template.to_owned(),
Revision::Unversioned,
RepoPath::parse(target).unwrap(),
Timestamp::parse("2026-09-30T04:12:00Z").unwrap(),
FrozenNow::parse("2026-03-14T09:26:53+00:00[UTC]").unwrap(),
ProjectPoint::new(
CommitId::parse("a41c0de00000000000000000000000000000beef").unwrap(),
None,
),
None,
submissions,
vec![PathOwnership::new(
toha::TargetPath::parse("x.txt").unwrap(),
Origin::Toha,
)],
);
let bytes = doc.to_json_bytes();
let files: BTreeSet<String> = ["x.txt".to_owned()].into_iter().collect();
Snapshot::validate(id, &bytes, &files).unwrap()
}
fn batch(pairs: &[(&str, Value)]) -> IndexMap<String, Value> {
pairs
.iter()
.map(|(k, v)| ((*k).to_owned(), v.clone()))
.collect()
}
const ID_A: &str = "01JA2B8M4R0C7W1Y5F3H9K2S6E";
const ID_B: &str = "01JA2B8M4R0C7W1Y5F3H9K2S6F";
#[test]
fn latest_picks_the_maximum_ulid_regardless_of_input_order() {
let older = snapshot(ID_A, "gh:example/widget", "a", vec![]);
let newer = snapshot(ID_B, "gh:example/widget", "b", vec![]);
let ascending = vec![older, newer];
assert_eq!(latest(&ascending).unwrap().id().to_string(), ID_B);
let descending: Vec<Snapshot> = ascending.into_iter().rev().collect();
assert_eq!(latest(&descending).unwrap().id().to_string(), ID_B);
assert!(latest(&[]).is_none());
}
#[test]
fn fold_takes_the_last_value_per_id_and_keeps_a_looped_array_whole() {
let submissions = vec![
batch(&[("label", json!("first")), ("style", json!("card"))]),
batch(&[
("label", json!("second")),
("items", json!(["a", "b", "c"])),
]),
];
let folded = fold_submissions(&submissions);
assert_eq!(folded[&Id::parse("label").unwrap()].0, json!("second"));
assert_eq!(folded[&Id::parse("style").unwrap()].0, json!("card"));
assert_eq!(
folded[&Id::parse("items").unwrap()].0,
json!(["a", "b", "c"])
);
}
#[test]
fn seed_carries_the_id_label_and_folded_defaults() {
let snap = snapshot(
ID_A,
"gh:example/widget@v1",
"src/widgets/alpha",
vec![batch(&[("label", json!("Alpha"))])],
);
let built = seed(&snap);
assert_eq!(built.id.to_string(), ID_A);
assert_eq!(built.engine.from, ID_A);
assert_eq!(
built.engine.defaults[&Id::parse("label").unwrap()].0,
json!("Alpha")
);
assert!(built.label.contains(ID_A));
assert!(built.label.contains("gh:example/widget"));
assert!(built.label.contains("src/widgets/alpha"));
}
use std::path::Path;
use std::process::Command;
fn git(repo: &Path, args: &[&str]) {
let ok = Command::new("git")
.arg("-C")
.arg(repo)
.args(args)
.env("GIT_AUTHOR_NAME", "Test")
.env("GIT_AUTHOR_EMAIL", "test@example.invalid")
.env("GIT_COMMITTER_NAME", "Test")
.env("GIT_COMMITTER_EMAIL", "test@example.invalid")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_NOSYSTEM", "1")
.status()
.unwrap()
.success();
assert!(ok, "git {args:?}");
}
fn git_out(repo: &Path, index: Option<&Path>, args: &[&str]) -> String {
let mut cmd = Command::new("git");
cmd.arg("-C").arg(repo).args(args);
if let Some(index) = index {
cmd.env("GIT_INDEX_FILE", index);
}
let out = cmd
.env("GIT_AUTHOR_NAME", "Test")
.env("GIT_AUTHOR_EMAIL", "test@example.invalid")
.env("GIT_COMMITTER_NAME", "Test")
.env("GIT_COMMITTER_EMAIL", "test@example.invalid")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_NOSYSTEM", "1")
.output()
.unwrap();
assert!(out.status.success(), "git {args:?}: {out:?}");
String::from_utf8(out.stdout).unwrap().trim().to_owned()
}
fn hash_object(repo: &Path, bytes: &[u8]) -> String {
use std::io::Write;
let mut child = Command::new("git")
.arg("-C")
.arg(repo)
.args(["hash-object", "-w", "--stdin"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap();
child.stdin.take().unwrap().write_all(bytes).unwrap();
String::from_utf8(child.wait_with_output().unwrap().stdout)
.unwrap()
.trim()
.to_owned()
}
fn write_ref(repo: &Path, id: &str, template: &str, target: &str) {
let doc = SnapshotDoc::new(
id.parse().unwrap(),
template.to_owned(),
Revision::Unversioned,
RepoPath::parse(target).unwrap(),
Timestamp::parse("2026-09-30T04:12:00Z").unwrap(),
FrozenNow::parse("2026-03-14T09:26:53+00:00[UTC]").unwrap(),
ProjectPoint::new(
CommitId::parse("a41c0de00000000000000000000000000000beef").unwrap(),
None,
),
None,
vec![batch(&[("label", json!("Alpha"))])],
vec![PathOwnership::new(
toha::TargetPath::parse("x.txt").unwrap(),
Origin::Toha,
)],
);
let index = repo.join(format!(".idx-{id}"));
let _ = std::fs::remove_file(&index);
let json_oid = hash_object(repo, &doc.to_json_bytes());
let file_oid = hash_object(repo, b"x\n");
git_out(
repo,
Some(&index),
&[
"update-index",
"--add",
"--cacheinfo",
&format!("100644,{json_oid},snapshot.json"),
],
);
git_out(
repo,
Some(&index),
&[
"update-index",
"--add",
"--cacheinfo",
&format!("100644,{file_oid},files/x.txt"),
],
);
let tree = git_out(repo, Some(&index), &["write-tree"]);
let commit = git_out(repo, None, &["commit-tree", &tree, "-m", "snapshot"]);
git_out(
repo,
None,
&["update-ref", &format!("refs/toha/snapshots/{id}"), &commit],
);
let _ = std::fs::remove_file(&index);
}
fn open(repo: &Path, sub: &str) -> Project {
let target = toha::staging::canonical_target(&repo.join(sub)).unwrap();
Project::open(&target).unwrap().expect("in git")
}
#[test]
fn candidates_and_find_required_match_on_source_identity_across_versions() {
let dir = tempfile::tempdir().unwrap();
let repo = dir.path();
git(repo, &["init", "-q", "-b", "main"]);
std::fs::write(repo.join("seed.txt"), "seed\n").unwrap();
git(repo, &["add", "-A"]);
git(repo, &["commit", "-q", "-m", "seed"]);
write_ref(repo, ID_A, "gh:example/widget@v1", "src/widgets/alpha");
write_ref(repo, ID_B, "gh:example/widget@v2", "src/widgets/beta");
write_ref(
repo,
"01JA2B8M4R0C7W1Y5F3H9K2S70",
"gh:example/gadget@v1",
"src/gadgets/one",
);
let project = open(repo, "src/widgets/gamma");
let cands = candidates(&project, "gh:example/widget").unwrap();
let ids: Vec<String> = cands.iter().map(|s| s.id().to_string()).collect();
assert_eq!(ids, vec![ID_B.to_owned(), ID_A.to_owned()], "{ids:?}");
let found = find_required(Some(&project), "gh:example/widget", ID_A).unwrap();
assert_eq!(found.id().to_string(), ID_A);
let err = find_required(
Some(&project),
"gh:example/widget",
"01JA2B8M4R0C7W1Y5F3H9K2S70",
)
.unwrap_err();
assert!(
matches!(err, LikeError::WrongSource { .. }),
"foreign source refused: {err:?}"
);
}
#[test]
fn a_prefix_matching_several_snapshots_is_ambiguous_and_a_unique_one_resolves() {
let dir = tempfile::tempdir().unwrap();
let repo = dir.path();
git(repo, &["init", "-q", "-b", "main"]);
std::fs::write(repo.join("seed.txt"), "seed\n").unwrap();
git(repo, &["add", "-A"]);
git(repo, &["commit", "-q", "-m", "seed"]);
write_ref(repo, ID_A, "gh:example/widget", "src/widgets/alpha");
write_ref(repo, ID_B, "gh:example/widget", "src/widgets/beta");
let project = open(repo, "src/widgets/gamma");
let shared = &ID_A[..6];
assert_eq!(shared, &ID_B[..6], "the two ids share this >=6-char prefix");
let err = find_required(Some(&project), "gh:example/widget", shared).unwrap_err();
assert!(
matches!(err, LikeError::Ambiguous { .. }),
"a shared prefix is ambiguous: {err:?}"
);
let found = find_required(Some(&project), "gh:example/widget", ID_A).unwrap();
assert_eq!(found.id().to_string(), ID_A);
}
}