fix: dogfood watch_test + resume_test — eliminate os + encoding/json

os.MkdirAll→fs.EnsureDir, os.WriteFile→fs.Write, os.ReadFile→fs.Read,
json.Marshal→core.JSONMarshalString. Both files now clean.

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Snider 2026-03-26 02:03:08 +00:00
parent 68b55572f1
commit 76b167a874
2 changed files with 22 additions and 34 deletions

View file

@ -4,8 +4,6 @@ package agentic
import (
"context"
"encoding/json"
"os"
"os/exec"
"testing"
"time"
@ -24,12 +22,11 @@ func TestResume_Resume_Good(t *testing.T) {
wsRoot := WorkspaceRoot()
ws := core.JoinPath(wsRoot, "ws-blocked")
repoDir := core.JoinPath(ws, "repo")
os.MkdirAll(repoDir, 0o755)
fs.EnsureDir(repoDir)
exec.Command("git", "init", repoDir).Run()
st := &WorkspaceStatus{Status: "blocked", Repo: "go-io", Agent: "codex", Task: "Fix the tests"}
data, _ := json.Marshal(st)
os.WriteFile(core.JoinPath(ws, "status.json"), data, 0o644)
fs.Write(core.JoinPath(ws, "status.json"), core.JSONMarshalString(st))
s := &PrepSubsystem{ServiceRuntime: core.NewServiceRuntime(testCore, AgentOptions{}), backoff: make(map[string]time.Time), failCount: make(map[string]int)}
_, out, err := s.resume(context.Background(), nil, ResumeInput{
@ -42,8 +39,8 @@ func TestResume_Resume_Good(t *testing.T) {
assert.Contains(t, out.Prompt, "Fix the tests")
assert.Contains(t, out.Prompt, "Use the new Core API")
answerContent, _ := os.ReadFile(core.JoinPath(repoDir, "ANSWER.md"))
assert.Contains(t, string(answerContent), "Use the new Core API")
answerR := fs.Read(core.JoinPath(repoDir, "ANSWER.md"))
assert.Contains(t, answerR.Value.(string), "Use the new Core API")
// Agent override
_, out2, _ := s.resume(context.Background(), nil, ResumeInput{
@ -53,11 +50,10 @@ func TestResume_Resume_Good(t *testing.T) {
// Completed workspace is resumable too
ws2 := core.JoinPath(wsRoot, "ws-done")
os.MkdirAll(core.JoinPath(ws2, "repo"), 0o755)
fs.EnsureDir(core.JoinPath(ws2, "repo"))
exec.Command("git", "init", core.JoinPath(ws2, "repo")).Run()
st2 := &WorkspaceStatus{Status: "completed", Repo: "go-io", Agent: "codex", Task: "Review code"}
data2, _ := json.Marshal(st2)
os.WriteFile(core.JoinPath(ws2, "status.json"), data2, 0o644)
fs.Write(core.JoinPath(ws2, "status.json"), core.JSONMarshalString(st2))
_, out3, err3 := s.resume(context.Background(), nil, ResumeInput{Workspace: "ws-done", DryRun: true})
require.NoError(t, err3)
@ -82,11 +78,10 @@ func TestResume_Resume_Bad(t *testing.T) {
// Not resumable (running)
ws := core.JoinPath(WorkspaceRoot(), "ws-running")
os.MkdirAll(core.JoinPath(ws, "repo"), 0o755)
fs.EnsureDir(core.JoinPath(ws, "repo"))
exec.Command("git", "init", core.JoinPath(ws, "repo")).Run()
st := &WorkspaceStatus{Status: "running", Repo: "test", Agent: "codex"}
data, _ := json.Marshal(st)
os.WriteFile(core.JoinPath(ws, "status.json"), data, 0o644)
fs.Write(core.JoinPath(ws, "status.json"), core.JSONMarshalString(st))
_, _, err = s.resume(context.Background(), nil, ResumeInput{Workspace: "ws-running"})
assert.Error(t, err)
@ -99,7 +94,7 @@ func TestResume_Resume_Ugly(t *testing.T) {
// Workspace exists but no status.json
ws := core.JoinPath(WorkspaceRoot(), "ws-nostatus")
os.MkdirAll(core.JoinPath(ws, "repo"), 0o755)
fs.EnsureDir(core.JoinPath(ws, "repo"))
exec.Command("git", "init", core.JoinPath(ws, "repo")).Run()
s := &PrepSubsystem{ServiceRuntime: core.NewServiceRuntime(testCore, AgentOptions{}), backoff: make(map[string]time.Time), failCount: make(map[string]int)}
@ -109,11 +104,10 @@ func TestResume_Resume_Ugly(t *testing.T) {
// No answer provided — prompt has no ANSWER section
ws2 := core.JoinPath(WorkspaceRoot(), "ws-noanswer")
os.MkdirAll(core.JoinPath(ws2, "repo"), 0o755)
fs.EnsureDir(core.JoinPath(ws2, "repo"))
exec.Command("git", "init", core.JoinPath(ws2, "repo")).Run()
st := &WorkspaceStatus{Status: "blocked", Repo: "test", Agent: "codex", Task: "Fix"}
data, _ := json.Marshal(st)
os.WriteFile(core.JoinPath(ws2, "status.json"), data, 0o644)
fs.Write(core.JoinPath(ws2, "status.json"), core.JSONMarshalString(st))
_, out, err := s.resume(context.Background(), nil, ResumeInput{Workspace: "ws-noanswer", DryRun: true})
require.NoError(t, err)

View file

@ -3,8 +3,6 @@
package agentic
import (
"encoding/json"
"os"
"testing"
"time"
@ -45,21 +43,18 @@ func TestWatch_FindActiveWorkspaces_Good_WithActive(t *testing.T) {
// Create running workspace
ws1 := core.JoinPath(wsRoot, "ws-running")
os.MkdirAll(ws1, 0o755)
st1, _ := json.Marshal(WorkspaceStatus{Status: "running", Repo: "go-io", Agent: "codex"})
os.WriteFile(core.JoinPath(ws1, "status.json"), st1, 0o644)
fs.EnsureDir(ws1)
fs.Write(core.JoinPath(ws1, "status.json"), core.JSONMarshalString(WorkspaceStatus{Status: "running", Repo: "go-io", Agent: "codex"}))
// Create completed workspace (should not be in active list)
ws2 := core.JoinPath(wsRoot, "ws-done")
os.MkdirAll(ws2, 0o755)
st2, _ := json.Marshal(WorkspaceStatus{Status: "completed", Repo: "go-crypt", Agent: "codex"})
os.WriteFile(core.JoinPath(ws2, "status.json"), st2, 0o644)
fs.EnsureDir(ws2)
fs.Write(core.JoinPath(ws2, "status.json"), core.JSONMarshalString(WorkspaceStatus{Status: "completed", Repo: "go-crypt", Agent: "codex"}))
// Create queued workspace
ws3 := core.JoinPath(wsRoot, "ws-queued")
os.MkdirAll(ws3, 0o755)
st3, _ := json.Marshal(WorkspaceStatus{Status: "queued", Repo: "go-log", Agent: "gemini"})
os.WriteFile(core.JoinPath(ws3, "status.json"), st3, 0o644)
fs.EnsureDir(ws3)
fs.Write(core.JoinPath(ws3, "status.json"), core.JSONMarshalString(WorkspaceStatus{Status: "queued", Repo: "go-log", Agent: "gemini"}))
s := &PrepSubsystem{
ServiceRuntime: core.NewServiceRuntime(testCore, AgentOptions{}),
@ -77,7 +72,7 @@ func TestWatch_FindActiveWorkspaces_Good_Empty(t *testing.T) {
t.Setenv("CORE_WORKSPACE", root)
// Ensure workspace dir exists but is empty
os.MkdirAll(core.JoinPath(root, "workspace"), 0o755)
fs.EnsureDir(core.JoinPath(root, "workspace"))
s := &PrepSubsystem{
ServiceRuntime: core.NewServiceRuntime(testCore, AgentOptions{}),
@ -114,14 +109,13 @@ func TestWatch_FindActiveWorkspaces_Ugly(t *testing.T) {
// Create workspace with corrupt status.json
ws1 := core.JoinPath(wsRoot, "ws-corrupt")
os.MkdirAll(ws1, 0o755)
os.WriteFile(core.JoinPath(ws1, "status.json"), []byte("not-valid-json{{{"), 0o644)
fs.EnsureDir(ws1)
fs.Write(core.JoinPath(ws1, "status.json"), "not-valid-json{{{")
// Create valid running workspace
ws2 := core.JoinPath(wsRoot, "ws-valid")
os.MkdirAll(ws2, 0o755)
st, _ := json.Marshal(WorkspaceStatus{Status: "running", Repo: "go-io", Agent: "codex"})
os.WriteFile(core.JoinPath(ws2, "status.json"), st, 0o644)
fs.EnsureDir(ws2)
fs.Write(core.JoinPath(ws2, "status.json"), core.JSONMarshalString(WorkspaceStatus{Status: "running", Repo: "go-io", Agent: "codex"}))
s := &PrepSubsystem{
ServiceRuntime: core.NewServiceRuntime(testCore, AgentOptions{}),