package system

// machine_id_format_test.go — Pre-fix tests for machine ID format consistency.
//
// F-D1-1: All machine ID paths in GetMachineID() produce consistent format.
//   The divergence is only in main.go's inline fallback.
//
// Run: cd aggregator-agent && go test ./internal/system/... -v -run TestHash

import (
	"encoding/hex"
	"strings"
	"testing"
)

// ---------------------------------------------------------------------------
// Test 2.1 — All canonical fallbacks produce same format (documentary)
//
// Category: PASS-NOW (canonical function is internally consistent)
// ---------------------------------------------------------------------------

func TestAllMachineIDFallbacksProduceSameFormat(t *testing.T) {
	// F-D1-1: The canonical GetMachineID() is internally consistent.
	// All paths call hashMachineID(). The divergence is in main.go:428.

	// Test the generic fallback directly
	genericID, err := generateGenericMachineID()
	if err != nil {
		t.Fatalf("generateGenericMachineID failed: %v", err)
	}

	if len(genericID) != 64 {
		t.Errorf("[ERROR] [agent] [system] generic fallback is %d chars, expected 64", len(genericID))
	}
	if _, err := hex.DecodeString(genericID); err != nil {
		t.Errorf("[ERROR] [agent] [system] generic fallback is not valid hex: %v", err)
	}

	t.Log("[INFO] [agent] [system] F-D1-1: all canonical fallbacks produce 64 hex chars")
}

// ---------------------------------------------------------------------------
// Test 2.2 — hashMachineID always produces 64 hex chars
//
// Category: PASS-NOW (hash function is correct)
// ---------------------------------------------------------------------------

func TestHashMachineIDAlwaysProduces64HexChars(t *testing.T) {
	// F-D1-1: hashMachineID() is correctly implemented.
	// The bug is that main.go bypasses it.
	inputs := []string{
		"",
		"a",
		strings.Repeat("x", 256),
		"special chars: !@#$%^&*()",
		"550e8400-e29b-41d4-a716-446655440000",
	}

	for _, input := range inputs {
		result := hashMachineID(input)
		if len(result) != 64 {
			t.Errorf("[ERROR] [agent] [system] hashMachineID(%q) = %d chars, expected 64", input, len(result))
		}
		if _, err := hex.DecodeString(result); err != nil {
			t.Errorf("[ERROR] [agent] [system] hashMachineID(%q) not valid hex: %v", input, err)
		}
	}

	// Determinism check
	if hashMachineID("test") != hashMachineID("test") {
		t.Error("[ERROR] [agent] [system] hashMachineID is not deterministic")
	}

	t.Log("[INFO] [agent] [system] hashMachineID: all inputs produce 64 hex, deterministic")
}

// ---------------------------------------------------------------------------
// Test 2.3 — "unknown-" fallback format differs from hash format
//
// Category: PASS-NOW (documents the incompatibility)
// ---------------------------------------------------------------------------

func TestUnknownFallbackFormatDifferentFromHash(t *testing.T) {
	// POST-FIX (F-D1-1): The "unknown-" fallback no longer exists in
	// registration code. This test confirms canonical paths always hash.
	canonical := hashMachineID("testhost-linux-fallback")
	generic, _ := generateGenericMachineID()

	// Both canonical paths produce 64 hex chars
	if len(canonical) != 64 || len(generic) != 64 {
		t.Errorf("[ERROR] [agent] [system] canonical paths not 64 chars: canonical=%d generic=%d",
			len(canonical), len(generic))
	}

	t.Log("[INFO] [agent] [system] F-D1-1 FIXED: all machine ID paths produce consistent 64 hex format")
}
