textarea_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package textarea
  2. import (
  3. "testing"
  4. "github.com/sst/opencode/internal/attachment"
  5. )
  6. func TestRemoveAttachmentAtCursor_ConvertsToText_WhenCursorAfterAttachment(t *testing.T) {
  7. m := New()
  8. m.InsertString("a ")
  9. att := &attachment.Attachment{ID: "1", Display: "@file.txt"}
  10. m.InsertAttachment(att)
  11. m.InsertString(" b")
  12. // Position cursor immediately after the attachment (index 3: 'a',' ',att,' ', 'b')
  13. m.SetCursorColumn(3)
  14. if ok := m.removeAttachmentAtCursor(); !ok {
  15. t.Fatalf("expected removal to occur")
  16. }
  17. got := m.Value()
  18. want := "a @file.txt b"
  19. if got != want {
  20. t.Fatalf("expected %q, got %q", want, got)
  21. }
  22. }
  23. func TestRemoveAttachmentAtCursor_ConvertsToText_WhenCursorOnAttachment(t *testing.T) {
  24. m := New()
  25. m.InsertString("x ")
  26. att := &attachment.Attachment{ID: "2", Display: "@img.png"}
  27. m.InsertAttachment(att)
  28. m.InsertString(" y")
  29. // Position cursor on the attachment token (index 2: 'x',' ',att,' ', 'y')
  30. m.SetCursorColumn(2)
  31. if ok := m.removeAttachmentAtCursor(); !ok {
  32. t.Fatalf("expected removal to occur")
  33. }
  34. got := m.Value()
  35. want := "x @img.png y"
  36. if got != want {
  37. t.Fatalf("expected %q, got %q", want, got)
  38. }
  39. }
  40. func TestRemoveAttachmentAtCursor_StartOfLine(t *testing.T) {
  41. m := New()
  42. att := &attachment.Attachment{ID: "3", Display: "@a.txt"}
  43. m.InsertAttachment(att)
  44. m.InsertString(" tail")
  45. // Position cursor immediately after the attachment at start of line (index 1)
  46. m.SetCursorColumn(1)
  47. if ok := m.removeAttachmentAtCursor(); !ok {
  48. t.Fatalf("expected removal to occur at start of line")
  49. }
  50. if got := m.Value(); got != "@a.txt tail" {
  51. t.Fatalf("unexpected value: %q", got)
  52. }
  53. }
  54. func TestRemoveAttachmentAtCursor_NoAttachment_NoChange(t *testing.T) {
  55. m := New()
  56. m.InsertString("hello world")
  57. col := m.CursorColumn()
  58. if ok := m.removeAttachmentAtCursor(); ok {
  59. t.Fatalf("did not expect removal to occur")
  60. }
  61. if m.Value() != "hello world" || m.CursorColumn() != col {
  62. t.Fatalf("value or cursor unexpectedly changed")
  63. }
  64. }