131 lines
5.8 KiB
Python
131 lines
5.8 KiB
Python
import json
|
|||
|
|
import shutil
|
||
|
|
import sys
|
||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
ROOT = Path(__file__).resolve().parents[1]
|
||
|
|
sys.path.insert(0, str(ROOT/'scripts'))
|
||
|
|
import folder_builder as fb
|
||
|
|
import media_builder as core
|
||
|
|
|
||
|
|
|
||
|
|
class FolderBuilderTests(unittest.TestCase):
|
||
|
|
def setUp(self):
|
||
|
|
(ROOT/'.test-work').mkdir(exist_ok=True)
|
||
|
|
self.temp = tempfile.TemporaryDirectory(dir=ROOT/'.test-work')
|
||
|
|
self.folder = Path(self.temp.name)/'recording'
|
||
|
|
self.folder.mkdir()
|
||
|
|
source = ROOT/'examples/example-webinar'
|
||
|
|
for ext in ('txt','vtt'):
|
||
|
|
shutil.copyfile(source/('transcript.'+ext),self.folder/('different-name.'+ext))
|
||
|
|
(self.folder/'recording.mp3').write_bytes(b'synthetic unavailable audio')
|
||
|
|
self.intake = self.folder/'media-intake.json'
|
||
|
|
self.plan = self.folder/'structure-plan.json'
|
||
|
|
self.output = self.folder/'structured'
|
||
|
|
fb.prepare(self.folder,self.intake)
|
||
|
|
fb.seed(self.folder,self.intake,self.plan)
|
||
|
|
|
||
|
|
def tearDown(self):
|
||
|
|
self.temp.cleanup()
|
||
|
|
|
||
|
|
def edit(self,path,change):
|
||
|
|
value=core.load(path);change(value);core.write(path,value)
|
||
|
|
|
||
|
|
def build(self):
|
||
|
|
return fb.build(self.folder,self.intake,self.plan,self.output)
|
||
|
|
|
||
|
|
def test_unknown_metadata_and_no_approval_allow_local_draft(self):
|
||
|
|
before=fb.inspect(self.folder)[0]['source_files']
|
||
|
|
self.build()
|
||
|
|
self.assertTrue(fb.verify(self.output)['valid'])
|
||
|
|
media=core.load(self.output/'media.json')
|
||
|
|
self.assertIsNone(media['canonical_url'])
|
||
|
|
self.assertFalse(media['human_approval'])
|
||
|
|
self.assertFalse(media['publication_ready'])
|
||
|
|
self.assertEqual(media['source_audit']['source_files'],before)
|
||
|
|
self.assertNotIn('url',core.load(self.output/'schema.jsonld'))
|
||
|
|
|
||
|
|
def test_originals_and_prior_output_preserved(self):
|
||
|
|
self.build()
|
||
|
|
original=(self.folder/'different-name.txt').read_bytes()
|
||
|
|
receipt=(self.output/'build-receipt.json').read_bytes()
|
||
|
|
with self.assertRaisesRegex(ValueError,'Output exists'):
|
||
|
|
self.build()
|
||
|
|
self.assertEqual((self.folder/'different-name.txt').read_bytes(),original)
|
||
|
|
self.assertEqual((self.output/'build-receipt.json').read_bytes(),receipt)
|
||
|
|
|
||
|
|
def test_txt_mismatch_is_review_issue_not_silent_rewrite(self):
|
||
|
|
(self.folder/'different-name.txt').write_text('An alternate transcript.',encoding='utf-8')
|
||
|
|
self.build()
|
||
|
|
self.assertFalse(core.load(self.output/'media.json')['source_audit']['txt_vtt_match'])
|
||
|
|
self.assertIn('TXT and VTT differ',(self.output/'review.md').read_text(encoding='utf-8'))
|
||
|
|
self.assertEqual((self.folder/'different-name.txt').read_text(),'An alternate transcript.')
|
||
|
|
|
||
|
|
def test_source_tampering_invalidates_verification(self):
|
||
|
|
self.build()
|
||
|
|
(self.folder/'recording.mp3').write_bytes(b'changed')
|
||
|
|
with self.assertRaisesRegex(ValueError,'Original source changed'):
|
||
|
|
fb.verify(self.output)
|
||
|
|
|
||
|
|
def test_mechanical_coverage_before_write(self):
|
||
|
|
self.edit(self.plan,lambda p:p['chunks'][0]['cue_ids'].pop())
|
||
|
|
with self.assertRaisesRegex(ValueError,'each included cue'):
|
||
|
|
self.build()
|
||
|
|
self.assertFalse(self.output.exists())
|
||
|
|
|
||
|
|
def test_local_exact_evidence_and_search(self):
|
||
|
|
quote='Keep the plain text transcript and the timed VTT file together.'
|
||
|
|
def change(plan):
|
||
|
|
plan['qa']=[{'qa_id':'recording:qa:files','question':'Which transcript files are useful?',
|
||
|
|
'answer':quote,'answer_type':'extractive','evidence':[{'chunk_id':plan['chunks'][0]['chunk_id'],
|
||
|
|
'cue_id':'cue-000002','quote':quote}]}]
|
||
|
|
self.edit(self.plan,change)
|
||
|
|
self.build()
|
||
|
|
results = fb.search(self.output,'transcript files',10)
|
||
|
|
self.assertEqual(results[0]['type'],'qa')
|
||
|
|
q=next(r for r in results if r['type']=='qa')
|
||
|
|
self.assertEqual(q['citations'][0]['transcript_ref'],'transcript.md#cue-000002')
|
||
|
|
self.assertIsNone(q['citations'][0]['transcript_url'])
|
||
|
|
self.assertIsNone(q['citations'][0]['playback_url'])
|
||
|
|
|
||
|
|
def test_excluded_cue_not_exported_and_original_kept(self):
|
||
|
|
self.edit(self.intake,lambda i:i.update(excluded_cue_ids=['cue-000001']))
|
||
|
|
self.edit(self.plan,lambda p:p['chunks'][0]['cue_ids'].remove('cue-000001'))
|
||
|
|
self.build()
|
||
|
|
cues=core.load(self.output/'transcript.json')['cues']
|
||
|
|
self.assertNotIn('cue-000001',{c['cue_id'] for c in cues})
|
||
|
|
self.assertIn('Today we discuss',(self.folder/'different-name.vtt').read_text())
|
||
|
|
|
||
|
|
def test_ambiguous_sources_require_selection(self):
|
||
|
|
(self.folder/'second.mp3').write_bytes(b'other')
|
||
|
|
with self.assertRaisesRegex(ValueError,'Expected one media'):
|
||
|
|
fb.inspect(self.folder)
|
||
|
|
# Existing explicit selection still works.
|
||
|
|
self.build()
|
||
|
|
|
||
|
|
def test_mp3_encoded_duration_from_frames(self):
|
||
|
|
path=self.folder/'encoded.mp3'
|
||
|
|
frame=bytes.fromhex('fffb9000')+bytes(413)
|
||
|
|
path.write_bytes(frame*100)
|
||
|
|
self.assertAlmostEqual(fb.mp3_duration(path),100*1152/44100,places=3)
|
||
|
|
|
||
|
|
def test_no_fake_human_approval(self):
|
||
|
|
self.edit(self.plan,lambda p:p['review'].update(reviewer='Invented reviewer'))
|
||
|
|
with self.assertRaisesRegex(ValueError,'claimed human approval'):
|
||
|
|
self.build()
|
||
|
|
|
||
|
|
def test_outside_source_folder_rejected(self):
|
||
|
|
with self.assertRaisesRegex(ValueError,'immediate subfolder'):
|
||
|
|
fb.build(self.folder,self.intake,self.plan,Path(self.temp.name)/'outside')
|
||
|
|
|
||
|
|
def test_deterministic_local_builds(self):
|
||
|
|
self.build()
|
||
|
|
other=self.folder/'structured-002'
|
||
|
|
fb.build(self.folder,self.intake,self.plan,other)
|
||
|
|
self.assertEqual(core.load(self.output/'build-receipt.json'),core.load(other/'build-receipt.json'))
|
||
|
|
|
||
|
|
|
||
|
|
if __name__=='__main__':
|
||
|
|
unittest.main()
|