package main

import (
	"path/filepath"
	"testing"
)

func TestGenerateAndLoadRegistration(t *testing.T) {
	path := filepath.Join(t.TempDir(), "registration.yaml")

	if err := GenerateRegistration(path, "http://ai-bot:8009", "ai", "vojo.chat"); err != nil {
		t.Fatalf("generate: %v", err)
	}
	reg, err := LoadRegistration(path)
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if reg.ID != "ai-bot" || reg.URL != "http://ai-bot:8009" || reg.SenderLocalpart != "ai" {
		t.Fatalf("unexpected registration: %+v", reg)
	}
	if len(reg.ASToken) != 64 || len(reg.HSToken) != 64 {
		t.Fatalf("tokens should be 64 hex chars, got %d/%d", len(reg.ASToken), len(reg.HSToken))
	}
	if reg.ASToken == reg.HSToken {
		t.Fatalf("as_token and hs_token must differ")
	}
	if len(reg.Namespaces.Users) != 1 || reg.Namespaces.Users[0].Regex != `@ai:vojo\.chat` || !reg.Namespaces.Users[0].Exclusive {
		t.Fatalf("unexpected user namespace: %+v", reg.Namespaces.Users)
	}

	// Refuse to overwrite (regenerating would rotate tokens and break Synapse).
	if err := GenerateRegistration(path, "http://x", "ai", "vojo.chat"); err == nil {
		t.Fatalf("expected refuse-overwrite error")
	}
}
