import pytest import datetime from django.utils import timezone from core.models import Channel, AppUser, Library, MediaSource, ChannelSourceRule, ScheduleTemplate, ScheduleBlock @pytest.mark.django_db def test_channel_status_and_download_flow(client): # 1. Setup mock user, library, channel user = AppUser.objects.create(username="testuser") library = Library.objects.create(name="Test Library", owner_user=user) channel = Channel.objects.create( name="Action Channel", slug="action-ch", owner_user=user, library=library, scheduling_mode="fill_blocks" ) # 2. Add the Solid Color source (this requires no real downloading) source = MediaSource.objects.create( library=library, name="Color Test", source_type="solid_color" ) from core.models import MediaItem MediaItem.objects.create( media_source=source, title="Test Solid Color Item", item_kind="movie", runtime_seconds=3600, file_path="dummy_path" ) # 3. Create a schedule template and a 24/7 block template = ScheduleTemplate.objects.create( channel=channel, name="Daily", timezone_name="UTC", is_active=True ) block = ScheduleBlock.objects.create( schedule_template=template, name="All Day Block", block_type="programming", start_local_time=datetime.time(0, 0), end_local_time=datetime.time(23, 59), day_of_week_mask=127 ) # Map the solid_color source to the channel ChannelSourceRule.objects.create( channel=channel, media_source=source, rule_mode="allow", weight=1.0, schedule_block_label="All Day Block" ) # 4. Check initial status (should be 0 airings) resp = client.get(f"/api/channel/{channel.id}/status") assert resp.status_code == 200 data = resp.json() assert data["total_upcoming_airings"] == 0 # 5. Generate schedule for today resp = client.post(f"/api/schedule/generate-today/{channel.id}") assert resp.status_code == 200 assert resp.json()["status"] == "success" assert resp.json()["airings_created"] > 0 # 6. Check status again (airings exist, but might not be cached yet depending on source type logic) resp = client.get(f"/api/channel/{channel.id}/status") assert resp.status_code == 200 data = resp.json() assert data["total_upcoming_airings"] > 0 # 7. Trigger the channel download endpoint resp = client.post(f"/api/channel/{channel.id}/download") assert resp.status_code == 200 cache_data = resp.json() assert "downloaded" in cache_data # 8. Final status check resp = client.get(f"/api/channel/{channel.id}/status") assert resp.status_code == 200 final_data = resp.json() # Solid colors don't need real downloads, but we ensure the API reported successfully assert "percent_cached" in final_data