feat(dingtalk): stabilize multimodal answer workflows
This commit is contained in:
@@ -76,3 +76,302 @@ func TestRewriteMarkdownResourceImagesDropsFailuresAndImagesBeyondLimit(t *testi
|
||||
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<img alt=\"步骤图\" width=\"100\" src=\"" + resource + "\"/>\n" +
|
||||
"重复\n<img src='" + resource + "' class='diagram'>\n后文"
|
||||
|
||||
got := r.Rewrite(context.Background(), input)
|
||||
|
||||
if strings.Contains(got, "resource://") || strings.Contains(strings.ToLower(got), "<img") {
|
||||
t.Fatalf("internal HTML image remained: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "") {
|
||||
t.Fatalf("alt image not normalized: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "") {
|
||||
t.Fatalf("default-alt image not normalized: %s", got)
|
||||
}
|
||||
if fetcher.calls[resource] != 1 || publisher.calls != 1 {
|
||||
t.Fatalf("calls fetch=%v publish=%d", fetcher.calls, publisher.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizePartialRemovesInternalImageNodesAndBareReferences(t *testing.T) {
|
||||
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
|
||||
input := "正文\n\n" +
|
||||
"<img alt='图' src='resource://SecondRefAbCdEfGhIjKlMn'>\n" +
|
||||
"内部引用 resource://BareRefAbCdEfGhIjKlMn"
|
||||
|
||||
got := r.SanitizePartial(input)
|
||||
|
||||
if strings.Contains(got, "resource://") || strings.Contains(strings.ToLower(got), "<img") {
|
||||
t.Fatalf("partial content leaked an internal image: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "正文") || !strings.Contains(got, "内部引用") {
|
||||
t.Fatalf("partial text was lost: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteFailsClosedOnUnquotedHTMLAndBareResourceReferences(t *testing.T) {
|
||||
fetcher := &fakeFetcher{}
|
||||
r := NewRewriter(fetcher, &fakePublisher{}, 5)
|
||||
input := "正文\n<img src=resource://UnsafeRefAbCdEfGhIjKlMn>\n内部引用 resource://BareRefAbCdEfGhIjKlMn"
|
||||
|
||||
got := r.Rewrite(context.Background(), input)
|
||||
|
||||
if strings.Contains(got, "resource://") || strings.Contains(strings.ToLower(got), "<img") {
|
||||
t.Fatalf("final content leaked an internal reference: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "正文") || !strings.Contains(got, "内部引用") {
|
||||
t.Fatalf("final text was lost: %s", got)
|
||||
}
|
||||
if len(fetcher.calls) != 0 {
|
||||
t.Fatalf("unsafe references must not be fetched: %v", fetcher.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteRemovesUnsafeImageLocatorsAndSignedURLs(t *testing.T) {
|
||||
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
|
||||
input := strings.Join([]string{
|
||||
"正文",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
}, "\n")
|
||||
|
||||
got := r.Rewrite(context.Background(), input)
|
||||
|
||||
for _, forbidden := range []string{"local://", "oss://", "127.0.0.1", "x-oss-signature"} {
|
||||
if strings.Contains(strings.ToLower(got), forbidden) {
|
||||
t.Fatalf("final content retained %q: %s", forbidden, got)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(got, "") {
|
||||
t.Fatalf("public HTTPS image was removed: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteNormalizesPublicHTMLImageAsDingTalkMarkdown(t *testing.T) {
|
||||
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
|
||||
input := `<img class="diagram" src="https://example.com/a.png" alt="公网图" width="100">`
|
||||
|
||||
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 := `正文<img alt="占位图" src="data:image/png;base64,iVBORw0KGgo=">后文`
|
||||
|
||||
got := r.SanitizePartial(input)
|
||||
|
||||
if strings.Contains(got, "data:image") || strings.Contains(strings.ToLower(got), "<img") {
|
||||
t.Fatalf("partial content retained a data HTML image: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "正文") || !strings.Contains(got, "后文") {
|
||||
t.Fatalf("surrounding text was lost: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewritePreservesPublicHTTPSImageWithOptionalTitle(t *testing.T) {
|
||||
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
|
||||
input := ``
|
||||
|
||||
if got := r.Rewrite(context.Background(), input); got != input {
|
||||
t.Fatalf("final public HTTPS image=%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizePartialPreservesPublicHTTPSImageWithOptionalTitle(t *testing.T) {
|
||||
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
|
||||
input := ``
|
||||
|
||||
if got := r.SanitizePartial(input); got != input {
|
||||
t.Fatalf("partial public HTTPS image=%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizePartialRemovesIncompleteDataMarkdownImage(t *testing.T) {
|
||||
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
|
||||
input := "正文\n
|
||||
|
||||
if strings.Contains(got, "data:image") || strings.Contains(got, "占位图") {
|
||||
t.Fatalf("partial content retained an incomplete data image: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "正文") {
|
||||
t.Fatalf("surrounding text was lost: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizePartialRemovesIncompleteDataHTMLImage(t *testing.T) {
|
||||
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
|
||||
input := `正文<img alt="占位图" src="data:image/png;base64,iVBORw0K`
|
||||
|
||||
got := r.SanitizePartial(input)
|
||||
|
||||
if strings.Contains(got, "data:image") || strings.Contains(strings.ToLower(got), "<img") {
|
||||
t.Fatalf("partial content retained an incomplete data HTML image: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "正文") {
|
||||
t.Fatalf("surrounding text was lost: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteRemovesBareDataImageReference(t *testing.T) {
|
||||
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
|
||||
input := "正文 data:image/png;base64,iVBORw0KGgo= 后文"
|
||||
|
||||
got := r.Rewrite(context.Background(), input)
|
||||
|
||||
if strings.Contains(got, "data:image") {
|
||||
t.Fatalf("final content retained a bare data image: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "正文") || !strings.Contains(got, "后文") {
|
||||
t.Fatalf("surrounding text was lost: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteRemovesIncompleteDataMarkdownImage(t *testing.T) {
|
||||
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
|
||||
input := "正文\n, input)
|
||||
|
||||
if strings.Contains(got, "data:image") || strings.Contains(got, "占位图") || strings.Contains(got, " 后文`
|
||||
|
||||
got := r.Rewrite(context.Background(), input)
|
||||
|
||||
if strings.Contains(got, "data:image") || strings.Contains(got, "占位图") {
|
||||
t.Fatalf("fail-closed rewrite retained a data image: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "正文") || !strings.Contains(got, "后文") {
|
||||
t.Fatalf("surrounding text was lost: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteDowngradesPDFImageToDocumentLink(t *testing.T) {
|
||||
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
|
||||
input := ``
|
||||
want := `[机械臂示教与应用课件](https://sales-edu.example/06%20robot-teaching.PDF?tenant=demo "课件")`
|
||||
|
||||
if got := r.Rewrite(context.Background(), input); got != want {
|
||||
t.Fatalf("PDF image rewrite=%q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFinalAndPartialDowngradeKnownDocumentImages(t *testing.T) {
|
||||
r := NewRewriter(&fakeFetcher{}, &fakePublisher{}, 5)
|
||||
for _, extension := range []string{
|
||||
".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx",
|
||||
".csv", ".txt", ".zip", ".rar", ".7z",
|
||||
} {
|
||||
t.Run(extension, func(t *testing.T) {
|
||||
url := "https://docs.example/manual" + extension + "?tenant=demo"
|
||||
input := ""
|
||||
want := "[资料](" + url + ")"
|
||||
|
||||
if got := r.Rewrite(context.Background(), input); got != want {
|
||||
t.Fatalf("final rewrite=%q, want %q", got, want)
|
||||
}
|
||||
if got := r.SanitizePartial(input); got != want {
|
||||
t.Fatalf("partial rewrite=%q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
html := `<img src="https://docs.example/manual.xlsx" alt="参数表">`
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user