2020-01-09 13:09:05 -09:00
|
|
|
package responses
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-27 16:19:28 -08:00
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2022-12-05 18:45:02 -09:00
|
|
|
"strings"
|
2020-01-09 13:09:05 -09:00
|
|
|
"testing"
|
|
|
|
|
|
2020-01-23 15:44:08 -09:00
|
|
|
"github.com/navidrome/navidrome/log"
|
2022-07-26 12:47:16 -08:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2020-01-09 13:09:05 -09:00
|
|
|
"github.com/onsi/gomega"
|
|
|
|
|
"github.com/onsi/gomega/types"
|
2026-04-27 16:19:28 -08:00
|
|
|
"github.com/pmezard/go-difflib/difflib"
|
2020-01-09 13:09:05 -09:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestSubsonicApiResponses(t *testing.T) {
|
|
|
|
|
log.SetLevel(log.LevelError)
|
|
|
|
|
gomega.RegisterFailHandler(ginkgo.Fail)
|
|
|
|
|
ginkgo.RunSpecs(t, "Subsonic API Responses Suite")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func MatchSnapshot() types.GomegaMatcher {
|
2026-04-27 16:19:28 -08:00
|
|
|
return &snapshotMatcher{}
|
2020-01-09 13:09:05 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type snapshotMatcher struct {
|
2026-04-27 16:19:28 -08:00
|
|
|
diff string
|
2020-01-09 13:09:05 -09:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 16:19:28 -08:00
|
|
|
func (matcher *snapshotMatcher) Match(actual any) (success bool, err error) {
|
|
|
|
|
actualBytes, ok := actual.([]byte)
|
|
|
|
|
if !ok {
|
|
|
|
|
return false, fmt.Errorf("MatchSnapshot expects []byte, got %T", actual)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name := ginkgo.CurrentSpecReport().FullText()
|
|
|
|
|
actualStr := strings.TrimSpace(string(actualBytes))
|
|
|
|
|
|
|
|
|
|
snapshotPath := filepath.Join(".snapshots", name)
|
|
|
|
|
|
|
|
|
|
// Support UPDATE_SNAPSHOTS=true for `make snapshots`
|
|
|
|
|
if _, update := os.LookupEnv("UPDATE_SNAPSHOTS"); update {
|
|
|
|
|
err := os.MkdirAll(".snapshots", os.ModePerm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
return true, os.WriteFile(snapshotPath, []byte(actualStr+"\n"), 0600)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected, err := os.ReadFile(snapshotPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Normalize line endings so snapshots work on Windows (CRLF) and Unix (LF)
|
|
|
|
|
normalizedExpected := strings.ReplaceAll(string(expected), "\r\n", "\n")
|
|
|
|
|
normalizedExpected = strings.TrimSpace(normalizedExpected)
|
|
|
|
|
|
|
|
|
|
if actualStr == normalizedExpected {
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
|
|
|
|
|
A: difflib.SplitLines(normalizedExpected),
|
|
|
|
|
B: difflib.SplitLines(actualStr),
|
|
|
|
|
FromFile: "snapshot",
|
|
|
|
|
ToFile: "actual",
|
|
|
|
|
Context: 3,
|
|
|
|
|
})
|
|
|
|
|
matcher.diff = diff
|
|
|
|
|
return false, nil
|
2020-01-09 13:09:05 -09:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 16:19:28 -08:00
|
|
|
func (matcher *snapshotMatcher) FailureMessage(_ any) (message string) {
|
|
|
|
|
return "Expected to match saved snapshot\n" + matcher.diff
|
2020-01-09 13:09:05 -09:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 16:19:28 -08:00
|
|
|
func (matcher *snapshotMatcher) NegatedFailureMessage(_ any) (message string) {
|
2020-05-13 12:49:55 -08:00
|
|
|
return "Expected to not match saved snapshot\n"
|
2020-01-09 13:09:05 -09:00
|
|
|
}
|