Skip to content

Commit

Permalink
improve test and attempt github actions w/ coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jaffee committed Nov 13, 2023
1 parent 3f19073 commit 62e6ae4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Go
on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: debug
run: |
echo matrix.os: ${{ matrix.os }}"
echo "Event name: ${{ github.event_name }}"
echo "Ref_name: ${{ github.ref_name }}"
echo "def branch: ${{ github.event.repository.default_branch }}"
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Test with the Go CLI
run: go test -v ./...
- name: Update coverage report
uses: ncruces/go-coverage-report@v0
with:
report: true
chart: true
amend: true
reuse-go: true
if: |
matrix.os == 'ubuntu-latest' &&
github.event_name == 'push' &&
github.ref_name == github.event.repository.default_branch
continue-on-error: true

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Go Coverage](https://github.com/jaffee/aicli/wiki/coverage.svg)](https://raw.githack.com/wiki/jaffee/aicli/coverage.html)

# aicli

aicli is a command line interface for AI chatbots. Currently only OpenAI is supported. Think of it like ChatGPT, but in your terminal instead of in the browser. You need an OpenAI API key to use it.
Expand Down
9 changes: 5 additions & 4 deletions pkg/aicli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func NewCmd(client AI) *Cmd {
stdin: os.Stdin,
stdout: os.Stdout,
stderr: os.Stderr,

client: client,
}
}

Expand All @@ -56,10 +58,9 @@ func (cmd *Cmd) Run() error {
}

rl, err := readline.NewEx(&readline.Config{
Prompt: "> ",
HistoryFile: cmd.getHistoryFilePath(),
HistoryLimit: 1000000,
ForceUseInteractive: true, // seems to be needed for testing
Prompt: "> ",
HistoryFile: cmd.getHistoryFilePath(),
HistoryLimit: 1000000,

Stdin: cmd.stdin,
Stdout: cmd.stdout,
Expand Down
38 changes: 31 additions & 7 deletions pkg/aicli/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,21 @@ func TestCmd(t *testing.T) {
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})
// expect(t, stdout, []byte{0x20, 0x08, 0x1b, 0x5b, 0x36, 0x6e, 0x3e, 0x20})
stdinw.Write([]byte("blah\n"))
require.NoError(t, runErr)
expect(t, stdout, []byte("msgs: 1, role: assistant, content: blah\n"))
stdinw.Write([]byte("bleh\n"))
require.NoError(t, runErr)
expect(t, stdout, []byte("msgs: 3, role: assistant, content: bleh\n"))
stdinw.Write([]byte("\\messages\n"))
expect(t, stdout, []byte(" user: blah\nassistant: msgs: 1, role: assistant, content: blah\n user: bleh\nassistant: msgs: 3, role: assistant, content: bleh\n"))
stdinw.Write([]byte("\\reset\n"))
require.NoError(t, runErr)
stdinw.Write([]byte("\\config\n"))
expect(t, stderr, []byte("OpenAI_API_Key: length=4\nOpenAIModel: gpt-3.5-turbo\nTemperature: 0.700000\nVerbose: false\n"))

stdinw.Close()

select {
Expand All @@ -43,11 +55,23 @@ func TestCmd(t *testing.T) {

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)
buffer := make([]byte, len(exp)*20)
i := 0
var n int
var err error
for {
i++
n, err = r.Read(buffer)
if err != nil && err.Error() != "EOF" {
require.NoError(t, err)
}
if n > 0 {
break
}
if i > 100 {
t.Fatal("spent too long waiting for output")
}
time.Sleep(time.Millisecond)
}

require.Equal(t, exp, buffer[:n])
Expand Down

0 comments on commit 62e6ae4

Please sign in to comment.