"""Coreを起動しない境界・既知公開鍵・エラー分類の回帰試験。"""
import importlib.util
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch

spec = importlib.util.spec_from_file_location('lab', Path(__file__).with_name('multisig-recovery-lab.py'))
lab = importlib.util.module_from_spec(spec)
spec.loader.exec_module(lab)


class SafetyTests(unittest.TestCase):
    def test_no_process_without_authorization(self):
        with patch.object(lab.subprocess, 'run') as runner:
            with self.assertRaises(lab.LabError):
                lab.run(Path('.'), Path('not-written.json'), False)
            runner.assert_not_called()

    def test_connection_failure_not_checksum_success(self):
        for error in (lab.LabError('connection'), lab.LabError('different', -5), lab.LabError('other code', -1, True)):
            self.assertFalse(lab.is_checksum_rejection(error))
        self.assertTrue(lab.is_checksum_rejection(lab.LabError('expected', -5, True)))

    def test_secp256k1_generator(self):
        self.assertEqual(lab.public_key(1).hex(), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')

    def test_temp_boundary_rejects_parent_and_other_prefix(self):
        parent = Path(tempfile.gettempdir()).resolve()
        for target in (parent, parent / 'other-task', parent / 'a05-test' / 'nested'):
            with self.assertRaises(lab.LabError):
                lab.verify_temp_path(target, parent, 'a05-')


if __name__ == '__main__':
    unittest.main()
