"""Build a deliberately tiny SSML subset from plain text; no engine call."""
import xml.etree.ElementTree as ET
NS='http://www.w3.org/2001/10/synthesis';XML='http://www.w3.org/XML/1998/namespace'
ET.register_namespace('',NS)
def build(text,pause_ms):
    if not isinstance(text,str) or not text.strip() or len(text)>2000:raise ValueError('1..2000 code points, nonblank')
    if '\r' in text:raise ValueError('CR/CRLF rejected; provide LF line endings')
    if any(ord(c)<32 and c not in '\n\t' for c in text):raise ValueError('XML control character')
    if any(0xD800<=ord(c)<=0xDFFF or ord(c) in (0xFFFE,0xFFFF) for c in text):raise ValueError('invalid XML character')
    if type(pause_ms) is not int or not 0<=pause_ms<=2000:raise ValueError('integer pause 0..2000 ms')
    root=ET.Element('{'+NS+'}speak',{'version':'1.1','{'+XML+'}lang':'fa-IR'})
    p=ET.SubElement(root,'{'+NS+'}p');p.text=text
    ET.SubElement(root,'{'+NS+'}break',{'time':str(pause_ms)+'ms'})
    out=ET.tostring(root,encoding='unicode');parsed=ET.fromstring(out)
    assert parsed.find('{'+NS+'}p').text==text
    assert {n.tag for n in parsed.iter()}=={'{'+NS+'}'+s for s in ['speak','p','break']}
    return out
if __name__=='__main__':
    text='نام آزمایشی: الف & ب؛ <audio src="demo.wav"/> فقط متن است.'
    result=build(text,300)
    assert '&amp;' in result and '&lt;audio' in result
    for bad,pause in [('',300),('الف',True),('الف',2001),('الف\x00',1),('الف\ud800',1),('الف\rب',1),('الف\r\nب',1)]:
        try:build(bad,pause)
        except ValueError:continue
        raise AssertionError('bad contract accepted')
    print(result)
