package codex_test

import (
	"testing"

	"github.com/flothus/tmux-xterm-research/server-go/internal/harness/runtime/tmux/codex"
)

func TestNewPaneConfiguresCodexCLI(t *testing.T) {
	p := codex.NewPane("sock", "sess", "/tmp/codex.log")
	if got := p.CLICmd; len(got) == 0 || got[0] != "codex" {
		t.Errorf("CLICmd = %v, want [codex …]", got)
	}
	if p.PromptRegex == nil {
		t.Error("PromptRegex should be set (turn-done detection)")
	}
	if p.RateLimitRegex == nil {
		t.Error("RateLimitRegex should be set (subscription mode)")
	}
}

func TestCodexPromptRegexMatchesShapes(t *testing.T) {
	p := codex.NewPane("sock", "sess", "/tmp/codex.log")
	cases := []struct {
		s    string
		want bool
	}{
		{"user> ", true},
		{"> ", true},
		{"some prose without a prompt", false},
	}
	for _, c := range cases {
		got := p.PromptRegex.MatchString(c.s + "\n")
		if got != c.want {
			t.Errorf("PromptRegex.Match(%q) = %v, want %v", c.s, got, c.want)
		}
	}
}

func TestCodexRateLimitRegexMatchesCommonPhrases(t *testing.T) {
	p := codex.NewPane("sock", "sess", "/tmp/codex.log")
	cases := []string{
		"Error: rate limit reached, retry after 60s",
		"Too many requests (429)",
		"quota exceeded for this minute",
	}
	for _, s := range cases {
		if !p.RateLimitRegex.MatchString(s) {
			t.Errorf("RateLimitRegex should match %q", s)
		}
	}
}

func TestCodexEstimateTokensIsCalibrated(t *testing.T) {
	got := codex.EstimateTokens("hello world", "the quick brown fox")
	if got.Prompt <= 0 || got.Completion <= 0 {
		t.Errorf("token estimation should produce non-zero counts: %+v", got)
	}
	// Calibration constant is 3.6 (denser tokens than claude's 4 baseline).
	// We just verify it produces a *different* result than chars/4 so the
	// calibration knob isn't accidentally turned off.
	uniform := len("hello world") / 4
	if got.Prompt == uniform {
		t.Errorf("codex estimator should differ from /4 baseline: got %d, uniform %d", got.Prompt, uniform)
	}
}
