package outboundimage
import (
"context"
"errors"
"strings"
"testing"
)
type fakeFetcher struct {
calls map[string]int
fail map[string]bool
}
func (f *fakeFetcher) Fetch(_ context.Context, ref string) ([]byte, string, error) {
if f.calls == nil {
f.calls = map[string]int{}
}
f.calls[ref]++
if f.fail[ref] {
return nil, "", errors.New("fetch failed")
}
return []byte("image:" + ref), "image/png", nil
}
type fakePublisher struct {
calls int
}
func (p *fakePublisher) Publish(_ context.Context, data []byte, contentType string) (string, error) {
p.calls++
if contentType != "image/png" || len(data) == 0 {
return "", errors.New("bad image")
}
return "https://img.example/" + string(rune('0'+p.calls)) + ".png?sig=short", nil
}
func TestRewriteMarkdownResourceImagesDeduplicatesAndPreservesOtherURLs(t *testing.T) {
fetcher := &fakeFetcher{}
publisher := &fakePublisher{}
r := NewRewriter(fetcher, publisher, 5)
input := "步骤一\n\n重复\n\n外部\n"
got := r.Rewrite(context.Background(), input)
if strings.Contains(got, "resource://") {
t.Fatalf("resource reference remained: %s", got)
}
if strings.Count(got, "https://img.example/1.png?sig=short") != 2 {
t.Fatalf("deduplicated signed URL count mismatch: %s", got)
}
if !strings.Contains(got, "") {
t.Fatalf("ordinary HTTPS image changed: %s", got)
}
if fetcher.calls["resource://AbCdEfGhIjKlMnOpQrStUv"] != 1 || publisher.calls != 1 {
t.Fatalf("calls fetch=%v publish=%d", fetcher.calls, publisher.calls)
}
}
func TestRewriteMarkdownResourceImagesDropsFailuresAndImagesBeyondLimit(t *testing.T) {
failed := "resource://FailedRefAbCdEfGhIjKlMn"
fetcher := &fakeFetcher{fail: map[string]bool{failed: true}}
publisher := &fakePublisher{}
r := NewRewriter(fetcher, publisher, 3)
input := "保留正文\n\n\n\n"
got := r.Rewrite(context.Background(), input)
if !strings.Contains(got, "保留正文") {
t.Fatalf("text was lost: %s", got)
}
if strings.Contains(got, "resource://") || strings.Contains(got, "坏图") || strings.Contains(got, "![三]") {
t.Fatalf("broken or over-limit image remained: %s", got)
}
if publisher.calls != 2 {
t.Fatalf("publisher calls=%d, want 2", publisher.calls)
}
}
func TestRewriteHTMLResourceImagesAsDingTalkMarkdown(t *testing.T) {
resource := "resource://AbCdEfGhIjKlMnOpQrStUv"
fetcher := &fakeFetcher{}
publisher := &fakePublisher{}
r := NewRewriter(fetcher, publisher, 5)
input := "前文\n
\n" +
"重复\n
\n后文"
got := r.Rewrite(context.Background(), input)
if strings.Contains(got, "resource://") || strings.Contains(strings.ToLower(got), "
\n" +
"内部引用 resource://BareRefAbCdEfGhIjKlMn"
got := r.SanitizePartial(input)
if strings.Contains(got, "resource://") || strings.Contains(strings.ToLower(got), "
`
got := r.Rewrite(context.Background(), input)
if got != "" {
t.Fatalf("normalized public HTML image=%q", got)
}
}
func TestRewriteRemovesDataImageWithOptionalTitle(t *testing.T) {
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
input := "正文\n\n后文"
got := r.Rewrite(context.Background(), input)
if strings.Contains(got, "data:image") || strings.Contains(got, "占位图") {
t.Fatalf("final content retained a data image: %s", got)
}
if !strings.Contains(got, "正文") || !strings.Contains(got, "后文") {
t.Fatalf("surrounding text was lost: %s", got)
}
}
func TestSanitizePartialRemovesDataImageWithOptionalTitle(t *testing.T) {
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
input := "正文\n\n后文"
got := r.SanitizePartial(input)
if strings.Contains(got, "data:image") || strings.Contains(got, "占位图") {
t.Fatalf("partial content retained a data image: %s", got)
}
if !strings.Contains(got, "正文") || !strings.Contains(got, "后文") {
t.Fatalf("surrounding text was lost: %s", got)
}
}
func TestRewritePublishesResourceImageWithOptionalTitle(t *testing.T) {
resource := "resource://AbCdEfGhIjKlMnOpQrStUv"
fetcher := &fakeFetcher{}
publisher := &fakePublisher{}
r := NewRewriter(fetcher, publisher, 5)
input := ""
got := r.Rewrite(context.Background(), input)
if got != "" {
t.Fatalf("resource image with title=%q", got)
}
if fetcher.calls[resource] != 1 || publisher.calls != 1 {
t.Fatalf("calls fetch=%v publish=%d", fetcher.calls, publisher.calls)
}
}
func TestSanitizePartialRemovesDataHTMLImage(t *testing.T) {
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
input := `正文
后文`
got := r.SanitizePartial(input)
if strings.Contains(got, "data:image") || strings.Contains(strings.ToLower(got), "
`
if got, want := r.Rewrite(context.Background(), html), `[参数表](https://docs.example/manual.xlsx)`; got != want {
t.Fatalf("HTML document image rewrite=%q, want %q", got, want)
}
if got, want := r.SanitizePartial(html), `[参数表](https://docs.example/manual.xlsx)`; got != want {
t.Fatalf("partial HTML document image rewrite=%q, want %q", got, want)
}
}
func TestDocumentImageDowngradePreservesImageAndLinkContracts(t *testing.T) {
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
input := strings.Join([]string{
``,
`[已有PDF链接](https://docs.example/manual.pdf)`,
``,
}, "\n")
got := r.Rewrite(context.Background(), input)
if !strings.Contains(got, ``) {
t.Fatalf("extensionless stable image changed: %s", got)
}
if !strings.Contains(got, `[已有PDF链接](https://docs.example/manual.pdf)`) {
t.Fatalf("existing document link changed: %s", got)
}
if strings.Contains(got, "127.0.0.1") || strings.Contains(got, "危险PDF") {
t.Fatalf("unsafe document image became a link: %s", got)
}
}