115 lines
3.5 KiB
Go
115 lines
3.5 KiB
Go
package rakestrawhome
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"testing"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func TestCatalogLayoutAndContents(t *testing.T) {
|
|
assets := FS()
|
|
if assets == nil {
|
|
t.Fatal("FS returned nil")
|
|
}
|
|
backendPath := Root + "/backend.yml"
|
|
profilePath := Root + "/profiles/google/rakestrawhome-gemma-4-31b.yml"
|
|
for _, assetPath := range []string{backendPath, profilePath} {
|
|
entry, err := fs.Stat(assets, assetPath)
|
|
if err != nil || !entry.Mode().IsRegular() {
|
|
t.Fatalf("asset %s is not a regular file: %v", assetPath, err)
|
|
}
|
|
}
|
|
count := 0
|
|
err := fs.WalkDir(assets, Root, func(assetPath string, entry fs.DirEntry, walkErr error) error {
|
|
if walkErr != nil {
|
|
return walkErr
|
|
}
|
|
if entry.IsDir() {
|
|
return nil
|
|
}
|
|
if assetPath != backendPath && assetPath != profilePath {
|
|
return fmt.Errorf("unexpected catalog asset %q", assetPath)
|
|
}
|
|
count++
|
|
return nil
|
|
})
|
|
if err != nil || count != 2 {
|
|
t.Fatalf("unexpected catalog layout: %d assets, %v", count, err)
|
|
}
|
|
|
|
backendData, err := fs.ReadFile(assets, backendPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
backend := singleMapping(t, "backend", singleMapping(t, "backend", decodeMapping(t, "backend", backendData), "backends"), "rakestrawhome")
|
|
for field, want := range map[string]string{
|
|
"endpoint": "https://inference.ai.rakestrawhome.com/v1",
|
|
"api_key_env": "RAKESTRAWHOME_INFERENCE_API_KEY",
|
|
"concurrency_limit": "4", "queue_capacity": "1024",
|
|
} {
|
|
value := backend[field]
|
|
if value == nil || value.Value != want {
|
|
t.Fatalf("backend %s: got %v, want %q", field, value, want)
|
|
}
|
|
}
|
|
if len(backend) != 4 {
|
|
t.Fatalf("unexpected backend fields: %v", backend)
|
|
}
|
|
|
|
profileData, err := fs.ReadFile(assets, profilePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
profile := singleMapping(t, "profile", singleMapping(t, "profile", decodeMapping(t, "profile", profileData), "profiles"), "rakestrawhome-gemma-4-31b")
|
|
if len(profile) != 2 || profile["backend"] == nil || profile["backend"].Value != "rakestrawhome" ||
|
|
profile["model"] == nil || profile["model"].Value != "google/gemma-4-31b-it" {
|
|
t.Fatalf("profile values changed: %v", profile)
|
|
}
|
|
}
|
|
|
|
func decodeMapping(t *testing.T, name string, data []byte) map[string]*yaml.Node {
|
|
t.Helper()
|
|
decoder := yaml.NewDecoder(bytes.NewReader(data))
|
|
var document yaml.Node
|
|
if err := decoder.Decode(&document); err != nil {
|
|
t.Fatalf("decode %s: %v", name, err)
|
|
}
|
|
var trailing yaml.Node
|
|
if err := decoder.Decode(&trailing); !errors.Is(err, io.EOF) {
|
|
t.Fatalf("%s must contain exactly one YAML document: %v", name, err)
|
|
}
|
|
if document.Kind != yaml.DocumentNode || len(document.Content) != 1 {
|
|
t.Fatalf("%s must contain one mapping document", name)
|
|
}
|
|
return mappingFields(t, name, document.Content[0])
|
|
}
|
|
|
|
func singleMapping(t *testing.T, name string, fields map[string]*yaml.Node, key string) map[string]*yaml.Node {
|
|
t.Helper()
|
|
if len(fields) != 1 || fields[key] == nil {
|
|
t.Fatalf("%s must contain only %s", name, key)
|
|
}
|
|
return mappingFields(t, name, fields[key])
|
|
}
|
|
|
|
func mappingFields(t *testing.T, name string, node *yaml.Node) map[string]*yaml.Node {
|
|
t.Helper()
|
|
if node.Kind != yaml.MappingNode {
|
|
t.Fatalf("%s must contain a mapping", name)
|
|
}
|
|
fields := make(map[string]*yaml.Node, len(node.Content)/2)
|
|
for index := 0; index+1 < len(node.Content); index += 2 {
|
|
key := node.Content[index].Value
|
|
if _, exists := fields[key]; exists {
|
|
t.Fatalf("%s repeats key %q", name, key)
|
|
}
|
|
fields[key] = node.Content[index+1]
|
|
}
|
|
return fields
|
|
}
|