| 1 | import json |
| 2 | import sys |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | import last30days as cli |
| 7 | from lib import planner |
| 8 | |
| 9 | |
| 10 | SINGLE_TOPICS = [ |
| 11 | "CI/CD", |
| 12 | "I/O performance", |
| 13 | "openai/openai-python", |
| 14 | "https://example.com", |
| 15 | "https://example.com/React/Vue", |
| 16 | "https://example.com/vs/topic", |
| 17 | "https://example.com/vs.Vue", |
| 18 | "React vsVue", |
| 19 | "vs.code", |
| 20 | "vs.code/extension", |
| 21 | "compare CI/CD workflows", |
| 22 | ] |
| 23 | |
| 24 | COMPARISONS = [ |
| 25 | ("React/Vue/Svelte", ["React", "Vue", "Svelte"]), |
| 26 | ("React vs Vue", ["React", "Vue"]), |
| 27 | ("React vs. Vue", ["React", "Vue"]), |
| 28 | ("React vs. vs. Vue", ["React", "Vue"]), |
| 29 | ("React vs.Vue", ["React", "Vue"]), |
| 30 | ("React VS.Vue", ["React", "Vue"]), |
| 31 | ("React versus Vue", ["React", "Vue"]), |
| 32 | ("vs.code versus Vue", ["vs.code", "Vue"]), |
| 33 | ("vs.code vs.Vue", ["vs.code", "Vue"]), |
| 34 | ("vs.code/extension versus Vue", ["vs.code/extension", "Vue"]), |
| 35 | ("React versus vs.code", ["React", "vs.code"]), |
| 36 | ("React vs.vs.code", ["React", "vs.code"]), |
| 37 | ("React versus vs.code/extension", ["React", "vs.code/extension"]), |
| 38 | ("vs.code/extension versus vs.code/other", ["vs.code/extension", "vs.code/other"]), |
| 39 | ("https://example.com/vs.code versus owner/repo", |
| 40 | ["https://example.com/vs.code", "owner/repo"]), |
| 41 | ("React versus https://example.com/vs.code", ["React", "https://example.com/vs.code"]), |
| 42 | ("React compared to Vue", ["React", "Vue"]), |
| 43 | ("difference between React and Vue", ["React", "Vue"]), |
| 44 | ("React/Vue/Svelte for CI/CD", ["React", "Vue", "Svelte"]), |
| 45 | ("CI/CD vs I/O", ["CI/CD", "I/O"]), |
| 46 | ("CI/CD vs.I/O", ["CI/CD", "I/O"]), |
| 47 | ("openai/openai-python vs anthropics/anthropic-sdk-python", |
| 48 | ["openai/openai-python", "anthropics/anthropic-sdk-python"]), |
| 49 | ] |
| 50 | |
| 51 | |
| 52 | @pytest.mark.parametrize("topic", SINGLE_TOPICS) |
| 53 | @pytest.mark.parametrize("uncapped", [False, True]) |
| 54 | def test_slashes_without_comparison_intent_do_not_produce_entities(topic, uncapped): |
| 55 | assert planner._comparison_entities(topic, uncapped=uncapped) == [] |
| 56 | |
| 57 | |
| 58 | @pytest.mark.parametrize("topic,entities", COMPARISONS) |
| 59 | def test_comparison_entities_preserve_slashes_inside_explicit_entities(topic, entities): |
| 60 | assert planner._comparison_entities(topic) == entities |
| 61 | |
| 62 | |
| 63 | @pytest.fixture |
| 64 | def run_cli(monkeypatch, tmp_path, capsys): |
| 65 | monkeypatch.setattr(cli.env, "CONFIG_DIR", tmp_path) |
| 66 | monkeypatch.setattr(cli.env, "get_config", lambda **kwargs: {}) |
| 67 | monkeypatch.setenv("PATH", "") |
| 68 | monkeypatch.setenv("LAST30DAYS_STORE", "") |
| 69 | |
| 70 | def run(topic): |
| 71 | monkeypatch.setattr(sys, "argv", [ |
| 72 | "last30days", topic, "--mock", "--quick", "--search=reddit", |
| 73 | "--no-browser-cookies", "--emit=json", "--json-profile=raw", |
| 74 | "--save-dir", "", |
| 75 | ]) |
| 76 | result = cli.main() |
| 77 | captured = capsys.readouterr() |
| 78 | assert result == 0, captured.err |
| 79 | return json.loads(captured.out) |
| 80 | |
| 81 | return run |
| 82 | |
| 83 | |
| 84 | @pytest.mark.parametrize("topic", SINGLE_TOPICS) |
| 85 | def test_main_preserves_slash_topics_as_single_research_runs(topic, run_cli): |
| 86 | payload = run_cli(topic) |
| 87 | assert payload.get("comparison") is not True |
| 88 | assert payload["topic"] == topic |
| 89 | assert payload["query_plan"]["raw_topic"] == topic |
| 90 | |
| 91 | |
| 92 | @pytest.mark.parametrize("topic,entities", COMPARISONS) |
| 93 | def test_main_keeps_supported_comparison_forms(topic, entities, run_cli): |
| 94 | payload = run_cli(topic) |
| 95 | assert payload["comparison"] is True |
| 96 | assert payload["entities"] == entities |
| 97 | assert [entry["report"]["topic"] for entry in payload["reports"]] == entities |
| 98 | |
| 99 | |
| 100 | @pytest.mark.parametrize("left,right", [ |
| 101 | pytest.param("React", "Vue", id="plain"), |
| 102 | pytest.param("vs.code", "vs.editor", id="dotted"), |
| 103 | pytest.param("org/repo", "other/lib", id="repository"), |
| 104 | pytest.param("vs.code/extension", "vs.editor/tool", id="dotted-repository"), |
| 105 | pytest.param("https://example.com/vs.code", "https://example.org/vs.editor", id="url"), |
| 106 | ]) |
| 107 | @pytest.mark.parametrize("form", [ |
| 108 | pytest.param("{left} vs {right}", id="spaced-vs"), |
| 109 | pytest.param("{left} vs. {right}", id="spaced-dotted-vs"), |
| 110 | pytest.param("{left} versus {right}", id="versus"), |
| 111 | pytest.param("{left} vs.{right}", id="compact-vs"), |
| 112 | pytest.param("{left} vs vs {right}", id="repeated-vs"), |
| 113 | pytest.param("{left} vs. vs. {right}", id="repeated-dotted-vs"), |
| 114 | pytest.param("{left} versus versus {right}", id="repeated-versus"), |
| 115 | pytest.param("{left} vs versus vs. {right}", id="mixed-separators"), |
| 116 | pytest.param("vs {left} vs {right}", id="leading-vs"), |
| 117 | pytest.param("vs. {left} vs.{right}", id="leading-dotted-vs"), |
| 118 | pytest.param("versus {left} versus {right}", id="leading-versus"), |
| 119 | pytest.param("vs vs. versus {left} versus vs. vs {right}", id="leading-and-repeated"), |
| 120 | ]) |
| 121 | def test_separator_tokens_preserve_entity_names(left, right, form, run_cli): |
| 122 | topic = form.format(left=left, right=right) |
| 123 | assert planner._comparison_entities(topic) == [left, right] |
| 124 | payload = run_cli(topic) |
| 125 | assert payload["comparison"] is True |
| 126 | assert payload["entities"] == [left, right] |
| 127 | assert [entry["report"]["topic"] for entry in payload["reports"]] == [left, right] |
| 128 |