package bitbucket import ( "context" "net/http" "net/http/httptest" "testing" ) func TestBitbucketGetFileContentSuccess(t *testing.T) { const expected = "file content" server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Verify path structure if r.URL.Path != "/rest/api/1.0/projects/owner/repos/repo/raw/path/to/file.go" { t.Fatalf("unexpected URL path: %s", r.URL.Path) } w.WriteHeader(http.StatusOK) w.Write([]byte(expected)) })) defer server.Close() // Trim trailing slash handling done in NewBitbucket adapter := NewBitbucket(server.URL, "") content, err := adapter.GetFileContent(context.Background(), "owner", "repo", "path/to/file.go", "") if err != nil { t.Fatalf("unexpected error: %v", err) } if content != expected { t.Fatalf("expected %q, got %q", expected, content) } } func TestBitbucketGetFileContentError(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) w.Write([]byte("not found")) })) defer server.Close() adapter := NewBitbucket(server.URL, "") _, err := adapter.GetFileContent(context.Background(), "owner", "repo", "missing.go", "") if err == nil { t.Fatalf("expected error for non‑200 response") } } func TestBitbucketGetPRHeadSHASuccess(t *testing.T) { const sha = "deadbeef" server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/rest/api/1.0/projects/owner/repos/repo/pull-requests/42" { t.Fatalf("unexpected URL: %s", r.URL.Path) } w.WriteHeader(http.StatusOK) w.Write([]byte(`{"toRef":{"latestCommit":"` + sha + `"}}`)) })) defer server.Close() adapter := NewBitbucket(server.URL, "") got, err := adapter.GetPRHeadSHA(context.Background(), "owner", "repo", 42) if err != nil { t.Fatalf("unexpected error: %v", err) } if got != sha { t.Fatalf("expected sha %s, got %s", sha, got) } } func TestBitbucketGetPRHeadSHAError(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("error")) })) defer server.Close() adapter := NewBitbucket(server.URL, "") _, err := adapter.GetPRHeadSHA(context.Background(), "owner", "repo", 1) if err == nil { t.Fatalf("expected error for non‑200 response") } }