package profile

import (
	"os"
	"path/filepath"
	"testing"
)

func TestVaultRoundTrip(t *testing.T) {
	value, err := Generate("pseudonym", "reader@example.org")
	if err != nil {
		t.Fatal(err)
	}
	path := filepath.Join(t.TempDir(), "profile.json.age")
	if err := Save(path, value, "correct horse battery staple"); err != nil {
		t.Fatal(err)
	}
	loaded, err := Load(path, "correct horse battery staple")
	if err != nil {
		t.Fatal(err)
	}
	if loaded.Username != value.Username || loaded.PublicKey != value.PublicKey || loaded.PrivateKey != value.PrivateKey {
		t.Fatal("profile vault round-trip changed identity")
	}
	if _, err := Load(path, "wrong password"); err == nil {
		t.Fatal("wrong password unlocked profile vault")
	}
	data, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	if len(data) == 0 || string(data) == "" {
		t.Fatal("empty profile vault")
	}
}

func TestPasswordMinimum(t *testing.T) {
	value, err := Generate("pseudonym", "reader@example.org")
	if err != nil {
		t.Fatal(err)
	}
	if err := Save(filepath.Join(t.TempDir(), "profile.json.age"), value, "short"); err == nil {
		t.Fatal("short profile password accepted")
	}
}
