@@ -845,6 +845,67 @@ func TestReloadFromFileKeepsExistingUndoHistory(t *testing.T) {
845845 }
846846}
847847
848+ func TestReloadFromFileAllowsTwoWayMergedConflictWhenCanonicalBaseLabelExists (t * testing.T ) {
849+ ctx := context .Background ()
850+ tmpDir := t .TempDir ()
851+
852+ basePath := filepath .Join (tmpDir , "base.txt" )
853+ localPath := filepath .Join (tmpDir , "local.txt" )
854+ remotePath := filepath .Join (tmpDir , "remote.txt" )
855+ mergedPath := filepath .Join (tmpDir , "merged.txt" )
856+
857+ if err := os .WriteFile (basePath , []byte ("intro\n outro\n " ), 0o644 ); err != nil {
858+ t .Fatal (err )
859+ }
860+ if err := os .WriteFile (localPath , []byte ("intro\n ours line\n outro\n " ), 0o644 ); err != nil {
861+ t .Fatal (err )
862+ }
863+ if err := os .WriteFile (remotePath , []byte ("intro\n theirs line\n outro\n " ), 0o644 ); err != nil {
864+ t .Fatal (err )
865+ }
866+ mergedContent := "intro\n <<<<<<< ours-label\n ours line\n =======\n theirs line\n >>>>>>> theirs-label\n outro\n "
867+ if err := os .WriteFile (mergedPath , []byte (mergedContent ), 0o644 ); err != nil {
868+ t .Fatal (err )
869+ }
870+
871+ diff3Bytes , err := gitmerge .MergeFileDiff3 (ctx , localPath , basePath , remotePath )
872+ if err != nil {
873+ t .Fatalf ("MergeFileDiff3 failed: %v" , err )
874+ }
875+ doc , err := markers .Parse (diff3Bytes )
876+ if err != nil {
877+ t .Fatalf ("Parse error = %v" , err )
878+ }
879+ state , err := engine .NewState (doc )
880+ if err != nil {
881+ t .Fatalf ("NewState error = %v" , err )
882+ }
883+
884+ m := model {
885+ ctx : ctx ,
886+ opts : cli.Options {BasePath : basePath , LocalPath : localPath , RemotePath : remotePath , MergedPath : mergedPath },
887+ state : state ,
888+ doc : doc ,
889+ }
890+
891+ if err := m .reloadFromFile (); err != nil {
892+ t .Fatalf ("reloadFromFile error = %v" , err )
893+ }
894+ seg := conflictSegment (t , m .doc , 0 )
895+ if len (seg .Base ) != 0 {
896+ t .Fatalf ("seg.Base = %q, want empty" , string (seg .Base ))
897+ }
898+ if seg .BaseLabel == "" {
899+ t .Fatal ("seg.BaseLabel = empty, want preserved canonical base label" )
900+ }
901+ if ! m .mergedLabelKnown [0 ] {
902+ t .Fatalf ("mergedLabelKnown[0] = false, want true" )
903+ }
904+ if m .mergedLabels [0 ].OursLabel != "ours-label" || m .mergedLabels [0 ].TheirsLabel != "theirs-label" {
905+ t .Fatalf ("mergedLabels[0] = %+v" , m .mergedLabels [0 ])
906+ }
907+ }
908+
848909func TestModelInitReturnsNil (t * testing.T ) {
849910 if cmd := (model {}).Init (); cmd != nil {
850911 t .Fatalf ("Init() = %v, want nil" , cmd )
0 commit comments