package phraser

import (
	"strings"
	"testing"

	"github.com/kami/maven/internal/dialogue"
)

// TestChatUserMessageMarksWhichTurnToAnswer — Vikunja #554. Four prior turns
// were joined with newlines and nothing else, so nothing in the message said
// which line was the question.
func TestChatUserMessageMarksWhichTurnToAnswer(t *testing.T) {
	got := chatUserMessage("как дела", []dialogue.Turn{
		{Text: "кто изобрёл телефон"},
		{Text: "а когда это было"},
	})
	if !strings.Contains(got, "кто изобрёл телефон") {
		t.Error("the prior turns must survive — they are what anaphora reads")
	}
	now := strings.LastIndex(got, "как дела")
	if now < strings.Index(got, "кто изобрёл телефон") {
		t.Error("the current utterance must come last, after the turns it follows")
	}
	if !strings.Contains(got, "Раньше ты говорил") {
		t.Errorf("the prior turns must be labelled as prior: %q", got)
	}
	if strings.Contains(got, " вы ") || strings.Contains(got, "Вы ") {
		t.Errorf("the persona addresses him informally: %q", got)
	}
}

// TestChatUserMessageWithNoHistoryIsJustTheUtterance — the common case must not
// grow a preamble that the model then has to see past.
func TestChatUserMessageWithNoHistoryIsJustTheUtterance(t *testing.T) {
	if got := chatUserMessage("  привет  ", nil); got != "привет" {
		t.Errorf("chatUserMessage = %q, want %q", got, "привет")
	}
	if got := chatUserMessage("привет", []dialogue.Turn{{Text: "   "}}); got != "привет" {
		t.Errorf("a blank prior turn must not label anything: %q", got)
	}
}
