package service

import (
	"strings"
	"testing"

	"hyapp/pkg/giftlimits"
)

func TestGiftTargetCommandIDBoundsMaxLengthParentAndStaysDeterministic(t *testing.T) {
	parent := strings.Repeat("p", giftlimits.MaxCommandIDBytes)
	first := giftTargetCommandID(parent, 9001, 2)
	retry := giftTargetCommandID(parent, 9001, 2)
	otherTarget := giftTargetCommandID(parent, 9002, 2)
	if first != retry {
		t.Fatalf("same parent/target must derive a stable child command: %q != %q", first, retry)
	}
	if len(first) > giftlimits.MaxCommandIDBytes {
		t.Fatalf("derived child command has %d bytes, max %d", len(first), giftlimits.MaxCommandIDBytes)
	}
	if first == otherTarget {
		t.Fatalf("different targets must not share a child command: %q", first)
	}
	if !strings.HasPrefix(first, "gift-target:v2:") {
		t.Fatalf("overflowing legacy key must use the bounded v2 derivation: %q", first)
	}
}

func TestGiftTargetCommandIDPreservesCompatibleKeys(t *testing.T) {
	if got := giftTargetCommandID("existing-command", 42, 1); got != "existing-command" {
		t.Fatalf("single target command changed: %q", got)
	}
	if got := giftTargetCommandID("existing-command", 42, 2); got != "existing-command:target:42" {
		t.Fatalf("existing bounded multi-target command changed: %q", got)
	}
}
