File size: 1,774 Bytes
7a9b4e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Unit tests for the LSPM pruning middleware. Run with:
    pytest tests/test_pruning.py -v
"""

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from middleware.pruning import split_sentences, DynamicRatioController, DynamicRatioConfig


def test_split_sentences_basic():
    text = "هذه جملة أولى. هذه جملة ثانية؟ وهذه جملة ثالثة!"
    sentences = split_sentences(text)
    assert len(sentences) == 3


def test_split_sentences_empty():
    assert split_sentences("") == []
    assert split_sentences("   ") == []


def test_dynamic_ratio_high_load():
    cfg = DynamicRatioConfig(min_ratio=0.2, max_ratio=0.8, high_load_threshold=0.75, low_load_threshold=0.25)
    controller = DynamicRatioController(cfg)
    controller._fetch_gpu_cache_usage = lambda: 0.9  # simulate high load
    assert controller.get_ratio() == 0.2


def test_dynamic_ratio_low_load():
    cfg = DynamicRatioConfig(min_ratio=0.2, max_ratio=0.8, high_load_threshold=0.75, low_load_threshold=0.25)
    controller = DynamicRatioController(cfg)
    controller._fetch_gpu_cache_usage = lambda: 0.1  # simulate low load
    assert controller.get_ratio() == 0.8


def test_dynamic_ratio_interpolation():
    cfg = DynamicRatioConfig(min_ratio=0.2, max_ratio=0.8, high_load_threshold=0.75, low_load_threshold=0.25)
    controller = DynamicRatioController(cfg)
    controller._fetch_gpu_cache_usage = lambda: 0.5  # midpoint
    ratio = controller.get_ratio()
    assert 0.2 < ratio < 0.8


def test_dynamic_ratio_fallback_on_none():
    controller = DynamicRatioController(DynamicRatioConfig(metrics_url="http://invalid:9999/metrics"))
    ratio = controller.get_ratio(fallback_ratio=0.42)
    assert ratio == 0.42