Spaces:
Running
Running
Download admin_ui_regression.py from vLAR/PhysInOne-Leaderboard: direct link, hf CLI and curl.
- Browser
- Download file 11.7 kB
-
https://huggingface.co/spaces/vLAR/PhysInOne-Leaderboard/resolve/main/admin_ui_regression.py
- Command line
-
hf download hf://spaces/vLAR/PhysInOne-Leaderboard/admin_ui_regression.py
-
curl -L -o admin_ui_regression.py https://huggingface.co/spaces/vLAR/PhysInOne-Leaderboard/resolve/main/admin_ui_regression.py
11.7 kB
| """Real Gradio callback checks with synthetic records and no external requests.""" | |
| import asyncio | |
| import json | |
| import os | |
| import time | |
| import unittest | |
| from unittest.mock import patch | |
| import staging_regression as staging | |
| class AdminUIChecks(unittest.TestCase): | |
| def setUpClass(cls): | |
| staging.StagingChecks.setUpClass() | |
| cls.ui = staging.StagingChecks.ui | |
| import gradio as gr | |
| cls.gr = gr | |
| cls.root = staging.StagingChecks.root / "data" | |
| def tearDownClass(cls): | |
| staging.StagingChecks.tearDownClass() | |
| def request(self, subject="audit-subject", expiry=None): | |
| return self.gr.Request(session_hash="admin-fixture", session={"oauth_info": { | |
| "userinfo": {"sub": subject, "preferred_username": "audit-user", "name": "Fixture", | |
| "profile": "https://huggingface.co/audit-user", "picture": ""}, | |
| "expires_at": expiry if expiry is not None else time.time() + 3600}}) | |
| def test_real_gradio_admin_visibility_and_ordinary_user_denial(self): | |
| from gradio.state_holder import SessionState | |
| access = next(function for function in self.ui.demo.fns.values() | |
| if function.name == "access" and function.outputs[0].elem_id == "admin-console") | |
| with patch.dict(os.environ, {"LEADERBOARD_ADMIN_HF_SUBJECTS": "audit-subject"}): | |
| state = SessionState(self.ui.demo) | |
| response = asyncio.run(self.ui.demo.process_api(access, [], state=state, request=self.request())) | |
| self.assertTrue(response["data"][0]["visible"]) | |
| response = asyncio.run(self.ui.demo.process_api(access, [], state=state, request=self.request("other-subject"))) | |
| self.assertFalse(response["data"][0]["visible"]) | |
| response = asyncio.run(self.ui.demo.process_api(access, [], state=state, request=self.request(expiry=time.time() - 1))) | |
| self.assertFalse(response["data"][0]["visible"]) | |
| def test_real_queue_callback_permissions_and_column_contract(self): | |
| from gradio.state_holder import SessionState | |
| load = next(function for function in self.ui.demo.fns.values() | |
| if function.name == "load" and "admin-queue-table" in function.outputs[0].elem_classes) | |
| with patch.dict(os.environ, {"LEADERBOARD_ADMIN_HF_SUBJECTS": "audit-subject"}): | |
| response = asyncio.run(self.ui.demo.process_api(load, ["All Tasks", 1, None, None, None, None], state=SessionState(self.ui.demo), request=self.request())) | |
| self.assertIn("Submission ID", response["data"][0]["value"]["headers"]) | |
| with self.assertRaises(self.gr.Error): | |
| asyncio.run(self.ui.demo.process_api(load, ["All Tasks", 1, None, None, None, None], state=SessionState(self.ui.demo), request=self.request("other-subject"))) | |
| def test_cancel_is_no_write_and_controls_have_confirmation(self): | |
| from src.maintenance_state import read_control | |
| from gradio.state_holder import SessionState | |
| functions = [function for function in self.ui.demo.fns.values() if function.name == "apply"] | |
| self.assertEqual(len(functions), 2) | |
| before = read_control(self.root) | |
| with patch.dict(os.environ, {"LEADERBOARD_ADMIN_HF_SUBJECTS": "audit-subject"}): | |
| for function in functions: | |
| self.assertIsInstance(function.inputs[0], self.gr.Textbox) | |
| asyncio.run(self.ui.demo.process_api(function, ["__cancelled__"], | |
| state=SessionState(self.ui.demo), request=self.request())) | |
| self.assertIn("window.confirm", function.js) | |
| self.assertEqual(read_control(self.root), before) | |
| def test_scope_and_no_mutation_controls_in_admin_details(self): | |
| tabs = [block for block in self.ui.demo.blocks.values() if getattr(block, "label", "") == "All Task History · Admin"] | |
| self.assertEqual(len(tabs), 0) | |
| reports = [block for block in self.ui.demo.blocks.values() if "admin-details" in (block.elem_classes or [])] | |
| self.assertEqual(len(reports), 2) | |
| self.assertTrue(all(not block.visible for block in reports)) | |
| panel = next(block for block in self.ui.demo.blocks.values() if block.elem_id == "admin-console") | |
| self.assertFalse(panel.visible) | |
| def test_real_history_callbacks_are_task_scoped_and_exclude_active_work(self): | |
| from gradio.state_holder import SessionState | |
| functions = [function for function in self.ui.demo.fns.values() | |
| if function.name == "load" | |
| and "admin-history-table" in function.outputs[0].elem_classes] | |
| self.assertEqual(len(functions), 1) | |
| for index, group in enumerate(self.ui.TASK_GROUPS): | |
| name = group["tasks"][0] | |
| record = dict(submission_id=f"{100 + index:010x}", task_name=name, status="done", | |
| display_name="Other user's completed method", hf_username_snapshot="other-user", | |
| submitted_at="2026-11-01T00:00:00Z", completed_at="2026-11-02T00:00:00Z") | |
| directory = self.root / "results" / name | |
| directory.mkdir(parents=True, exist_ok=True) | |
| (directory / "_history.2026-11.jsonl").write_text(json.dumps(record) + "\n" + | |
| json.dumps(dict(record, submission_id=f"{200 + index:010x}", status="running")) + "\n", encoding="utf-8") | |
| with patch.dict(os.environ, {"LEADERBOARD_ADMIN_HF_SUBJECTS": "audit-subject"}): | |
| function = functions[0] | |
| for index, group in enumerate(self.ui.TASK_GROUPS): | |
| response = asyncio.run(self.ui.demo.process_api(function, [group['tab'], 1, None, None, None, None], | |
| state=SessionState(self.ui.demo), request=self.request())) | |
| data = response["data"][0]["value"] | |
| self.assertEqual([row[data["headers"].index("Submission ID")] for row in data["data"]], [f"{100 + index:010x}"]) | |
| response = asyncio.run(self.ui.demo.process_api(function, ["All Tasks", None, None, None, None, None], | |
| state=SessionState(self.ui.demo), request=self.request())) | |
| self.assertEqual(len(response['data'][0]['value']['data']), 4) | |
| with self.assertRaises(self.gr.Error): | |
| asyncio.run(self.ui.demo.process_api(function, ["Invalid task", 1, None, None, None, None], | |
| state=SessionState(self.ui.demo), request=self.request())) | |
| def test_real_enable_disable_callbacks_persist_and_expired_session_cannot_write(self): | |
| from src.maintenance_state import read_control | |
| functions = [function for function in self.ui.demo.fns.values() if function.name == "apply"] | |
| actions = {dict(zip(function.fn.__code__.co_freevars, | |
| [cell.cell_contents for cell in function.fn.__closure__]))["enabled"]: function.fn | |
| for function in functions} | |
| profile = self.gr.OAuthProfile({"sub": "audit-subject", "preferred_username": "audit-user", "name": "Fixture", | |
| "profile": "https://huggingface.co/audit-user", "picture": ""}) | |
| with patch.dict(os.environ, {"LEADERBOARD_ADMIN_HF_SUBJECTS": "audit-subject"}): | |
| initial = read_control(self.root) | |
| with self.assertRaises(self.gr.Error): | |
| actions[True](initial["generation"], profile, self.request(expiry=time.time() - 1)) | |
| self.assertEqual(read_control(self.root), initial) | |
| actions[True](initial["generation"], profile, self.request()) | |
| enabled = read_control(self.root) | |
| self.assertTrue(enabled["enabled"]) | |
| actions[False](enabled["generation"], profile, self.request()) | |
| self.assertFalse(read_control(self.root)["enabled"]) | |
| def test_shared_filters_pagination_empty_state_and_invalid_inputs(self): | |
| from gradio.state_holder import SessionState | |
| from src.submission import admin | |
| import pandas as pd | |
| functions = [function for function in self.ui.demo.fns.values() | |
| if function.name in {"load", "reset", "navigate"} | |
| and "admin-history-table" in function.outputs[0].elem_classes] | |
| seen = [] | |
| def page(account, tasks, **filters): | |
| admin.require_admin(account) | |
| seen.append((tasks, filters)) | |
| frame = pd.DataFrame(columns=admin.HISTORY_COLUMNS) | |
| frame.attrs.update(total_rows=51) | |
| return frame | |
| with patch.dict(os.environ, {"LEADERBOARD_ADMIN_HF_SUBJECTS": "audit-subject"}), patch.object(admin, "history_page", side_effect=page): | |
| load = next(function for function in functions if function.name == "load") | |
| inputs = ["All Tasks", None, None, None, None, None] | |
| response = asyncio.run(self.ui.demo.process_api(load, inputs, state=SessionState(self.ui.demo), request=self.request())) | |
| self.assertFalse(response['data'][0]['visible']) | |
| self.assertTrue(response['data'][1]['visible']) | |
| self.assertFalse(response['data'][6]['visible']) | |
| self.assertEqual(response['data'][2]['value'], 1) | |
| for invalid in (["All Tasks", 0, None, None, None, None],): | |
| with self.assertRaises(self.gr.Error): | |
| asyncio.run(self.ui.demo.process_api(load, invalid, state=SessionState(self.ui.demo), request=self.request())) | |
| from src.display.admin_console import _page_value | |
| for invalid in (1.5, float('nan'), True, 'bad'): | |
| with self.assertRaises(ValueError): | |
| _page_value(invalid) | |
| for function in functions: | |
| result = asyncio.run(self.ui.demo.process_api(function, ["Motion Transfer", 2, "alice", "done", "2026-11-01", "2026-11-03"], | |
| state=SessionState(self.ui.demo), request=self.request())) | |
| self.assertIn(result['data'][2]['value'], (1, 2)) | |
| self.assertEqual(seen[-1][0], self.ui.TASK_GROUPS[-1]['tasks']) | |
| self.assertEqual(seen[-1][1]['user'], 'alice') | |
| with self.assertRaises(self.gr.Error): | |
| asyncio.run(self.ui.demo.process_api(load, inputs, state=SessionState(self.ui.demo), request=self.request('other-subject'))) | |
| def test_display_values_do_not_change_raw_records_and_controls_are_explicit(self): | |
| import pandas as pd | |
| from src.display.admin_console import _display_frame | |
| source = pd.DataFrame([dict(Status='done', **{'Submitted At': '2026-11-03T03:04:05.123Z', 'Completed At': None})]) | |
| shown = _display_frame(source) | |
| self.assertEqual(shown.iloc[0]['Status'], 'Completed') | |
| self.assertEqual(shown.iloc[0]['Submitted At'], '2026-11-03 03:04:05.123000 UTC') | |
| self.assertEqual(source.iloc[0]['Status'], 'done') | |
| dropdown = next(block for block in self.ui.demo.blocks.values() | |
| if getattr(block, 'label', '') == 'Status' and ('All Statuses', '') in getattr(block, 'choices', [])) | |
| self.assertEqual(dropdown.value, '') | |
| clear = next(function for function in self.ui.demo.fns.values() | |
| if getattr(function.fn, '__name__', '') == '<lambda>' and function.inputs == [dropdown]) | |
| self.assertEqual(clear.fn(None)['value'], '') | |
| pages = [block for block in self.ui.demo.blocks.values() if getattr(block, 'label', '') == 'Page'] | |
| self.assertEqual(len(pages), 2) | |
| self.assertTrue(all(not block.show_label for block in pages)) | |
| if __name__ == "__main__": | |
| unittest.main() | |