200 lines
6.2 KiB
Go
200 lines
6.2 KiB
Go
package contracts
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidBoundsOutputProducesKeepSelector(t *testing.T) {
|
|
dir := t.TempDir()
|
|
boundsPath := filepath.Join(dir, "bounds.json")
|
|
transcriptPath := filepath.Join(dir, "polished.json")
|
|
|
|
writeBoundsTestFile(t, boundsPath, `{
|
|
"confidence":"high",
|
|
"trim_action":"trim",
|
|
"start_segment_id":10,
|
|
"end_segment_id":868,
|
|
"warnings":[]
|
|
}`)
|
|
writeBoundsTestFile(t, transcriptPath, `{
|
|
"segments":[
|
|
{"id":10,"text":"start"},
|
|
{"id":868,"text":"end"}
|
|
]
|
|
}`)
|
|
|
|
bounds, err := ParseSessionBoundsFile(boundsPath)
|
|
if err != nil {
|
|
t.Fatalf("ParseSessionBoundsFile() error = %v", err)
|
|
}
|
|
if err := ValidateSessionBoundsAgainstTranscript(bounds, transcriptPath); err != nil {
|
|
t.Fatalf("ValidateSessionBoundsAgainstTranscript() error = %v", err)
|
|
}
|
|
selector, copyUnchanged, err := BuildSeriatimKeepSelector(bounds)
|
|
if err != nil {
|
|
t.Fatalf("BuildSeriatimKeepSelector() error = %v", err)
|
|
}
|
|
if copyUnchanged {
|
|
t.Fatal("copyUnchanged = true, want false for trim action")
|
|
}
|
|
if selector != "10-868" {
|
|
t.Fatalf("selector = %q, want %q", selector, "10-868")
|
|
}
|
|
}
|
|
|
|
func TestBoundsMissingStartSegmentIDFails(t *testing.T) {
|
|
bounds := SessionBounds{TrimAction: "trim", EndSegmentID: intPtr(5)}
|
|
err := ValidateSessionBoundsAgainstTranscript(bounds, writeTranscriptWithIDs(t, 1, 5))
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "start_segment_id") {
|
|
t.Fatalf("error = %q, want start_segment_id context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestBoundsMissingEndSegmentIDFails(t *testing.T) {
|
|
bounds := SessionBounds{TrimAction: "trim", StartSegmentID: intPtr(1)}
|
|
err := ValidateSessionBoundsAgainstTranscript(bounds, writeTranscriptWithIDs(t, 1, 5))
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "end_segment_id") {
|
|
t.Fatalf("error = %q, want end_segment_id context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestBoundsDescendingRangeFails(t *testing.T) {
|
|
bounds := SessionBounds{TrimAction: "trim", StartSegmentID: intPtr(9), EndSegmentID: intPtr(5)}
|
|
err := ValidateSessionBoundsAgainstTranscript(bounds, writeTranscriptWithIDs(t, 5, 9))
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "<=") {
|
|
t.Fatalf("error = %q, want range ordering context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestBoundsNonExistentStartIDFails(t *testing.T) {
|
|
bounds := SessionBounds{TrimAction: "trim", StartSegmentID: intPtr(3), EndSegmentID: intPtr(9)}
|
|
err := ValidateSessionBoundsAgainstTranscript(bounds, writeTranscriptWithIDs(t, 4, 9))
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "start_segment_id 3 does not exist") {
|
|
t.Fatalf("error = %q, want missing start id context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestBoundsNonExistentEndIDFails(t *testing.T) {
|
|
bounds := SessionBounds{TrimAction: "trim", StartSegmentID: intPtr(3), EndSegmentID: intPtr(9)}
|
|
err := ValidateSessionBoundsAgainstTranscript(bounds, writeTranscriptWithIDs(t, 3, 8))
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "end_segment_id 9 does not exist") {
|
|
t.Fatalf("error = %q, want missing end id context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestInvalidTranscriptJSONFails(t *testing.T) {
|
|
bounds := SessionBounds{TrimAction: "trim", StartSegmentID: intPtr(1), EndSegmentID: intPtr(2)}
|
|
path := filepath.Join(t.TempDir(), "polished.json")
|
|
writeBoundsTestFile(t, path, "not-json")
|
|
|
|
err := ValidateSessionBoundsAgainstTranscript(bounds, path)
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "transcript json parse failed") {
|
|
t.Fatalf("error = %q, want transcript json parse context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestTranscriptWithoutSegmentsFails(t *testing.T) {
|
|
bounds := SessionBounds{TrimAction: "trim", StartSegmentID: intPtr(1), EndSegmentID: intPtr(2)}
|
|
path := filepath.Join(t.TempDir(), "polished.json")
|
|
writeBoundsTestFile(t, path, `{"schema":"audita.processed.v1"}`)
|
|
|
|
err := ValidateSessionBoundsAgainstTranscript(bounds, path)
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "segments array is required") {
|
|
t.Fatalf("error = %q, want segments required context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestWarningsArePreservedInParsedOutput(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "bounds.json")
|
|
writeBoundsTestFile(t, path, `{
|
|
"confidence":"medium",
|
|
"trim_action":"trim",
|
|
"start_segment_id":10,
|
|
"end_segment_id":12,
|
|
"warnings":["weak opening marker","possible flashback"]
|
|
}`)
|
|
|
|
bounds, err := ParseSessionBoundsFile(path)
|
|
if err != nil {
|
|
t.Fatalf("ParseSessionBoundsFile() error = %v", err)
|
|
}
|
|
if len(bounds.Warnings) != 2 {
|
|
t.Fatalf("warnings len = %d, want 2", len(bounds.Warnings))
|
|
}
|
|
if bounds.Warnings[0] != "weak opening marker" || bounds.Warnings[1] != "possible flashback" {
|
|
t.Fatalf("warnings = %#v, want preserved warnings", bounds.Warnings)
|
|
}
|
|
}
|
|
|
|
func TestNoTrimActionCopyIsSupported(t *testing.T) {
|
|
bounds := SessionBounds{TrimAction: "copy"}
|
|
if err := ValidateSessionBoundsAgainstTranscript(bounds, writeTranscriptWithIDs(t, 1)); err != nil {
|
|
t.Fatalf("ValidateSessionBoundsAgainstTranscript() error = %v", err)
|
|
}
|
|
selector, copyUnchanged, err := BuildSeriatimKeepSelector(bounds)
|
|
if err != nil {
|
|
t.Fatalf("BuildSeriatimKeepSelector() error = %v", err)
|
|
}
|
|
if selector != "" {
|
|
t.Fatalf("selector = %q, want empty for copy action", selector)
|
|
}
|
|
if !copyUnchanged {
|
|
t.Fatal("copyUnchanged = false, want true for copy action")
|
|
}
|
|
}
|
|
|
|
func writeTranscriptWithIDs(t *testing.T, ids ...int) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "polished.json")
|
|
if len(ids) == 0 {
|
|
writeBoundsTestFile(t, path, `{"segments":[]}`)
|
|
return path
|
|
}
|
|
parts := make([]string, 0, len(ids))
|
|
for _, id := range ids {
|
|
parts = append(parts, `{"id":`+strconv.Itoa(id)+`}`)
|
|
}
|
|
writeBoundsTestFile(t, path, `{"segments":[`+strings.Join(parts, ",")+`]}`)
|
|
return path
|
|
}
|
|
|
|
func writeBoundsTestFile(t *testing.T, path, contents string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatalf("MkdirAll(%q): %v", path, err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
|
|
t.Fatalf("WriteFile(%q): %v", path, err)
|
|
}
|
|
}
|
|
|
|
func intPtr(v int) *int {
|
|
value := v
|
|
return &value
|
|
}
|