@@ -385,6 +385,72 @@ def test_load_plugins_index_rejects_non_dict_values(self):
385385 os .remove (index_path )
386386
387387
388+ class DummyContextStubTests (unittest .IsolatedAsyncioTestCase ):
389+ def test_dummy_context_defers_plugin_data_dir_creation_until_requested (self ):
390+ module = load_validator_module ()
391+
392+ with tempfile .TemporaryDirectory () as tmp_dir :
393+ astrbot_root = Path (tmp_dir ) / "astrbot-root"
394+ plugin_data_dir = astrbot_root / "data" / "plugin_data"
395+
396+ with mock .patch .dict (os .environ , {"ASTRBOT_ROOT" : str (astrbot_root )}, clear = True ):
397+ context = module .DummyContext ()
398+ dir_exists_before = plugin_data_dir .exists ()
399+ created_dir = Path (context .get_data_dir ())
400+ dir_exists_after = plugin_data_dir .is_dir ()
401+
402+ self .assertFalse (dir_exists_before )
403+ self .assertEqual (created_dir .resolve (), plugin_data_dir .resolve ())
404+ self .assertTrue (dir_exists_after )
405+
406+ def test_dummy_context_returns_worker_data_dir_for_plugin_storage (self ):
407+ module = load_validator_module ()
408+
409+ with tempfile .TemporaryDirectory () as tmp_dir :
410+ astrbot_root = Path (tmp_dir ) / "astrbot-root"
411+ with mock .patch .dict (os .environ , {"ASTRBOT_ROOT" : str (astrbot_root )}, clear = True ):
412+ data_dir = Path (module .DummyContext ().get_data_dir ())
413+ data_dir_exists = data_dir .is_dir ()
414+
415+ self .assertEqual (data_dir .resolve (), (astrbot_root / "data" / "plugin_data" ).resolve ())
416+ self .assertTrue (data_dir_exists )
417+
418+ async def test_null_stub_supports_async_database_context_pattern (self ):
419+ module = load_validator_module ()
420+
421+ db = module .DummyContext ().get_db ()
422+
423+ async with db .get_db () as session :
424+ self .assertIsInstance (session , module .NullStub )
425+ async with session .begin () as transaction :
426+ self .assertIs (transaction , session )
427+ result = await session .execute ("SELECT 1" )
428+
429+ self .assertIs (result , session )
430+
431+ async def test_null_stub_returns_defaults_for_restart_style_config_access (self ):
432+ module = load_validator_module ()
433+
434+ with mock .patch .dict (os .environ , {}, clear = True ):
435+ dashboard_config = module .DummyContext ().get_config ().get ("dashboard" , {})
436+
437+ self .assertEqual (dashboard_config .get ("host" , "127.0.0.1" ), "127.0.0.1" )
438+ self .assertEqual (
439+ int (os .environ .get ("DASHBOARD_PORT" , dashboard_config .get ("port" , 6185 ))),
440+ 6185 ,
441+ )
442+
443+ def test_dummy_context_exposes_dict_like_config_defaults (self ):
444+ module = load_validator_module ()
445+
446+ with mock .patch .dict (os .environ , {}, clear = True ):
447+ context = module .DummyContext ()
448+
449+ self .assertEqual (context .get_config ()["wake_prefix" ], [])
450+ self .assertEqual (context .get_config ()["dashboard" ].get ("port" , 6185 ), 6185 )
451+ self .assertEqual (context ._config .get ("expire_seconds" , 300 ), 300 )
452+
453+
388454class ValidationProgressTests (unittest .TestCase ):
389455 def test_build_parser_defaults_max_workers_to_sixteen (self ):
390456 module = load_validator_module ()
0 commit comments