75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
package integration_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
type extractionResponse struct {
|
|
SpellCasts []spellCastResponse `json:"spell_casts"`
|
|
}
|
|
|
|
type spellCastResponse struct {
|
|
Caster string `json:"caster"`
|
|
Spell string `json:"spell"`
|
|
SourceRefs []spellSourceRefResponse `json:"source_refs"`
|
|
}
|
|
|
|
type spellSourceRefResponse struct {
|
|
StartUnitID int `json:"start_unit_id"`
|
|
EndUnitID int `json:"end_unit_id"`
|
|
}
|
|
|
|
type fakeSpellsLLMClient struct {
|
|
response extractionResponse
|
|
responses []extractionResponse
|
|
rawResponses [][]byte
|
|
requests []promptkit.GenerateRequest
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func (client *fakeSpellsLLMClient) Generate(ctx context.Context, req promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
client.mu.Lock()
|
|
defer client.mu.Unlock()
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
client.requests = append(client.requests, req)
|
|
var content []byte
|
|
if client.rawResponses != nil {
|
|
index := len(client.requests) - 1
|
|
if index >= len(client.rawResponses) {
|
|
return nil, fmt.Errorf("missing fake response %d", index)
|
|
}
|
|
content = append([]byte(nil), client.rawResponses[index]...)
|
|
} else {
|
|
response := client.response
|
|
if client.responses != nil {
|
|
index := len(client.requests) - 1
|
|
if index >= len(client.responses) {
|
|
return nil, fmt.Errorf("missing fake response %d", index)
|
|
}
|
|
response = client.responses[index]
|
|
}
|
|
var err error
|
|
content, err = json.Marshal(response)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return &promptkit.GenerateResponse{Content: string(content)}, nil
|
|
}
|
|
|
|
func responseSourceRefs(startUnitID int, endUnitID int) []spellSourceRefResponse {
|
|
return []spellSourceRefResponse{
|
|
{
|
|
StartUnitID: startUnitID,
|
|
EndUnitID: endUnitID,
|
|
},
|
|
}
|
|
}
|