package agentcap

import (
	"testing"
)

func TestEncryptDecrypt(t *testing.T) {
	key := DeriveKey("secret", 1, 2)
	cap, err := Encrypt(key, &Descriptor{
		Version: "1",
		BaseURL: "http://127.0.0.1:8888",
		AppSlug: "demo",
		Auth:    AuthSpec{Type: "bearer_jwt", Header: "Authorization"},
		Resources: []ResourceSpec{{
			Name: "items", Path: "/api/v1/apps/demo/items",
			Methods: []string{"GET", "POST"}, PrimaryKey: "id",
			Fields: []FieldSpec{{Name: "title", Type: "string"}},
		}},
	})
	if err != nil {
		t.Fatal(err)
	}
	if cap[:4] != Prefix {
		t.Fatalf("prefix: %s", cap)
	}
	desc, err := Decrypt(key, cap)
	if err != nil {
		t.Fatal(err)
	}
	if desc.AppSlug != "demo" || len(desc.Resources) != 1 {
		t.Fatalf("%+v", desc)
	}
	if _, err := Decrypt([]byte("bad-key-bad-key-bad-key-bad!!!!"), cap); err == nil {
		t.Fatal("expected decrypt fail")
	}
}
