Accept and protect processing glossary files
This commit is contained in:
90
internal/cli/runtime_glossary_test.go
Normal file
90
internal/cli/runtime_glossary_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
commandkit "go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
func TestProcessProtectsRuntimeGlossarySources(t *testing.T) {
|
||||
for _, mode := range []string{"transcript", "report", "separate"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
var calls atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls.Add(1)
|
||||
fmt.Fprint(w, `{"choices":[{"message":{"content":"{\"corrections\":[]}"}}]}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
imported := "imported.yml"
|
||||
if mode == "report" {
|
||||
imported = "errors.yml"
|
||||
}
|
||||
files := map[string]string{
|
||||
"config.yml": "version: 3\nllm: {proposal_profile: fixture}\npipeline: {stages: [{module: grammar}]}\n",
|
||||
"input.json": `[{"speaker":"Alice","start":0,"end":1,"text":"Hello."}]`,
|
||||
"runtime.yml": fmt.Sprintf("config:\n imports: [{path: %s, required: true}]\nparty: {entries: []}\n", imported),
|
||||
imported: "reference: {entries: []}\n",
|
||||
"prompts.yml": fmt.Sprintf("profiles:\n - id: fixture\n endpoint: %s/v1\n model: test\n api_key_required: false\n", server.URL),
|
||||
}
|
||||
for name, data := range files {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
output := filepath.Join(dir, "output.json")
|
||||
if mode == "transcript" {
|
||||
output = filepath.Join(dir, "runtime.yml")
|
||||
}
|
||||
app, err := buildApplication()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out, stderr bytes.Buffer
|
||||
result := app.Execute(context.Background(), []string{"process", "--config", filepath.Join(dir, "config.yml"), "--glossary-file", filepath.Join(dir, "runtime.yml"), "--output", output, filepath.Join(dir, "input.json")}, commandkit.Streams{Out: &out, Err: &stderr})
|
||||
if mode == "separate" {
|
||||
if result.Status != 0 || calls.Load() != 1 {
|
||||
t.Fatalf("status=%d calls=%d error=%s", result.Status, calls.Load(), stderr.String())
|
||||
}
|
||||
} else if result.Status == 0 || calls.Load() != 0 || !strings.Contains(stderr.String(), "conflicts with input") {
|
||||
t.Fatalf("status=%d calls=%d error=%s", result.Status, calls.Load(), stderr.String())
|
||||
}
|
||||
for _, name := range []string{"runtime.yml", imported} {
|
||||
raw, err := os.ReadFile(filepath.Join(dir, name))
|
||||
if err != nil || string(raw) != files[name] {
|
||||
t.Fatalf("source %s changed: %s %v", name, raw, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessRuntimeGlossaryFailures(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
files := map[string]string{"config.yml": "version: 3\nglossaries: [party]\npipeline: {stages: [{module: glossary}]}\n", "input.json": `[{"speaker":"Alice","start":0,"end":1,"text":"Hello."}]`, "empty.yml": "party: {entries: []}\n", "bad.yml": "party: [\n"}
|
||||
for name, data := range files {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"missing.yml", "bad.yml", "empty.yml"} {
|
||||
var stdout, stderr bytes.Buffer
|
||||
output := filepath.Join(dir, "output.json")
|
||||
status := Run(context.Background(), []string{"process", "--config", filepath.Join(dir, "config.yml"), "--glossary-file", filepath.Join(dir, name), "--output", output, filepath.Join(dir, "input.json")}, &stdout, &stderr)
|
||||
if status != 2 {
|
||||
t.Fatalf("%s: status=%d stderr=%s", name, status, stderr.String())
|
||||
}
|
||||
if _, err := os.Stat(output); !os.IsNotExist(err) {
|
||||
t.Fatalf("%s: output prepared: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user