github.com/instill-ai/component@v0.16.0-beta/pkg/connector/integration_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package pkg
     5  
     6  import (
     7  	"testing"
     8  
     9  	qt "github.com/frankban/quicktest"
    10  	"github.com/gofrs/uuid"
    11  	"go.uber.org/zap"
    12  	"google.golang.org/protobuf/types/known/structpb"
    13  
    14  	"github.com/instill-ai/component/pkg/base"
    15  	"github.com/instill-ai/component/pkg/connector/openai/v0"
    16  	"github.com/instill-ai/x/errmsg"
    17  )
    18  
    19  var (
    20  	// TODO read keys from env variables and check for valid response.
    21  	openAIKey = "invalid-key"
    22  	openAIOrg = "no-such-org"
    23  
    24  	emptyOptions = ConnectorOptions{}
    25  )
    26  
    27  func TestOpenAITextGeneration(t *testing.T) {
    28  	c := qt.New(t)
    29  
    30  	config, err := structpb.NewStruct(map[string]any{
    31  		"api_key":      openAIKey,
    32  		"organization": openAIOrg,
    33  	})
    34  	c.Assert(err, qt.IsNil)
    35  
    36  	in, err := base.ConvertToStructpb(openai.TextCompletionInput{
    37  		Prompt: "how are you doing?",
    38  		Model:  "gpt-3.5-turbo",
    39  	})
    40  	c.Assert(err, qt.IsNil)
    41  
    42  	logger := zap.NewNop()
    43  	conn := Init(logger, emptyOptions)
    44  
    45  	def, err := conn.GetConnectorDefinitionByID("openai", nil)
    46  	c.Assert(err, qt.IsNil)
    47  
    48  	uid, err := uuid.FromString(def.GetUid())
    49  	c.Assert(err, qt.IsNil)
    50  
    51  	exec, err := conn.CreateExecution(uid, "TASK_TEXT_GENERATION", config, logger)
    52  	c.Assert(err, qt.IsNil)
    53  
    54  	op, err := exec.Execution.Execute([]*structpb.Struct{in})
    55  
    56  	// This assumes invalid API credentials that trigger a 401 API error. We
    57  	// should check for valid behaviour in integration tests, although
    58  	// validating the format of error messages can also be useful to detect
    59  	// breaking changes in the API.
    60  	c.Check(err, qt.ErrorMatches, "unsuccessful HTTP response")
    61  	c.Check(errmsg.Message(err), qt.Matches, ".*Incorrect API key provided.*")
    62  
    63  	c.Logf("op: %v", op)
    64  	c.Logf("err: %v", err)
    65  	c.Log("end-user err:", errmsg.MessageOrErr(err))
    66  }