/ contexa / GO / cortexa / workspace_test.go
Swadhin Biswas refactor: rename package to 'cortexa' and repo to 'cortexa' · 3/6/2026
34324f9
376 lines · 11.0 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package cortexa

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

// helper: creates a temp dir, initialises a Workspace, returns ws + cleanup func.
func setupWorkspace(t *testing.T, roadmap string) (*Workspace, func()) {
	t.Helper()
	dir := t.TempDir()
	ws := New(dir)
	if err := ws.Init(roadmap); err != nil {
		t.Fatalf("Init: %v", err)
	}
	return ws, func() {} // t.TempDir handles cleanup
}

// ------------------------------------------------------------------ //
// Init                                                                //
// ------------------------------------------------------------------ //

func TestInit_CreatesGCCStructure(t *testing.T) {
	ws, _ := setupWorkspace(t, "Build an AI agent")

	// .GCC/main.md exists
	data, err := os.ReadFile(ws.mainMd())
	if err != nil {
		t.Fatalf("main.md missing: %v", err)
	}
	if !strings.Contains(string(data), "Build an AI agent") {
		t.Errorf("main.md missing roadmap content")
	}

	// branches/main/ directory created
	info, err := os.Stat(ws.branchDir(mainBranch))
	if err != nil || !info.IsDir() {
		t.Errorf("expected branches/main directory")
	}

	// log.md, commit.md, metadata.yaml present
	for _, f := range []string{ws.logPath(mainBranch), ws.commitPath(mainBranch), ws.metaPath(mainBranch)} {
		if _, err := os.Stat(f); err != nil {
			t.Errorf("expected %s to exist", filepath.Base(f))
		}
	}
}

func TestInit_DoubleInit_Fails(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")
	if err := ws.Init("again"); err == nil {
		t.Errorf("expected error on double init")
	}
}

// ------------------------------------------------------------------ //
// LogOTA                                                              //
// ------------------------------------------------------------------ //

func TestLogOTA_IncrementsStep(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")

	r1, err := ws.LogOTA("obs1", "thought1", "act1")
	if err != nil {
		t.Fatalf("LogOTA: %v", err)
	}
	if r1.Step != 1 {
		t.Errorf("expected step 1, got %d", r1.Step)
	}

	r2, err := ws.LogOTA("obs2", "thought2", "act2")
	if err != nil {
		t.Fatalf("LogOTA: %v", err)
	}
	if r2.Step != 2 {
		t.Errorf("expected step 2, got %d", r2.Step)
	}
}

func TestLogOTA_AppendsToLogMd(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")
	ws.LogOTA("observation text", "thinking hard", "do something")

	data := ws.read(ws.logPath(mainBranch))
	if !strings.Contains(data, "observation text") {
		t.Errorf("log.md missing observation")
	}
	if !strings.Contains(data, "thinking hard") {
		t.Errorf("log.md missing thought")
	}
}

// ------------------------------------------------------------------ //
// Commit                                                              //
// ------------------------------------------------------------------ //

func TestCommit_WritesToCommitMd(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")

	c, err := ws.Commit("Finished setup phase", nil, nil)
	if err != nil {
		t.Fatalf("Commit: %v", err)
	}
	if c.CommitID == "" {
		t.Error("expected non-empty commit ID")
	}
	if c.BranchName != mainBranch {
		t.Errorf("expected branch %s, got %s", mainBranch, c.BranchName)
	}

	data := ws.read(ws.commitPath(mainBranch))
	if !strings.Contains(data, "Finished setup phase") {
		t.Error("commit.md missing contribution text")
	}
}

func TestCommit_AutoPreviousSummary(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")

	// First commit — no prior, should default
	c1, _ := ws.Commit("First milestone", nil, nil)
	if c1.PreviousProgressSummary != "Initial state — no prior commits." {
		t.Errorf("unexpected first prev summary: %q", c1.PreviousProgressSummary)
	}

	// Second commit — auto-uses first's contribution
	c2, _ := ws.Commit("Second milestone", nil, nil)
	if c2.PreviousProgressSummary != "First milestone" {
		t.Errorf("expected auto previous summary 'First milestone', got %q", c2.PreviousProgressSummary)
	}
}

