# -*- coding: utf-8 -*- from __future__ import annotations import time from app.timeutil import push_date_from_ts def test_claim_upload_before_pending_record(temp_db): ch = temp_db.create_shorts_channel(name="P", enabled=True) day = push_date_from_ts(time.time()) pending = temp_db.create_shorts_job( session_id="", room_id="pending1", channel_pk=ch["id"], push_date=day, ) due = temp_db.create_shorts_job( session_id="", room_id="due1", channel_pk=ch["id"], push_date=day, ) temp_db.update_shorts_job( due["id"], status="scheduled", clip_path="/tmp/x.mp4", upload_at=time.time() - 10, ) claimed = temp_db.claim_due_shorts_upload() assert claimed is not None assert claimed["id"] == due["id"] assert claimed["status"] == "uploading" # pending still pending row = temp_db.get_shorts_job(pending["id"]) assert row["status"] == "pending" def test_reclaim_stale_uploading(temp_db): ch = temp_db.create_shorts_channel(name="S", enabled=True) day = push_date_from_ts(time.time()) job = temp_db.create_shorts_job( session_id="", room_id="stale1", channel_pk=ch["id"], push_date=day, ) assert job is not None temp_db.update_shorts_job( job["id"], status="uploading", clip_path="/tmp/x.mp4", upload_at=time.time(), started_at=time.time() - 500, ) # Heartbeat uses updated_at — force it stale with temp_db._lock: conn = temp_db._connect() try: conn.execute( "UPDATE shorts_jobs SET updated_at = ? WHERE id = ?", (time.time() - 500, job["id"]), ) conn.commit() finally: conn.close() n = temp_db.reclaim_stale_shorts_uploads(max_age_sec=120) assert n == 1 row = temp_db.get_shorts_job(job["id"]) assert row["status"] == "scheduled" def test_create_shorts_job_dedupes_room_day(temp_db): ch = temp_db.create_shorts_channel(name="D", enabled=True) day = push_date_from_ts(time.time()) a = temp_db.create_shorts_job(session_id="", room_id="dup1", channel_pk=ch["id"], push_date=day) b = temp_db.create_shorts_job(session_id="", room_id="dup1", channel_pk=ch["id"], push_date=day) assert a is not None assert b is None def test_finalize_shorts_upload_once(temp_db): ch = temp_db.create_shorts_channel(name="F", enabled=True) job = temp_db.create_shorts_job(session_id="", room_id="fin1", channel_pk=ch["id"]) assert job is not None temp_db.update_shorts_job(job["id"], status="uploading", clip_path="/tmp/x.mp4") assert temp_db.finalize_shorts_upload(job["id"], youtube_video_id="vid1", youtube_url="http://y/1") assert not temp_db.finalize_shorts_upload(job["id"], youtube_video_id="vid2", youtube_url="http://y/2") row = temp_db.get_shorts_job(job["id"]) assert row["youtube_video_id"] == "vid1" assert row["status"] == "uploaded"