python: add comprehensive test suite (13 tests)
Swadhin Biswas committed 7 months ago
c60a930
1 parent 654799b
commit c60a930
1 file changed
+120 -0
Showing 1 changed file+120-0
▼PYTHON/test/test.py
+120-0
| @@ -0,0 +1,120 @@ | ||
1 | +"""Tests for gcc-context Python package.""" | |
2 | + | |
3 | +import pytest | |
4 | +from contexa import GCCWorkspace | |
5 | + | |
6 | + | |
7 | +@pytest.fixture | |
8 | +def ws(tmp_path): | |
9 | + workspace = GCCWorkspace(str(tmp_path)) | |
10 | + workspace.init("Test project roadmap") | |
11 | + return workspace | |
12 | + | |
13 | + | |
14 | +def test_init_creates_gcc_directory(tmp_path): | |
15 | + ws = GCCWorkspace(str(tmp_path)) | |
16 | + ws.init("Test project") | |
17 | + assert (tmp_path / ".GCC" / "main.md").exists() | |
18 | + assert (tmp_path / ".GCC" / "branches" / "main" / "log.md").exists() | |
19 | + assert (tmp_path / ".GCC" / "branches" / "main" / "commit.md").exists() | |
20 | + assert (tmp_path / ".GCC" / "branches" / "main" / "metadata.yaml").exists() | |
21 | + | |
22 | + | |
23 | +def test_log_ota(ws): | |
24 | + rec = ws.log_ota("saw empty dir", "should read files", "list_files()") | |
25 | + assert rec.step == 1 | |
26 | + assert rec.observation == "saw empty dir" | |
27 | + | |
28 | + | |
29 | +def test_commit(ws): | |
30 | + ws.log_ota("obs", "thought", "action") | |
31 | + commit = ws.commit("Initial scaffold done") | |
32 | + assert commit.this_commit_contribution == "Initial scaffold done" | |
33 | + assert commit.branch_name == "main" | |
34 | + assert len(commit.commit_id) == 8 | |
35 | + | |
36 | + | |
37 | +def test_branch_creates_isolated_workspace(ws): | |
38 | + ws.branch("experiment-a", "Try alternative algorithm") | |
39 | + assert ws.current_branch == "experiment-a" | |
40 | + branches = ws.list_branches() | |
41 | + assert "main" in branches | |
42 | + assert "experiment-a" in branches | |
43 | + | |
44 | + | |
45 | +def test_branch_has_fresh_ota_log(ws): | |
46 | + ws.log_ota("main obs", "main thought", "main action") | |
47 | + ws.branch("clean-branch", "Fresh start") | |
48 | + ota = ws._read_ota("clean-branch") | |
49 | + assert ota == [] | |
50 | + | |
51 | + | |
52 | +def test_merge_integrates_branch(ws): | |
53 | + ws.commit("Main first commit") | |
54 | + ws.branch("feature", "Add feature X") | |
55 | + ws.log_ota("feature obs", "feature thought", "feature action") | |
56 | + ws.commit("Feature X implemented") | |
57 | + merge_commit = ws.merge("feature", target="main") | |
58 | + assert "feature" in merge_commit.this_commit_contribution.lower() | |
59 | + assert ws.current_branch == "main" | |
60 | + | |
61 | + | |
62 | +def test_context_k1_returns_last_commit(ws): | |
63 | + ws.commit("First commit") | |
64 | + ws.commit("Second commit") | |
65 | + ws.commit("Third commit") | |
66 | + ctx = ws.context(k=1) | |
67 | + assert len(ctx.commits) == 1 | |
68 | + assert ctx.commits[0].this_commit_contribution == "Third commit" | |
69 | + | |
70 | + | |
71 | +def test_context_k3_returns_last_three(ws): | |
72 | + ws.commit("C1") | |
73 | + ws.commit("C2") | |
74 | + ws.commit("C3") | |
75 | + ws.commit("C4") | |
76 | + ctx = ws.context(k=3) | |
77 | + assert len(ctx.commits) == 3 | |
78 | + | |
79 | + | |
80 | +def test_context_includes_roadmap(ws): | |
81 | + ctx = ws.context() | |
82 | + assert "Test project roadmap" in ctx.main_roadmap | |
83 | + | |
84 | + | |
85 | +def test_branch_metadata_records_purpose(ws): | |
86 | + ws.branch("jwt-branch", "Test JWT auth approach") | |
87 | + from contexa.models import BranchMetadata | |
88 | + | |
89 | + meta_text = ws._read(ws._meta_path("jwt-branch")) | |
90 | + meta = BranchMetadata.from_yaml(meta_text) | |
91 | + assert meta.purpose == "Test JWT auth approach" | |
92 | + assert meta.created_from == "main" | |
93 | + assert meta.status == "active" | |
94 | + | |
95 | + | |
96 | +def test_merge_marks_branch_as_merged(ws): | |
97 | + ws.branch("to-merge", "Will be merged") | |
98 | + ws.commit("Branch work done") | |
99 | + ws.merge("to-merge", target="main") | |
100 | + from contexa.models import BranchMetadata | |
101 | + | |
102 | + meta_text = ws._read(ws._meta_path("to-merge")) | |
103 | + meta = BranchMetadata.from_yaml(meta_text) | |
104 | + assert meta.status == "merged" | |
105 | + assert meta.merged_into == "main" | |
106 | + | |
107 | + | |
108 | +def test_switch_branch(ws): | |
109 | + ws.branch("b1", "Branch one") | |
110 | + ws.switch_branch("main") | |
111 | + assert ws.current_branch == "main" | |
112 | + | |
113 | + | |
114 | +def test_ota_step_increments(ws): | |
115 | + r1 = ws.log_ota("o1", "t1", "a1") | |
116 | + r2 = ws.log_ota("o2", "t2", "a2") | |
117 | + r3 = ws.log_ota("o3", "t3", "a3") | |
118 | + assert r1.step == 1 | |
119 | + assert r2.step == 2 | |
120 | + assert r3.step == 3 | |
0 | 121 | |