summaryrefslogtreecommitdiffhomepage
path: root/packages/tui/internal/components/textarea/textarea_test.go
blob: fb3c5b8bad805fe5a3df90e8d5e75a549ffae84f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package textarea

import (
	"testing"

	"github.com/sst/opencode/internal/attachment"
)

func TestRemoveAttachmentAtCursor_ConvertsToText_WhenCursorAfterAttachment(t *testing.T) {
	m := New()
	m.InsertString("a ")
	att := &attachment.Attachment{ID: "1", Display: "@file.txt"}
	m.InsertAttachment(att)
	m.InsertString(" b")

	// Position cursor immediately after the attachment (index 3: 'a',' ',att,' ', 'b')
	m.SetCursorColumn(3)

	if ok := m.removeAttachmentAtCursor(); !ok {
		t.Fatalf("expected removal to occur")
	}
	got := m.Value()
	want := "a @file.txt b"
	if got != want {
		t.Fatalf("expected %q, got %q", want, got)
	}
}

func TestRemoveAttachmentAtCursor_ConvertsToText_WhenCursorOnAttachment(t *testing.T) {
	m := New()
	m.InsertString("x ")
	att := &attachment.Attachment{ID: "2", Display: "@img.png"}
	m.InsertAttachment(att)
	m.InsertString(" y")

	// Position cursor on the attachment token (index 2: 'x',' ',att,' ', 'y')
	m.SetCursorColumn(2)

	if ok := m.removeAttachmentAtCursor(); !ok {
		t.Fatalf("expected removal to occur")
	}
	got := m.Value()
	want := "x @img.png y"
	if got != want {
		t.Fatalf("expected %q, got %q", want, got)
	}
}

func TestRemoveAttachmentAtCursor_StartOfLine(t *testing.T) {
	m := New()
	att := &attachment.Attachment{ID: "3", Display: "@a.txt"}
	m.InsertAttachment(att)
	m.InsertString(" tail")

	// Position cursor immediately after the attachment at start of line (index 1)
	m.SetCursorColumn(1)
	if ok := m.removeAttachmentAtCursor(); !ok {
		t.Fatalf("expected removal to occur at start of line")
	}
	if got := m.Value(); got != "@a.txt tail" {
		t.Fatalf("unexpected value: %q", got)
	}
}

func TestRemoveAttachmentAtCursor_NoAttachment_NoChange(t *testing.T) {
	m := New()
	m.InsertString("hello world")
	col := m.CursorColumn()
	if ok := m.removeAttachmentAtCursor(); ok {
		t.Fatalf("did not expect removal to occur")
	}
	if m.Value() != "hello world" || m.CursorColumn() != col {
		t.Fatalf("value or cursor unexpectedly changed")
	}
}