package service

import "testing"

func TestEncodeEmailJob(t *testing.T) {
	data, err := EncodeEmailJob(EmailJob{
		To:      "user@example.com",
		Subject: "Welcome",
		Body:    "Hello",
	})
	if err != nil {
		t.Fatalf("encode failed: %v", err)
	}
	if len(data) == 0 {
		t.Fatalf("expected encoded payload")
	}
}

func TestDecodeEmailJob(t *testing.T) {
	t.Run("success", func(t *testing.T) {
		job, err := DecodeEmailJob([]byte(`{"to":"user@example.com","subject":"Welcome","body":"Hello"}`))
		if err != nil {
			t.Fatalf("decode failed: %v", err)
		}
		if job == nil {
			t.Fatalf("expected email job")
		}
		if job.To != "user@example.com" || job.Subject != "Welcome" || job.TextBody != "Hello" {
			t.Fatalf("unexpected decoded value: %+v", job)
		}
	})

	t.Run("invalid json", func(t *testing.T) {
		if _, err := DecodeEmailJob([]byte(`{"to":`)); err == nil {
			t.Fatalf("expected decode error")
		}
	})
}
