package lottery import ( "context" "testing" "github.com/perfect-panel/server/pkg/constant" ) // TestRequestMeta_EmptyWhenCtxUnset asserts requestMeta returns empty strings // when neither typed context key is populated (unit tests, non-admin paths). func TestRequestMeta_EmptyWhenCtxUnset(t *testing.T) { ip, ua := requestMeta(context.Background()) if ip != "" || ua != "" { t.Fatalf("expected empty, got ip=%q ua=%q", ip, ua) } } // TestRequestMeta_ReadsTypedKeys asserts requestMeta picks up the values // AdminMetaMiddleware pins onto ctx via the typed CtxKey constants. func TestRequestMeta_ReadsTypedKeys(t *testing.T) { ctx := context.Background() ctx = context.WithValue(ctx, constant.CtxKeyIP, "10.99.99.7") ctx = context.WithValue(ctx, constant.CtxKeyUserAgent, "qa-audit-probe") ip, ua := requestMeta(ctx) if ip != "10.99.99.7" { t.Fatalf("ip = %q, want %q", ip, "10.99.99.7") } if ua != "qa-audit-probe" { t.Fatalf("ua = %q, want %q", ua, "qa-audit-probe") } } // TestRequestMeta_IgnoresBareStringKeys guards against the F2 root cause: // pre-fix, the writer used bare-string keys "ip" / "user_agent" which never // collided with anyone's typed reader — so audit rows always saw empty // strings. The test proves the reader now IGNORES bare-string writes: only // the typed CtxKey path counts. func TestRequestMeta_IgnoresBareStringKeys(t *testing.T) { ctx := context.Background() //nolint:staticcheck // intentional bare-string key to prove reader ignores it ctx = context.WithValue(ctx, "ip", "should-be-ignored") //nolint:staticcheck // intentional bare-string key to prove reader ignores it ctx = context.WithValue(ctx, "user_agent", "should-be-ignored") ip, ua := requestMeta(ctx) if ip != "" || ua != "" { t.Fatalf("bare-string keys must be ignored; got ip=%q ua=%q", ip, ua) } }