78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
from journal_bot.ingest import ingest_once
|
|
from journal_bot.queue import Queue
|
|
from journal_bot.state import State
|
|
from journal_bot.telegram_client import ParsedUpdate
|
|
|
|
|
|
class FakeTelegram:
|
|
def __init__(self, updates):
|
|
self._updates = updates
|
|
self.reactions: list[tuple[int, int]] = []
|
|
self.downloads: list[str] = []
|
|
|
|
async def get_updates(self, offset, timeout=0):
|
|
return [u for u in self._updates if u.update_id > offset]
|
|
|
|
async def download_file(self, file_id, dest):
|
|
self.downloads.append(file_id)
|
|
dest.write_bytes(b"audio")
|
|
return dest
|
|
|
|
async def react_ok(self, chat_id, message_id):
|
|
self.reactions.append((chat_id, message_id))
|
|
|
|
|
|
class FakeTranscriber:
|
|
def transcribe(self, path, premium_text=None):
|
|
from journal_bot.transcribe import TranscriptionResult
|
|
return TranscriptionResult(True, "transkribiert", "whisper")
|
|
|
|
|
|
async def test_ingest_text_creates_queue_item(tmp_path):
|
|
upd = ParsedUpdate(
|
|
update_id=10, message_id=1, chat_id=42,
|
|
date=datetime(2026, 6, 14, 14, 32, tzinfo=timezone.utc),
|
|
kind="text", text="Hallo",
|
|
)
|
|
queue = Queue(tmp_path / "q")
|
|
state = State(tmp_path / "s")
|
|
tg = FakeTelegram([upd])
|
|
await ingest_once(tg, FakeTranscriber(), queue, state, attachments_dir=tmp_path / "att")
|
|
items = list((tmp_path / "q" / "pending").iterdir())
|
|
assert len(items) == 1
|
|
assert state.last_update_id == 10
|
|
assert state.has_processed(10)
|
|
assert (42, 1) in tg.reactions
|
|
|
|
|
|
async def test_ingest_voice_transcribes(tmp_path):
|
|
upd = ParsedUpdate(
|
|
update_id=20, message_id=2, chat_id=42,
|
|
date=datetime(2026, 6, 14, 14, 32, tzinfo=timezone.utc),
|
|
kind="voice", voice_file_id="VOICE",
|
|
)
|
|
queue = Queue(tmp_path / "q")
|
|
state = State(tmp_path / "s")
|
|
tg = FakeTelegram([upd])
|
|
await ingest_once(tg, FakeTranscriber(), queue, state, attachments_dir=tmp_path / "att")
|
|
import json
|
|
item_data = json.loads(next((tmp_path / "q" / "pending").iterdir()).read_text(encoding="utf-8"))
|
|
assert item_data["text"] == "transkribiert"
|
|
assert item_data["type"] == "voice"
|
|
|
|
|
|
async def test_ingest_skips_already_processed(tmp_path):
|
|
upd = ParsedUpdate(
|
|
update_id=30, message_id=3, chat_id=42,
|
|
date=datetime(2026, 6, 14, 14, 32, tzinfo=timezone.utc),
|
|
kind="text", text="Hallo",
|
|
)
|
|
queue = Queue(tmp_path / "q")
|
|
state = State(tmp_path / "s")
|
|
state.mark_processed(30)
|
|
tg = FakeTelegram([upd])
|
|
await ingest_once(tg, FakeTranscriber(), queue, state, attachments_dir=tmp_path / "att")
|
|
assert not any((tmp_path / "q" / "pending").iterdir())
|