-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
153 lines (126 loc) · 4.41 KB
/
integration_test.go
File metadata and controls
153 lines (126 loc) · 4.41 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
package main
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/ActiveMemory/ctx-telephony/internal/agent"
"github.com/ActiveMemory/ctx-telephony/internal/config"
"github.com/ActiveMemory/ctx-telephony/internal/model"
)
// TestIntegrationEndToEnd simulates a full laptop→agent→laptop cycle using
// a bare git repo as the bus.
func TestIntegrationEndToEnd(t *testing.T) {
dir := t.TempDir()
bare := filepath.Join(dir, "bus.git")
laptopClone := filepath.Join(dir, "laptop")
agentClone := filepath.Join(dir, "agent")
// Init bare repo
gitRun(t, dir, "init", "--bare", "--initial-branch=main", bare)
// Clone for laptop and agent
gitRun(t, dir, "clone", bare, laptopClone)
gitRun(t, dir, "clone", bare, agentClone)
// Configure git identity in both clones
for _, clone := range []string{laptopClone, agentClone} {
gitRunIn(t, clone, "config", "user.name", "test")
gitRunIn(t, clone, "config", "user.email", "test@test")
}
// Create initial commit and repo structure from laptop
for _, sub := range []string{"outbox/hades", "inbox/hades", "archive/hades", "heartbeats", ".seq"} {
os.MkdirAll(filepath.Join(laptopClone, sub), 0755)
}
os.WriteFile(filepath.Join(laptopClone, ".gitkeep"), []byte(""), 0644)
gitRunIn(t, laptopClone, "add", "-A")
gitRunIn(t, laptopClone, "commit", "-m", "init repo structure")
gitRunIn(t, laptopClone, "push", "origin", "main")
// --- Laptop: queue a command ---
// Write seq file
seqDir := filepath.Join(laptopClone, ".seq")
os.MkdirAll(seqDir, 0755)
os.WriteFile(filepath.Join(seqDir, "hades"), []byte("0"), 0644)
// Write command file
cmd := model.Command{
ID: "1",
TS: model.NowUTC(),
Cmd: "echo integration-test-output",
Timeout: 10,
}
cmdData, _ := json.MarshalIndent(cmd, "", " ")
outboxDir := filepath.Join(laptopClone, "outbox", "hades")
cmdFile := "001_1000000_echo-integration-test-output.cmd"
os.WriteFile(filepath.Join(outboxDir, cmdFile), cmdData, 0644)
// Laptop pushes
gitRunIn(t, laptopClone, "add", "-A")
gitRunIn(t, laptopClone, "commit", "-m", "cmd → hades: echo integration test")
gitRunIn(t, laptopClone, "push", "origin", "main")
// --- Agent: pull, process, push ---
gitRunIn(t, agentClone, "pull", "--rebase", "origin", "main")
agentCfg := &config.Config{
Identity: "hades",
RepoPath: agentClone,
PollSecs: 60,
}
a := agent.New(agentCfg)
a.ProcessCommands()
gitRunIn(t, agentClone, "add", "-A")
gitRunIn(t, agentClone, "commit", "-m", "hades: processed commands")
gitRunIn(t, agentClone, "push", "origin", "main")
// --- Laptop: pull and verify results ---
gitRunIn(t, laptopClone, "pull", "--rebase", "origin", "main")
base := "001_1000000_echo-integration-test-output"
// Verify result file
resultPath := filepath.Join(laptopClone, "inbox", "hades", base+".result")
resultData, err := os.ReadFile(resultPath)
if err != nil {
t.Fatalf("result file not found after agent push: %v", err)
}
var r model.Result
json.Unmarshal(resultData, &r)
if r.ID != "1" {
t.Errorf("result ID = %q, want %q", r.ID, "1")
}
if r.ExitCode != 0 {
t.Errorf("exit code = %d, want 0", r.ExitCode)
}
if r.Hostname != "hades" {
t.Errorf("hostname = %q, want %q", r.Hostname, "hades")
}
// Verify stdout file
stdoutPath := filepath.Join(laptopClone, "inbox", "hades", base+".stdout.txt")
stdout, err := os.ReadFile(stdoutPath)
if err != nil {
t.Fatalf("stdout file not found: %v", err)
}
if got := string(stdout); got != "integration-test-output\n" {
t.Errorf("stdout = %q, want %q", got, "integration-test-output\n")
}
// Verify cmd archived
archivedCmd := filepath.Join(laptopClone, "archive", "hades", cmdFile)
if _, err := os.Stat(archivedCmd); err != nil {
t.Errorf("cmd not found in archive: %v", err)
}
// Verify cmd removed from outbox
origCmd := filepath.Join(laptopClone, "outbox", "hades", cmdFile)
if _, err := os.Stat(origCmd); !os.IsNotExist(err) {
t.Error("cmd still in outbox after agent processing")
}
}
func gitRun(t *testing.T, dir string, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, out)
}
}
func gitRunIn(t *testing.T, repoDir string, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = repoDir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v in %s failed: %v\n%s", args, repoDir, err, out)
}
}