Skip to content

Commit

Permalink
tests(call): add tests for call methods and metrics data
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnatarHe committed Aug 17, 2023
1 parent 5236b47 commit c35983a
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 33 deletions.
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/PromptPal/PromptPal/routes"
"github.com/PromptPal/PromptPal/schema"
"github.com/PromptPal/PromptPal/service"
"github.com/graph-gophers/graphql-go"
"github.com/sirupsen/logrus"
)

Expand All @@ -30,9 +31,13 @@ func startHTTPServer() {
w3 := service.NewWeb3Service()
o := service.NewOpenAIService()
hi := service.NewHashIDService()
var graphqlSchema = graphql.MustParseSchema(
schema.String(),
&schema.QueryResolver{},
)

schema.Setup(hi)
h := routes.SetupGinRoutes(GitCommit, w3, o, hi)
schema.Setup(hi, o, w3)
h := routes.SetupGinRoutes(GitCommit, w3, o, hi, graphqlSchema)
server := &http.Server{
Addr: publicDomain,
Handler: h,
Expand Down
12 changes: 9 additions & 3 deletions routes/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
brotli "github.com/anargu/gin-brotli"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/graph-gophers/graphql-go"
)

type errorResponse struct {
ErrorCode int `json:"code"`
ErrorMessage string `json:"error"`
}

// var s
var web3Service service.Web3Service
var openAIService service.OpenAIService
var hashidService service.HashIDService
Expand All @@ -24,10 +26,12 @@ func SetupGinRoutes(
w3 service.Web3Service,
o service.OpenAIService,
hi service.HashIDService,
graphqlSchema *graphql.Schema,
) *gin.Engine {
web3Service = w3
openAIService = o
hashidService = hi
s = graphqlSchema

h := gin.Default()

Expand Down Expand Up @@ -86,10 +90,12 @@ func SetupGinRoutes(
adminRoutes.DELETE("/open-tokens", deleteOpenToken)
}

if true {
h.GET("/api/v2/graphql", graphqlPlaygroundHandler)
if graphqlSchema != nil {
if true {
h.GET("/api/v2/graphql", graphqlPlaygroundHandler)
}
h.POST("/api/v2/graphql", authMiddleware, graphqlExecuteHandler)
}
h.POST("/api/v2/graphql", authMiddleware, graphqlExecuteHandler)

h.LoadHTMLFiles("./public/index.html")
h.Static("/public", "./public")
Expand Down
11 changes: 6 additions & 5 deletions routes/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
"encoding/json"
"net/http"

"github.com/PromptPal/PromptPal/schema"
"github.com/PromptPal/PromptPal/service"
"github.com/gin-gonic/gin"
"github.com/graph-gophers/graphql-go"
)

var s = graphql.MustParseSchema(
schema.String(),
&schema.QueryResolver{},
)
var s *graphql.Schema

// var s = graphql.MustParseSchema(
// schema.String(),
// &schema.QueryResolver{},
// )

//go:embed graphql.html
var graphqliTemplate []byte
Expand Down
2 changes: 1 addition & 1 deletion routes/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *projectTestSuite) SetupTest() {
hs := service.NewHashIDService()

service.InitDB()
s.router = SetupGinRoutes("test", w3, oi, hs)
s.router = SetupGinRoutes("test", w3, oi, hs, nil)
}

func (s *projectTestSuite) TestCreateProject() {
Expand Down
2 changes: 1 addition & 1 deletion routes/prompt.api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *promptPublicAPITestSuite) SetupTest() {
s.oi = oi

service.InitDB()
s.router = SetupGinRoutes("test", w3, oi, hs)
s.router = SetupGinRoutes("test", w3, oi, hs, nil)
}

func (s *promptPublicAPITestSuite) TestCreateProjectAndPrompt() {
Expand Down
2 changes: 1 addition & 1 deletion routes/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s *userTestSuite) SetupTest() {
Return(true, nil)

service.InitDB()
s.router = SetupGinRoutes("test", w3, oi, hs)
s.router = SetupGinRoutes("test", w3, oi, hs, nil)
}

func (s *userTestSuite) GetAuthToken() (result authResponse, err error) {
Expand Down
70 changes: 70 additions & 0 deletions schema/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package schema

import (
"context"
"errors"
"net/http"
"strings"
"time"

"github.com/PromptPal/PromptPal/ent"
"github.com/PromptPal/PromptPal/ent/user"
"github.com/PromptPal/PromptPal/service"
)

type authAuthData struct {
Address string
Signature string
Message string
}

type authInput struct {
Auth authAuthData
}

type authResponse struct {
token string
u *ent.User
}

func (q QueryResolver) Auth(ctx context.Context, args authInput) (result authResponse, err error) {
payload := args.Auth
verified, err := web3Service.VerifySignature(payload.Address, payload.Message, payload.Signature)

if err != nil {
err = NewGraphQLHttpError(http.StatusBadRequest, err)
return
}
if !verified {
err = NewGraphQLHttpError(http.StatusBadRequest, errors.New("invalid signature"))
return
}

u, err := service.
EntClient.
User.
Query().
Where(user.Addr(strings.ToLower(payload.Address))).
Only(ctx)

if err != nil {
err = NewGraphQLHttpError(http.StatusNotFound, err)
return
}

token, err := service.SignJWT(u, time.Hour*24*30)
if err != nil {
err = NewGraphQLHttpError(http.StatusInternalServerError, err)
return
}
result.token = token
result.u = u

return
}
func (a authResponse) Token() string {
return a.token
}
func (a authResponse) User() userResponse {
return userResponse{u: a.u}
}
6 changes: 6 additions & 0 deletions schema/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/sirupsen/logrus"
)

var web3Service service.Web3Service
var openaiService service.OpenAIService
var hashidService service.HashIDService

type paginationInput struct {
Expand Down Expand Up @@ -45,6 +47,10 @@ func String() string {

func Setup(
hi service.HashIDService,
oi service.OpenAIService,
w3 service.Web3Service,
) {
hashidService = hi
openaiService = oi
web3Service = w3
}
16 changes: 8 additions & 8 deletions schema/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,31 @@ func (p promptCallListResponse) Edges(ctx context.Context) (res []promptCallResp
return
}

func (p promptCallResponse) ID(ctx context.Context) int32 {
func (p promptCallResponse) ID() int32 {
return int32(p.pc.ID)
}
func (p promptCallResponse) UserId(ctx context.Context) string {
func (p promptCallResponse) UserId() string {
return p.pc.UserId
}
func (p promptCallResponse) ResponseToken(ctx context.Context) int32 {
func (p promptCallResponse) ResponseToken() int32 {
return int32(p.pc.ResponseToken)
}
func (p promptCallResponse) TotalToken(ctx context.Context) int32 {
func (p promptCallResponse) TotalToken() int32 {
return int32(p.pc.TotalToken)
}
func (p promptCallResponse) Duration(ctx context.Context) int32 {
func (p promptCallResponse) Duration() int32 {
return int32(p.pc.Duration)
}
func (p promptCallResponse) Result(ctx context.Context) string {
func (p promptCallResponse) Result() string {
result := p.pc.Result
if result == 1 {
return "success"
}
return "fail"
}
func (p promptCallResponse) Message(ctx context.Context) *string {
func (p promptCallResponse) Message() *string {
return p.pc.Message
}
func (p promptCallResponse) CreatedAt(ctx context.Context) string {
func (p promptCallResponse) CreatedAt() string {
return p.pc.CreateTime.Format(time.RFC3339)
}
Loading

0 comments on commit c35983a

Please sign in to comment.