package app

import "testing"

func TestAtTokenBoundary(t *testing.T) {
	cases := []struct {
		buf  string
		cur  int
		want bool
	}{
		{"", 0, true},       // start of empty line
		{"see ", 4, true},   // just after a space
		{"email", 5, false}, // mid-word (e.g. user@host)
		{"a b", 2, true},    // after the space
		{"a b", 3, false},   // after 'b'
	}
	for _, c := range cases {
		m := NewModel(nil)
		m.inputBuffer = c.buf
		m.inputCursor = c.cur
		if got := m.atTokenBoundary(); got != c.want {
			t.Errorf("atTokenBoundary(%q,@%d) = %v, want %v", c.buf, c.cur, got, c.want)
		}
	}
}

// `@` consumes itself to open the picker; selecting a file inserts `@path ` at the cursor.
func TestInsertFileRef(t *testing.T) {
	m := NewModel(nil)
	m.inputBuffer = "look at "
	m.inputCursor = len(m.inputBuffer)

	m.insertFileRef("src/main.go")

	if want := "look at @src/main.go "; m.inputBuffer != want {
		t.Fatalf("buffer = %q, want %q", m.inputBuffer, want)
	}
	if m.inputCursor != len(m.inputBuffer) {
		t.Fatalf("cursor = %d, want %d", m.inputCursor, len(m.inputBuffer))
	}
}

func TestFilteredFiles(t *testing.T) {
	m := NewModel(nil)
	m.files = []string{"cmd/main.go", "internal/app/view.go", "README.md", "internal/app/model.go"}

	m.fileFilter = ""
	if len(m.filteredFiles()) != 4 {
		t.Fatalf("empty filter should pass all 4, got %d", len(m.filteredFiles()))
	}

	m.fileFilter = "app/"
	got := m.filteredFiles()
	if len(got) != 2 {
		t.Fatalf("filter %q matched %d, want 2 (%v)", m.fileFilter, len(got), got)
	}

	m.fileFilter = "README"
	if got := m.filteredFiles(); len(got) != 1 || got[0] != "README.md" {
		t.Fatalf("filter README = %v, want [README.md]", got)
	}
}