func TestCommit_UpdateRoadmap(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")

	update := "We pivoted to approach B"
	ws.Commit("Pivot done", nil, &update)

	data := ws.read(ws.mainMd())
	if !strings.Contains(data, "We pivoted to approach B") {
		t.Error("main.md should contain roadmap update")
	}
}

// ------------------------------------------------------------------ //
// Branch                                                              //
// ------------------------------------------------------------------ //

func TestBranch_CreatesIsolatedWorkspace(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")

	if err := ws.Branch("experiment", "Try alternative approach"); err != nil {
		t.Fatalf("Branch: %v", err)
	}
	if ws.CurrentBranch() != "experiment" {
		t.Errorf("expected current branch 'experiment', got %q", ws.CurrentBranch())
	}

	// Branch should have its own log.md
	info, err := os.Stat(ws.logPath("experiment"))
	if err != nil {
		t.Fatalf("experiment/log.md should exist: %v", err)
	}
	if info.Size() == 0 {
		t.Error("log.md should not be empty (should have header)")
	}

	// metadata.yaml should record purpose
	meta, err := ws.parseMeta("experiment")
	if err != nil {
		t.Fatalf("parseMeta: %v", err)
	}
	if meta == nil || meta.Purpose != "Try alternative approach" {
		t.Error("metadata should record purpose")
	}
}

func TestBranch_DuplicateName_Fails(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")
	ws.Branch("exp", "first")
	if err := ws.Branch("exp", "second"); err == nil {
		t.Error("expected error creating duplicate branch")
	}
}

// ------------------------------------------------------------------ //
// Merge                                                               //
// ------------------------------------------------------------------ //

func TestMerge_IntegratesBranchIntoTarget(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")

	// Work on main, then branch
	ws.LogOTA("main obs", "main thought", "main action")
	ws.Commit("Main setup done", nil, nil)

	ws.Branch("feature", "Add new capability")
	ws.LogOTA("feature obs", "feature thought", "feature action")
	ws.Commit("Feature implemented", nil, nil)

	// Merge back to main
	mergeCommit, err := ws.Merge("feature", nil, mainBranch)
	if err != nil {
		t.Fatalf("Merge: %v", err)
	}

	// Check merge commit
	if !strings.Contains(mergeCommit.ThisCommitContribution, "feature") {
		t.Error("merge commit should reference the merged branch")
	}
	if ws.CurrentBranch() != mainBranch {
		t.Errorf("after merge, should be on %s, got %s", mainBranch, ws.CurrentBranch())
	}

	// OTA from feature should appear in main's log
	data := ws.read(ws.logPath(mainBranch))
	if !strings.Contains(data, "feature obs") {
		t.Error("main log should contain merged OTA from feature branch")
	}

	// Feature branch metadata should say "merged"
	meta, _ := ws.parseMeta("feature")
	if meta == nil || meta.Status != "merged" {
		t.Error("feature branch metadata should be status=merged")
	}
}

func TestMerge_NonexistentBranch_Fails(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")
	_, err := ws.Merge("nope", nil, mainBranch)
	if err == nil {
		t.Error("expected error merging nonexistent branch")
	}
}

func TestMerge_NonexistentTarget_Fails(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")
	ws.Branch("feature", "test")
	_, err := ws.Merge("feature", nil, "nonexistent")
	if err == nil {
		t.Error("expected error merging into nonexistent target")
	}
}

// ------------------------------------------------------------------ //
// Context                                                             //
// ------------------------------------------------------------------ //

