-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_nightshift.py
More file actions
79 lines (64 loc) · 3 KB
/
test_nightshift.py
File metadata and controls
79 lines (64 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import tempfile
import unittest
from pathlib import Path
import glm_quota
import nightshift
class NightshiftLifecycleTests(unittest.TestCase):
def test_prune_state_runs_drops_stale_entries(self):
state = {
"runs": [
{"timestamp": "2026-02-01T00:00:00+00:00", "repo": "old/repo", "task": "old"},
{"timestamp": "2026-04-01T00:00:00+00:00", "repo": "new/repo", "task": "new"},
{"timestamp": "not-a-date", "repo": "bad/repo", "task": "bad"},
]
}
removed = nightshift.prune_state_runs(state, retention_days=30)
self.assertEqual(removed, 2)
self.assertEqual(state["runs"], [{"timestamp": "2026-04-01T00:00:00+00:00", "repo": "new/repo", "task": "new"}])
def test_cleanup_workspace_only_removes_directories(self):
with tempfile.TemporaryDirectory() as tmpdir:
original_workspace = nightshift.WORKSPACE
workspace = Path(tmpdir)
nightshift.WORKSPACE = workspace
try:
(workspace / "keep.txt").write_text("keep")
(workspace / "stale-clone").mkdir()
(workspace / "preserved-clone").mkdir()
removed = nightshift.cleanup_workspace({"preserved-clone"})
self.assertEqual(removed, ["stale-clone"])
self.assertTrue((workspace / "keep.txt").exists())
self.assertTrue((workspace / "preserved-clone").exists())
self.assertFalse((workspace / "stale-clone").exists())
finally:
nightshift.WORKSPACE = original_workspace
def test_validate_config_rejects_unknown_keys(self):
with self.assertRaises(ValueError):
nightshift.validate_config({"nope": True})
class GlmQuotaTests(unittest.TestCase):
def test_cache_round_trip(self):
with tempfile.TemporaryDirectory() as tmpdir:
original_cache_dir = glm_quota.CACHE_DIR
original_cache_file = glm_quota.CACHE_FILE
cache_dir = Path(tmpdir)
glm_quota.CACHE_DIR = cache_dir
glm_quota.CACHE_FILE = cache_dir / "glm-quota-cache.json"
try:
payload = {"success": True, "data": {"hello": "world"}}
glm_quota._write_cached_response("/test", {"a": 1}, payload)
self.assertEqual(glm_quota._read_cached_response("/test", {"a": 1}), payload)
self.assertIsNone(glm_quota._read_cached_response("/test", {"a": 2}))
finally:
glm_quota.CACHE_DIR = original_cache_dir
glm_quota.CACHE_FILE = original_cache_file
def test_should_run_reports_missing_key(self):
original_key = glm_quota.GLM_KEY
glm_quota.GLM_KEY = ""
try:
self.assertEqual(
glm_quota.should_run_nightshift(),
(False, "GLM_API_KEY is not set", None),
)
finally:
glm_quota.GLM_KEY = original_key
if __name__ == "__main__":
unittest.main()