package app

import "strings"

import "testing"

func TestArtifactDisplayLines_ExecutionPlan(t *testing.T) {
	plan := `{"goal":"Scaffold a UI","stages":[
		{"id":"explore","role":"implementer","prompt":"Explore the project.\nMore detail.","produces":"exploration_report"},
		{"id":"design_ui","role":"architect","prompt":"Design it.","produces":"ui_design_doc"}]}`
	lines, ok := artifactDisplayLines(plan)
	if !ok {
		t.Fatal("expected plan to be recognized as a structured artifact")
	}
	joined := strings.Join(lines, "\n")
	for _, want := range []string{"Goal", "Scaffold a UI", "Plan · 2 stages", "explore", "implementer", "→ exploration_report", "design_ui"} {
		if !strings.Contains(joined, want) {
			t.Errorf("summary missing %q in:\n%s", want, joined)
		}
	}
	// Multi-line prompts collapse to a single first-line summary, never a raw dump.
	if strings.Contains(joined, "More detail.") {
		t.Errorf("prompt body leaked into summary:\n%s", joined)
	}
}

func TestArtifactDisplayLines_NonJSONFallsBack(t *testing.T) {
	if _, ok := artifactDisplayLines("just some text"); ok {
		t.Error("plain text must not be treated as a structured artifact")
	}
}

func TestArtifactDisplayLines_GenericObject(t *testing.T) {
	lines, ok := artifactDisplayLines(`{"summary":"did the thing","risks":["a","b"]}`)
	if !ok {
		t.Fatal("expected generic JSON object to summarize")
	}
	joined := strings.Join(lines, "\n")
	if !strings.Contains(joined, "summary:") || !strings.Contains(joined, "risks: (2)") {
		t.Errorf("generic object summary wrong:\n%s", joined)
	}
}
