Skip to content

Commit

Permalink
fix ci: update the tests, and prepare scripts and go work file
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez committed Dec 17, 2024
1 parent f6d40cf commit ed889bf
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 23 deletions.
2 changes: 2 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
go 1.23

use (
./servlets/crypto-hash
./servlets/historical-flight-api
./test/testsuite
)
4 changes: 4 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
11 changes: 8 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@

prepare:
#!/usr/bin/env bash
if [ -f "./prepare.sh" ]; then
bash ./prepare.sh
fi
build:
#!/usr/bin/env bash
set -eou pipefail
for dir in servlets/*/; do
cd "$dir"
bash ./prepare.sh
just prepare
xtp plugin build
cd ../..
done
Expand All @@ -14,8 +20,7 @@ push:
set -eou pipefail
for dir in servlets/*/; do
cd "$dir"
bash ./prepare.sh
xtp plugin push
just prepare
cd ../..
done
Expand Down
8 changes: 4 additions & 4 deletions servlets/crypto-hash/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ missing_deps=0
# Check for Go
if ! (command_exists go); then
missing_deps=1
echo "❌ Go (supported version between 1.18 - 1.22) is not installed."
echo "❌ Go (supported version between 1.18 - 1.23) is not installed."
echo ""
echo "To install Go, visit the official download page:"
echo "👉 https://go.dev/dl/"
Expand All @@ -28,17 +28,17 @@ if ! (command_exists go); then
echo ""
fi

# Check for the right version of Go, needed by TinyGo (supports go 1.18 - 1.22)
# Check for the right version of Go, needed by TinyGo (supports go 1.18 - 1.23)
if (command_exists go); then
compat=0
for v in `seq 18 22`; do
for v in `seq 18 23`; do
if (go version | grep -q "go1.$v"); then
compat=1
fi
done

if [ $compat -eq 0 ]; then
echo "❌ Supported Go version is not installed. Must be Go 1.18 - 1.22."
echo "❌ Supported Go version is not installed. Must be Go 1.18 - 1.23."
echo ""
fi
fi
Expand Down
8 changes: 4 additions & 4 deletions servlets/historical-flight-api/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ missing_deps=0
# Check for Go
if ! (command_exists go); then
missing_deps=1
echo "❌ Go (supported version between 1.18 - 1.22) is not installed."
echo "❌ Go (supported version between 1.18 - 1.23) is not installed."
echo ""
echo "To install Go, visit the official download page:"
echo "👉 https://go.dev/dl/"
Expand All @@ -28,17 +28,17 @@ if ! (command_exists go); then
echo ""
fi

# Check for the right version of Go, needed by TinyGo (supports go 1.18 - 1.22)
# Check for the right version of Go, needed by TinyGo (supports go 1.18 - 1.23)
if (command_exists go); then
compat=0
for v in `seq 18 22`; do
for v in `seq 18 23`; do
if (go version | grep -q "go1.$v"); then
compat=1
fi
done

if [ $compat -eq 0 ]; then
echo "❌ Supported Go version is not installed. Must be Go 1.18 - 1.22."
echo "❌ Supported Go version is not installed. Must be Go 1.18 - 1.23."
echo ""
fi
fi
Expand Down
72 changes: 62 additions & 10 deletions test/testsuite/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/base64"
"fmt"
"regexp"

Expand All @@ -11,19 +12,21 @@ import (
//go:export test
func test() int32 {
output := xtptest.CallBytes("describe", []byte{})
description, err := parseToolDescription(output)
list, err := parseToolListResult(output)
if err != nil {
xtptest.Assert("Failed to parse tool description", false, err.Error())
xtptest.Assert("Failed to parse tool list", false, err.Error())
return 1
}

if len(description.Name) == 0 {
xtptest.Assert("Name should be non-empty", false, "Name is empty")
} else {
xtptest.Assert("Successfully parsed tool description", true, description.Name)
}
for _, tool := range list.Tools {
if len(tool.Name) == 0 {
xtptest.Assert("Name should be non-empty", false, "Name is empty")
} else {
xtptest.Assert("Successfully parsed tool description", true, tool.Name)
}

testToolCall(description.Name)
testToolCall(tool.Name)
}

return 0
}
Expand All @@ -40,6 +43,8 @@ func testToolCall(name string) {
testEvalJS()
case "fetch":
testFetch()
case "fetch-image":
testFetchImage()
default:
xtptest.Assert(fmt.Sprintf("Unable to test '%s'", name), false, "Unknown tool")
}
Expand Down Expand Up @@ -83,6 +88,54 @@ func testFetch() {
})
}

func testFetchImage() {
xtptest.Group("test fetch-image tool", func() {
pdk.Log(pdk.LogDebug, "Testing fetch image tool")

arguments := map[string]interface{}{"url": "https://httpbin.org/image/jpeg", "mime-type": "image/jpeg"}
input := CallToolRequest{
Method: nil,
Params: Params{
Arguments: &arguments,
Name: "fetch-image",
},
}

inputBytes, err := input.Marshal()
if err != nil {
xtptest.Assert("Failed to marshal input", false, err.Error())
}

output := xtptest.CallBytes("call", inputBytes)
result, err := parseCallToolResult(output)
if err != nil {
xtptest.Assert("Failed to parse tool call result", false, err.Error())
}

pdk.Log(pdk.LogDebug, fmt.Sprintf("Tool call result: %v", string(output)))

hasErrored := result.IsError != nil && *result.IsError

xtptest.AssertEq("Tool call should not have errored", hasErrored, false)
xtptest.AssertEq("Tool call should have one content item", len(result.Content), 1)
xtptest.AssertEq("Content type should be text", result.Content[0].Type, ContentTypeImage)

imageData, err := base64.StdEncoding.DecodeString(*result.Content[0].Data)
if err != nil {
xtptest.Assert("Failed to decode image data", false, err.Error())
return
}

if len(imageData) == 0 {
xtptest.Assert("Image data should be greater than 0", false, "Image data is empty")
return
}

// test the image for the magic number for JPEG
xtptest.AssertEq("Image data should start with JPEG magic number", string(imageData[:2]), "\xFF\xD8")
})
}

func testEvalJS() {
xtptest.Group("test eval-js tool", func() {
pdk.Log(pdk.LogDebug, "Testing eval-js tool")
Expand Down Expand Up @@ -150,8 +203,7 @@ func testGreet() {
xtptest.AssertEq("Tool call should have one content item", len(result.Content), 1)
xtptest.AssertEq("Content type should be text", result.Content[0].Type, ContentTypeText)

regex := regexp.MustCompile(`^(.)+ says: Hello, Steve!$`)
xtptest.Assert("Content text should match `X says: Hello, Steve!`", regex.MatchString(*result.Content[0].Text), *result.Content[0].Text)
xtptest.AssertEq("Content text should be `Hello Steve!!!`", "Hello Steve!!!", *result.Content[0].Text)
})
}

Expand Down
8 changes: 6 additions & 2 deletions test/testsuite/pdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package main

import "encoding/json"

type ListToolsResult struct {
Tools []ToolDescription `json:"tools"`
}

type ToolDescription struct {
Description string `json:"description"`
InputSchema interface{} `json:"inputSchema"`
Name string `json:"name"`
}

func parseToolDescription(data []byte) (ToolDescription, error) {
var res ToolDescription
func parseToolListResult(data []byte) (ListToolsResult, error) {
var res ListToolsResult
err := json.Unmarshal(data, &res)
return res, err
}
Expand Down

0 comments on commit ed889bf

Please sign in to comment.