automerge / autosurgeon

MIT License
256 stars 17 forks source link

Autosurgeon reconcile not clearing all old data #46

Open aran opened 1 month ago

aran commented 1 month ago

I'm not sure the test below is 100% minimal, but I am seeing that the old key "discard" is not removed from the converted document.

automerge = "0.5.11"
autosurgeon = "0.8.3"

#[cfg(test)]
mod tests {
    use automerge::AutoCommit;
    use autosurgeon::{reconcile, Hydrate, Reconcile};

    pub fn debug_dump_doc<D>(doc: &D) -> String
    where
        D: automerge::ReadDoc,
    {
        serde_json::to_string_pretty(&automerge::AutoSerde::from(doc)).unwrap()
    }

    #[derive(Clone, Debug, Default, Hydrate, Reconcile)]
    pub struct V1Container {
        pub items: Vec<V1Item>,
    }

    #[derive(Clone, Debug, Default, Hydrate, Reconcile)]
    pub struct V1Item{
        pub keep: String,
        pub discard: String,
    }

    #[derive(Clone, Debug, Default, Hydrate, Reconcile)]
    pub struct V2Container {
        pub items: Vec<V2Item>,
    }

    #[derive(Clone, Debug, Default, Hydrate, Reconcile)]
    pub struct V2Item{
        pub keep: String,
    }

    pub fn convert_item(old: V1Item) -> V2Item {
        V2Item {
            keep: old.keep,
        }
    }

    pub fn convert_container(old: V1Container) -> V2Container {
        V2Container {
            items: old.items.into_iter().map(convert_item).collect(),
        }
    }

    fn make_test_v1() -> V1Container {
        V1Container {
            items: vec![
                V1Item {
                    keep: "keep".to_string(),
                    discard: "discard".to_string(),
                },
                V1Item {
                    keep: "keep".to_string(),
                    discard: "discard".to_string(),
                },
            ],
        }
    }

    #[test]
    fn test_container_with_items() {
        let orig = make_test_v1();

        let converted = convert_container(orig.clone());

        let mut doc = AutoCommit::new();
        reconcile(&mut doc, &orig).unwrap();
        reconcile(&mut doc, &converted).unwrap();
        let orig_dump = debug_dump_doc(&doc);

        let mut new_doc = AutoCommit::new();
        reconcile(&mut new_doc, converted.clone()).unwrap();

        let new_dump = debug_dump_doc(&new_doc);

        assert_eq!(orig_dump, new_dump);
    }
}
aran commented 3 weeks ago

An reason not to do this in https://github.com/automerge/autosurgeon/issues/52