From 2125a7d12e06de669484142cbacddea7ca5a5972 Mon Sep 17 00:00:00 2001 From: brunnodev <169157603+brunnojob@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:35:02 -0300 Subject: [PATCH] Add daily Jalali report workflow and mocked integration tests --- CHANGELOG.md | 5 ++ README.md | 8 +++ examples/workflows/daily-report-jalali.yaml | 34 +++++++++++ tests/test_examples.py | 63 +++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 examples/workflows/daily-report-jalali.yaml create mode 100644 tests/test_examples.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 6671540..51b7621 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ project uses [Semantic Versioning](https://semver.org/). ## [Unreleased] +### Added + +- `daily-report-jalali` example: a daily website check with a Persian Telegram + report, Tehran-local Jalali date and Persian digits. + ## [0.1.0] - 2026-10-08 First public release. diff --git a/README.md b/README.md index d16842d..acf2a62 100644 --- a/README.md +++ b/README.md @@ -155,11 +155,19 @@ All in [`examples/workflows`](examples/workflows) — run `flowpilot -p examples | [`github-release-watcher`](examples/workflows/github-release-watcher.yaml) | cron | Announces each new GitHub release exactly once (persistent state) | | [`uptime-monitor`](examples/workflows/uptime-monitor.yaml) | interval | Alerts only when a site goes **down** or **recovers** | | [`crypto-digest`](examples/workflows/crypto-digest.yaml) | cron 09:00 Asia/Tehran | Crypto prices (CoinGecko) + USD exchange rates, with the Jalali date | +| [`daily-report-jalali`](examples/workflows/daily-report-jalali.yaml) | cron 09:00 Asia/Tehran | Website HTTP check → Persian Telegram report with the Jalali date | | [`rss-to-telegram`](examples/workflows/rss-to-telegram.yaml) | feed | One channel post per new feed entry, cleaned by a custom plugin step | | [`nightly-backup`](examples/workflows/nightly-backup.yaml) | cron | `tar.gz` backup with 7-day retention, uploaded to Telegram, alert on failure | | [`contact-form`](examples/workflows/contact-form.yaml) | webhook + secret | Website contact form → Telegram | | [`disk_space.py`](examples/workflows/disk_space.py) | cron | A workflow **written in Python** with its own custom step | +To try `daily-report-jalali`, copy `examples/.env.example` to `examples/.env` and set +`TELEGRAM_BOT_TOKEN` and `TELEGRAM_CHAT_ID`. Edit `vars.url` in the workflow to +choose the website, then run `flowpilot -p examples run daily-report-jalali`. +Use `flowpilot -p examples serve` to enable its daily 09:00 `Asia/Tehran` schedule. +The report includes HTTP errors such as 503; network failures or timeouts fail +the run. The Jalali date uses Tehran time and Persian digits. + ## 🏗 Architecture ```mermaid diff --git a/examples/workflows/daily-report-jalali.yaml b/examples/workflows/daily-report-jalali.yaml new file mode 100644 index 0000000..535bfb9 --- /dev/null +++ b/examples/workflows/daily-report-jalali.yaml @@ -0,0 +1,34 @@ +name: daily-report-jalali +description: Check a website daily and send a Persian report with the Jalali date to Telegram. +tags: [monitoring, telegram, jalali] +trigger: + type: cron + cron: "0 9 * * *" + timezone: Asia/Tehran + +vars: + url: https://example.com + +steps: + - id: check + type: http + with: + url: "{{ vars.url }}" + timeout: 15 + fail_on_error: false + + - id: report + type: transform + with: + template: | + گزارش روزانه وضعیت وب‌سایت + تاریخ: {{ now('Asia/Tehran') | jalali('%d %B %Y', true) }} + نشانی: {{ vars.url | e }} + وضعیت: {{ 'در دسترس' if steps.check.output.ok else 'خطای HTTP' }} + کد HTTP: {{ steps.check.output.status | fa_digits }} + زمان پاسخ: {{ steps.check.output.elapsed_ms | fa_digits }} میلی‌ثانیه + + - id: send + type: telegram + with: + text: "{{ steps.report.output }}" diff --git a/tests/test_examples.py b/tests/test_examples.py new file mode 100644 index 0000000..8f973e9 --- /dev/null +++ b/tests/test_examples.py @@ -0,0 +1,63 @@ +"""Run the Jalali report example with a fixed clock and mocked HTTP/Telegram APIs.""" + +from __future__ import annotations + +import json +from datetime import datetime +from pathlib import Path +from zoneinfo import ZoneInfo + +import httpx +import pytest +import respx + +from flowpilot.loader import load_yaml_file +from flowpilot.templating import ENV + +EXAMPLE = Path(__file__).resolve().parent.parent / "examples/workflows/daily-report-jalali.yaml" + + +@pytest.mark.parametrize( + ("status", "expected_status", "expected_code"), + [(200, "در دسترس", "۲۰۰"), (503, "خطای HTTP", "۵۰۳")], +) +@respx.mock +async def test_daily_report_jalali(engine, monkeypatch, status, expected_status, expected_code): + monkeypatch.setenv("TELEGRAM_BOT_TOKEN", "TEST_TOKEN") + monkeypatch.setenv("TELEGRAM_CHAT_ID", "42") + engine.settings.telegram_api_base = "https://telegram.test" + clock_zones = [] + + def fixed_now(tz): + clock_zones.append(tz) + return datetime(2026, 3, 21, 9, tzinfo=ZoneInfo(tz)) + + monkeypatch.setitem(ENV.globals, "now", fixed_now) + wf = load_yaml_file(EXAMPLE, engine.registry) + assert wf.trigger.type == "cron" + assert wf.trigger.cron == "0 9 * * *" + assert wf.trigger.timezone == "Asia/Tehran" + wf.vars["url"] = "https://example.com/?a=1&b=2" + engine.workflows[wf.name] = wf + check = respx.get(wf.vars["url"]).mock(return_value=httpx.Response(status)) + telegram = respx.post("https://telegram.test/botTEST_TOKEN/sendMessage").mock( + return_value=httpx.Response(200, json={"ok": True, "result": {"message_id": 7}}) + ) + + result = await engine.run(wf.name) + + assert result.status == "success" + assert check.call_count == telegram.call_count == 1 + assert clock_zones == ["Asia/Tehran"] + message = json.loads(telegram.calls[0].request.content) + assert message["chat_id"] == "42" + assert message["parse_mode"] == "HTML" + assert "۰۱ فروردین ۱۴۰۵" in message["text"] + assert expected_status in message["text"] + assert f"کد HTTP: {expected_code}" in message["text"] + assert "https://example.com/?a=1&b=2" in message["text"] + elapsed = str(result.steps["check"]["output"]["elapsed_ms"]).translate( + str.maketrans("0123456789", "۰۱۲۳۴۵۶۷۸۹") + ) + assert f"زمان پاسخ: {elapsed} میلی‌ثانیه" in message["text"] + assert result.steps["send"]["output"]["message_ids"] == [7]