package main

import (
	"testing"
	"time"
)

// A ClubLog exception has a validity window, and a DXpedition's window closes.
// So a backdated entry must be enriched as of WHEN IT HAPPENED — 3Y0K typed
// months after the activation, with the activation's date in the form, resolved
// at today's date, found no live exception, and fell back to cty.dat:
// Antarctica, where the log says Bouvet Island.
//
// The date is trusted to move the resolution BACKWARDS only. A half-typed date
// must not send the lookup to the year 20, and a mistyped future one must not
// resolve against a window that has not opened.
func TestLookupWhen(t *testing.T) {
	now := time.Now().UTC()

	if got := lookupWhen("2026-03-08"); got.Format("2006-01-02") != "2026-03-08" {
		t.Errorf("a past date gave %v — the activation's own date is the whole point", got)
	}
	// Midday, not midnight: a window given in whole days is inclusive of its end
	// date, and 00:00 sits exactly on the boundary.
	if h := lookupWhen("2026-03-08").Hour(); h != 12 {
		t.Errorf("resolved at %02d:00, want 12:00 — midnight sits on the window boundary", h)
	}

	for name, in := range map[string]string{
		"empty":        "",
		"spaces":       "   ",
		"half-typed":   "2026-0",
		"not a date":   "hier",
		"wrong format": "08/03/2026",
	} {
		if got := lookupWhen(in); got.Before(now.Add(-time.Minute)) {
			t.Errorf("%s (%q) resolved to %v — anything unusable must mean now", name, in, got)
		}
	}

	future := now.AddDate(1, 0, 0).Format("2006-01-02")
	if got := lookupWhen(future); got.After(now.Add(time.Minute)) {
		t.Errorf("a future date (%s) resolved to %v — a mistyped year must not open a window early", future, got)
	}
}
