package cli

import "testing"

func TestResolvePromptSessionIDUsesVersionedInputIdentity(t *testing.T) {
	got, err := resolvePromptSessionID("", " seriatim/input/transcript ", []byte("{\"entries\":[\"one\"]}\n"))
	if err != nil {
		t.Fatal(err)
	}
	const want = "notarius:v1:e15fefdca48653e73248b4157900547be1fd34250c0138f8d3d1f2e89b43bb25"
	if got != want {
		t.Fatalf("resolved session = %q, want %q", got, want)
	}
}

func TestResolvePromptSessionIDStabilityAndOverride(t *testing.T) {
	rawInput := []byte("same input")
	baseline, err := resolvePromptSessionID("", "input/transcript", rawInput)
	if err != nil {
		t.Fatal(err)
	}
	repeated, err := resolvePromptSessionID("", "input/transcript", rawInput)
	if err != nil {
		t.Fatal(err)
	}
	if baseline != repeated {
		t.Fatalf("resolved sessions = %q and %q, want stable value", baseline, repeated)
	}

	differentModule, err := resolvePromptSessionID("", "input/other", rawInput)
	if err != nil {
		t.Fatal(err)
	}
	if baseline == differentModule {
		t.Fatalf("resolved sessions = %q for distinct input modules", baseline)
	}

	differentInput, err := resolvePromptSessionID("", "input/transcript", []byte("same inpuu"))
	if err != nil {
		t.Fatal(err)
	}
	if baseline == differentInput {
		t.Fatalf("resolved sessions = %q for distinct input bytes", baseline)
	}

	override, err := resolvePromptSessionID(" explicit-session ", "input/other", []byte("different input"))
	if err != nil {
		t.Fatal(err)
	}
	if override != "explicit-session" {
		t.Fatalf("resolved override = %q, want %q", override, "explicit-session")
	}
}

func TestResolvePromptSessionIDRejectsEmptyInputModule(t *testing.T) {
	for _, explicitSessionID := range []string{"", "explicit-session"} {
		if _, err := resolvePromptSessionID(explicitSessionID, " \t", []byte("input")); err == nil {
			t.Fatalf("resolvePromptSessionID(%q) error = nil, want empty module failure", explicitSessionID)
		}
	}
}
