Spaces:
Running
Running
File size: 2,225 Bytes
8f3b9c2 | 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 52 53 54 55 56 57 58 59 60 61 62 63 64 | """Fast unit tests for SyntaxChecker helpers and --no-ml mode (no model downloads)."""
from __future__ import annotations
import random
from syntax.syntax_detector import (
SyntaxChecker,
_align_norm_sentences,
_is_pii_like,
_make_shuffles,
_split_sentences,
)
def test_is_pii_like_filters_structured_and_numeric_lines() -> None:
assert _is_pii_like("NIK: 3273015208950002") is True
assert _is_pii_like("Hubungi 0812-3456-7890 sekarang") is True
assert _is_pii_like("Server IP 192.168.1.10 aktif") is True
assert _is_pii_like("123 456 789 012") is True
assert _is_pii_like("Saya mengajar siswa kelas VII tentang ekosistem") is False
def test_split_sentences_respects_min_words_and_preserves_offsets() -> None:
text = "Halo. NIK: 3273015208950002 di sini. Proses pernapasan manusia berlangsung di paru."
sentences = _split_sentences(text)
assert len(sentences) == 1
sent, start, end = sentences[0]
assert sent == "Proses pernapasan manusia berlangsung di paru."
assert text[start:end] == sent
def test_make_shuffles_is_deterministic_and_excludes_original() -> None:
words = ["saya", "membuat", "laporan", "keuangan"]
s1 = _make_shuffles(words, 6, random.Random(1234))
s2 = _make_shuffles(words, 6, random.Random(1234))
assert s1 == s2
assert len(s1) == 6
assert words not in s1
assert len({tuple(item) for item in s1}) == 6
assert _make_shuffles(["satu"], 6, random.Random(1234)) == []
def test_align_norm_sentences_maps_parallel_sentences() -> None:
orig = "Kucing mengejar tikus kecil. Anjing berlari cepat sekali."
norm = "Kucing mengejar tikus kecil. Anjing berlari dengan cepat."
orig_sents = _split_sentences(orig)
aligned = _align_norm_sentences(orig_sents, norm)
assert len(aligned) == 2
assert aligned[0] == "Kucing mengejar tikus kecil."
assert aligned[1] == "Anjing berlari dengan cepat."
def test_syntax_checker_no_ml_returns_empty_safely() -> None:
chk = SyntaxChecker(use_ml=False)
assert chk.load() is False
assert chk.is_loaded is True
assert chk.ml_active is False
assert chk.check("makan suka aku nasi goreng") == []
assert chk.check("") == []
|