188 lines
5.7 KiB
Go
188 lines
5.7 KiB
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestTrimLoadAndValidate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
trimYAML string
|
|
wantLoadErr string
|
|
wantValidateErr string
|
|
assert func(t *testing.T, cfg *Config)
|
|
}{
|
|
{
|
|
name: "trim defaults when omitted",
|
|
trimYAML: "",
|
|
assert: func(t *testing.T, cfg *Config) {
|
|
t.Helper()
|
|
assertDefaultTrimConfig(t, cfg)
|
|
if cfg.Pipeline.Scriptorium == nil {
|
|
t.Fatal("scriptorium config should be defaulted when trim is enabled by default")
|
|
}
|
|
if cfg.Pipeline.Scriptorium.Binary != DefaultScriptoriumBinary {
|
|
t.Fatalf("scriptorium.binary = %q, want %q", cfg.Pipeline.Scriptorium.Binary, DefaultScriptoriumBinary)
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: "valid trim config",
|
|
trimYAML: `trim:
|
|
enabled: true
|
|
output_path: transcripts/final.trimmed.json
|
|
bounds:
|
|
prompt_id: dnd_session.bounds
|
|
profile_id: ""
|
|
transcript_input_name: transcript
|
|
output_path: artifacts/session_bounds.json
|
|
timeout: 10m
|
|
render_debug: false
|
|
render_output_path: artifacts/session_bounds.render.json
|
|
seriatim:
|
|
report: false
|
|
`,
|
|
assert: func(t *testing.T, cfg *Config) {
|
|
t.Helper()
|
|
if cfg.Pipeline.Trim == nil {
|
|
t.Fatal("trim config should be present")
|
|
}
|
|
if cfg.Pipeline.Trim.Enabled == nil || !*cfg.Pipeline.Trim.Enabled {
|
|
t.Fatalf("trim.enabled = %#v, want true", cfg.Pipeline.Trim.Enabled)
|
|
}
|
|
if cfg.Pipeline.Trim.Bounds.ProfileID != "" {
|
|
t.Fatalf("trim.bounds.profile_id = %q, want empty", cfg.Pipeline.Trim.Bounds.ProfileID)
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: "enabled omitted defaults enabled",
|
|
trimYAML: `trim:
|
|
`,
|
|
assert: func(t *testing.T, cfg *Config) {
|
|
t.Helper()
|
|
assertDefaultTrimConfig(t, cfg)
|
|
},
|
|
},
|
|
{
|
|
name: "explicit disabled remains disabled",
|
|
trimYAML: `trim:
|
|
enabled: false
|
|
`,
|
|
assert: func(t *testing.T, cfg *Config) {
|
|
t.Helper()
|
|
if cfg.Pipeline.Trim == nil || cfg.Pipeline.Trim.Enabled == nil || *cfg.Pipeline.Trim.Enabled {
|
|
t.Fatalf("trim.enabled = %#v, want false", cfg.Pipeline.Trim)
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: "invalid timeout fails",
|
|
trimYAML: `trim:
|
|
enabled: true
|
|
output_path: transcripts/final.trimmed.json
|
|
bounds:
|
|
prompt_id: dnd_session.bounds
|
|
transcript_input_name: transcript
|
|
output_path: artifacts/session_bounds.json
|
|
timeout: definitely-not-a-duration
|
|
`,
|
|
wantValidateErr: "pipeline.trim.bounds.timeout must be a valid duration",
|
|
},
|
|
{
|
|
name: "render debug true requires render output path",
|
|
trimYAML: `trim:
|
|
enabled: true
|
|
output_path: transcripts/final.trimmed.json
|
|
bounds:
|
|
prompt_id: dnd_session.bounds
|
|
transcript_input_name: transcript
|
|
output_path: artifacts/session_bounds.json
|
|
render_debug: true
|
|
`,
|
|
wantValidateErr: "pipeline.trim.bounds.render_output_path is required when pipeline.trim.bounds.render_debug is true",
|
|
},
|
|
{
|
|
name: "unknown trim field fails strict decoding",
|
|
trimYAML: `trim:
|
|
enabled: true
|
|
output_path: transcripts/final.trimmed.json
|
|
bounds:
|
|
prompt_id: dnd_session.bounds
|
|
transcript_input_name: transcript
|
|
output_path: artifacts/session_bounds.json
|
|
bogus: true
|
|
`,
|
|
wantLoadErr: "strict decode failed",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
pipelineYAML := testPipelineBaseYAML + "\n" + tt.trimYAML
|
|
pipelinePath, sessionPath := writeConfigFiles(t, pipelineYAML, testSessionBaseYAML)
|
|
|
|
cfg, err := Load(pipelinePath, sessionPath)
|
|
if tt.wantLoadErr != "" {
|
|
if err == nil {
|
|
t.Fatalf("expected load error containing %q, got nil", tt.wantLoadErr)
|
|
}
|
|
if !strings.Contains(err.Error(), tt.wantLoadErr) {
|
|
t.Fatalf("load error = %q, want to contain %q", err.Error(), tt.wantLoadErr)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
|
|
if tt.assert != nil {
|
|
tt.assert(t, cfg)
|
|
}
|
|
|
|
err = Validate(cfg)
|
|
if tt.wantValidateErr != "" {
|
|
if err == nil {
|
|
t.Fatalf("expected validation error containing %q, got nil", tt.wantValidateErr)
|
|
}
|
|
if !strings.Contains(err.Error(), tt.wantValidateErr) {
|
|
t.Fatalf("validation error = %q, want to contain %q", err.Error(), tt.wantValidateErr)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("Validate() error = %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func assertDefaultTrimConfig(t *testing.T, cfg *Config) {
|
|
t.Helper()
|
|
if cfg.Pipeline.Trim == nil {
|
|
t.Fatal("trim config should be present")
|
|
}
|
|
if cfg.Pipeline.Trim.Enabled == nil || !*cfg.Pipeline.Trim.Enabled {
|
|
t.Fatalf("trim.enabled = %#v, want true", cfg.Pipeline.Trim.Enabled)
|
|
}
|
|
if cfg.Pipeline.Trim.OutputPath != DefaultTrimOutputPath {
|
|
t.Fatalf("trim.output_path = %q, want %q", cfg.Pipeline.Trim.OutputPath, DefaultTrimOutputPath)
|
|
}
|
|
if cfg.Pipeline.Trim.Bounds.PromptID != DefaultTrimBoundsPromptID {
|
|
t.Fatalf("trim.bounds.prompt_id = %q, want %q", cfg.Pipeline.Trim.Bounds.PromptID, DefaultTrimBoundsPromptID)
|
|
}
|
|
if cfg.Pipeline.Trim.Bounds.TranscriptInputName != DefaultTrimBoundsTranscriptInputName {
|
|
t.Fatalf("trim.bounds.transcript_input_name = %q, want %q", cfg.Pipeline.Trim.Bounds.TranscriptInputName, DefaultTrimBoundsTranscriptInputName)
|
|
}
|
|
if cfg.Pipeline.Trim.Bounds.OutputPath != DefaultTrimBoundsOutputPath {
|
|
t.Fatalf("trim.bounds.output_path = %q, want %q", cfg.Pipeline.Trim.Bounds.OutputPath, DefaultTrimBoundsOutputPath)
|
|
}
|
|
if cfg.Pipeline.Trim.Bounds.Timeout != DefaultTrimBoundsTimeout {
|
|
t.Fatalf("trim.bounds.timeout = %q, want %q", cfg.Pipeline.Trim.Bounds.Timeout, DefaultTrimBoundsTimeout)
|
|
}
|
|
if cfg.Pipeline.Trim.Seriatim.Report == nil || *cfg.Pipeline.Trim.Seriatim.Report != DefaultTrimSeriatimReport {
|
|
t.Fatalf("trim.seriatim.report = %#v, want %t", cfg.Pipeline.Trim.Seriatim.Report, DefaultTrimSeriatimReport)
|
|
}
|
|
}
|