package usecase

import (
	"testing"

	domusecase "haixun-backend/internal/model/threads_account/domain/usecase"
)

func TestApplyConnectionPatchSearchSourceInFormalMode(t *testing.T) {
	current := formalConnectionPrefs()
	patch := domusecase.ConnectionPrefsPatch{
		SearchSourceMode: strPtr("threads_crawler"),
	}
	next := applyConnectionPatch(current, patch)
	if next.SearchSourceMode != "threads_brave" {
		t.Fatalf("expected crawler mode stripped to threads_brave, got %s", next.SearchSourceMode)
	}
	if next.DevMode {
		t.Fatal("expected dev_mode to stay disabled")
	}
}

func TestNormalizeConnectionPrefsKeepsScrapeReplies(t *testing.T) {
	prefs := formalConnectionPrefs()
	prefs.ScrapeReplies = true
	next := normalizeConnectionPrefs(prefs)
	if !next.ScrapeReplies {
		t.Fatal("expected scrape_replies to persist")
	}
	if next.PublishViaApi != true || !next.SearchViaApi {
		t.Fatal("expected formal API routing")
	}
}

func TestApplyConnectionPatchIgnoresLegacyDevMode(t *testing.T) {
	current := formalConnectionPrefs()
	enabled := true
	next := applyConnectionPatch(current, domusecase.ConnectionPrefsPatch{
		DevMode: &enabled,
	})
	if next.DevMode {
		t.Fatal("account dev_mode should not enable crawler routing")
	}
}

func strPtr(v string) *string {
	return &v
}
