SPB Git forge
28commits 1branches 0releases
7.7 MBsize
maindefault branch
10 days agolast push
Python 66.3% TypeScript 22.7% JavaScript 8.6% HTML 1.4% CSS 0.7%
4.1 KB · 74 lines python
Raw Blame History
1"""Deterministic question parser for `/ask`: countries, industries, intents → event types, windows, answer style; never invents results."""2from __future__ import annotations34from companyatlas.services.llm.ask import Interpretation, build_answer, parse_question, route_question56INDUSTRIES = {"fintech": "Fintech", "artificial-intelligence": "Artificial intelligence", "retail": "Retail", "software": "Software"}7COUNTRIES = {"CA": "Canada", "US": "United States", "JP": "Japan", "DE": "Germany"}8910def test_hiring_ai_in_canada_last_month():11    it = parse_question("Which companies are hiring AI engineers in Canada in the last month?", industries=INDUSTRIES, countries=COUNTRIES)12    assert it.countries == ["CA"]13    assert it.intent == "ai" and "hiring" in it.intents14    assert "HIRING" in it.event_types and "AI_HIRING" in it.event_subtypes and "ai" in it.tags15    assert it.window_days == 3016    assert "engineers" in it.keywords and "canada" not in it.keywords17    assert it.answer_style == "list" and 0.5 <= it.confidence <= 0.918    assert it.filters["countries"] == ["CA"]192021def test_pricing_increase_fintech_this_week():22    it = parse_question("fintech companies that raised prices this week", industries=INDUSTRIES)23    assert it.industries == ["fintech"] and it.intent == "pricing"24    assert it.event_types == ["PRICING"] and "PRICE_INCREASE" in it.event_subtypes and "PRICE_DECREASE" not in it.event_subtypes25    assert it.window_days == 7262728def test_launch_expansion_leadership_and_count():29    launch = parse_question("How many Japanese companies launched new products since 2026-01-01?", countries=COUNTRIES)30    assert launch.countries == ["JP"] and launch.intent == "launch" and launch.event_types == ["PRODUCT"]31    assert launch.answer_style == "count" and launch.window_days is not None and launch.window_days > 20032    exp = parse_question("companies expanding into new countries", countries=COUNTRIES)33    assert exp.intent == "expansion" and "COUNTRY_EXPANSION" in exp.event_subtypes34    lead = parse_question("new CEO appointed at German companies", countries=COUNTRIES)35    assert lead.intent == "leadership" and lead.countries == ["DE"]363738def test_compare_timeline_trend_and_quotes():39    cmp = parse_question('compare "Stripe" vs "Adyen" hiring over the past 90 days')40    assert cmp.answer_style == "compare" and cmp.companies == ["Stripe", "Adyen"] and cmp.window_days == 90 and "HIRING" in cmp.event_types41    tl = parse_question("when did Acme change its terms of service?")42    assert tl.answer_style == "timeline" and tl.intent == "legal" and tl.event_types == ["LEGAL"]43    tr = parse_question("which industries are trending in developer docs")44    assert tr.answer_style == "trend" and "DEVELOPER" in tr.event_types454647def test_plain_search_has_no_filters_and_low_confidence():48    it = parse_question("acme corporation")49    assert it.intent == "search" and it.filters == {"keywords": ["acme", "corporation"]}50    assert it.confidence < 0.551    empty = parse_question("")52    assert empty.filters == {} and empty.to_dict()["parser_version"].startswith("ask-parser")535455def test_important_events_and_regions():56    it = parse_question("major pricing changes at European software companies", industries=INDUSTRIES)57    assert it.min_importance == 0.6 and it.regions == ["europe"] and it.countries == [] and it.industries == ["software"]585960async def test_route_question_without_llm_is_deterministic(monkeypatch):61    from companyatlas.config import settings6263    monkeypatch.setattr(settings, "llm_base_url", "")64    it = await route_question("companies hiring in Canada", countries=COUNTRIES)65    assert it.source == "deterministic" and it.countries == ["CA"]666768def test_build_answer_only_phrases_measured_counts():69    it = Interpretation(question="q", intent="hiring", countries=["CA"], window_days=30)70    assert build_answer(it, companies=0, events=0).startswith("No monitored evidence matches this question yet in CA over the last 30 days")71    text = build_answer(it, companies=12, events=48)72    assert text.startswith("48 hiring-related events in CA over the last 30 days across 12 monitored companies.")73    assert "confidence" in text74