fleet-memory/hindsight-api-slim/tests/test_query_analyzer.py
Nicolò Boschi 15ea23d5d6
feat: introduce hindsight-api-slim and hindsight-all-slim packages (#560)
* feat: introduce hindsight-api-slim and hindsight-all-slim packages

Closes #552

- Move all source code from hindsight-api/ to new hindsight-api-slim/
- hindsight-api-slim has heavy ML deps (torch, sentence-transformers,
  transformers, einops, flashrank, mlx, mlx-lm, safetensors) and
  pg0-embedded as optional extras: [local-ml], [embedded-db], [all]
- hindsight-api becomes a zero-code meta-package depending on
  hindsight-api-slim[all] for full backward compatibility
- Add hindsight-all-slim meta-package: hindsight-api-slim + client + embed
- hindsight-all updated to depend on hindsight-api-slim[all]
- pg0.py: lazy-import pg0 with clear ImportError pointing to [embedded-db]
- Dockerfile: replace sed hack with proper uv sync --extra flags
- Update release.yml, test.yml, lint.sh, release.sh, CLAUDE.md and
  all path references throughout the repo

* refactor: rename hindsight/ directory to hindsight-all/

* docs: document hindsight-api-slim and hindsight-all-slim package variants

Add package variants table and extras explanation to installation.md

* docs: remove emojis from installation.md, use professional tone

* docs: link Docker slim variant to pip package variants section

* docs: consolidate Docker image variants into single table

* ci: fix working-directory paths after package restructure

- Replace all hindsight-api → hindsight-api-slim in test.yml
- Replace hindsight → hindsight-all in test.yml
- Add --extra embedded-db to test-embed API install step

* ci: add local-ml and embedded-db extras to API sync steps

These extras were previously implicit in the old hindsight-api package
(which bundled everything). Now that hindsight-api-slim uses optional
extras, we must explicitly request local-ml and embedded-db in CI.

* ci: add API install step with embedded-db to test-embed smoke test

The smoke test starts hindsight-api as a daemon, which requires pg0-embedded.
Add a dedicated install step for hindsight-api-slim with embedded-db extra
so the daemon can start successfully.

* ci: remove --no-install-project when using optional extras

When --no-install-project is combined with --extra, the optional deps
are not installed because extras require the project to be active.
Remove --no-install-project from steps that need local-ml or embedded-db.

* ci: fix ordering of uv sync steps to preserve optional extras

When uv sync runs for a different workspace member, it removes optional
extras installed for other members. Fix by always running extra-requiring
API sync last, after other workspace member syncs.

Also remove --no-install-project from embedded-db sync in test-embed,
as --no-install-project prevents optional extras from being active.

* ci: add local-ml extra to test-embed API install for smoke test

The smoke test starts the full API server which needs sentence-transformers
for local embeddings (default provider). Add local-ml extra to the install.

* ci: simplify extras with --all-extras and add slim pip smoke test

- Replace explicit --extra local-ml --extra embedded-db with --all-extras
  for cleaner, more maintainable sync steps
- Add test-pip-slim job: tests hindsight-api-slim[embedded-db] without
  local ML models, using Cohere for embeddings/reranking (mirrors Docker
  slim smoke test approach)

* ci: simplify slim smoke test to health check only (mirrors Docker test)
2026-03-13 13:50:03 +01:00

285 lines
12 KiB
Python

"""
Test query analyzer for temporal extraction.
"""
import pytest
from datetime import datetime
from hindsight_api.engine.query_analyzer import DateparserQueryAnalyzer, QueryAnalysis
def test_query_analyzer_june_2024(query_analyzer):
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "june 2024"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint"
assert analysis.temporal_constraint.start_date.year == 2024
assert analysis.temporal_constraint.start_date.month == 6
assert analysis.temporal_constraint.start_date.day == 1
assert analysis.temporal_constraint.end_date.year == 2024
assert analysis.temporal_constraint.end_date.month == 6
assert analysis.temporal_constraint.end_date.day == 30
def test_query_analyzer_dogs_june_2023(query_analyzer):
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "dogs in June 2023"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint"
assert analysis.temporal_constraint.start_date.year == 2023
assert analysis.temporal_constraint.start_date.month == 6
assert analysis.temporal_constraint.start_date.day == 1
assert analysis.temporal_constraint.end_date.year == 2023
assert analysis.temporal_constraint.end_date.month == 6
assert analysis.temporal_constraint.end_date.day == 30
def test_query_analyzer_march_2023(query_analyzer):
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "March 2023"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint"
assert analysis.temporal_constraint.start_date.year == 2023
assert analysis.temporal_constraint.start_date.month == 3
assert analysis.temporal_constraint.start_date.day == 1
assert analysis.temporal_constraint.end_date.year == 2023
assert analysis.temporal_constraint.end_date.month == 3
assert analysis.temporal_constraint.end_date.day == 31
def test_query_analyzer_last_year(query_analyzer):
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "last year"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint"
assert analysis.temporal_constraint.start_date.year == 2024
assert analysis.temporal_constraint.start_date.month == 1
assert analysis.temporal_constraint.start_date.day == 1
assert analysis.temporal_constraint.end_date.year == 2024
assert analysis.temporal_constraint.end_date.month == 12
assert analysis.temporal_constraint.end_date.day == 31
def test_query_analyzer_no_temporal(query_analyzer):
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "what is the weather"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is None, "Should not extract temporal constraint"
def test_query_analyzer_activities_june_2024(query_analyzer):
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "melanie activities in june 2024"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint"
assert analysis.temporal_constraint.start_date.year == 2024
assert analysis.temporal_constraint.start_date.month == 6
assert analysis.temporal_constraint.start_date.day == 1
assert analysis.temporal_constraint.end_date.year == 2024
assert analysis.temporal_constraint.end_date.month == 6
assert analysis.temporal_constraint.end_date.day == 30
def test_query_analyzer_last_saturday(query_analyzer):
"""Test extraction of 'last Saturday' relative date."""
# Reference date is Wednesday, January 15, 2025
# Last Saturday would be January 11, 2025
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "I received a piece of jewelry last Saturday from whom?"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Reference date: {reference_date.strftime('%A, %Y-%m-%d')}")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint for 'last Saturday'"
# Last Saturday from Wed Jan 15 is Sat Jan 11
assert analysis.temporal_constraint.start_date.year == 2025
assert analysis.temporal_constraint.start_date.month == 1
assert analysis.temporal_constraint.start_date.day == 11
assert analysis.temporal_constraint.end_date.year == 2025
assert analysis.temporal_constraint.end_date.month == 1
assert analysis.temporal_constraint.end_date.day == 11
def test_query_analyzer_yesterday(query_analyzer):
"""Test extraction of 'yesterday' relative date."""
# Reference date is Wednesday, January 15, 2025
# Yesterday would be January 14, 2025
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "what did I do yesterday?"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Reference date: {reference_date.strftime('%A, %Y-%m-%d')}")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint for 'yesterday'"
assert analysis.temporal_constraint.start_date.year == 2025
assert analysis.temporal_constraint.start_date.month == 1
assert analysis.temporal_constraint.start_date.day == 14
assert analysis.temporal_constraint.end_date.day == 14
def test_query_analyzer_last_week(query_analyzer):
"""Test extraction of 'last week' relative date."""
# Reference date is Wednesday, January 15, 2025
# Last week would be January 6-12, 2025 (Mon-Sun)
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "what meetings did I have last week?"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Reference date: {reference_date.strftime('%A, %Y-%m-%d')}")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint for 'last week'"
assert analysis.temporal_constraint.start_date.year == 2025
assert analysis.temporal_constraint.start_date.month == 1
assert analysis.temporal_constraint.start_date.day == 6 # Monday
assert analysis.temporal_constraint.end_date.day == 12 # Sunday
def test_query_analyzer_last_month(query_analyzer):
"""Test extraction of 'last month' relative date."""
# Reference date is Wednesday, January 15, 2025
# Last month would be December 2024
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "expenses from last month"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Reference date: {reference_date.strftime('%A, %Y-%m-%d')}")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint for 'last month'"
assert analysis.temporal_constraint.start_date.year == 2024
assert analysis.temporal_constraint.start_date.month == 12
assert analysis.temporal_constraint.start_date.day == 1
assert analysis.temporal_constraint.end_date.month == 12
assert analysis.temporal_constraint.end_date.day == 31
def test_query_analyzer_last_friday(query_analyzer):
"""Test extraction of 'last Friday' relative date."""
# Reference date is Wednesday, January 15, 2025
# Last Friday would be January 10, 2025
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "who did I meet last Friday?"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Reference date: {reference_date.strftime('%A, %Y-%m-%d')}")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint for 'last Friday'"
assert analysis.temporal_constraint.start_date.year == 2025
assert analysis.temporal_constraint.start_date.month == 1
assert analysis.temporal_constraint.start_date.day == 10
assert analysis.temporal_constraint.end_date.day == 10
def test_query_analyzer_last_weekend(query_analyzer):
"""Test extraction of 'last weekend' relative date."""
# Reference date is Wednesday, January 15, 2025
# Last weekend would be January 11-12, 2025 (Sat-Sun)
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "what did I do last weekend?"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Reference date: {reference_date.strftime('%A, %Y-%m-%d')}")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint for 'last weekend'"
assert analysis.temporal_constraint.start_date.year == 2025
assert analysis.temporal_constraint.start_date.month == 1
assert analysis.temporal_constraint.start_date.day == 11 # Saturday
assert analysis.temporal_constraint.end_date.day == 12 # Sunday
def test_query_analyzer_couple_days_ago(query_analyzer):
"""Test extraction of 'a couple of days ago' colloquial expression."""
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "I mentioned cooking something for my friend a couple of days ago. What was it?"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Reference date: {reference_date.strftime('%A, %Y-%m-%d')}")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint for 'a couple of days ago'"
# Range should be 1-3 days ago: Jan 12-14
assert analysis.temporal_constraint.start_date.day == 12
assert analysis.temporal_constraint.end_date.day == 14
def test_query_analyzer_few_days_ago(query_analyzer):
"""Test extraction of 'a few days ago' colloquial expression."""
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "What did I do a few days ago?"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Reference date: {reference_date.strftime('%A, %Y-%m-%d')}")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint for 'a few days ago'"
# Range should be 2-5 days ago: Jan 10-13
assert analysis.temporal_constraint.start_date.day == 10
assert analysis.temporal_constraint.end_date.day == 13
def test_query_analyzer_couple_weeks_ago(query_analyzer):
"""Test extraction of 'a couple of weeks ago' colloquial expression."""
reference_date = datetime(2025, 1, 15, 12, 0, 0)
query = "a couple of weeks ago we discussed this"
analysis = query_analyzer.analyze(query, reference_date)
print(f"\nQuery: '{query}'")
print(f"Reference date: {reference_date.strftime('%A, %Y-%m-%d')}")
print(f"Analysis: {analysis}")
assert analysis.temporal_constraint is not None, "Should extract temporal constraint for 'a couple of weeks ago'"
# Range should be 1-3 weeks ago
assert analysis.temporal_constraint.start_date.month == 12 # Dec 25 (3 weeks before Jan 15)
assert analysis.temporal_constraint.end_date.month == 1 # Jan 8 (1 week before Jan 15)