package main

import (
	"encoding/json"
	"os"
	"testing"
)

func TestLocationUnmarshalCityFallback(t *testing.T) {
	payload := []byte(`{
    "lat": "42.1234",
    "lon": "-71.5678",
    "display_name": "Somerville, Massachusetts, USA",
    "address": {
      "town": "Somerville",
      "state": "Massachusetts",
      "country": "United States",
      "postcode": "02143"
    }
  }`)

	var loc Location
	if err := json.Unmarshal(payload, &loc); err != nil {
		t.Fatalf("unmarshal failed: %v", err)
	}
	if loc.City != "Somerville" {
		t.Fatalf("expected city fallback to town, got %q", loc.City)
	}
	if loc.State != "Massachusetts" {
		t.Fatalf("expected state, got %q", loc.State)
	}
}

func TestLocationLabel(t *testing.T) {
	loc := Location{City: "Somerville", State: "MA"}
	if got := loc.Label(); got != "Somerville, MA" {
		t.Fatalf("unexpected label: %q", got)
	}

	loc = Location{DisplayName: "Somerville, Massachusetts, USA"}
	if got := loc.Label(); got != loc.DisplayName {
		t.Fatalf("unexpected label: %q", got)
	}
}

func TestHasTTYFalse(t *testing.T) {
	f, err := os.CreateTemp(t.TempDir(), "tty-check-*")
	if err != nil {
		t.Fatalf("temp file: %v", err)
	}
	defer f.Close()

	if hasTTY(f, f) {
		t.Fatalf("expected non-tty for temp file")
	}
}
