OrbiSubs ASR: skip a segment whose PTS cannot be read; prune Master.vistos

Revisión externa 1.4 y 3.2: después de 10 intentos, y con un segmento posterior en la lista, el seq sale con un .vtt vacío (sin inventar PTS) y se loguea pts_skip. test_revision.py cubre también 1.3, 1.5 y 1.6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JAUCv2ADyD2NSg4ub9YPr1
This commit is contained in:
guy
2026-11-22 14:12:04 -03:00
parent 2d7fe8a43b
commit 85309585af
2 changed files with 164 additions and 5 deletions
+111
View File
@@ -0,0 +1,111 @@
#!/usr/bin/env python3
"""Tests de los arreglos de la revisión externa (097). Correr: python3 -m unittest test_revision"""
import os
import tempfile
import types
import unittest
import orbisubs as O
import orbisubs_srt as S
import orbisubs_tr as T
class Vistos(unittest.TestCase):
# 1.5: la poda tiene que olvidar los más viejos, nunca los recién vistos.
def test_poda_fifo(self):
v = S.Vistos(maximo=20, deja=10)
for i in range(21):
v.add(i)
self.assertEqual(len(v), 10)
self.assertNotIn(0, v)
self.assertNotIn(10, v)
for i in range(11, 21):
self.assertIn(i, v)
def test_cue_repetido_no_entra_tras_la_poda(self):
e = S.Enc608(1)
# 2500 cues distintos (pasa la poda) y el último vuelve a llegar (por la cola y el .vtt).
for i in range(2500):
e.agregar((1000.0 + i, 1000.5 + i, f"cue {i}"), 0)
n = len(e.cola)
e.agregar((1000.0 + 2499, 1000.5 + 2499, "cue 2499"), 0)
self.assertEqual(len(e.cola), n)
class Cola(unittest.TestCase):
# 1.6: al truncarse la cola se cierra el archivo viejo.
def test_cierra_al_reabrir(self):
with tempfile.TemporaryDirectory() as d:
p = os.path.join(d, "cues.jsonl")
open(p, "w").write('[1, 2, "a"]\n')
c = S.Cola(p)
c.primera = False
self.assertEqual(len(c.nuevos()), 1)
viejo = c.f
open(p, "w").write("") # truncada por un reinicio del ASR
c.nuevos()
self.assertTrue(viejo.closed)
os.unlink(p) # se borra: no se cae
self.assertEqual(c.nuevos(), [])
class Think(unittest.TestCase):
# 1.3: un <think> cortado por max_tokens no sale como subtítulo.
def test_think(self):
self.assertEqual(T.sin_think("Hello."), "Hello.")
self.assertEqual(T.sin_think("<think>hmm</think> Hello."), "Hello.")
self.assertEqual(T.sin_think("<think>The user wants me to translate"), "")
class PtsFallidos(unittest.TestCase):
# 1.4: un .ts dañado se saltea después de N intentos, sin inventar PTS.
def test_ts_danado(self):
with tempfile.TemporaryDirectory() as d:
p = os.path.join(d, "1.ts")
open(p, "wb").write(os.urandom(4000))
with self.assertRaises(Exception):
O.pts_inicio(p)
def test_saltea_despues_de_n(self):
f = O.PtsFallidos()
for _ in range(f.INTENTOS - 1):
self.assertFalse(f.fallo(5, 7))
self.assertTrue(f.fallo(5, 7))
self.assertIn(5, f.salteados)
self.assertFalse(f.fallo(5, 7)) # una sola vez
def test_el_ultimo_no_se_saltea(self):
# El último de la lista puede estar escribiéndose: se espera a que haya otro.
f = O.PtsFallidos()
for _ in range(3 * f.INTENTOS):
f.fallo(7, 7)
self.assertNotIn(7, f.salteados)
self.assertTrue(f.fallo(7, 8))
def test_podar(self):
f = O.PtsFallidos()
for _ in range(f.INTENTOS):
f.fallo(5, 9)
f.podar(6)
self.assertEqual((f.n, f.salteados), ({}, set()))
class MasterVistos(unittest.TestCase):
# 3.2: Master.vistos solo guarda los seq que siguen en la lista.
def test_poda(self):
with tempfile.TemporaryDirectory() as d:
src = os.path.join(d, "index.m3u8")
m = O.Master(types.SimpleNamespace(resolution="1920x1080", bandwidth=8000000, dir=d))
m.vistos = set()
for base in range(0, 100, 10):
lst = "#EXTM3U\n#EXT-X-TARGETDURATION:6\n#EXT-X-MEDIA-SEQUENCE:%d\n" % base
for i in range(10):
open(os.path.join(d, f"{base + i}.ts"), "wb").write(b"x" * 100)
lst += f"#EXTINF:6.0,\n{base + i}.ts\n"
open(src, "w").write(lst)
m.medir(src)
self.assertEqual(m.vistos, set(range(90, 99)))
if __name__ == "__main__":
unittest.main()