-func TestOneProcessHoldsTheTokens(t *testing.T) {
- dir := t.TempDir()
- lock, err := lockTokens(dir)
- require.NoError(t, err)
-
- _, err = lockTokens(dir)
- require.ErrorIs(t, err, errTokensInUse)
- require.NoError(t, lock.Close())
-
- lock, err = lockTokens(dir)
- require.NoError(t, err)
- require.NoError(t, lock.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: "user message not shown",
- event: Event{Kind: "user", Text: "typed"},
- text: "",
- },
- {
- 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, eventText(tc.event))
- })
- }
-}
-
-// testClient is a terminal client of a test daemon, with a pipe as its input.
-type testClient struct {
- input *io.PipeWriter
- done chan error
- out *syncBuffer
-}
-
-// Type writes one line to the input of the client.
-func (c *testClient) Type(line string) {
- _, err := io.WriteString(c.input, line+"\n")
- if err != nil {
- panic(err)
- }
-}
-
-// Output returns what the client showed.
-func (c *testClient) Output() string {
- return c.out.String()
-}
-
-// WaitFor waits until the client showed text.
-func (c *testClient) 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 *testClient) Close() error {
- err := c.input.Close()
- if err != nil {
- return err
- }
-
- return <-c.done
-}
-
-// syncBuffer is a buffer that the test and the client use at once.
-type syncBuffer struct {
- mutex sync.Mutex
- buffer bytes.Buffer
-}
-
-func (b *syncBuffer) Write(p []byte) (int, error) {
- b.mutex.Lock()
- defer b.mutex.Unlock()
-
- return b.buffer.Write(p)
-}
-
-func (b *syncBuffer) String() string {
- b.mutex.Lock()
- defer b.mutex.Unlock()
-
- return b.buffer.String()
-}
-
-// startTestDaemon serves a new agent on a socket in a temporary directory and