Files
2026-07-13 13:00:08 +08:00

95 lines
2.7 KiB
Go

package cli
import (
"os"
"path/filepath"
"reflect"
"testing"
)
func TestExpandPastedBlocksImage(t *testing.T) {
m := &chatTUI{pastedBlocks: []pastedBlock{
{label: "[image #1]", text: "@.reasonix/attachments/clipboard-20260601-010203.000001.png", image: true},
{label: "[Pasted text #2 · 3 lines]", text: "a\nb\nc"},
}}
got := m.expandPastedBlocks("look at [image #1] and [Pasted text #2 · 3 lines]")
want := "look at @.reasonix/attachments/clipboard-20260601-010203.000001.png and " +
renderFoldedPasteBlock(m.pastedBlocks[1])
if got != want {
t.Fatalf("expandPastedBlocks = %q, want %q", got, want)
}
if displayLineForImageRefs(got) != "look at [image1] and "+renderFoldedPasteBlock(m.pastedBlocks[1]) {
t.Fatalf("image ref should collapse to a label in the bubble: %q", displayLineForImageRefs(got))
}
}
func TestDisplayLineForImageRefs(t *testing.T) {
got := displayLineForImageRefs("describe @.reasonix/attachments/clipboard-20260601-010203.000001.png @.reasonix/attachments/clipboard-20260601-010204.000002-000002.jpg")
want := "describe [image1] [image2]"
if got != want {
t.Fatalf("displayLineForImageRefs = %q, want %q", got, want)
}
}
func TestPastedFileRef(t *testing.T) {
dir := t.TempDir()
pdf := filepath.Join(dir, "report.pdf")
if err := os.WriteFile(pdf, []byte("%PDF-1.4 fake"), 0o644); err != nil {
t.Fatal(err)
}
if got, ok := pastedFileRef(pdf); !ok || got != "@"+filepath.Clean(pdf) {
t.Fatalf("pastedFileRef(existing pdf) = %q, %v", got, ok)
}
if got, ok := pastedFileRef(`"` + pdf + `"`); !ok || got != "@"+filepath.Clean(pdf) {
t.Fatalf("pastedFileRef(quoted pdf) = %q, %v", got, ok)
}
if _, ok := pastedFileRef("just-a-word"); ok {
t.Fatal("a bare word with no separator must not be a file ref")
}
if _, ok := pastedFileRef(filepath.Join(dir, "missing.pdf")); ok {
t.Fatal("a non-existent path must not be a file ref")
}
if _, ok := pastedFileRef(dir); ok {
t.Fatal("a directory must not be a file ref")
}
}
func TestPastedImageSources(t *testing.T) {
cases := []struct {
name string
text string
want []string
ok bool
}{
{
name: "data URL",
text: "data:image/png;base64,aaa",
want: []string{"data:image/png;base64,aaa"},
ok: true,
},
{
name: "markdown images",
text: "![a](/tmp/a.png)\n![b](file:///tmp/b.jpg)",
want: []string{"/tmp/a.png", "file:///tmp/b.jpg"},
ok: true,
},
{
name: "plain text",
text: "hello /tmp/a.png",
ok: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, ok := pastedImageSources(c.text)
if ok != c.ok {
t.Fatalf("ok = %v, want %v", ok, c.ok)
}
if !reflect.DeepEqual(got, c.want) {
t.Fatalf("sources = %v, want %v", got, c.want)
}
})
}
}