135 lines
3.5 KiB
Go
135 lines
3.5 KiB
Go
package artifact
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/seriatim/schema"
|
|
)
|
|
|
|
func TestParseOutputArtifactJSONParsesFullIntermediateAndMinimal(t *testing.T) {
|
|
t.Run("full", func(t *testing.T) {
|
|
first := 0
|
|
value := schema.Transcript{
|
|
Metadata: schema.Metadata{
|
|
Application: "seriatim",
|
|
Version: "v-test",
|
|
InputReader: "json-files",
|
|
InputFiles: []string{"input.json"},
|
|
PreprocessingModules: []string{"validate-raw"},
|
|
PostprocessingModules: []string{"assign-ids", "validate-output"},
|
|
OutputModules: []string{"json"},
|
|
},
|
|
Segments: []schema.Segment{
|
|
{
|
|
ID: 1,
|
|
Source: "input.json",
|
|
SourceSegmentIndex: &first,
|
|
Speaker: "Alice",
|
|
Start: 1,
|
|
End: 2,
|
|
Text: "hello",
|
|
Categories: []string{"backchannel"},
|
|
},
|
|
},
|
|
OverlapGroups: []schema.OverlapGroup{},
|
|
}
|
|
|
|
parsed := mustParseOutputArtifact(t, value)
|
|
if parsed.Schema != OutputSchemaFull {
|
|
t.Fatalf("schema = %q, want %q", parsed.Schema, OutputSchemaFull)
|
|
}
|
|
if parsed.Full == nil {
|
|
t.Fatal("expected full payload")
|
|
}
|
|
})
|
|
|
|
t.Run("intermediate", func(t *testing.T) {
|
|
value := schema.IntermediateTranscript{
|
|
Metadata: schema.IntermediateMetadata{
|
|
Application: "seriatim",
|
|
Version: "v-test",
|
|
OutputSchema: OutputSchemaIntermediate,
|
|
},
|
|
Segments: []schema.IntermediateSegment{
|
|
{ID: 1, Start: 1, End: 2, Speaker: "Alice", Text: "hello", Categories: []string{"filler"}},
|
|
},
|
|
}
|
|
|
|
parsed := mustParseOutputArtifact(t, value)
|
|
if parsed.Schema != OutputSchemaIntermediate {
|
|
t.Fatalf("schema = %q, want %q", parsed.Schema, OutputSchemaIntermediate)
|
|
}
|
|
if parsed.Intermediate == nil {
|
|
t.Fatal("expected intermediate payload")
|
|
}
|
|
})
|
|
|
|
t.Run("minimal", func(t *testing.T) {
|
|
value := schema.MinimalTranscript{
|
|
Metadata: schema.MinimalMetadata{
|
|
Application: "seriatim",
|
|
Version: "v-test",
|
|
OutputSchema: OutputSchemaMinimal,
|
|
},
|
|
Segments: []schema.MinimalSegment{
|
|
{ID: 1, Start: 1, End: 2, Speaker: "Alice", Text: "hello"},
|
|
},
|
|
}
|
|
|
|
parsed := mustParseOutputArtifact(t, value)
|
|
if parsed.Schema != OutputSchemaMinimal {
|
|
t.Fatalf("schema = %q, want %q", parsed.Schema, OutputSchemaMinimal)
|
|
}
|
|
if parsed.Minimal == nil {
|
|
t.Fatal("expected minimal payload")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestParseOutputArtifactJSONRejectsMalformedJSON(t *testing.T) {
|
|
_, err := ParseOutputArtifactJSON([]byte(`{"metadata":`))
|
|
if err == nil {
|
|
t.Fatal("expected malformed JSON error")
|
|
}
|
|
if !strings.Contains(err.Error(), "input JSON is malformed") {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestParseOutputArtifactJSONRejectsRawWhisperXLikeInput(t *testing.T) {
|
|
data := []byte(`{
|
|
"segments": [
|
|
{
|
|
"id": 0,
|
|
"start": 0.1,
|
|
"end": 1.2,
|
|
"text": "hello",
|
|
"words": [{"word":"hello","start":0.1,"end":0.8}]
|
|
}
|
|
]
|
|
}`)
|
|
|
|
_, err := ParseOutputArtifactJSON(data)
|
|
if err == nil {
|
|
t.Fatal("expected artifact validation error")
|
|
}
|
|
if !strings.Contains(err.Error(), "not a valid seriatim output artifact") {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func mustParseOutputArtifact(t *testing.T, value any) OutputArtifact {
|
|
t.Helper()
|
|
data, err := json.Marshal(value)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
parsed, err := ParseOutputArtifactJSON(data)
|
|
if err != nil {
|
|
t.Fatalf("parse: %v", err)
|
|
}
|
|
return parsed
|
|
}
|