]> git.giacomolli.uk - onechat/blob - terminal_test.go
Shorten user service updates
[onechat] / terminal_test.go
1 package main
2
3 import (
4         "io"
5         "net"
6         "path/filepath"
7         "strings"
8         "testing"
9
10         "github.com/stretchr/testify/require"
11 )
12
13 func TestClientEndsWhenTheDaemonEnds(t *testing.T) {
14         dir := t.TempDir()
15         listener, err := daemonListenSocket(filepath.Join(dir, socketFileName))
16         require.NoError(t, err)
17
18         accepted := make(chan net.Conn)
19         go func() {
20                 conn, err := listener.Accept()
21                 if err == nil {
22                         accepted <- conn
23                 }
24         }()
25
26         conn, err := net.Dial("unix", filepath.Join(dir, socketFileName))
27         require.NoError(t, err)
28
29         in, _ := io.Pipe()
30         done := make(chan error)
31         go func() {
32                 done <- terminalSession(t.Context(), conn, in, io.Discard)
33         }()
34
35         require.NoError(t, (<-accepted).Close())
36         require.ErrorIs(t, <-done, errTerminalDaemonClosed)
37         require.NoError(t, listener.Close())
38 }
39
40 func TestEventText(t *testing.T) {
41         testCases := []struct {
42                 name  string
43                 event Event
44                 text  string
45         }{
46                 {
47                         name:  "reply as it is",
48                         event: Event{Kind: "reply", Text: "Done."},
49                         text:  "Done.",
50                 },
51                 {
52                         name:  "own user message not shown",
53                         event: Event{Kind: "user", Text: "typed", Source: terminalSource},
54                         text:  "",
55                 },
56                 {
57                         name:  "user message of another source marked",
58                         event: Event{Kind: "user", Text: "from the phone", Source: "telegram"},
59                         text:  "[telegram] from the phone",
60                 },
61                 {
62                         name:  "start of a subagent",
63                         event: Event{Kind: "subagent", Text: "GreenOtter"},
64                         text:  "[GreenOtter] started",
65                 },
66                 {
67                         name:  "report of a subagent whole",
68                         event: Event{Kind: "work", Text: "[GreenOtter]\nThere are 3 files.\n\nFull chat: zoom(\"GreenOtter\")", Source: "GreenOtter", Status: subagentFinished},
69                         text:  "[GreenOtter]\nThere are 3 files.\n\nFull chat: zoom(\"GreenOtter\")",
70                 },
71                 {
72                         name:  "long result cut",
73                         event: Event{Kind: "echo", Text: strings.Repeat("line\n", 12)},
74                         text:  "[result] " + strings.Repeat("line\n", 9) + "line\n[2 more lines]",
75                 },
76         }
77
78         for _, tc := range testCases {
79                 t.Run(tc.name, func(t *testing.T) {
80                         require.Equal(t, tc.text, terminalEventText(tc.event))
81                 })
82         }
83 }