返回 last30days-skill
test_as_of_date.py
根目录 / tests / test_as_of_date.py
1 # ruff: noqa: E402
2 """Tests for --as-of date support."""
3
4 import io
5 import json
6 import subprocess
7 import sys
8 import unittest
9 from datetime import datetime, timedelta, timezone
10 from pathlib import Path
11 from unittest import mock
12
13
14 REPO_ROOT = Path(__file__).resolve().parents[1]
15 sys.path.insert(0, str(REPO_ROOT / "skills" / "last30days" / "scripts"))
16
17 import last30days as cli
18 from lib import dates
19
20
21 class AsOfDateTests(unittest.TestCase):
22 def test_get_date_range_with_as_of_date_and_7_days(self):
23 from_date, to_date = dates.get_date_range(7, "2020-05-25")
24
25 self.assertEqual("2020-05-18", from_date)
26 self.assertEqual("2020-05-25", to_date)
27
28 def test_get_date_range_with_as_of_date_and_30_days(self):
29 from_date, to_date = dates.get_date_range(30, "2020-05-25")
30
31 self.assertEqual("2020-04-25", from_date)
32 self.assertEqual("2020-05-25", to_date)
33
34 def test_get_date_range_rejects_invalid_as_of_date(self):
35 with self.assertRaisesRegex(ValueError, "YYYY-MM-DD"):
36 dates.get_date_range(7, "2020/05/25")
37
38 def test_build_parser_accepts_as_of_date(self):
39 parser = cli.build_parser()
40 args, extra = parser.parse_known_args(
41 ["--as-of", "2020-05-25", "--days", "7", "OpenAI"]
42 )
43
44 self.assertEqual("2020-05-25", args.as_of_date)
45 self.assertEqual(7, args.lookback_days)
46 self.assertEqual(["OpenAI"], args.topic)
47 self.assertEqual([], extra)
48
49 def test_mock_json_cli_uses_as_of_date_range(self):
50 result = subprocess.run(
51 [
52 sys.executable,
53 "skills/last30days/scripts/last30days.py",
54 "OpenAI",
55 "--mock",
56 "--emit=json",
57 "--json-profile=raw",
58 "--as-of",
59 "2020-05-25",
60 "--days",
61 "7",
62 ],
63 cwd=REPO_ROOT,
64 capture_output=True,
65 text=True,
66 encoding="utf-8",
67 errors="replace",
68 check=False,
69 )
70
71 self.assertEqual(0, result.returncode, result.stderr)
72 payload = json.loads(result.stdout)
73
74 self.assertEqual("2020-05-18", payload["range_from"])
75 self.assertEqual("2020-05-25", payload["range_to"])
76
77 def test_cli_rejects_invalid_as_of_date(self):
78 result = subprocess.run(
79 [
80 sys.executable,
81 "skills/last30days/scripts/last30days.py",
82 "OpenAI",
83 "--mock",
84 "--emit=json",
85 "--as-of",
86 "2020/05/25",
87 ],
88 cwd=REPO_ROOT,
89 capture_output=True,
90 text=True,
91 encoding="utf-8",
92 errors="replace",
93 check=False,
94 )
95
96 self.assertNotEqual(0, result.returncode)
97 self.assertIn("YYYY-MM-DD", result.stderr)
98
99 def test_days_ago_uses_reference_date(self):
100 self.assertEqual(
101 5,
102 dates.days_ago("2020-05-20", reference_date="2020-05-25"),
103 )
104
105 def test_recency_score_uses_reference_date(self):
106 self.assertEqual(
107 50,
108 dates.recency_score(
109 "2020-05-20",
110 max_days=10,
111 reference_date="2020-05-25",
112 ),
113 )
114
115
116 class AsOfTodayIsLiveTests(unittest.TestCase):
117 """--as-of naming today (UTC or the reader's calendar) is a live run."""
118
119 LA = timezone(timedelta(hours=-8))
120
121 def _fake_now(self):
122 # Local Nov 13 5pm in Los Angeles is Nov 14 in UTC.
123 return datetime(2026, 11, 13, 17, 0, tzinfo=self.LA)
124
125 def _run_main(self, argv, target="_main"):
126 seen = {}
127
128 def fake(*call_args, **call_kwargs):
129 seen["args"] = call_args[1] if target == "_main" else call_args[0]
130 return 0
131
132 with mock.patch.object(dates, "_local_now", self._fake_now), \
133 mock.patch.object(sys, "argv", ["last30days.py", *argv]), \
134 mock.patch.object(cli, target, fake):
135 rc = cli.main()
136 return rc, seen.get("args")
137
138 def test_as_of_local_today_is_cleared(self):
139 rc, args = self._run_main(["OpenAI", "--as-of=2026-11-13"])
140 self.assertEqual(0, rc)
141 self.assertIsNone(args.as_of_date)
142
143 def test_as_of_utc_today_is_cleared_when_local_is_a_day_behind(self):
144 rc, args = self._run_main(["OpenAI", "--as-of=2026-11-14"])
145 self.assertEqual(0, rc)
146 self.assertIsNone(args.as_of_date)
147
148 def test_as_of_yesterday_is_kept(self):
149 rc, args = self._run_main(["OpenAI", "--as-of=2026-11-12"])
150 self.assertEqual(0, rc)
151 self.assertEqual("2026-11-12", args.as_of_date)
152
153 def test_past_as_of_still_skips_jobs_lane(self):
154 past = (datetime.now(timezone.utc) - timedelta(days=2)).date().isoformat()
155 result = subprocess.run(
156 [
157 sys.executable,
158 "skills/last30days/scripts/last30days.py",
159 "Microsoft",
160 "--mock",
161 "--emit=json",
162 "--json-profile=raw",
163 "--search=jobs",
164 "--as-of",
165 past,
166 ],
167 cwd=REPO_ROOT,
168 capture_output=True,
169 text=True,
170 check=True,
171 )
172 report = json.loads(result.stdout)
173 jobs = report["source_status"]["jobs"]
174 self.assertFalse(jobs["attempted"])
175 self.assertIn("past window", jobs["detail"])
176
177 def test_as_of_today_with_discover_is_accepted(self):
178 stderr = io.StringIO()
179 with mock.patch.object(dates, "_local_now", self._fake_now), \
180 mock.patch.object(
181 sys, "argv", ["last30days.py", "--discover", "--as-of=2026-11-13"]
182 ), \
183 mock.patch.object(cli, "_run_discover", return_value=0) as run_discover, \
184 mock.patch.object(sys, "stderr", stderr):
185 rc = cli.main()
186 self.assertEqual(0, rc)
187 run_discover.assert_called_once()
188 self.assertNotIn("--as-of cannot be used", stderr.getvalue())
189
190 def test_past_as_of_with_discover_is_still_rejected(self):
191 stderr = io.StringIO()
192 with mock.patch.object(dates, "_local_now", self._fake_now), \
193 mock.patch.object(
194 sys, "argv", ["last30days.py", "--discover", "--as-of=2026-11-12"]
195 ), \
196 mock.patch.object(sys, "stderr", stderr):
197 rc = cli.main()
198 self.assertEqual(2, rc)
199 self.assertIn("--as-of cannot be used", stderr.getvalue())
200
201
202 if __name__ == "__main__":
203 unittest.main()
204
204 lines PYTHON