---
Tests for the analytics context.

These exercise the `Hits` agent (state: `count: Int`) through the track/count
services. Every test begins with a *fresh* agent key, so the suite collectively
depends on how a never-seen agent initialises its state — the central question
this fixture exists to answer.

STATE-INITIALISATION HYPOTHESIS
  A fresh `Hits` key has `count == 0`. The first probe test below isolates this:
  it reads `count.call` on a code that was never tracked, with no prior write.
    - If it returns Ok(0): Int state zero-initialises. (Hypothesis holds.)
    - If the assertion fails (e.g. count is undefined → undefined == 0 is false):
      fresh state is NOT zero-initialised; agents need an explicit initialiser.
    - If the test errors/throws: loadState rejects a missing key outright.
  Whichever happens is finding #10, by demonstration.

Construction note: refined values use `ShortCode.of(...)` (Result), unwrapped
with a match — finding #7 (refined-construction ergonomics) is not yet
addressed, hence the verbosity.
---
test shortener.analytics

uses shortener.core

---
STATE-INIT PROBE. A fresh Hits key, read before any write, should report 0.
This is the cleanest isolation of agent state initialisation in the suite.
---
test "a fresh Hits key reads count as 0" {
	match ShortCode.of("fresh1") {
		Err(_) => assert false
		Ok(code) => {
			let reading <- count.call(code)
			match reading {
				Ok(total) => assert total == 0
				Err(_)    => assert false
			}
		}
	}
}

test "recording a hit returns a running total" {
	match ShortCode.of("abc123") {
		Err(_) => assert false
		Ok(code) => {
			let result <- track.call(code)
			match result {
				Ok(total) => assert total == 1
				Err(_)    => assert false
			}
		}
	}
}

test "two hits on the same code accumulate" {
	match ShortCode.of("abc123") {
		Err(_) => assert false
		Ok(code) => {
			let _ <- track.call(code)
			let second <- track.call(code)
			match second {
				Ok(total) => assert total == 2
				Err(_)    => assert false
			}
		}
	}
}

test "count reads back the current total without incrementing" {
	match ShortCode.of("zzz999") {
		Err(_) => assert false
		Ok(code) => {
			let _ <- track.call(code)
			let reading <- count.call(code)
			match reading {
				Ok(total) => assert total == 1
				Err(_)    => assert false
			}
		}
	}
}
