package main

import (
	"strings"
	"testing"

	"fn-registry/registry"
)

func TestRenderFunctionMarkdown(t *testing.T) {
	f := &registry.Function{
		ID:          "filter_slice_go_core",
		Name:        "filter_slice",
		Kind:        "function",
		Lang:        "go",
		Domain:      "core",
		Purity:      "pure",
		Version:     "1.0.0",
		Signature:   "func FilterSlice[T any](xs []T, pred func(T) bool) []T",
		Description: "Filtra un slice.",
		Tags:        []string{"slice", "functional"},
		Code:        "func FilterSlice[T any](xs []T, pred func(T) bool) []T { return nil }",
		FilePath:    "functions/core/filter_slice.go",
	}
	out := renderFunctionMarkdown(f)
	wants := []string{
		"# filter_slice_go_core",
		"- name: filter_slice",
		"- purity: pure",
		"- signature: `func FilterSlice",
		"## code\n\n```go",
		"Filtra un slice.",
	}
	for _, w := range wants {
		if !strings.Contains(out, w) {
			t.Errorf("markdown missing %q\n---\n%s", w, out)
		}
	}
}

func TestLangFence(t *testing.T) {
	cases := map[string]string{"go": "go", "py": "python", "ts": "typescript", "bash": "bash", "ps": "powershell", "cpp": "cpp"}
	for in, want := range cases {
		if got := langFence(in); got != want {
			t.Errorf("langFence(%q) = %q, want %q", in, got, want)
		}
	}
}

func TestTruncate(t *testing.T) {
	if got := truncate("abc", 10); got != "abc" {
		t.Errorf("no-trunc: %q", got)
	}
	if got := truncate("abcdefghij", 5); !strings.HasPrefix(got, "abcde") || !strings.Contains(got, "truncated") {
		t.Errorf("trunc: %q", got)
	}
}