func TestContext_ReturnsRoadmapAndCommits(t *testing.T) {
	ws, _ := setupWorkspace(t, "Build a multi-agent system")
	ws.LogOTA("o", "t", "a")
	ws.Commit("Phase 1 complete", nil, nil)
	ws.Commit("Phase 2 complete", nil, nil)

	// K=1 should return only last commit
	ctx, err := ws.Context(nil, 1)
	if err != nil {
		t.Fatalf("Context: %v", err)
	}
	if !strings.Contains(ctx.MainRoadmap, "multi-agent system") {
		t.Error("context should include roadmap")
	}
	if len(ctx.Commits) != 1 {
		t.Errorf("K=1 should return 1 commit, got %d", len(ctx.Commits))
	}
	if ctx.Commits[0].ThisCommitContribution != "Phase 2 complete" {
		t.Error("K=1 should return the most recent commit")
	}

	// K=5 (more than available) should return all
	ctx2, _ := ws.Context(nil, 5)
	if len(ctx2.Commits) != 2 {
		t.Errorf("K=5 should return all 2 commits, got %d", len(ctx2.Commits))
	}
}

func TestContext_NonexistentBranch_Fails(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")
	nope := "nonexistent"
	_, err := ws.Context(&nope, 1)
	if err == nil {
		t.Error("expected error for nonexistent branch")
	}
}

func TestContext_Summary(t *testing.T) {
	ws, _ := setupWorkspace(t, "Project X")
	ws.LogOTA("obs", "thought", "action")
	ws.Commit("Done step 1", nil, nil)

	ctx, _ := ws.Context(nil, 1)
	summary := ctx.Summary()
	if !strings.Contains(summary, "CONTEXT") {
		t.Error("summary should contain CONTEXT header")
	}
	if !strings.Contains(summary, mainBranch) {
		t.Error("summary should mention branch name")
	}
}

// ------------------------------------------------------------------ //
// Helpers                                                             //
// ------------------------------------------------------------------ //

func TestSwitchBranch(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")
	ws.Branch("alt", "alternative path")
	ws.SwitchBranch(mainBranch)
	if ws.CurrentBranch() != mainBranch {
		t.Errorf("expected main, got %s", ws.CurrentBranch())
	}
}

func TestSwitchBranch_Nonexistent_Fails(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")
	if err := ws.SwitchBranch("nope"); err == nil {
		t.Error("expected error switching to nonexistent branch")
	}
}

func TestListBranches(t *testing.T) {
	ws, _ := setupWorkspace(t, "test")
	ws.Branch("alpha", "a")
	ws.Branch("beta", "b")

	branches, err := ws.ListBranches()
	if err != nil {
		t.Fatalf("ListBranches: %v", err)
	}
	// Should have main, alpha, beta
	if len(branches) != 3 {
		t.Errorf("expected 3 branches, got %d: %v", len(branches), branches)
	}
}

// ------------------------------------------------------------------ //
// Models                                                              //
// ------------------------------------------------------------------ //

func TestOTARecord_ToMarkdown(t *testing.T) {
	r := OTARecord{
		Step:        1,
		Timestamp:   "2025-01-01T00:00:00Z",
		Observation: "saw an error",
		Thought:     "need to fix it",
		Action:      "patched the code",
	}
	md := r.ToMarkdown()
	if !strings.Contains(md, "Step 1") {
		t.Error("markdown should contain step number")
	}
	if !strings.Contains(md, "saw an error") {
		t.Error("markdown should contain observation")
	}
}

func TestCommitRecord_ToMarkdown(t *testing.T) {
	c := CommitRecord{
		CommitID:                "abc123",
		BranchName:              "main",
		BranchPurpose:           "primary",
		PreviousProgressSummary: "none",
		ThisCommitContribution:  "added feature X",
		Timestamp:               "2025-01-01T00:00:00Z",
	}
	md := c.ToMarkdown()
	if !strings.Contains(md, "abc123") {
		t.Error("markdown should contain commit ID")
	}
	if !strings.Contains(md, "added feature X") {
		t.Error("markdown should contain contribution")
	}
}