Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion frontend/electron/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,26 @@ function stopSidecar() {

async function apiIsRunning(timeoutMs = 250) {
try {
await fetchStatus(timeoutMs)
await fetchHealth(timeoutMs)
return true
} catch {
return false
}
}

function fetchHealth(timeoutMs = 1000) {
return new Promise((resolve, reject) => {
const request = http.get(`${apiBase}/health`, (response) => {
response.resume()
response.statusCode === 200
? resolve()
: reject(new Error(`status ${response.statusCode}`))
})
request.on("error", reject)
request.setTimeout(timeoutMs, () => request.destroy(new Error("timeout")))
})
}

function fetchStatus(timeoutMs = 1000) {
return new Promise((resolve, reject) => {
const request = http.get(`${apiBase}/status`, (response) => {
Expand Down
4 changes: 4 additions & 0 deletions src/resolve_time_tracker/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ def create_app(
)
app.state.api = api

@app.get("/health")
def health() -> dict[str, bool]:
return {"ok": True}

@app.get("/status")
def status() -> dict[str, Any]:
return api.refresh()
Expand Down
15 changes: 14 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest
from datetime import datetime, timezone
from pathlib import Path
from unittest.mock import patch
from unittest.mock import Mock, patch

from fastapi.testclient import TestClient

Expand Down Expand Up @@ -296,6 +296,19 @@ def test_allows_frontend_origin(self):

self.assertEqual("*", response.headers["access-control-allow-origin"])

def test_health_does_not_poll_resolve(self):
with tempfile.TemporaryDirectory() as tmp:
engine = Mock()
with SQLiteStore(
Path(tmp) / "tracker.sqlite3", check_same_thread=False
) as store:
response = TestClient(create_app(store, tracking_engine=engine)).get(
"/health"
)

self.assertEqual({"ok": True}, response.json())
engine.poll.assert_not_called()

def test_run_api_starts_tracking_api_with_resolve_bridge(self):
with tempfile.TemporaryDirectory() as tmp:
with (
Expand Down
Loading