Files
baji-vc/gateway/tests/test_v08.py
T

82 lines
2.6 KiB
Python

"""v0.8: LAN 直连授权 + active_profile 单元测试(不需要网络/模型)。"""
import json
import os
import tempfile
import time
import unittest
from pathlib import Path
from unittest.mock import MagicMock
import gateway
from guest_sessions import GuestSessions
class FakeClient:
def __init__(self, host):
self.host = host
class FakeConn:
def __init__(self, host):
self.client = FakeClient(host)
class TestLanOpenMode(unittest.TestCase):
def setUp(self):
gateway.app.state.guests = GuestSessions()
gateway.app.state.lan_open = True
def test_open_key_from_lan_ip(self):
key = gateway.open_key(FakeConn('192.168.31.200'))
self.assertEqual(key, 'lan:192.168.31.200')
self.assertIsNotNone(gateway.app.state.guests.get(key))
def test_open_key_reusable_and_own_bind(self):
key = gateway.open_key(FakeConn('192.168.31.200'))
gateway.app.state.guests.bind(key, 'cid-1')
gateway.app.state.guests.own(key, 'cid-1')
with self.assertRaises(ValueError):
gateway.app.state.guests.own(key, 'cid-other')
def test_open_key_none_when_disabled(self):
gateway.app.state.lan_open = False
self.assertIsNone(gateway.open_key(FakeConn('192.168.31.200')))
def test_open_key_none_for_foreign_ip(self):
self.assertIsNone(gateway.open_key(FakeConn('10.9.9.9')))
class TestActiveProfile(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.orig_root = gateway.ROOT
root = Path(self.tmp.name)
(root / '.runtime').mkdir()
gateway.ROOT = root
self.addCleanup(setattr, gateway, 'ROOT', self.orig_root)
def write(self, data):
(gateway.ROOT / '.runtime' / 'active_profile.json').write_text(json.dumps(data), encoding='utf-8')
def test_missing_file_returns_none(self):
self.assertIsNone(gateway.load_active_profile())
def test_valid_profile(self):
self.write({'persona': 'yangmi_fiction', 'voice_id': 'v-1'})
p = gateway.load_active_profile()
self.assertEqual(p['persona'], 'yangmi_fiction')
self.assertEqual(p['voice_id'], 'v-1')
def test_invalid_json_returns_none(self):
(gateway.ROOT / '.runtime' / 'active_profile.json').write_text('{bad', encoding='utf-8')
self.assertIsNone(gateway.load_active_profile())
def test_non_dict_returns_none(self):
self.write([1, 2])
self.assertIsNone(gateway.load_active_profile())
if __name__ == '__main__':
unittest.main()