"""Fresh spare adoption restores a namespace through the daemon's real link stack.""" from pathlib import Path import unittest from harness import Albedo, exclusive, exclusive_class from inspect_support import Inspection from kernel_support import editable_daemon @exclusive_class class KernelSpareTests(unittest.TestCase): def test_unused_spares_end_on_stop_and_owner_death(self): with Albedo(prepare=lambda app: app.env.update(ALBEDO_INSPECT="1")) as app: probe = Inspection( app, Path(__file__).with_name("albedo_kernel_spare_probe.erl") ) self.assertTrue(probe.call_json("stop_json")["stopped"]) # exclusive: boots an edited daemon bundle @exclusive def test_stale_spare_is_cleaned_before_cold_fallback(self): def prepare(app): launcher, app.bundle = editable_daemon(app.root) app.env.update(ALBEDO_INSPECT="1", ALBEDO_DAEMON=str(launcher)) with Albedo(prepare=prepare) as app: session = app.session() probe = Inspection( app, Path(__file__).with_name("albedo_kernel_spare_probe.erl") ) changed = app.bundle / "albedo_bundle.py" result = probe.call_json( "run_json", f'[<<"{session}">>, <<"{app.workspace}">>, <<"{changed}">>]' ) self.assertEqual(result["recalled"]["status"], "ok", result) self.assertIn("SPARE_OK", result["recalled"]["output"]) def test_spare_is_single_use_and_restores_definitions(self): with Albedo(prepare=lambda app: app.env.update(ALBEDO_INSPECT="1")) as app: session = app.session() probe = Inspection( app, Path(__file__).with_name("albedo_kernel_spare_probe.erl") ) result = probe.call_json( "run_json", f'[<<"{session}">>, <<"{app.workspace}">>, <<>>]' ) self.assertEqual(result["recalled"]["status"], "ok", result) self.assertIn("SPARE_OK", result["recalled"]["output"]) self.assertEqual(result["live_work"], 0) def test_a_spare_that_cannot_be_stopped_fails_the_open_without_leaking_its_kernel( self, ): with Albedo(prepare=lambda app: app.env.update(ALBEDO_INSPECT="1")) as app: session = app.session() probe = Inspection( app, Path(__file__).with_name("albedo_kernel_spare_probe.erl") ) result = probe.call_json( "stop_failure_json", f'[<<"{session}">>, <<"{app.workspace}">>]' ) self.assertTrue(result["failed"], result) self.assertEqual(result["booted"], 1, result) self.assertEqual(result["outlived"], 0, result)