package subscription

import (
	"encoding/base64"
	"strings"
	"testing"
)

func TestParseBodyPlain(t *testing.T) {
	body := `# comment
hysteria2://pass@ex.com:443/?sni=ex.com&obfs=salamander&obfs-password=x#EU
naive+https://u:p@n.example.com#naive1
`
	items, err := ParseBody(body)
	if err != nil {
		t.Fatal(err)
	}
	if len(items) != 2 {
		t.Fatalf("got %d items", len(items))
	}
}

func TestParseBodyBase64(t *testing.T) {
	plain := "hysteria2://secret@host.example:443/?sni=host.example#HY\nhttps://user:pass@naive.example.com#NV\n"
	body := base64.StdEncoding.EncodeToString([]byte(plain))
	items, err := ParseBody(body)
	if err != nil {
		t.Fatal(err)
	}
	if len(items) != 2 {
		t.Fatalf("got %d items: %+v", len(items), items)
	}
}

func TestParseBodyClashYAML(t *testing.T) {
	body := `
proxies:
  - name: "DE HY2"
    type: hysteria2
    server: de.example.com
    port: 443
    password: secret
    sni: de.example.com
    obfs: salamander
    obfs-password: obfspass
`
	items, err := ParseBody(body)
	if err != nil {
		t.Fatal(err)
	}
	if len(items) != 1 {
		t.Fatalf("got %d", len(items))
	}
	if items[0].Protocol != "hysteria2" {
		t.Fatalf("proto %s", items[0].Protocol)
	}
	if !strings.Contains(items[0].URI, "de.example.com:443") {
		t.Fatalf("uri %s", items[0].URI)
	}
}
