package app

import (
	"strings"
	"testing"

	"github.com/correx/tui-go/internal/protocol"
)

func TestTemplateNarrationAppendsDimLines(t *testing.T) {
	m := NewModel(nil)
	m.width, m.height = 120, 40
	m.theme = NewTheme(SoftBlue)
	sid := "s1"
	m.selectedID = sid
	m.sessionEntered = true
	sess := Session{ID: sid, Status: "ACTIVE"}
	m.sessions = []Session{sess}

	frames := []protocol.ServerMessage{
		{Type: protocol.TypeStageStarted, SessionID: sid, StageID: "validate"},
		{Type: protocol.TypeStageCompleted, SessionID: sid, StageID: "validate"},
		{Type: protocol.TypeStageFailed, SessionID: sid, StageID: "generate", Reason: "schema mismatch"},
		{Type: protocol.TypeSessionCompleted, SessionID: sid},
	}
	for _, f := range frames {
		m.applyServer(f)
	}
	got := m.routerMessages[sid]
	if len(got) != 4 {
		t.Fatalf("want 4 narration entries, got %d: %+v", len(got), got)
	}
	for _, e := range got {
		if e.Role != "narration" {
			t.Fatalf("want role=narration, got %q", e.Role)
		}
	}
	if !strings.Contains(got[0].Content, "validate") || !strings.HasPrefix(got[0].Content, "→") {
		t.Fatalf("stage.started line: %q", got[0].Content)
	}
	if !strings.Contains(got[2].Content, "schema mismatch") {
		t.Fatalf("stage.failed line should carry reason: %q", got[2].Content)
	}
	rows := m.routerRows(80, 20)
	joined := strings.Join(rows, "\n")
	if strings.Contains(joined, "awaiting router response") {
		t.Fatalf("placeholder must be gone once feed has entries")
	}
}
