package mihomoapi

import (
	"testing"
	"time"
)

func TestParseConnectionSnapshot(t *testing.T) {
	raw := []byte(`{
		"downloadTotal": 200,
		"uploadTotal": 100,
		"connections": [{
			"id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
			"metadata": {
				"network": "tcp",
				"type": "HTTP",
				"sourceIP": "127.0.0.1",
				"destinationIP": "142.250.185.78",
				"destinationPort": "443",
				"host": "www.google.com"
			},
			"upload": 100,
			"download": 200,
			"start": "2026-06-16T10:00:00Z",
			"chains": ["DIRECT"],
			"rule": "DOMAIN",
			"rulePayload": "google.com"
		}],
		"memory": 0
	}`)

	snap, err := ParseConnectionSnapshot(raw)
	if err != nil {
		t.Fatalf("parse failed: %v", err)
	}
	if len(snap.Connections) != 1 {
		t.Fatalf("expected 1 connection, got %d", len(snap.Connections))
	}
	conn := snap.Connections[0]
	if conn.ID == "" {
		t.Fatal("expected connection id")
	}
	if conn.Metadata.DestinationPort != "443" {
		t.Fatalf("destination port: %q", conn.Metadata.DestinationPort)
	}
	if conn.RequestHost() != "www.google.com" {
		t.Fatalf("host: %q", conn.RequestHost())
	}
	if conn.OutboundNode() != "DIRECT" {
		t.Fatalf("outbound: %q", conn.OutboundNode())
	}
	if conn.Start.IsZero() {
		t.Fatal("expected start time")
	}
	if conn.Start.UTC() != time.Date(2026, 6, 16, 10, 0, 0, 0, time.UTC) {
		t.Fatalf("unexpected start: %v", conn.Start)
	}
}
