package crm

import (
	"context"
	"encoding/json"
	"errors"
	"net/http"
	"net/http/httptest"
	"testing"

	"apps/backend/internal/domain"
	crmDomain "apps/backend/internal/module/crm/domain"
	"apps/backend/internal/response"
)

// notReady envelope 契約：殘留未實作 capability 須 501，不可 102000 空成功。
func TestNotReadyEnvelope(t *testing.T) {
	err := notReady("example.capability")
	if !errors.Is(err, crmDomain.ErrNotReady) {
		t.Fatalf("expected ErrNotReady, got %v", err)
	}
	rec := httptest.NewRecorder()
	response.Write(context.Background(), rec, nil, err)
	if rec.Code != http.StatusNotImplemented {
		t.Fatalf("expected HTTP 501, got %d", rec.Code)
	}
	var env response.Envelope
	if decErr := json.NewDecoder(rec.Body).Decode(&env); decErr != nil {
		t.Fatalf("decode envelope: %v", decErr)
	}
	if env.Code == domain.SuccessCode {
		t.Fatal("not-ready must not use success code")
	}
}
