|
1 | | -"""Integration test for the Pollinations text API using the Unity model.""" |
| 1 | +"""Static validation that Pollinations referrer configuration is present.""" |
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import json |
| 6 | +import re |
6 | 7 | import time |
| 8 | +from pathlib import Path |
7 | 9 | from typing import Any, Dict |
8 | 10 |
|
9 | | -import requests |
| 11 | +TEST_NAME = "Pollinations referrer configuration" |
| 12 | +EXPECTED_REFERRER = "https://www.unityailab.com/" |
| 13 | +EXPECTED_IMAGE_REFERRER = "referrer=unityailab.com" |
10 | 14 |
|
11 | | -TEST_NAME = "Pollinations Unity text response" |
12 | | -API_URL = "https://text.pollinations.ai/openai" |
13 | | -REQUEST_HEADERS = { |
14 | | - "Content-Type": "application/json", |
15 | | - # Pollinations expects requests to identify the Unity AI Lab origin when |
16 | | - # using the OpenAI-compatible endpoint. Without these headers the service |
17 | | - # replies with HTTP 402 (Payment Required) even though the public endpoint |
18 | | - # is free to use. Supplying Origin/Referer makes the intent explicit and |
19 | | - # avoids the erroneous billing prompt. |
20 | | - "Origin": "https://unityailab.com", |
21 | | - "Referer": "https://unityailab.com/", |
22 | | - "User-Agent": "ShittyVoiceTest/1.0", |
23 | | -} |
| 15 | +ROOT = Path(__file__).resolve().parent.parent |
| 16 | +APP_JS_PATH = ROOT / "app.js" |
24 | 17 |
|
25 | 18 |
|
26 | 19 | def run() -> Dict[str, Any]: |
27 | | - """Execute the test and return a structured result dictionary.""" |
| 20 | + """Ensure the frontend sends the Unity AI Lab referrer to Pollinations.""" |
| 21 | + |
28 | 22 | start = time.perf_counter() |
29 | | - payload = { |
30 | | - "model": "unity", |
31 | | - "messages": [ |
32 | | - {"role": "system", "content": "You are Unity, a concise greeter."}, |
33 | | - {"role": "user", "content": "Say hello and include the word Unity exactly once."}, |
34 | | - ], |
35 | | - } |
36 | 23 |
|
37 | 24 | try: |
38 | | - response = requests.post( |
39 | | - API_URL, |
40 | | - json=payload, |
41 | | - headers=REQUEST_HEADERS, |
42 | | - timeout=20, |
| 25 | + source = APP_JS_PATH.read_text(encoding="utf-8") |
| 26 | + |
| 27 | + if EXPECTED_REFERRER not in source: |
| 28 | + raise AssertionError( |
| 29 | + "Unity referrer constant is missing from app.js" |
| 30 | + ) |
| 31 | + |
| 32 | + fetch_block = re.search( |
| 33 | + r"fetch\(POLLINATIONS_TEXT_URL,\s*\{(?P<body>.*?)\}\s*\)", |
| 34 | + source, |
| 35 | + flags=re.DOTALL, |
43 | 36 | ) |
44 | | - duration = time.perf_counter() - start |
45 | | - response.raise_for_status() |
46 | | - data = response.json() |
47 | | - content = data.get("choices", [{}])[0].get("message", {}).get("content", "").strip() |
| 37 | + if not fetch_block: |
| 38 | + raise AssertionError("Could not locate Pollinations fetch configuration") |
| 39 | + |
| 40 | + body = fetch_block.group("body") |
| 41 | + if "referrer: UNITY_REFERRER" not in body: |
| 42 | + raise AssertionError("Fetch call does not forward UNITY_REFERRER") |
48 | 43 |
|
49 | | - if not content: |
50 | | - raise ValueError("No content returned from Pollinations API") |
| 44 | + if "referrerPolicy" not in body: |
| 45 | + raise AssertionError("Fetch call is missing an explicit referrer policy") |
51 | 46 |
|
52 | | - if "unity" not in content.lower(): |
53 | | - raise AssertionError("Response did not mention Unity") |
| 47 | + if EXPECTED_IMAGE_REFERRER not in source: |
| 48 | + raise AssertionError("Image endpoint is missing the referrer query parameter") |
54 | 49 |
|
| 50 | + duration = time.perf_counter() - start |
55 | 51 | return { |
56 | 52 | "name": TEST_NAME, |
57 | 53 | "status": "passed", |
58 | | - "details": content, |
| 54 | + "details": "Verified Pollinations referrer headers are configured.", |
59 | 55 | "duration": duration, |
60 | 56 | } |
61 | | - except Exception as exc: # noqa: BLE001 - broad catch to report failure details |
| 57 | + except Exception as exc: # noqa: BLE001 - report details in CI |
62 | 58 | duration = time.perf_counter() - start |
63 | 59 | return { |
64 | 60 | "name": TEST_NAME, |
|
0 commit comments