Skip to content

Commit 828bffb

Browse files
committed
chore(tests): Mock LLM in tests for PRs
This saves time when testing on CPU which is the only sensible thing to do on GitHub CI for PRs. For releases or once the commit is merged we could use an external runner with GPU or just wait. Signed-off-by: Richard Palethorpe <[email protected]>
1 parent 8b504a5 commit 828bffb

File tree

12 files changed

+431
-64
lines changed

12 files changed

+431
-64
lines changed

.github/workflows/tests.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ jobs:
4848
4949
- name: Run tests
5050
run: |
51-
make tests
51+
if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
52+
make tests-mock
53+
else
54+
make tests
55+
fi
5256
#sudo mv coverage/coverage.txt coverage.txt
5357
#sudo chmod 777 coverage.txt
5458

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Run Fragile Go Tests
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- '**'
7+
8+
concurrency:
9+
group: ci-non-blocking-tests-${{ github.head_ref || github.ref }}-${{ github.repository }}
10+
cancel-in-progress: true
11+
12+
jobs:
13+
llm-tests:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
- run: |
19+
# Add Docker's official GPG key:
20+
sudo apt-get update
21+
sudo apt-get install -y ca-certificates curl
22+
sudo install -m 0755 -d /etc/apt/keyrings
23+
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
24+
sudo chmod a+r /etc/apt/keyrings/docker.asc
25+
26+
# Add the repository to Apt sources:
27+
echo \
28+
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
29+
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
30+
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
31+
sudo apt-get update
32+
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin make
33+
docker version
34+
35+
docker run --rm hello-world
36+
- uses: actions/setup-go@v5
37+
with:
38+
go-version: '>=1.17.0'
39+
- name: Free up disk space
40+
run: |
41+
sudo rm -rf /usr/share/dotnet
42+
sudo rm -rf /usr/local/lib/android
43+
sudo rm -rf /opt/ghc
44+
sudo apt-get clean
45+
docker system prune -af || true
46+
df -h
47+
- name: Run non-blocking tests
48+
run: |
49+
make tests
50+
continue-on-error: true

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ cleanup-tests:
1313
tests: prepare-tests
1414
LOCALAGI_MCPBOX_URL="http://localhost:9090" LOCALAGI_MODEL="gemma-3-12b-it-qat" LOCALAI_API_URL="http://localhost:8081" LOCALAGI_API_URL="http://localhost:8080" $(GOCMD) run github.com/onsi/ginkgo/v2/ginkgo --fail-fast -v -r ./...
1515

16+
tests-mock: prepare-tests
17+
LOCALAGI_MCPBOX_URL="http://localhost:9090" LOCALAI_API_URL="http://localhost:8081" LOCALAGI_API_URL="http://localhost:8080" $(GOCMD) run github.com/onsi/ginkgo/v2/ginkgo --fail-fast -v -r ./...
18+
1619
run-nokb:
1720
$(MAKE) run KBDISABLEINDEX=true
1821

core/agent/agent.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Agent struct {
2929
sync.Mutex
3030
options *options
3131
Character Character
32-
client *openai.Client
32+
client llm.LLMClient
3333
jobQueue chan *types.Job
3434
context *types.ActionContext
3535

@@ -61,7 +61,12 @@ func New(opts ...Option) (*Agent, error) {
6161
return nil, fmt.Errorf("failed to set options: %v", err)
6262
}
6363

64-
client := llm.NewClient(options.LLMAPI.APIKey, options.LLMAPI.APIURL, options.timeout)
64+
var client llm.LLMClient
65+
if options.llmClient != nil {
66+
client = options.llmClient
67+
} else {
68+
client = llm.NewClient(options.LLMAPI.APIKey, options.LLMAPI.APIURL, options.timeout)
69+
}
6570

6671
c := context.Background()
6772
if options.context != nil {
@@ -118,6 +123,11 @@ func New(opts ...Option) (*Agent, error) {
118123
return a, nil
119124
}
120125

126+
// LLMClient returns the agent's LLM client (for testing)
127+
func (a *Agent) LLMClient() llm.LLMClient {
128+
return a.client
129+
}
130+
121131
func (a *Agent) startNewConversationsConsumer() {
122132
go func() {
123133
for {

core/agent/agent_suite_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package agent_test
22

33
import (
4+
"net/url"
45
"os"
56
"testing"
67

@@ -13,15 +14,18 @@ func TestAgent(t *testing.T) {
1314
RunSpecs(t, "Agent test suite")
1415
}
1516

16-
var testModel = os.Getenv("LOCALAGI_MODEL")
17-
var apiURL = os.Getenv("LOCALAI_API_URL")
18-
var apiKeyURL = os.Getenv("LOCALAI_API_KEY")
17+
var (
18+
testModel = os.Getenv("LOCALAGI_MODEL")
19+
apiURL = os.Getenv("LOCALAI_API_URL")
20+
apiKeyURL = os.Getenv("LOCALAI_API_KEY")
21+
useRealLocalAI bool
22+
)
23+
24+
func isValidURL(u string) bool {
25+
parsed, err := url.ParseRequestURI(u)
26+
return err == nil && parsed.Scheme != "" && parsed.Host != ""
27+
}
1928

2029
func init() {
21-
if testModel == "" {
22-
testModel = "hermes-2-pro-mistral"
23-
}
24-
if apiURL == "" {
25-
apiURL = "http://192.168.68.113:8080"
26-
}
30+
useRealLocalAI = isValidURL(apiURL) && apiURL != "" && testModel != ""
2731
}

0 commit comments

Comments
 (0)