From d11637c4b11d0792d0fa8c5ec34e3fc63d01a803 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 28 May 2026 14:26:30 +0800 Subject: [PATCH] fix --- tests/integration/api_packages_npm_test.go | 27 +++------------- tests/integration/html_helper.go | 36 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/tests/integration/api_packages_npm_test.go b/tests/integration/api_packages_npm_test.go index 80934af2a18..1159b64da87 100644 --- a/tests/integration/api_packages_npm_test.go +++ b/tests/integration/api_packages_npm_test.go @@ -303,29 +303,10 @@ func TestPackageNpm(t *testing.T) { AddBasicAuth(user.Name) resp := MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) - markup := doc.Find(".markup.markdown") - paragraph := markup.Find("p") - require.Len(t, paragraph.Nodes, 1) - assert.Equal(t, "auto", paragraph.AttrOr("dir", "")) - - links := paragraph.Find("a") - require.Len(t, links.Nodes, 2) - - docLink := links.Eq(0) - assert.Equal(t, "docs", strings.TrimSpace(docLink.Text())) - assert.Equal(t, "/user2/repo1/src/branch/master/package-subdir/docs/usage.md", docLink.AttrOr("href", "")) - assert.Equal(t, "nofollow", docLink.AttrOr("rel", "")) - - imageLink := links.Eq(1) - assert.Equal(t, "/user2/repo1/src/branch/master/package-subdir/logo.png", imageLink.AttrOr("href", "")) - assert.Equal(t, "nofollow noopener", imageLink.AttrOr("rel", "")) - assert.Equal(t, "_blank", imageLink.AttrOr("target", "")) - - image := imageLink.Find("img") - require.Len(t, image.Nodes, 1) - assert.Equal(t, "/user2/repo1/media/branch/master/package-subdir/logo.png", image.AttrOr("src", "")) - assert.Equal(t, "logo", image.AttrOr("alt", "")) - assert.Equal(t, "lazy", image.AttrOr("loading", "")) + rendered, _ := doc.Find(".markup.markdown").Html() + assertHTMLEq(t, `

docs +logo

+`, rendered) }) t.Run("Delete", func(t *testing.T) { diff --git a/tests/integration/html_helper.go b/tests/integration/html_helper.go index c2045453e65..2f52f10875c 100644 --- a/tests/integration/html_helper.go +++ b/tests/integration/html_helper.go @@ -5,10 +5,13 @@ package integration import ( "io" + "slices" + "strings" "testing" "github.com/PuerkitoBio/goquery" "github.com/stretchr/testify/assert" + "golang.org/x/net/html" ) // HTMLDoc struct @@ -47,3 +50,36 @@ func AssertHTMLElement[T int | bool](t testing.TB, doc *HTMLDoc, selector string assert.Equal(t, v, sel.Length()) } } + +func assertHTMLEq(t testing.TB, expected, actual string) { + t.Helper() + exp, err := html.Parse(strings.NewReader(expected)) + if !assert.NoError(t, err) { + return + } + act, err := html.Parse(strings.NewReader(actual)) + if !assert.NoError(t, err) { + return + } + var normalize func(n *html.Node) + normalize = func(n *html.Node) { + slices.SortFunc(n.Attr, func(a, b html.Attribute) int { + if cmp := strings.Compare(a.Namespace, b.Namespace); cmp != 0 { + return cmp + } + if cmp := strings.Compare(a.Key, b.Key); cmp != 0 { + return cmp + } + return strings.Compare(a.Val, b.Val) + }) + for c := n.FirstChild; c != nil; c = c.NextSibling { + normalize(c) + } + } + normalize(exp) + normalize(act) + var expNormalized, actNormalized strings.Builder + assert.NoError(t, html.Render(&expNormalized, exp)) + assert.NoError(t, html.Render(&actNormalized, act)) + assert.Equal(t, expNormalized.String(), actNormalized.String()) +}