Add artifact trim transformation
This commit is contained in:
364
internal/trim/apply_test.go
Normal file
364
internal/trim/apply_test.go
Normal file
@@ -0,0 +1,364 @@
|
||||
package trim
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/seriatim/schema"
|
||||
)
|
||||
|
||||
func TestApplyKeepModeRenumbersFromOne(t *testing.T) {
|
||||
input := fullTranscriptFixture()
|
||||
selector := mustParseSelector(t, "2,4")
|
||||
|
||||
result, err := Apply(input, Options{
|
||||
Mode: ModeKeep,
|
||||
Selector: selector,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply failed: %v", err)
|
||||
}
|
||||
|
||||
if len(result.Transcript.Segments) != 2 {
|
||||
t.Fatalf("segment count = %d, want 2", len(result.Transcript.Segments))
|
||||
}
|
||||
assertSegmentIDs(t, result.Transcript.Segments, []int{1, 2})
|
||||
assertSegmentTexts(t, result.Transcript.Segments, []string{"beta", "delta"})
|
||||
assertIntMap(t, result.OldToNewID, map[int]int{2: 1, 4: 2})
|
||||
assertIntSlice(t, result.RemovedIDs, []int{1, 3})
|
||||
}
|
||||
|
||||
func TestApplyRemoveModeRenumbersFromOne(t *testing.T) {
|
||||
input := fullTranscriptFixture()
|
||||
selector := mustParseSelector(t, "2,4")
|
||||
|
||||
result, err := Apply(input, Options{
|
||||
Mode: ModeRemove,
|
||||
Selector: selector,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply failed: %v", err)
|
||||
}
|
||||
|
||||
assertSegmentIDs(t, result.Transcript.Segments, []int{1, 2})
|
||||
assertSegmentTexts(t, result.Transcript.Segments, []string{"alpha", "gamma"})
|
||||
assertIntMap(t, result.OldToNewID, map[int]int{1: 1, 3: 2})
|
||||
assertIntSlice(t, result.RemovedIDs, []int{2, 4})
|
||||
}
|
||||
|
||||
func TestApplySelectorOrderDoesNotChangeTranscriptOrder(t *testing.T) {
|
||||
input := fullTranscriptFixture()
|
||||
selector := mustParseSelector(t, "4,1,3")
|
||||
|
||||
result, err := Apply(input, Options{
|
||||
Mode: ModeKeep,
|
||||
Selector: selector,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply failed: %v", err)
|
||||
}
|
||||
|
||||
assertSegmentIDs(t, result.Transcript.Segments, []int{1, 2, 3})
|
||||
assertSegmentTexts(t, result.Transcript.Segments, []string{"alpha", "gamma", "delta"})
|
||||
}
|
||||
|
||||
func TestApplyFailsWhenSelectedIDDoesNotExist(t *testing.T) {
|
||||
input := fullTranscriptFixture()
|
||||
selector := mustParseSelector(t, "2,99")
|
||||
|
||||
_, err := Apply(input, Options{
|
||||
Mode: ModeKeep,
|
||||
Selector: selector,
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected missing selected ID error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "does not exist") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyFailsOnDuplicateInputIDs(t *testing.T) {
|
||||
input := fullTranscriptFixture()
|
||||
input.Segments[2].ID = 2
|
||||
selector := mustParseSelector(t, "2")
|
||||
|
||||
_, err := Apply(input, Options{
|
||||
Mode: ModeKeep,
|
||||
Selector: selector,
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected duplicate input ID error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "duplicate segment ID") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyFailsOnMissingOrNonSequentialInputIDs(t *testing.T) {
|
||||
input := fullTranscriptFixture()
|
||||
input.Segments[1].ID = 5
|
||||
selector := mustParseSelector(t, "1")
|
||||
|
||||
_, err := Apply(input, Options{
|
||||
Mode: ModeKeep,
|
||||
Selector: selector,
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected non-sequential input ID error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "must be sequential") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyFailsOnNonPositiveInputIDs(t *testing.T) {
|
||||
input := fullTranscriptFixture()
|
||||
input.Segments[0].ID = 0
|
||||
selector := mustParseSelector(t, "1")
|
||||
|
||||
_, err := Apply(input, Options{
|
||||
Mode: ModeKeep,
|
||||
Selector: selector,
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected non-positive input ID error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "non-positive") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyEmptyOutputFailsUnlessAllowEmpty(t *testing.T) {
|
||||
input := fullTranscriptFixture()
|
||||
selector := mustParseSelector(t, "1-4")
|
||||
|
||||
_, err := Apply(input, Options{
|
||||
Mode: ModeRemove,
|
||||
Selector: selector,
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected empty-output error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "empty transcript") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
allowed, err := Apply(input, Options{
|
||||
Mode: ModeRemove,
|
||||
Selector: selector,
|
||||
AllowEmpty: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply with AllowEmpty failed: %v", err)
|
||||
}
|
||||
if len(allowed.Transcript.Segments) != 0 {
|
||||
t.Fatalf("segment count = %d, want 0", len(allowed.Transcript.Segments))
|
||||
}
|
||||
assertIntMap(t, allowed.OldToNewID, map[int]int{})
|
||||
assertIntSlice(t, allowed.RemovedIDs, []int{1, 2, 3, 4})
|
||||
}
|
||||
|
||||
func TestApplyPreservesRetainedSegmentFieldsAndClearsOverlapIDs(t *testing.T) {
|
||||
input := fullTranscriptFixture()
|
||||
selector := mustParseSelector(t, "2")
|
||||
|
||||
result, err := Apply(input, Options{
|
||||
Mode: ModeKeep,
|
||||
Selector: selector,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply failed: %v", err)
|
||||
}
|
||||
|
||||
if len(result.Transcript.Segments) != 1 {
|
||||
t.Fatalf("segment count = %d, want 1", len(result.Transcript.Segments))
|
||||
}
|
||||
segment := result.Transcript.Segments[0]
|
||||
if segment.ID != 1 {
|
||||
t.Fatalf("segment ID = %d, want 1", segment.ID)
|
||||
}
|
||||
if segment.Source != "b.json" {
|
||||
t.Fatalf("source = %q, want %q", segment.Source, "b.json")
|
||||
}
|
||||
if segment.SourceSegmentIndex == nil || *segment.SourceSegmentIndex != 20 {
|
||||
t.Fatalf("source_segment_index = %v, want 20", segment.SourceSegmentIndex)
|
||||
}
|
||||
if segment.SourceRef != "b.json#20" {
|
||||
t.Fatalf("source_ref = %q, want %q", segment.SourceRef, "b.json#20")
|
||||
}
|
||||
if !equalStringSlices(segment.DerivedFrom, []string{"b.json#19", "b.json#20"}) {
|
||||
t.Fatalf("derived_from = %v, want %v", segment.DerivedFrom, []string{"b.json#19", "b.json#20"})
|
||||
}
|
||||
if !equalStringSlices(segment.Categories, []string{"filler", "backchannel"}) {
|
||||
t.Fatalf("categories = %v, want %v", segment.Categories, []string{"filler", "backchannel"})
|
||||
}
|
||||
if segment.Speaker != "Bob" {
|
||||
t.Fatalf("speaker = %q, want Bob", segment.Speaker)
|
||||
}
|
||||
if segment.Start != 2 || segment.End != 3 {
|
||||
t.Fatalf("times = %.3f-%.3f, want 2.000-3.000", segment.Start, segment.End)
|
||||
}
|
||||
if segment.Text != "beta" {
|
||||
t.Fatalf("text = %q, want beta", segment.Text)
|
||||
}
|
||||
if segment.OverlapGroupID != 0 {
|
||||
t.Fatalf("overlap_group_id = %d, want 0", segment.OverlapGroupID)
|
||||
}
|
||||
if len(result.Transcript.OverlapGroups) != 0 {
|
||||
t.Fatalf("overlap_groups count = %d, want 0", len(result.Transcript.OverlapGroups))
|
||||
}
|
||||
}
|
||||
|
||||
func mustParseSelector(t *testing.T, value string) Selector {
|
||||
t.Helper()
|
||||
selector, err := ParseSelector(value)
|
||||
if err != nil {
|
||||
t.Fatalf("selector parse failed for %q: %v", value, err)
|
||||
}
|
||||
return selector
|
||||
}
|
||||
|
||||
func fullTranscriptFixture() schema.Transcript {
|
||||
firstIndex := 10
|
||||
secondIndex := 20
|
||||
thirdIndex := 30
|
||||
fourthIndex := 40
|
||||
|
||||
return schema.Transcript{
|
||||
Metadata: schema.Metadata{
|
||||
Application: "seriatim",
|
||||
Version: "v-test",
|
||||
InputReader: "json-files",
|
||||
InputFiles: []string{"a.json", "b.json"},
|
||||
PreprocessingModules: []string{"validate-raw"},
|
||||
PostprocessingModules: []string{"detect-overlaps"},
|
||||
OutputModules: []string{"json"},
|
||||
},
|
||||
Segments: []schema.Segment{
|
||||
{
|
||||
ID: 1,
|
||||
Source: "a.json",
|
||||
SourceSegmentIndex: &firstIndex,
|
||||
SourceRef: "a.json#10",
|
||||
DerivedFrom: []string{"a.json#10"},
|
||||
Speaker: "Alice",
|
||||
Start: 1,
|
||||
End: 2,
|
||||
Text: "alpha",
|
||||
Categories: []string{"word-run"},
|
||||
OverlapGroupID: 7,
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
Source: "b.json",
|
||||
SourceSegmentIndex: &secondIndex,
|
||||
SourceRef: "b.json#20",
|
||||
DerivedFrom: []string{"b.json#19", "b.json#20"},
|
||||
Speaker: "Bob",
|
||||
Start: 2,
|
||||
End: 3,
|
||||
Text: "beta",
|
||||
Categories: []string{"filler", "backchannel"},
|
||||
OverlapGroupID: 7,
|
||||
},
|
||||
{
|
||||
ID: 3,
|
||||
Source: "c.json",
|
||||
SourceSegmentIndex: &thirdIndex,
|
||||
SourceRef: "c.json#30",
|
||||
DerivedFrom: []string{"c.json#30"},
|
||||
Speaker: "Carol",
|
||||
Start: 3,
|
||||
End: 4,
|
||||
Text: "gamma",
|
||||
Categories: []string{"normal"},
|
||||
OverlapGroupID: 8,
|
||||
},
|
||||
{
|
||||
ID: 4,
|
||||
Source: "d.json",
|
||||
SourceSegmentIndex: &fourthIndex,
|
||||
SourceRef: "d.json#40",
|
||||
DerivedFrom: []string{"d.json#40"},
|
||||
Speaker: "Dan",
|
||||
Start: 4,
|
||||
End: 5,
|
||||
Text: "delta",
|
||||
Categories: []string{"normal"},
|
||||
OverlapGroupID: 9,
|
||||
},
|
||||
},
|
||||
OverlapGroups: []schema.OverlapGroup{
|
||||
{
|
||||
ID: 7,
|
||||
Start: 1.5,
|
||||
End: 3.1,
|
||||
Segments: []string{"a.json#10", "b.json#20"},
|
||||
Speakers: []string{"Alice", "Bob"},
|
||||
Class: "unknown",
|
||||
Resolution: "unresolved",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func assertSegmentIDs(t *testing.T, segments []schema.Segment, want []int) {
|
||||
t.Helper()
|
||||
got := make([]int, len(segments))
|
||||
for index, segment := range segments {
|
||||
got[index] = segment.ID
|
||||
}
|
||||
assertIntSlice(t, got, want)
|
||||
}
|
||||
|
||||
func assertSegmentTexts(t *testing.T, segments []schema.Segment, want []string) {
|
||||
t.Helper()
|
||||
got := make([]string, len(segments))
|
||||
for index, segment := range segments {
|
||||
got[index] = segment.Text
|
||||
}
|
||||
if !equalStringSlices(got, want) {
|
||||
t.Fatalf("segment texts = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func assertIntSlice(t *testing.T, got []int, want []int) {
|
||||
t.Helper()
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("slice length = %d, want %d", len(got), len(want))
|
||||
}
|
||||
for index := range got {
|
||||
if got[index] != want[index] {
|
||||
t.Fatalf("slice[%d] = %d, want %d (full got=%v, want=%v)", index, got[index], want[index], got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func assertIntMap(t *testing.T, got map[int]int, want map[int]int) {
|
||||
t.Helper()
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("map length = %d, want %d", len(got), len(want))
|
||||
}
|
||||
for key, wantValue := range want {
|
||||
gotValue, exists := got[key]
|
||||
if !exists {
|
||||
t.Fatalf("missing map key %d", key)
|
||||
}
|
||||
if gotValue != wantValue {
|
||||
t.Fatalf("map[%d] = %d, want %d", key, gotValue, wantValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func equalStringSlices(got []string, want []string) bool {
|
||||
if len(got) != len(want) {
|
||||
return false
|
||||
}
|
||||
for index := range got {
|
||||
if got[index] != want[index] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user