# -*- coding: utf-8 -*-
"""_globals 模块测试"""
from abyss._globals import ctx


class TestLazyAccessor:
    """_ra() 懒访问模式测试"""

    def test_default_returns_context(self):
        """ctx() 返回默认 context。"""
        c = ctx()
        assert c is not None
        assert c.is_ready() is False

    def test_override_and_restore(self):
        """override 后 ctx() 返回覆盖的值，restore 后恢复。"""
        old = ctx()
        ctx.override(registry="fake")
        assert ctx().registry == "fake"
        ctx.restore()
        assert ctx() is old

    def test_override_partial(self):
        """只覆盖部分字段。"""
        ctx.override(registry="r", esc="e")
        assert ctx().registry == "r"
        assert ctx().esc == "e"
        assert ctx().host is None  # 未覆盖的保持默认
        ctx.restore()

    def test_is_ready(self):
        """registry 非空时 is_ready() 返回 True。"""
        ctx.override(registry=True)
        assert ctx().is_ready() is True
        ctx.restore()
