stackchain-dashboard/tests/test_context_polling.py
timmy 32d9d9413f
All checks were successful
CI / lint (pull_request) Successful in 15s
CI / build-frontend (pull_request) Successful in 6s
feat: make My Work refresh resumable (#119)
2026-08-06 16:30:01 +00:00

66 lines
1.6 KiB
Python

import json
import subprocess
from pathlib import Path
POLLER = Path(__file__).parents[1] / "frontend" / "context-poller.js"
def run_node(script: str) -> dict:
result = subprocess.run(
["node", "-e", script], check=True, capture_output=True, text=True
)
return json.loads(result.stdout)
def test_context_poller_is_single_flight_and_pauses_until_visible():
script = f"""
const createContextPoller = require({json.dumps(str(POLLER))});
let calls = 0;
let hidden = false;
let resolveRequest;
const timers = [];
const poller = createContextPoller({{
fetchContext: () => {{
calls += 1;
if (calls > 1) return Promise.resolve({{ repos: [] }});
return new Promise((resolve) => {{ resolveRequest = resolve; }});
}},
onSnapshot: () => {{}},
onError: () => {{}},
isHidden: () => hidden,
setTimer: (callback) => {{ timers.push(callback); return timers.length; }},
clearTimer: () => {{}},
intervalMs: 8000,
}});
(async () => {{
const first = poller.start();
const joined = poller.refresh();
const callsWhilePending = calls;
resolveRequest({{ repos: [] }});
await Promise.all([first, joined]);
hidden = true;
poller.setVisible(false);
const scheduled = timers.shift();
if (scheduled) scheduled();
await Promise.resolve();
const callsWhileHidden = calls;
hidden = false;
await poller.setVisible(true);
process.stdout.write(JSON.stringify({{
callsWhilePending,
callsWhileHidden,
callsAfterResume: calls,
}}));
}})();
"""
assert run_node(script) == {
"callsWhilePending": 1,
"callsWhileHidden": 1,
"callsAfterResume": 2,
}