"""Live details/cast/related checks on the onn. Temporarily toggles unwatched fixtures. The movie and every episode of the show must start unwatched with no resume point. Both are restored, including on failure. Do not run beside another device/UI test. """ import json,time,urllib.parse,urllib.request from device import adb,OUT,screenshot from inspect_server import get,base,token from verify_ui import launch,key,text,focused,wait,focus,labels,snapshot MOVIE='36486' # Matchbox: first recent-release movie on the private test server. SHOW='16413' # 3 Body Problem: first A-Z show, eight initially unwatched episodes. def metadata(ident):return get('/library/metadata/'+ident)['Metadata'][0] def leaves(ident):return get('/library/metadata/'+ident+'/allLeaves').get('Metadata',[]) def clean(item):return not item.get('viewCount',0) and not item.get('viewOffset',0) def reset(ident): # Cleanup uses Plex's same endpoint if a failing UI cannot restore the fixture. path='/:/unscrobble?'+urllib.parse.urlencode({'key':ident,'identifier':'com.plexapp.plugins.library'}) request=urllib.request.Request(base+path,headers={'X-Plex-Token':token,'Accept':'application/json'}) with urllib.request.urlopen(request,timeout=20) as response:assert response.status==200 def main(): movie=metadata(MOVIE);show=metadata(SHOW);episodes=leaves(SHOW) assert clean(movie),'Movie fixture has existing watched/resume state; choose another fixture' assert episodes and all(clean(e) for e in episodes),'Show fixture has existing watched/resume state; choose another fixture' results=[];touched=set() try: launch();key(22);focused('Movies');text('Recently released');key(20);focused('Discover');key(20);key(23) text(movie['title']);text('Cast');focused('Play');text('Mark watched') key(22);focused('Mark watched');touched.add(MOVIE);key(23);focused('Mark unwatched') assert metadata(MOVIE).get('viewCount',0)>0 screenshot('details-movie-watched');key(23);focused('Mark watched');assert clean(metadata(MOVIE)) results.append('Movie watched/unwatched persisted in Plex');print('PASS '+results[-1],flush=True) key(21);focused('Play');key(20) actor=movie['Role'][0];focused(actor['tag']);key(23) expected=[] for section in get('/library/sections').get('Directory',[]): if section['type'] in ('movie','show'): expected.extend(get('/library/sections/'+section['key']+'/all?actor='+str(actor['id'])).get('Metadata',[])) expected={j['ratingKey']:j for j in expected};text(actor['tag']);text(str(len(expected))+' titles') wait(lambda r:focus(r) not in ('','Back'),'Actor title grid focus');first=focus(snapshot());key(22);second=focus(snapshot());key(23) wait(lambda r:'Cast' in labels(r),'Selected actor title details');key(4);focused(second);key(4);focused(actor['tag']) screenshot('details-cast-restored') results.append('Actor titles match both Plex libraries; title and cast focus restore');print('PASS '+results[-1],flush=True) key(4);key(4);focused('Home');key(22,2);focused('TV Shows');key(20);focused('Discover');key(22);focused('Library');key(23) wait(lambda r:any(x.startswith('All genres') for x in labels(r)),'Show library filters loaded');focused('Library');key(20) if show['title'] not in focus(snapshot()):key(21) focused(show['title']);key(23) text('Seasons');focused('Watch next');text('Mark watched');key(22);focused('Mark watched');touched.add(SHOW);key(23);focused('Mark unwatched') assert metadata(SHOW).get('viewedLeafCount')==len(episodes) assert all(e.get('viewCount',0)>0 for e in leaves(SHOW)) screenshot('details-show-watched');key(23);focused('Mark watched') assert metadata(SHOW).get('viewedLeafCount',0)==0 and all(clean(e) for e in leaves(SHOW)) results.append('Series watched/unwatched updates every episode in Plex');print('PASS '+results[-1],flush=True) related=get('/library/metadata/'+SHOW+'/related').get('Hub',[]) expected_related={m['ratingKey'] for h in related for m in h.get('Metadata',[]) if m.get('type')=='show' and m['ratingKey']!=SHOW} assert expected_related,'Related-show fixture missing server-generated hubs' key(21);focused('Watch next');key(20);text('Seasons');key(20);focused(show['Role'][0]['tag']);key(20);text('Related shows') selected=focus(snapshot());assert selected and selected not in ('Watch next','Mark watched') screenshot('details-related-shows');key(23);focused('Watch next');key(4);focused(selected) results.append('Server-related shows render, open and restore focus');print('PASS '+results[-1],flush=True) key(4,3);focused('Home') finally: for ident in touched: changed=not clean(metadata(ident)) if ident==MOVIE else any(not clean(e) for e in leaves(ident)) if changed:reset(ident) assert clean(metadata(MOVIE)) and all(clean(e) for e in leaves(SHOW)),'Fixture restoration failed' (OUT/'details-feature-results.json').write_text(json.dumps(results,indent=2)+'\n') print('PASS original unwatched/resume state restored for both fixtures',flush=True) if __name__=='__main__':main()