package kiwix

import "testing"

// The cases the 2026-08-09 measurement turned on, plus the ones a topic must
// not damage. Each left column returned a wrong article when it was sent whole.
func TestTopicKeepsTheThingTheQuestionIsAbout(t *testing.T) {
	cases := []struct{ utterance, want string }{
		{"что такое TCP?", "TCP"},
		{"что такое фотосинтез", "фотосинтез"},
		{"кто такой Линус Торвальдс?", "Линус Торвальдс"},
		{"кто написал Войну и мир", "Войну и мир"},
		{"расскажи про битву при Ватерлоо", "битву при Ватерлоо"},
		{"what is photosynthesis", "photosynthesis"},
		// No question word, so there is nothing to drop. The topic is the
		// whole utterance and the search is what it was before.
		{"столица Франции", "столица Франции"},
		{"почему небо голубое", "небо голубое"},
	}
	for _, c := range cases {
		if got := Topic(c.utterance); got != c.want {
			t.Errorf("Topic(%q) = %q, want %q", c.utterance, got, c.want)
		}
	}
}

// A verb only goes when a question word already went. Otherwise "написал
// отчёт" loses the verb that names what he means.
func TestTopicDropsAVerbOnlyBehindAQuestionWord(t *testing.T) {
	if got := Topic("написал отчёт"); got != "написал отчёт" {
		t.Errorf("Topic dropped a leading verb with no question word: %q", got)
	}
}

// Question words alone reduce to nothing, and the caller reads that as "no
// topic" and searches the sentence rather than searching the empty string.
func TestTopicIsEmptyWhenNothingIsLeft(t *testing.T) {
	for _, q := range []string{"что такое?", "кто?", "почему", "???"} {
		if got := Topic(q); got != "" {
			t.Errorf("Topic(%q) = %q, want empty", q, got)
		}
	}
}

func TestTitlePathEscapesAndUnderscores(t *testing.T) {
	got := TitlePath("wikipedia_ru_all_maxi_2026-02", "Чёрная дыра")
	want := "/content/wikipedia_ru_all_maxi_2026-02/A/%D0%A7%D1%91%D1%80%D0%BD%D0%B0%D1%8F_%D0%B4%D1%8B%D1%80%D0%B0"
	if got != want {
		t.Errorf("TitlePath = %q, want %q", got, want)
	}
}

// A ZIM title carries a leading capital and the utterance does not. The spoken
// form is still tried first, so a title that begins lowercase on purpose keeps
// its chance.
func TestTitleCandidatesTryTheSpokenFormFirst(t *testing.T) {
	got := TitleCandidates("фотосинтез")
	if len(got) != 2 || got[0] != "фотосинтез" || got[1] != "Фотосинтез" {
		t.Errorf("TitleCandidates = %q", got)
	}
	if got := TitleCandidates("TCP"); len(got) != 1 || got[0] != "TCP" {
		t.Errorf("an already-capital topic was tried twice: %q", got)
	}
	if got := TitleCandidates(""); got != nil {
		t.Errorf("TitleCandidates(\"\") = %q, want nil", got)
	}
}
