76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package tui
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPickImageMime(t *testing.T) {
|
|
cases := []struct {
|
|
types string
|
|
wantMime string
|
|
wantExt string
|
|
}{
|
|
{"text/plain\nimage/png\nTEXT", "image/png", "png"},
|
|
{"image/jpeg", "image/jpeg", "jpg"},
|
|
{"image/gif\nimage/png", "image/png", "png"}, // png preferred
|
|
{"text/plain;charset=utf-8\nTEXT", "", ""},
|
|
{"image/webp", "image/webp", "webp"},
|
|
}
|
|
for _, c := range cases {
|
|
mime, ext := pickImageMime(c.types)
|
|
if mime != c.wantMime || ext != c.wantExt {
|
|
t.Errorf("pickImageMime(%q) = (%q,%q), want (%q,%q)", c.types, mime, ext, c.wantMime, c.wantExt)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestImagePastedMsg_InsertsToken(t *testing.T) {
|
|
m := makeLoadedModel(nil, 0, 0)
|
|
m, _ = sendKey(m, "n")
|
|
|
|
next, _ := m.Update(imagePastedMsg{path: "/tmp/spx-inbox/x/1.png", index: 1})
|
|
m = next.(Model)
|
|
if len(m.createImages) != 1 || m.createImages[0] != "/tmp/spx-inbox/x/1.png" {
|
|
t.Fatalf("createImages = %v, want one path", m.createImages)
|
|
}
|
|
if !strings.Contains(m.createArea.Value(), "[Image #1]") {
|
|
t.Errorf("textarea value %q missing [Image #1]", m.createArea.Value())
|
|
}
|
|
|
|
next2, _ := m.Update(imagePastedMsg{path: "/tmp/spx-inbox/x/2.png", index: 2})
|
|
m = next2.(Model)
|
|
if len(m.createImages) != 2 {
|
|
t.Errorf("createImages len = %d, want 2", len(m.createImages))
|
|
}
|
|
if !strings.Contains(m.createArea.Value(), "[Image #2]") {
|
|
t.Errorf("textarea value %q missing [Image #2]", m.createArea.Value())
|
|
}
|
|
}
|
|
|
|
func TestImagePastedMsg_Error_NoImage(t *testing.T) {
|
|
m := makeLoadedModel(nil, 0, 0)
|
|
m, _ = sendKey(m, "n")
|
|
|
|
next, _ := m.Update(imagePastedMsg{err: errNoClipboardImage})
|
|
m = next.(Model)
|
|
if len(m.createImages) != 0 {
|
|
t.Errorf("createImages should stay empty on error, got %v", m.createImages)
|
|
}
|
|
if !strings.Contains(m.statusMsg, "剪贴板没有图片") {
|
|
t.Errorf("statusMsg = %q, want clipboard-empty message", m.statusMsg)
|
|
}
|
|
}
|
|
|
|
func TestCtrlV_InCreate_DispatchesCmd(t *testing.T) {
|
|
m := makeLoadedModel(nil, 0, 0)
|
|
m, _ = sendKey(m, "n")
|
|
m2, cmd := sendKey(m, "ctrl+v")
|
|
if m2.state != stateCreate {
|
|
t.Errorf("state = %v, want stateCreate after ctrl+v", m2.state)
|
|
}
|
|
if cmd == nil {
|
|
t.Error("ctrl+v should dispatch a paste command")
|
|
}
|
|
}
|