package main import ( "encoding/json" "os" "os/exec" "path/filepath" "testing" ) func TestCleanSameHeadCanResyncWithoutAcceptingStaleBuild(t *testing.T) { if _, e := exec.LookPath("git"); e != nil { t.Skip("Git unavailable") } s := testServer(t) s.workspaceRoot = t.TempDir() work := filepath.Join(s.workspaceRoot, "checkout") os.MkdirAll(work, 0700) git := func(args ...string) { t.Helper() if out, e := exec.Command("git", append([]string{"-C", work}, args...)...).CombinedOutput(); e != nil { t.Fatalf("git: %v %s", e, out) } } git("init", "-q") git("config", "core.autocrlf", "false") os.WriteFile(filepath.Join(work, "a.txt"), []byte("original\n"), 0600) os.WriteFile(filepath.Join(work, "restored.txt"), []byte("tracked\n"), 0600) git("add", ".") git("-c", "user.name=Test", "-c", "user.email=test@example.invalid", "commit", "-qm", "base") g, e := gitSnapshot(work) if e != nil { t.Fatal(e) } w := call(t, s, "POST", "/projects", WorkspaceRequest{WorkspacePath: work, Git: g}) if w.Code != 200 { t.Fatal(w.Body.String()) } var out map[string]string json.Unmarshal(w.Body.Bytes(), &out) p := out["id"] // A successful sync intentionally removed a tracked file; later checkout restores it. w = call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "old", Files: map[string]File{"a.txt": file("original\n")}, Git: g}) if w.Code != 200 { t.Fatal(w.Body.String()) } git("restore", "--worktree", "--", "restored.txt") // CRLF checkout is Git-clean, even though raw bytes no longer match the old snapshot. git("config", "core.autocrlf", "true") os.WriteFile(filepath.Join(work, "a.txt"), []byte("original\r\n"), 0600) git("add", "--renormalize", ".") g, e = gitSnapshot(work) if e != nil || g.Dirty { out, _ := gitCommand(work, nil, "status", "--porcelain") diff, _ := gitCommand(work, nil, "diff", "--numstat") t.Fatalf("fixture not Git-clean: %v %v status=%s diff=%s", g, e, out, diff) } originalRevision := s.projects[p].Revision if e = s.checkGit(p, g); e != nil { t.Fatal("clean same-HEAD checkout blocked", e) } manifest := s.manifest(p) if manifest.Files["a.txt"].Hash != digest([]byte("original\r\n")) || manifest.Files["restored.txt"].Hash == "" || manifest.Revision != "" { t.Fatal("manifest did not expose current checkout", manifest) } if s.projects[p].Revision != originalRevision || len(s.projects[p].Files) != 1 { t.Fatal("read-only check rebased persistent snapshot") } w = call(t, s, "POST", "/projects/"+p+"/builds", map[string]string{"revision": "old", "task": ":app:assembleDebug"}) if w.Code != 409 { t.Fatal("stale revision built", w.Body.String()) } // Reuse the remote raw hash, preserving CRLF, while applying local deletion again. w = call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "new", Files: map[string]File{"a.txt": {Hash: manifest.Files["a.txt"].Hash}}, Git: g}) if w.Code != 200 { t.Fatal(w.Body.String()) } if _, e = os.Stat(filepath.Join(work, "restored.txt")); !os.IsNotExist(e) { t.Fatal("restored tracked deletion not synchronized") } if s.projects[p].Files["a.txt"].TextHash == "" { t.Fatal("normalized baseline not persisted") } // A later real remote edit must remain protected. os.WriteFile(filepath.Join(work, "a.txt"), []byte("remote real edit\n"), 0600) if e = s.checkGit(p, g); e == nil { t.Fatal("real remote edit was accepted") } }