package service

import "testing"

func TestNormalizeSortByRules(t *testing.T) {
	t.Run("field asc and desc", func(t *testing.T) {
		if got := normalizeSortByRules("name", sortField("name")); got != "name ASC" {
			t.Fatalf("unexpected asc sort: %q", got)
		}
		if got := normalizeSortByRules("-name", sortField("name")); got != "name DESC" {
			t.Fatalf("unexpected desc sort: %q", got)
		}
	})

	t.Run("alias and custom expression", func(t *testing.T) {
		got := normalizeSortByRules("sort_order", sortExpr("sort_order ASC, created_at DESC", "sort_order DESC, created_at DESC", "sort_order"))
		if got != "sort_order ASC, created_at DESC" {
			t.Fatalf("unexpected custom sort: %q", got)
		}
		got = normalizeSortByRules("-hospital_name", sortField("hospital_name", "name", "hospital_name"))
		if got != "hospital_name DESC" {
			t.Fatalf("unexpected alias sort: %q", got)
		}
	})

	t.Run("unknown", func(t *testing.T) {
		if got := normalizeSortByRules("unknown", sortField("name")); got != "" {
			t.Fatalf("expected empty for unknown, got %q", got)
		}
	})
}
