]> git.giacomolli.uk - onechat/blobdiff - terminal_test.go
Move whatsapp_send to its own file
[onechat] / terminal_test.go
index 9df3e0a99902382e0d59ebc24e8c2393b961b542..62ffb040abe37518915d7f15560e001cfe51fa17 100644 (file)
@@ -6,14 +6,13 @@ import (
        "path/filepath"
        "strings"
        "testing"
        "path/filepath"
        "strings"
        "testing"
-       "time"
 
        "github.com/stretchr/testify/require"
 )
 
 func TestClientEndsWhenTheDaemonEnds(t *testing.T) {
        dir := t.TempDir()
 
        "github.com/stretchr/testify/require"
 )
 
 func TestClientEndsWhenTheDaemonEnds(t *testing.T) {
        dir := t.TempDir()
-       listener, err := listenSocket(filepath.Join(dir, socketFileName))
+       listener, err := daemonListenSocket(filepath.Join(dir, socketFileName))
        require.NoError(t, err)
 
        accepted := make(chan net.Conn)
        require.NoError(t, err)
 
        accepted := make(chan net.Conn)
@@ -59,6 +58,16 @@ func TestEventText(t *testing.T) {
                        event: Event{Kind: "user", Text: "from the phone", Source: "telegram"},
                        text:  "[telegram] from the phone",
                },
                        event: Event{Kind: "user", Text: "from the phone", Source: "telegram"},
                        text:  "[telegram] from the phone",
                },
+               {
+                       name:  "start of a subagent",
+                       event: Event{Kind: "subagent", Text: "GreenOtter"},
+                       text:  "[GreenOtter] started",
+               },
+               {
+                       name:  "report of a subagent whole",
+                       event: Event{Kind: "work", Text: "[GreenOtter]\nThere are 3 files.\n\nFull chat: zoom(\"GreenOtter\")", Source: "GreenOtter", Status: subagentFinished},
+                       text:  "[GreenOtter]\nThere are 3 files.\n\nFull chat: zoom(\"GreenOtter\")",
+               },
                {
                        name:  "long result cut",
                        event: Event{Kind: "echo", Text: strings.Repeat("line\n", 12)},
                {
                        name:  "long result cut",
                        event: Event{Kind: "echo", Text: strings.Repeat("line\n", 12)},
@@ -72,68 +81,3 @@ func TestEventText(t *testing.T) {
                })
        }
 }
                })
        }
 }
-
-// TerminalTestClient is a terminal client of a test daemon, with a pipe as its
-// input.
-type TerminalTestClient struct {
-       input *io.PipeWriter
-       done  chan error
-       out   *SyncBuffer
-}
-
-// Type writes one line to the input of the client.
-func (c *TerminalTestClient) Type(line string) {
-       _, err := io.WriteString(c.input, line+"\n")
-       if err != nil {
-               panic(err)
-       }
-}
-
-// Output returns what the client showed.
-func (c *TerminalTestClient) Output() string {
-       return c.out.String()
-}
-
-// WaitFor waits until the client showed text.
-func (c *TerminalTestClient) WaitFor(t *testing.T, text string) {
-       t.Helper()
-
-       require.Eventually(t, func() bool {
-               return strings.Contains(c.Output(), text)
-       }, 10*time.Second, time.Millisecond, "output so far: %q", c.Output())
-}
-
-// Close ends the input and waits for the client.
-func (c *TerminalTestClient) Close() error {
-       err := c.input.Close()
-       if err != nil {
-               return err
-       }
-
-       return <-c.done
-}
-
-// terminalStartTestClient connects a client to the daemon at socket.
-func terminalStartTestClient(t *testing.T, socket string) *TerminalTestClient {
-       t.Helper()
-
-       conn, err := net.Dial("unix", socket)
-       require.NoError(t, err)
-
-       reader, writer := io.Pipe()
-       client := &TerminalTestClient{
-               input: writer,
-               done:  make(chan error, 1),
-               out:   &SyncBuffer{},
-       }
-
-       go func() {
-               client.done <- terminalSession(t.Context(), conn, reader, client.out)
-       }()
-
-       t.Cleanup(func() {
-               _ = writer.Close()
-       })
-
-       return client
-}