]> git.giacomolli.uk - onechat/blobdiff - subagent_run_test.go
Check spacing between Go statement blocks
[onechat] / subagent_run_test.go
index 0341ba6dde6e8fd77deb1f3ca48ae44c2855c140..f637dcfcc9938edc983bfc75d9c1aba41d3948b6 100644 (file)
@@ -21,7 +21,9 @@ const subagentTestFullContext = `{"error": {"message": "Your input exceeds the c
 
 func TestSpawnRunsASubagentInTheBackground(t *testing.T) {
        server := newModelFake(t)
+
        var test *TurnTestLoop
+
        server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
                last := subagentTestLastItem(call)
                switch {
@@ -47,6 +49,7 @@ func TestSpawnRunsASubagentInTheBackground(t *testing.T) {
 
        name := test.events.Last("subagent")
        require.True(t, subagentNameValid(name), name)
+
        work := filepath.Join(test.loop.runs.subagents.dir, name, "work")
        require.DirExists(t, work)
 
@@ -69,6 +72,7 @@ func TestSpawnRunsASubagentInTheBackground(t *testing.T) {
        require.Equal(t, "subagent "+name+" started\nsubagent "+name+" done after 0s\n", test.logs.String())
 
        var first, second *ModelFakeCall
+
        for n := range server.Calls() {
                call := server.Call(n)
                if !subagentTestCall(call) {
@@ -97,6 +101,7 @@ func TestSpawnRunsASubagentInTheBackground(t *testing.T) {
        require.Equal(t, instructions, first.body["instructions"])
 
        var tools []string
+
        for _, tool := range first.body["tools"].([]any) {
                tools = append(tools, tool.(map[string]any)["name"].(string))
        }
@@ -112,6 +117,7 @@ func TestSpawnRunsASubagentInTheBackground(t *testing.T) {
        require.Equal(t, "0|note: subagent\n\n1|user: Count the files.\n\n2|tool: shell {\"command\":\"pwd\"}\n\n3|echo: "+work+"\n\n\n4|reply: There are 3 files.", log)
 
        var agents []string
+
        for _, record := range modelReadUsage(t, test.usagePath) {
                agents = append(agents, record.Agent)
        }
@@ -140,6 +146,7 @@ func TestReportJoinsTheNextRequestOfARunningTurn(t *testing.T) {
        require.NoError(t, err)
 
        <-arrived
+
        report := subagentReport("GreenOtter", []string{"It is done."})
        test.loop.Report("GreenOtter", report, subagentFinished)
        close(release)
@@ -170,6 +177,7 @@ func TestFullContextGivesAFailedReport(t *testing.T) {
        server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
                if subagentTestCall(call) {
                        w.WriteHeader(http.StatusBadRequest)
+
                        _, _ = io.WriteString(w, subagentTestFullContext)
 
                        return
@@ -185,11 +193,11 @@ func TestFullContextGivesAFailedReport(t *testing.T) {
 
        name := test.events.Last("subagent")
        require.Equal(t, []string{
-               "work: [" + name + "]\nfailed: model call: HTTP 400: " + subagentTestFullContext + "\n\nFull chat: zoom(\"" + name + "\")",
+               "work: [" + name + "]\nfailed: create model response: model call: HTTP 400: " + subagentTestFullContext + "\n\nFull chat: zoom(\"" + name + "\")",
                "reply: It failed.",
        }, test.Log(t))
        require.Equal(t, subagentFailed, test.events.LastEvent("work").Status)
-       require.Equal(t, "subagent "+name+" started\nsubagent "+name+" failed after 0s: model call: HTTP 400: "+subagentTestFullContext+"\n", test.logs.String())
+       require.Equal(t, "subagent "+name+" started\nsubagent "+name+" failed after 0s: create model response: model call: HTTP 400: "+subagentTestFullContext+"\n", test.logs.String())
 }
 
 func TestSubagentHasItsOwnEffortAndNoSpawn(t *testing.T) {
@@ -260,6 +268,7 @@ func TestTellReachesARunningSubagentBetweenItsToolCalls(t *testing.T) {
        require.NoError(t, err)
 
        <-arrived
+
        name := test.events.Last("subagent")
        output, err := runs.Tell(t.Context(), name, "Use the second disk.")
        require.NoError(t, err)
@@ -294,6 +303,7 @@ func TestTellAfterTheFinalAnswerStartsAnotherChain(t *testing.T) {
        require.NoError(t, err)
 
        <-arrived
+
        name := test.events.Last("subagent")
        _, err = runs.Tell(t.Context(), name, "Count again.")
        require.NoError(t, err)
@@ -428,10 +438,12 @@ func TestRestartResumesTheCutSubagents(t *testing.T) {
        })
 
        var restarted atomic.Bool
+
        server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
                switch {
                case subagentTestCall(call) && !restarted.Load():
                        arrived <- struct{}{}
+
                        <-release
                case subagentTestCall(call):
                        modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("Built.")}, 100, 0, 5))
@@ -449,7 +461,9 @@ func TestRestartResumesTheCutSubagents(t *testing.T) {
        require.NoError(t, err)
 
        <-arrived
+
        name := first.Last("subagent")
+
        stop()
        require.Zero(t, first.Count("work"))
 
@@ -458,11 +472,13 @@ func TestRestartResumesTheCutSubagents(t *testing.T) {
        require.JSONEq(t, `{"running": ["`+name+`"]}`, string(data))
 
        restarted.Store(true)
+
        second := &TurnEventRecorder{}
        loop, memory, stop := subagentTestStart(t, agent, server, second)
        defer stop()
 
        second.WaitFor(t, "idle", 1)
+
        note := "The daemon restarted, cutting " + name + "; nothing was lost: go on."
        log := subagentTestChat(t, memory)
        require.Equal(t, []string{
@@ -543,6 +559,7 @@ func subagentTestChat(t *testing.T, memory *Memory) []string {
        t.Helper()
 
        var lines []string
+
        for id := range memory.Len() {
                message, err := memory.Message(id)
                require.NoError(t, err)
@@ -571,7 +588,9 @@ func subagentTestLastItem(call ModelFakeCall) map[string]any {
 // another item.
 func subagentTestText(item map[string]any) string {
        content, _ := item["content"].([]any)
+
        var text strings.Builder
+
        for _, part := range content {
                value, _ := part.(map[string]any)["text"].(string)
                text.WriteString(value)