"""Same PCM in different WAV wrappers; not a FLAC encode/decode test."""
import io,wave,struct,hashlib,json
def chunk(name,payload):return name+struct.pack('<I',len(payload))+payload+(b'\0' if len(payload)%2 else b'')
def wrap(samples,extra=False):
    pcm=struct.pack('<'+'h'*len(samples),*samples)
    fmt=struct.pack('<HHIIHH',1,1,8000,16000,2,16)
    body=b'WAVE'+chunk(b'fmt ',fmt)+(chunk(b'JUNK',b'notes-v1') if extra else b'')+chunk(b'data',pcm)
    return b'RIFF'+struct.pack('<I',len(body))+body
def read(raw):
    with wave.open(io.BytesIO(raw),'rb') as f:
        state=(f.getnchannels(),f.getsampwidth(),f.getframerate(),f.getnframes(),f.getcomptype())
        pcm=f.readframes(f.getnframes())
    return state,pcm
if __name__=='__main__':
    samples=[0,1000,-1000,300,-300,0]
    a=wrap(samples);b=wrap(samples,True);changed=wrap([0,1000,-999,300,-300,0],True)
    sa,pa=read(a);sb,pb=read(b);sc,pc=read(changed)
    assert a!=b and hashlib.sha256(a).hexdigest()!=hashlib.sha256(b).hexdigest()
    assert sa==sb==sc and pa==pb and pa!=pc
    assert len(pa)==12 and sa[:4]==(1,2,8000,6)
    print(json.dumps({'file_A_sha256':hashlib.sha256(a).hexdigest(),'file_B_sha256':hashlib.sha256(b).hexdigest(),'whole_files_equal':a==b,'decoded_PCM_equal':pa==pb,'state_equal':sa==sb,'changed_one_sample_detected':pa!=pc,'PCM_bytes':len(pa),'FLAC_encoder_or_decoder_run':False},indent=2))
