Spaces:
Sleeping
Sleeping
Download tests/test_remote_writer_isolation.py from Simzy/Wan-2.2-Studio: direct link, hf CLI and curl.
- Browser
- Download file 4.53 kB
-
https://huggingface.co/spaces/Simzy/Wan-2.2-Studio/resolve/main/tests/test_remote_writer_isolation.py
- Command line
-
hf download hf://spaces/Simzy/Wan-2.2-Studio/tests/test_remote_writer_isolation.py
-
curl -L -o test_remote_writer_isolation.py https://huggingface.co/spaces/Simzy/Wan-2.2-Studio/resolve/main/tests/test_remote_writer_isolation.py
4.53 kB
| """Exercise writer routing without sending any AI or network requests.""" | |
| import ast | |
| import contextvars | |
| from pathlib import Path | |
| import sys | |
| import types | |
| import unittest | |
| from unittest.mock import patch | |
| from gradio_client import Client | |
| SOURCE = Path(__file__).resolve().parents[1] / "app.py" | |
| class RemoteWriterIsolationTests(unittest.TestCase): | |
| def test_private_economy_cannot_inherit_the_owner_writer_token(self): | |
| seen = [] | |
| class OfflineClient: | |
| def __init__(self, source, token=None): | |
| seen.append((source, token)) | |
| tree = ast.parse(SOURCE.read_text(encoding="utf-8")) | |
| function = next(node for node in tree.body | |
| if isinstance(node, ast.FunctionDef) and node.name == "_make_remote_client") | |
| target = "amisima/Qwen3.5-Picture-Captioner" | |
| namespace = { | |
| "os": types.SimpleNamespace(environ={"EXPANDER_HF_TOKEN": "synthetic-owner-only"}), | |
| "is_economy": lambda writer: writer == target, | |
| } | |
| exec(compile(ast.Module([function], type_ignores=[]), str(SOURCE), "exec"), namespace) | |
| client_module = types.ModuleType("gradio_client") | |
| client_module.Client = OfflineClient | |
| with patch.dict(sys.modules, {"gradio_client": client_module}): | |
| namespace["_make_remote_client"](target) | |
| namespace["_make_remote_client"]("owner/original-writer") | |
| self.assertEqual(seen, [(target, False), ("owner/original-writer", "synthetic-owner-only")]) | |
| def test_writer_does_not_reuse_a_previous_visitors_quota_header(self): | |
| request = contextvars.ContextVar("test_request", default=None) | |
| context = types.ModuleType("gradio.context") | |
| context.LocalContext = types.SimpleNamespace(request=request) | |
| seen = [] | |
| clients = [] | |
| closed = [] | |
| class OfflineClient: | |
| space_id = "owner/writer" | |
| should_fail = False | |
| def __init__(self): | |
| self.headers = {} | |
| clients.append(self) | |
| def submit(self, *args, **kwargs): | |
| if self.should_fail: | |
| raise RuntimeError("synthetic request failure") | |
| # This is how upstream Client.send_data passes its headers. | |
| headers = Client.add_zero_gpu_headers(self, self.headers) | |
| seen.append(headers.get("x-ip-token")) | |
| return types.SimpleNamespace(result=lambda **kwargs: "A complete prompt.") | |
| def close(self): | |
| closed.append(self) | |
| tree = ast.parse(SOURCE.read_text(encoding="utf-8")) | |
| function = next(node for node in tree.body | |
| if isinstance(node, ast.FunctionDef) and node.name == "_remote_ask") | |
| namespace = { | |
| "is_economy": lambda writer: False, | |
| "_REMOTE_CLIENTS": {}, "_make_remote_client": lambda _: OfflineClient(), | |
| "prepare_request": lambda writer, message, image: (writer, message, image), | |
| "_remote_plan": lambda *args, **kwargs: ([((), {"api_name": "/write_prompt"})], []), | |
| "writer_result": lambda response: response, | |
| "_runtime_event": lambda *args, **kwargs: None, | |
| "os": types.SimpleNamespace(environ={}), | |
| } | |
| exec(compile(ast.Module([function], type_ignores=[]), str(SOURCE), "exec"), namespace) | |
| fake_gradio = types.ModuleType("gradio") | |
| fake_gradio.context = context | |
| with patch.dict(sys.modules, {"gradio": fake_gradio, "gradio.context": context}): | |
| for visitor in ("synthetic-visitor-a", None, "synthetic-visitor-b"): | |
| headers = {"x-ip-token": visitor} if visitor else {} | |
| token = request.set(types.SimpleNamespace(headers=headers)) | |
| try: | |
| result = namespace["_remote_ask"]("owner/writer", "A dog turns its head") | |
| self.assertEqual(result, "A complete prompt.") | |
| finally: | |
| request.reset(token) | |
| self.assertEqual(seen, ["synthetic-visitor-a", None, "synthetic-visitor-b"]) | |
| self.assertEqual(len(clients), 3) | |
| self.assertEqual(closed, clients) | |
| OfflineClient.should_fail = True | |
| with self.assertRaisesRegex(RuntimeError, "synthetic request failure"): | |
| namespace["_remote_ask"]("owner/writer", "A dog turns its head") | |
| self.assertEqual(len(clients), 4) | |
| self.assertEqual(closed, clients) | |
| if __name__ == "__main__": | |
| unittest.main() | |