package roadmapsync

import (
	"strings"
	"testing"
)

const sampleMilestone = `# Milestone Work Item Creation Sync

## 목표

- Plane 티켓을 develop Milestone과 연결한다.

## 범위

- Plane 본문/제목 갱신과 Todo 이동.

## Provider identity

- provider: plane
- work item id: NOMAD-13
`

func TestFormatPlaneProjectionTitleUsesSlugPrefix(t *testing.T) {
	proj := FormatPlaneProjection(
		"agent-roadmap/phase/agent-ops-mcp-control-plane/milestones/milestone-work-item-creation-sync.md",
		sampleMilestone,
	)
	want := "[milestone-work-item-creation-sync] Milestone Work Item Creation Sync"
	if proj.Title != want {
		t.Fatalf("title: got %q want %q", proj.Title, want)
	}
}

func TestFormatPlaneProjectionBodyIncludesGoalAndIsEscaped(t *testing.T) {
	proj := FormatPlaneProjection(
		"agent-roadmap/phase/p/milestones/m.md",
		"# T\n\n## 목표\n\n- 목표 <b>raw</b> 본문\n",
	)
	if !strings.Contains(proj.BodyHTML, "목표") {
		t.Errorf("body should include goal section: %q", proj.BodyHTML)
	}
	// Raw markup in the Milestone text must be escaped, not rendered live.
	if strings.Contains(proj.BodyHTML, "<b>raw</b>") {
		t.Errorf("body must escape untrusted markup: %q", proj.BodyHTML)
	}
	if !strings.Contains(proj.BodyHTML, "&lt;b&gt;raw&lt;/b&gt;") {
		t.Errorf("body should contain escaped markup: %q", proj.BodyHTML)
	}
}

func TestFormatPlaneProjectionExcludesProviderIdentityBlock(t *testing.T) {
	proj := FormatPlaneProjection("agent-roadmap/phase/p/milestones/m.md", sampleMilestone)
	// Provider/work item id metadata is not a body section, so it must not leak
	// into the Plane body.
	if strings.Contains(proj.BodyHTML, "NOMAD-13") || strings.Contains(proj.BodyHTML, "provider") {
		t.Errorf("body must not include provider identity metadata: %q", proj.BodyHTML)
	}
}

func TestMilestoneSlug(t *testing.T) {
	cases := map[string]string{
		"agent-roadmap/phase/p/milestones/m-foo.md": "m-foo",
		"m-foo.md":            "m-foo",
		"":                    "",
		"agent-roadmap/x.txt": "",
	}
	for in, want := range cases {
		if got := MilestoneSlug(in); got != want {
			t.Errorf("MilestoneSlug(%q): got %q want %q", in, got, want)
		}
	}
}
