package internal_test

// polling_jitter_test.go — Tests for jitter capping at polling interval.
//
// F-B2-5 FIXED: Jitter is now capped at pollingInterval/2.
//   Rapid mode (5s) gets 0-2s jitter, standard (300s) gets 0-30s.

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestJitterExceedsRapidModeInterval(t *testing.T) {
	// POST-FIX: Fixed 30s jitter no longer applied to rapid mode.
	mainPath := filepath.Join("..", "cmd", "agent", "main.go")
	content, err := os.ReadFile(mainPath)
	if err != nil {
		t.Fatalf("failed to read agent main.go: %v", err)
	}

	src := string(content)

	// The old fixed jitter should be replaced with proportional jitter
	if strings.Contains(src, "rand.Intn(30)") {
		t.Error("[ERROR] [agent] [polling] F-B2-5 NOT FIXED: fixed 30s jitter still present")
	}

	if !strings.Contains(src, "pollingInterval / 2") && !strings.Contains(src, "pollingInterval/2") {
		t.Error("[ERROR] [agent] [polling] expected jitter capped at pollingInterval/2")
	}

	t.Log("[INFO] [agent] [polling] F-B2-5 FIXED: jitter capped at polling interval")
}

func TestJitterDoesNotExceedPollingInterval(t *testing.T) {
	mainPath := filepath.Join("..", "cmd", "agent", "main.go")
	content, err := os.ReadFile(mainPath)
	if err != nil {
		t.Fatalf("failed to read agent main.go: %v", err)
	}

	src := string(content)

	// Must have proportional jitter calculation
	hasProportionalJitter := strings.Contains(src, "pollingInterval / 2") ||
		strings.Contains(src, "maxJitter")

	if !hasProportionalJitter {
		t.Errorf("[ERROR] [agent] [polling] jitter is not proportional to polling interval.\n" +
			"F-B2-5: jitter must be capped at pollingInterval/2.")
	}

	t.Log("[INFO] [agent] [polling] F-B2-5 FIXED: jitter proportional to interval")
}
