11 "github.com/stretchr/testify/require"
14 func TestClientEndsWhenTheDaemonEnds(t *testing.T) {
16 listener, err := daemonListenSocket(filepath.Join(dir, socketFileName))
17 require.NoError(t, err)
19 accepted := make(chan net.Conn)
21 conn, err := listener.Accept()
27 conn, err := net.Dial("unix", filepath.Join(dir, socketFileName))
28 require.NoError(t, err)
31 done := make(chan error)
33 done <- terminalSession(t.Context(), conn, in, io.Discard)
36 require.NoError(t, (<-accepted).Close())
37 require.ErrorIs(t, <-done, errTerminalDaemonClosed)
38 require.NoError(t, listener.Close())
41 func TestEventText(t *testing.T) {
42 testCases := []struct {
48 name: "reply as it is",
49 event: Event{Kind: "reply", Text: "Done."},
53 name: "own user message not shown",
54 event: Event{Kind: "user", Text: "typed", Source: terminalSource},
58 name: "user message of another source marked",
59 event: Event{Kind: "user", Text: "from the phone", Source: "telegram"},
60 text: "[telegram] from the phone",
63 name: "long result cut",
64 event: Event{Kind: "echo", Text: strings.Repeat("line\n", 12)},
65 text: "[result] " + strings.Repeat("line\n", 9) + "line\n[2 more lines]",
69 for _, tc := range testCases {
70 t.Run(tc.name, func(t *testing.T) {
71 require.Equal(t, tc.text, terminalEventText(tc.event))
76 // TerminalTestClient is a terminal client of a test daemon, with a pipe as its
78 type TerminalTestClient struct {
84 // Type writes one line to the input of the client.
85 func (c *TerminalTestClient) Type(line string) {
86 _, err := io.WriteString(c.input, line+"\n")
92 // Output returns what the client showed.
93 func (c *TerminalTestClient) Output() string {
97 // WaitFor waits until the client showed text.
98 func (c *TerminalTestClient) WaitFor(t *testing.T, text string) {
101 require.Eventually(t, func() bool {
102 return strings.Contains(c.Output(), text)
103 }, 10*time.Second, time.Millisecond, "output so far: %q", c.Output())
106 // Close ends the input and waits for the client.
107 func (c *TerminalTestClient) Close() error {
108 err := c.input.Close()
116 // terminalStartTestClient connects a client to the daemon at socket.
117 func terminalStartTestClient(t *testing.T, socket string) *TerminalTestClient {
120 conn, err := net.Dial("unix", socket)
121 require.NoError(t, err)
123 reader, writer := io.Pipe()
124 client := &TerminalTestClient{
126 done: make(chan error, 1),
131 client.done <- terminalSession(t.Context(), conn, reader, client.out)