Elffuss BraidHub

utopia-ia/elffuss-braid public

main · 3.5 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! Engine bugs the landing simulation found, each cut down to a minimal test.
5
6use braid_engine::{LandOutcome, commit, land};
7use braid_format::{Change, ChangeOp, Kind, MemoryObjects, ObjectId, Objects, Signature};
8
9fn signature(name: &str) -> Signature {
10 Signature {
11 name: name.as_bytes().to_vec(),
12 email: format!("{name}@example.org").into_bytes(),
13 seconds: 1_700_000_000,
14 offset_minutes: 0,
15 }
16}
17
18fn set(objects: &MemoryObjects, path: &str, content: &str) -> Change {
19 let blob = objects.put(Kind::Blob, content.as_bytes()).unwrap();
20 Change {
21 path: path.as_bytes().to_vec(),
22 op: ChangeOp::File {
23 blob,
24 size: content.len() as u64,
25 },
26 }
27}
28
29fn change(objects: &MemoryObjects, parents: &[ObjectId], changes: Vec<Change>) -> ObjectId {
30 commit(
31 objects,
32 parents.to_vec(),
33 changes,
34 signature("agent"),
35 signature("agent"),
36 b"change\n".to_vec(),
37 Vec::new(),
38 )
39 .unwrap()
40}
41
42fn land_on(objects: &MemoryObjects, head: ObjectId, change: ObjectId) -> LandOutcome {
43 land(objects, Some(head), change, signature("lander")).unwrap()
44}
45
46/// Found by the in-memory run, seed 1 (invariant I4; 30 of the first 40
47/// seeds hit it): a change landed by a rebase, the answer was lost, and the
48/// agent sent the same changeset again. `land` recognised a change that had
49/// landed by a fast-forward, which is on the branch itself, but not one that
50/// had landed rebased, which is on the branch as a new changeset whose
51/// `rebased-from` names it. It rebased the change a second time: after an
52/// edit of the same file the retry reported a conflict for a change that had
53/// landed, and after a revert it landed the same edit twice.
54///
55/// This failed until `land` learned to look for `rebased-from` on its walk
56/// from the head down to the base.
57#[test]
58fn a_change_that_landed_rebased_is_already_applied() {
59 let objects = MemoryObjects::new();
60 let base = change(
61 &objects,
62 &[],
63 vec![set(&objects, "f", "1\n"), set(&objects, "g", "1\n")],
64 );
65 let ours = change(&objects, &[base], vec![set(&objects, "g", "2\n")]);
66 let theirs = change(&objects, &[base], vec![set(&objects, "f", "2\n")]);
67 assert_eq!(
68 land_on(&objects, base, ours),
69 LandOutcome::FastForward(ours)
70 );
71 let LandOutcome::Rebased {
72 changeset: rebased, ..
73 } = land_on(&objects, ours, theirs)
74 else {
75 panic!("expected a rebase");
76 };
77
78 // Sent again right away: nothing to do, as before.
79 assert_eq!(
80 land_on(&objects, rebased, theirs),
81 LandOutcome::AlreadyApplied
82 );
83
84 // Sent again after another change edited the same file: it landed, so it
85 // must not be reported as a conflict.
86 let edit = change(&objects, &[rebased], vec![set(&objects, "f", "3\n")]);
87 assert_eq!(
88 land_on(&objects, rebased, edit),
89 LandOutcome::FastForward(edit)
90 );
91 assert_eq!(land_on(&objects, edit, theirs), LandOutcome::AlreadyApplied);
92
93 // Sent again after another change put the file back: it must not land a
94 // second time over the revert.
95 let revert = change(&objects, &[rebased], vec![set(&objects, "f", "1\n")]);
96 assert_eq!(
97 land_on(&objects, rebased, revert),
98 LandOutcome::FastForward(revert)
99 );
100 assert_eq!(
101 land_on(&objects, revert, theirs),
102 LandOutcome::AlreadyApplied
103 );
104}