68 lines
2.0 KiB
Go
68 lines
2.0 KiB
Go
package app
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestExtractReferencedImageAssetIDsFromMarkdown_DeduplicatesInOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
markdown := strings.Join([]string{
|
|
`before`,
|
|
`<p><img src="https://example.com/1.webp" data-asset-id="42" /></p>`,
|
|
`<p><img src="https://example.com/2.webp" data-asset-id='7' /></p>`,
|
|
`<p><img src="https://example.com/3.webp" data-asset-id="42" /></p>`,
|
|
}, "\n")
|
|
|
|
got := extractReferencedImageAssetIDsFromMarkdown(markdown)
|
|
want := []int64{42, 7}
|
|
|
|
if len(got) != len(want) {
|
|
t.Fatalf("expected %d ids, got %d (%v)", len(want), len(got), got)
|
|
}
|
|
for idx := range want {
|
|
if got[idx] != want[idx] {
|
|
t.Fatalf("expected ids %v, got %v", want, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStripInactiveImageAssetIDsFromMarkdown_RemovesOnlyInactiveAttrs(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
markdown := strings.Join([]string{
|
|
`<p class="article-editor-image"><img src="https://example.com/active.webp" data-asset-id="12" /></p>`,
|
|
`<p class="article-editor-image"><img src="https://example.com/inactive.webp" data-asset-id="34" /></p>`,
|
|
}, "\n")
|
|
|
|
got := stripInactiveImageAssetIDsFromMarkdown(markdown, map[int64]struct{}{
|
|
12: {},
|
|
})
|
|
|
|
if !strings.Contains(got, `data-asset-id="12"`) {
|
|
t.Fatalf("expected active asset id to remain, got %q", got)
|
|
}
|
|
if strings.Contains(got, `data-asset-id="34"`) {
|
|
t.Fatalf("expected inactive asset id to be removed, got %q", got)
|
|
}
|
|
if !strings.Contains(got, `inactive.webp`) {
|
|
t.Fatalf("expected image markup to remain after stripping asset id, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestStripInactiveImageAssetIDsFromMarkdown_RemovesAllAttrsWhenNoAssetsAreActive(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
markdown := `<p><img src="https://example.com/cover.webp" data-asset-id="88" /></p>`
|
|
|
|
got := stripInactiveImageAssetIDsFromMarkdown(markdown, map[int64]struct{}{})
|
|
|
|
if strings.Contains(got, `data-asset-id="88"`) {
|
|
t.Fatalf("expected inactive asset id to be removed, got %q", got)
|
|
}
|
|
if !strings.Contains(got, `cover.webp`) {
|
|
t.Fatalf("expected image src to remain, got %q", got)
|
|
}
|
|
}
|