File size: 2,406 Bytes
c349d51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efe917e
c349d51
efe917e
 
c349d51
efe917e
c349d51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib
import json
from pathlib import Path

import lexia.legal as _legal
from lexia.legal.corpus import CorpusManifest

LEGAL = Path(_legal.__file__).resolve().parent


def test_official_source_manifest_is_packaged() -> None:
    manifest = CorpusManifest.load(LEGAL / "data" / "corpus_sources.json")
    assert len(manifest.sources) >= 16
    assert all(source.official_url.startswith("https://") for source in manifest.sources)


def test_seed_corpus_matches_declared_hash() -> None:
    records = LEGAL / "seed" / "corpus" / "records.jsonl"
    snapshot = json.loads(
        (LEGAL / "seed" / "corpus" / "snapshot.json").read_text(encoding="utf-8")
    )
    expected = snapshot["records_sha256"]
    assert hashlib.sha256(records.read_bytes()).hexdigest() == expected
    assert sum(1 for _ in records.open(encoding="utf-8")) == snapshot["record_count"]


# -- cola de ampliación del corpus -------------------------------------------


def test_la_cola_de_ampliacion_es_valida_y_no_duplica_lo_que_ya_hay():
    """Metadatos contrastados el 2026-09-15 contra EUR-Lex y el BOE."""
    from lexia.legal.corpus import CorpusManifest

    datos = Path(__file__).resolve().parent.parent / "lexia" / "legal" / "data"
    cola = CorpusManifest.load(datos / "corpus_sources_ampliacion.json")
    principal = CorpusManifest.load(datos / "corpus_sources.json")
    ya = {fuente.source_id for fuente in principal.sources}
    for fuente in cola.sources:
        assert fuente.source_id not in ya, fuente.source_id
        assert fuente.official_url.startswith("https://")
        assert fuente.published_on.year >= 2002


def test_ninguna_fuente_de_la_cola_entra_en_el_corpus_sin_texto():
    """Una fuente declarada y sin fragmentos sería una cita insostenible."""
    import json

    datos = Path(__file__).resolve().parent.parent / "lexia" / "legal" / "data"
    declaradas = {
        fuente["id"]
        for fuente in json.loads(
            (datos / "corpus_sources.json").read_text(encoding="utf-8")
        )["sources"]
    }
    con_texto = set()
    semilla = Path(__file__).resolve().parent.parent / "lexia" / "legal" / "seed" / "corpus"
    for linea in (semilla / "records.jsonl").read_text(encoding="utf-8").splitlines():
        if linea.strip():
            con_texto.add(json.loads(linea)["source_id"])
    assert declaradas == con_texto, declaradas.symmetric_difference(con_texto)