// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package integration
import (
	"fmt"
	"net/http"
	"regexp"
	"testing"
	"forgejo.org/tests"
	"github.com/stretchr/testify/assert"
)
func TestReleaseFeed(t *testing.T) {
	defer tests.PrepareTestEnv(t)()
	normalize := func(body string) string {
		// Remove port.
		body = regexp.MustCompile(`localhost:\d+`).ReplaceAllString(body, "localhost")
		// date is timezone dependent.
		body = regexp.MustCompile(`.*`).ReplaceAllString(body, "")
		body = regexp.MustCompile(`.*`).ReplaceAllString(body, "")
		return body
	}
	t.Run("RSS feed", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()
		releasesPath := "/user2/repo1/releases"
		MakeRequest(t, NewRequest(t, "GET", releasesPath), http.StatusOK)
		resp := MakeRequest(t, NewRequest(t, "GET", releasesPath+".rss"), http.StatusOK)
		assert.Equal(t, fmt.Sprintf(`
  
    Releases for user2/repo1
    http://localhost%[1]s
    
    
    - 
      pre-release
      http://localhost%[1]s/tag/v1.0
      
      some text for a pre release
 
]]>
      user2
      5: http://localhost%[1]s/tag/v1.0
      
    
    - 
      testing-release
      http://localhost%[1]s/tag/v1.1
      
      user2
      1: http://localhost%[1]s/tag/v1.1
      
    
 
  
`, releasesPath), normalize(resp.Body.String()))
	})
	t.Run("Atom feed", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()
		releasesPath := "/user2/repo1/releases"
		MakeRequest(t, NewRequest(t, "GET", releasesPath), http.StatusOK)
		resp := MakeRequest(t, NewRequest(t, "GET", releasesPath+".atom"), http.StatusOK)
		assert.Equal(t, fmt.Sprintf(`
  Releases for user2/repo1
  http://localhost%[1]s
  
  
  
    pre-release
    
    5: http://localhost%[1]s/tag/v1.0
    <p dir="auto">some text for a pre release</p>
    
    
      user2
      user2@noreply.example.org
    
  
  
    testing-release
    
    1: http://localhost%[1]s/tag/v1.1
    
    
      user2
      user2@noreply.example.org
    
  
`, releasesPath), normalize(resp.Body.String()))
	})
}