package main import ( "io" "net" "path/filepath" "strings" "testing" "github.com/stretchr/testify/require" ) func TestClientEndsWhenTheDaemonEnds(t *testing.T) { dir := t.TempDir() listener, err := daemonListenSocket(filepath.Join(dir, socketFileName)) require.NoError(t, err) accepted := make(chan net.Conn) go func() { conn, err := listener.Accept() if err == nil { accepted <- conn } }() conn, err := net.Dial("unix", filepath.Join(dir, socketFileName)) require.NoError(t, err) in, _ := io.Pipe() done := make(chan error) go func() { done <- terminalSession(t.Context(), conn, in, io.Discard) }() require.NoError(t, (<-accepted).Close()) require.ErrorIs(t, <-done, errTerminalDaemonClosed) require.NoError(t, listener.Close()) } func TestEventText(t *testing.T) { testCases := []struct { name string event Event text string }{ { name: "reply as it is", event: Event{Kind: "reply", Text: "Done."}, text: "Done.", }, { name: "own user message not shown", event: Event{Kind: "user", Text: "typed", Source: terminalSource}, text: "", }, { name: "user message of another source marked", event: Event{Kind: "user", Text: "from the phone", Source: "telegram"}, text: "[telegram] from the phone", }, { name: "long result cut", event: Event{Kind: "echo", Text: strings.Repeat("line\n", 12)}, text: "[result] " + strings.Repeat("line\n", 9) + "line\n[2 more lines]", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { require.Equal(t, tc.text, terminalEventText(tc.event)) }) } }