-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests of the CLI itself have uncovered some interesting oddities with testing readline applications which I'll have to think about how to work with
- Loading branch information
Showing
9 changed files
with
162 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package aicli | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCmd(t *testing.T) { | ||
cmd := NewCmd(&Echo{}) | ||
stdinr, stdinw := io.Pipe() | ||
stdout := &bytes.Buffer{} | ||
stderr := &bytes.Buffer{} | ||
|
||
cmd.stdin = stdinr | ||
cmd.stdout = stdout | ||
cmd.stderr = stderr | ||
cmd.historyPath = t.TempDir() + "/.aicli_history" | ||
cmd.OpenAI_API_Key = "blah" | ||
|
||
done := make(chan struct{}) | ||
var runErr error | ||
go func() { | ||
runErr = cmd.Run() | ||
close(done) | ||
}() | ||
time.Sleep(time.Millisecond * 10) | ||
require.NoError(t, runErr) | ||
expect(t, stdout, []byte{0x20, 0x08, 0x1b, 0x5b, 0x36, 0x6e, 0x3e, 0x20}) | ||
stdinw.Close() | ||
|
||
select { | ||
case <-done: | ||
case <-time.After(time.Second * 2): | ||
t.Fatal("command should have ended after stdin was closed") | ||
} | ||
|
||
require.NoError(t, runErr) | ||
} | ||
|
||
func expect(t *testing.T, r io.Reader, exp []byte) { | ||
t.Helper() | ||
buffer := make([]byte, len(exp)) | ||
|
||
n, err := r.Read(buffer) | ||
if err != nil && err.Error() != "EOF" { | ||
require.NoError(t, err) | ||
} | ||
|
||
require.Equal(t, exp, buffer[:n]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package aicli | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var _ AI = &Echo{} // assert that Client satisfies AI interface | ||
|
||
// Echo is an AI implementation for testing which repeats the user's last | ||
// message back with some extra information. | ||
type Echo struct{} | ||
|
||
func (c *Echo) StreamResp(msgs []Message, output io.Writer) (Message, error) { | ||
var resp string | ||
if len(msgs) == 0 { | ||
resp = "0 msgs" | ||
} else { | ||
resp = fmt.Sprintf("msgs: %d, role: %s, content: %s", len(msgs), RoleAssistant, msgs[len(msgs)-1].Content()) | ||
} | ||
|
||
_, err := output.Write([]byte(resp)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "writing msg") | ||
} | ||
return SimpleMsg{RoleField: RoleAssistant, ContentField: resp}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package aicli_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/jaffee/aicli/pkg/aicli" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEcho(t *testing.T) { | ||
c := &aicli.Echo{} | ||
|
||
cases := []struct { | ||
msgs []aicli.Message | ||
exp string | ||
}{ | ||
{msgs: nil, exp: "0 msgs"}, | ||
{ | ||
msgs: []aicli.Message{ | ||
aicli.SimpleMsg{ | ||
ContentField: "hello", | ||
RoleField: aicli.RoleUser, | ||
}, | ||
}, | ||
exp: "msgs: 1, role: assistant, content: hello", | ||
}, | ||
} | ||
|
||
for i, tst := range cases { | ||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
msg, err := c.StreamResp(tst.msgs, buf) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, tst.exp, string(buf.Bytes())) | ||
assert.Equal(t, tst.exp, msg.Content()) | ||
assert.Equal(t, aicli.RoleAssistant, msg.Role()) | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters