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
4 changes: 4 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ jobs:
npm ci
npm run build

- name: Run frontend tests (vitest)
working-directory: frontend
run: npm run test

- name: Run unit tests (no Firestore/GCS)
run: |
python -m pytest tests/test_*.py -v --tb=short
Expand Down
44 changes: 44 additions & 0 deletions tests/test_inventory_fallback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Regression guard: a transient Firestore/JSON outage must not be cached.

Before this, _load_inventory cached whatever it produced for 300s — including
the single-fake-home sample fallback — so a brief Firestore blip froze a
near-empty/fake catalog in front of customers for 5 minutes. Healthy results are
still cached; degraded (sample) results are not, so the next request recovers.
"""

from __future__ import annotations

import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import tools.inventory_tools as inv # noqa: E402


def _patch(monkeypatch, firestore_homes, website_homes=()):
monkeypatch.setattr(inv, "_load_inventory_from_firestore", lambda: list(firestore_homes))
monkeypatch.setattr(inv, "_load_inventory_from_json", lambda: [])
monkeypatch.setattr(inv, "_get_sample_inventory", lambda: [{"model_name": "The Nassau"}])
monkeypatch.setattr(inv, "_load_website_homes", lambda: list(website_homes))
monkeypatch.setattr(inv, "cache_get", lambda key: None)
calls = []
monkeypatch.setattr(
inv, "cache_set", lambda key, value, ttl_seconds=None: calls.append((key, len(value)))
)
return calls


def test_degraded_sample_fallback_is_not_cached(monkeypatch):
calls = _patch(monkeypatch, firestore_homes=[]) # firestore + json empty -> sample
result = inv._load_inventory()
assert any(h["model_name"] == "The Nassau" for h in result) # degraded path taken
assert calls == [], "transient sample fallback must NOT be cached"


def test_healthy_inventory_is_cached(monkeypatch):
homes = [{"model_name": f"Home {i}"} for i in range(12)]
calls = _patch(monkeypatch, firestore_homes=homes)
result = inv._load_inventory()
assert len(result) == 12
assert len(calls) == 1, "healthy inventory should be cached"
9 changes: 7 additions & 2 deletions tools/inventory_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,10 @@ def _load_inventory():
inventory = _load_inventory_from_json()

# Strategy 3: Use sample data (ultimate fallback)
degraded = False
if not inventory:
inventory = _get_sample_inventory()
degraded = True # both real sources failed — this is a transient outage

# Merge website homes — avoid duplicates by checking model names
existing_names = {h["model_name"].lower() for h in inventory}
Expand All @@ -303,8 +305,11 @@ def _load_inventory():
inventory.append(wh)
existing_names.add(wh["model_name"].lower())

# Save to cache if found
if inventory:
# Cache only a HEALTHY result. Caching the sample fallback would freeze a
# transient Firestore/JSON outage (a near-empty/fake catalog) into the cache
# for 5 minutes; instead, leave it uncached so the next request re-tries the
# real sources and recovers immediately.
if inventory and not degraded:
cache_set(INVENTORY_CACHE_KEY, inventory, ttl_seconds=300)

return inventory
Expand Down
Loading