package remote

import (
	"os"
	"path/filepath"
	"testing"
)

// Reproduces model.go same-host branch: Exec("cp -r src dst") / Exec("mv src dst")
// where dst = targetDir/base(src) already exists as a directory.
func TestSameHostCpIntoExistingDir(t *testing.T) {
	_, c, dir := setup(t)
	src := filepath.Join(dir, "A", "proj")
	writeFile(t, filepath.Join(src, "new.txt"), 5, 0o644)
	dst := filepath.Join(dir, "B", "proj")
	writeFile(t, filepath.Join(dst, "old.txt"), 5, 0o644)
	out, err := c.Exec("cp -r " + ShellQuote(src) + " " + ShellQuote(dst))
	t.Logf("cp out=%q err=%v", out, err)
	if _, e := os.Stat(filepath.Join(dst, "proj", "new.txt")); e == nil {
		t.Errorf("BUG CONFIRMED: cp -r nested source into %s/proj/ instead of merging", dst)
	}
	src2 := filepath.Join(dir, "A", "proj2")
	writeFile(t, filepath.Join(src2, "x.txt"), 5, 0o644)
	dst2 := filepath.Join(dir, "B", "proj2")
	os.MkdirAll(dst2, 0o755)
	_, err = c.Exec("mv " + ShellQuote(src2) + " " + ShellQuote(dst2))
	if _, e := os.Stat(filepath.Join(dst2, "proj2", "x.txt")); e == nil {
		t.Errorf("BUG CONFIRMED: mv moved source into %s/proj2/ (err=%v)", dst2, err)
	}
}
