54 lines
2.0 KiB
Go
54 lines
2.0 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"git.a-wan.net/alan/pipelines/internal/cli"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestProviderCheckExecutesGenerationAndReportsFailures(t *testing.T) {
|
|
// Isolate harness settings so an unrelated developer profile cannot route the
|
|
// native check through Docker.
|
|
t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json"))
|
|
for _, fail := range []bool{false, true} {
|
|
bin := filepath.Join(t.TempDir(), "opencode")
|
|
script := `#!/bin/sh
|
|
case "$*" in
|
|
*--help*) echo 'api';;
|
|
*'/api/session/ses_check/prompt'*) ` + map[bool]string{true: `echo '{"_tag":"UnauthorizedError","message":"expired provider credentials","token":"private-token"}'; exit 7`, false: `echo '{}'`}[fail] + `;;
|
|
*'/api/session/ses_check/message?'*) echo '{"data":[{"finish":"stop","content":[{"type":"text","text":"PIPELINES_PROVIDER_OK"}]}]}';;
|
|
*'/api/session/ses_check/interrupt'*) echo '{}';;
|
|
*'/api/session/active'*) echo '{"data":{}}';;
|
|
*'/api/session'*) echo '{"data":{"id":"ses_check"}}';;
|
|
*) exit 9;;
|
|
esac
|
|
`
|
|
if err := os.WriteFile(bin, []byte(script), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PIPELINES_OPENCODE", bin)
|
|
var out, stderr bytes.Buffer
|
|
app := cli.App{Out: &out, Err: &stderr, In: strings.NewReader("")}
|
|
err := app.Run(context.Background(), []string{"provider-check", "--model", "fixture/test", "--json", "--yes"})
|
|
if (err != nil) != fail || !strings.Contains(out.String(), "ses_check") {
|
|
t.Fatal(err, out.String())
|
|
}
|
|
if fail && (!strings.Contains(out.String(), "expired provider credentials") || strings.Contains(out.String(), "private-token")) {
|
|
t.Fatal(out.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProviderCheckDeclineDoesNotExecute(t *testing.T) {
|
|
t.Setenv("PIPELINES_OPENCODE", "/does-not-exist")
|
|
var out bytes.Buffer
|
|
err := (cli.App{Out: &out, Err: &out, In: strings.NewReader("n\n")}).Run(context.Background(), []string{"provider-check", "--model", "fixture/test"})
|
|
if err == nil || strings.Contains(err.Error(), "OpenCode") {
|
|
t.Fatal(err)
|
|
}
|
|
}
|