oneiros / tests /test_normalisasi.py
Adinda Panca Mochamad
Day 1: foundation — ekstraksi JSON, Gradio minimal, trace logger
c10196a
Raw
History Blame Contribute Delete
3.31 kB
"""Unit test normalisasi tanpa model."""
import sys
sys.path.insert(0, ".")
from model.normalisasi import (
entitas_memadai,
normalisasi_koneksi,
parse_json_dari_teks,
validasi_dan_perbaiki,
validasi_skema_json,
)
def test_parse_markdown_fence():
raw = '```json\n{"title": "T", "mood": "joyful", "core_theme": "x"}\n```'
data, err = parse_json_dari_teks(raw)
assert err is None
assert data["mood"] == "joyful"
def test_parse_json_dengan_teks_sisa():
raw = 'Here you go:\n{"title": "T", "mood": "surreal", "core_theme": "x"}\nThanks!'
data, err = parse_json_dari_teks(raw)
assert err is None
assert data["mood"] == "surreal"
def test_mood_fallback():
ent = validasi_dan_perbaiki({"title": "A", "mood": "INVALID", "core_theme": "b"})
assert ent["mood"] == "mysterious"
def test_koneksi_cocokkan_nama():
ent = {
"characters": [
{"name": "Dreamer", "role": "self", "emotion": "afraid"},
{"name": "White Deer", "role": "creature", "emotion": "x"},
],
"places": [],
"symbols": [],
"connections": [{"from": "deer", "to": "Dreamer", "type": "pursuit"}],
"mood": "mysterious",
"title": "T",
"core_theme": "c",
}
hasil = normalisasi_koneksi(ent)
assert len(hasil["connections"]) == 1
assert hasil["connections"][0]["from"] == "White Deer"
assert hasil["connections"][0]["to"] == "Dreamer"
def test_substring_pendek_tidak_salah_kocok():
"""'ann' tidak boleh map ke 'Grandmother' hanya via substring."""
ent = {
"characters": [{"name": "Grandmother", "role": "known", "emotion": "x"}],
"places": [],
"symbols": [],
"connections": [{"from": "ann", "to": "Grandmother", "type": "harmony"}],
"mood": "mysterious",
"title": "T",
"core_theme": "c",
}
hasil = normalisasi_koneksi(ent)
assert len(hasil["connections"]) == 0
def test_deduplikasi_edge():
ent = {
"characters": [
{"name": "A", "role": "self", "emotion": "x"},
{"name": "B", "role": "stranger", "emotion": "y"},
],
"places": [],
"symbols": [],
"connections": [
{"from": "A", "to": "B", "type": "tension"},
{"from": "A", "to": "B", "type": "tension"},
],
"mood": "mysterious",
"title": "T",
"core_theme": "c",
}
hasil = normalisasi_koneksi(ent)
assert len(hasil["connections"]) == 1
def test_jsonschema_menolak_tipe_salah():
data = {"title": "T", "mood": "joyful", "core_theme": "x", "characters": "bukan array"}
assert validasi_skema_json(data) is not None
def test_entitas_memadai():
assert entitas_memadai({"title": "T", "mood": "joyful", "core_theme": "ada tema", "characters": []})
assert not entitas_memadai({"title": "Parse Failed", "mood": "mysterious", "core_theme": ""})
def run_all():
test_parse_markdown_fence()
test_parse_json_dengan_teks_sisa()
test_mood_fallback()
test_koneksi_cocokkan_nama()
test_substring_pendek_tidak_salah_kocok()
test_deduplikasi_edge()
test_jsonschema_menolak_tipe_salah()
test_entitas_memadai()
print("test_normalisasi: 8/8 OK")
if __name__ == "__main__":
run_all()