package provider import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" ) // A worker-owned checkout lives on another machine and has already pushed the // commit. The coordinator must publish by verifying the forge, not by running // git in a directory it does not have. func TestPushAcceptsACommitTheRemoteAlreadyHolds(t *testing.T) { const sha = "1111111111111111111111111111111111111111" var asked string srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { asked = r.URL.Path json.NewEncoder(w).Encode(map[string]any{"commit": map[string]string{"id": sha}}) })) defer srv.Close() // Root is deliberately empty: a push would have nowhere to run. p := GiteaPublisher{Gitea: Gitea{BaseURL: srv.URL, Owner: "kami", Repo: "test-e2e"}} got, err := p.Push(context.Background(), "origin", "orchestra/T1", sha) if err != nil { t.Fatalf("push: %v", err) } if got != sha { t.Fatalf("got %q, want %q", got, sha) } if asked != "/api/v1/repos/kami/test-e2e/branches/orchestra/T1" { t.Fatalf("unexpected branch read %q", asked) } } func TestPushRefusesWhenTheRemoteHoldsAnotherCommitAndThereIsNoCheckout(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(map[string]any{"commit": map[string]string{"id": "2222222222222222222222222222222222222222"}}) })) defer srv.Close() p := GiteaPublisher{Gitea: Gitea{BaseURL: srv.URL, Owner: "kami", Repo: "test-e2e"}} if _, err := p.Push(context.Background(), "origin", "orchestra/T1", "1111111111111111111111111111111111111111"); err == nil { t.Fatal("expected a refusal, got a published commit") } }