414 lines
13 KiB
Go
414 lines
13 KiB
Go
package schema
|
||
|
||
import (
|
||
"os"
|
||
"strconv"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
func TestParseSourceTranscriptJSON_BareArray(t *testing.T) {
|
||
raw, err := os.ReadFile("testdata/transcript_bare_array.json")
|
||
if err != nil {
|
||
t.Fatalf("failed to read test fixture: %v", err)
|
||
}
|
||
|
||
transcript, err := ParseSourceTranscriptJSON(raw)
|
||
if err != nil {
|
||
t.Fatalf("ParseSourceTranscriptJSON failed: %v", err)
|
||
}
|
||
|
||
if len(transcript.Segments) != 2 {
|
||
t.Fatalf("expected 2 segments, got %d", len(transcript.Segments))
|
||
}
|
||
|
||
s1 := transcript.Segments[0]
|
||
if *s1.ID != 1 {
|
||
t.Errorf("expected segment[0].id = 1, got %d", *s1.ID)
|
||
}
|
||
if s1.Speaker != "Alice" {
|
||
t.Errorf("expected segment[0].speaker = Alice, got %q", s1.Speaker)
|
||
}
|
||
if s1.Start != 0.0 {
|
||
t.Errorf("expected segment[0].start = 0.0, got %g", s1.Start)
|
||
}
|
||
if s1.End != 1.5 {
|
||
t.Errorf("expected segment[0].end = 1.5, got %g", s1.End)
|
||
}
|
||
if s1.Text != "Hello world." {
|
||
t.Errorf("expected segment[0].text = Hello world., got %q", s1.Text)
|
||
}
|
||
if s1.Categories != nil {
|
||
t.Errorf("expected segment[0].categories = nil, got %v", s1.Categories)
|
||
}
|
||
|
||
s2 := transcript.Segments[1]
|
||
if *s2.ID != 2 {
|
||
t.Errorf("expected segment[1].id = 2, got %d", *s2.ID)
|
||
}
|
||
if len(s2.Categories) != 1 || s2.Categories[0] != "greeting" {
|
||
t.Errorf("expected segment[1].categories = [greeting], got %v", s2.Categories)
|
||
}
|
||
}
|
||
|
||
func TestParseSourceTranscriptJSON_ObjectWithSegments(t *testing.T) {
|
||
raw, err := os.ReadFile("testdata/transcript_object.json")
|
||
if err != nil {
|
||
t.Fatalf("failed to read test fixture: %v", err)
|
||
}
|
||
|
||
transcript, err := ParseSourceTranscriptJSON(raw)
|
||
if err != nil {
|
||
t.Fatalf("ParseSourceTranscriptJSON failed: %v", err)
|
||
}
|
||
|
||
if len(transcript.Segments) != 2 {
|
||
t.Fatalf("expected 2 segments, got %d", len(transcript.Segments))
|
||
}
|
||
|
||
if *transcript.Segments[0].ID != 1 {
|
||
t.Errorf("expected segment[0].id = 1, got %d", *transcript.Segments[0].ID)
|
||
}
|
||
}
|
||
|
||
func TestParseSourceTranscriptJSON_MalformedJSON(t *testing.T) {
|
||
raw, err := os.ReadFile("testdata/transcript_malformed.json")
|
||
if err != nil {
|
||
t.Fatalf("failed to read test fixture: %v", err)
|
||
}
|
||
|
||
_, err = ParseSourceTranscriptJSON(raw)
|
||
if err == nil {
|
||
t.Fatal("expected error for malformed JSON, got nil")
|
||
}
|
||
|
||
parseErr, ok := err.(*ParseError)
|
||
if !ok {
|
||
t.Errorf("expected *ParseError, got %T", err)
|
||
} else if parseErr.Message != "transcript is not valid JSON" {
|
||
t.Errorf("expected 'transcript is not valid JSON', got %q", parseErr.Message)
|
||
}
|
||
}
|
||
|
||
func TestParseSourceTranscriptJSON_EmptySpeaker(t *testing.T) {
|
||
raw, err := os.ReadFile("testdata/transcript_empty_speaker.json")
|
||
if err != nil {
|
||
t.Fatalf("failed to read test fixture: %v", err)
|
||
}
|
||
|
||
_, err = ParseSourceTranscriptJSON(raw)
|
||
if err == nil {
|
||
t.Fatal("expected error for empty speaker, got nil")
|
||
}
|
||
|
||
assertValidationError(t, err, "speaker", "must not be empty")
|
||
}
|
||
|
||
func TestParseSourceTranscriptJSON_EmptyText(t *testing.T) {
|
||
raw, err := os.ReadFile("testdata/transcript_empty_text.json")
|
||
if err != nil {
|
||
t.Fatalf("failed to read test fixture: %v", err)
|
||
}
|
||
|
||
_, err = ParseSourceTranscriptJSON(raw)
|
||
if err == nil {
|
||
t.Fatal("expected error for empty text, got nil")
|
||
}
|
||
|
||
assertValidationError(t, err, "text", "must not be empty")
|
||
}
|
||
|
||
func TestParseSourceTranscriptJSON_InvalidTimes(t *testing.T) {
|
||
raw, err := os.ReadFile("testdata/transcript_invalid_times.json")
|
||
if err != nil {
|
||
t.Fatalf("failed to read test fixture: %v", err)
|
||
}
|
||
|
||
_, err = ParseSourceTranscriptJSON(raw)
|
||
if err == nil {
|
||
t.Fatal("expected error for invalid times, got nil")
|
||
}
|
||
|
||
assertValidationErrorContains(t, err, "end", "must be greater than or equal to start")
|
||
}
|
||
|
||
func TestParseSourceTranscriptJSON_DuplicateIDs(t *testing.T) {
|
||
raw, err := os.ReadFile("testdata/transcript_duplicate_ids.json")
|
||
if err != nil {
|
||
t.Fatalf("failed to read test fixture: %v", err)
|
||
}
|
||
|
||
_, err = ParseSourceTranscriptJSON(raw)
|
||
if err == nil {
|
||
t.Fatal("expected error for duplicate IDs, got nil")
|
||
}
|
||
|
||
assertValidationErrorContains(t, err, "id", "duplicate id")
|
||
}
|
||
|
||
func TestParseSourceTranscriptJSON_EmptyArray(t *testing.T) {
|
||
raw, err := os.ReadFile("testdata/transcript_empty_array.json")
|
||
if err != nil {
|
||
t.Fatalf("failed to read test fixture: %v", err)
|
||
}
|
||
|
||
_, err = ParseSourceTranscriptJSON(raw)
|
||
if err == nil {
|
||
t.Fatal("expected error for empty array, got nil")
|
||
}
|
||
|
||
parseErr, ok := err.(*ParseError)
|
||
if !ok {
|
||
t.Errorf("expected *ParseError, got %T", err)
|
||
} else if parseErr.Message != "transcript must contain at least one segment" {
|
||
t.Errorf("expected 'transcript must contain at least one segment', got %q", parseErr.Message)
|
||
}
|
||
}
|
||
|
||
func TestCanonicalPreservesNonSequentialSourceIDs(t *testing.T) {
|
||
raw, err := os.ReadFile("testdata/transcript_non_sequential_id.json")
|
||
if err != nil {
|
||
t.Fatalf("failed to read test fixture: %v", err)
|
||
}
|
||
|
||
source, err := ParseSourceTranscriptJSON(raw)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
canonical := source.Canonical()
|
||
if len(canonical.Segments) != 1 || canonical.Segments[0].ID != 2 {
|
||
t.Fatalf("IDs: %+v", canonical.Segments)
|
||
}
|
||
}
|
||
|
||
func TestCanonicalAssignsMissingIDs(t *testing.T) {
|
||
raw, err := os.ReadFile("testdata/transcript_no_ids.json")
|
||
if err != nil {
|
||
t.Fatalf("failed to read test fixture: %v", err)
|
||
}
|
||
|
||
source, err := ParseSourceTranscriptJSON(raw)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
transcript := source.Canonical()
|
||
|
||
if len(transcript.Segments) != 2 {
|
||
t.Fatalf("expected 2 segments, got %d", len(transcript.Segments))
|
||
}
|
||
|
||
if transcript.Segments[0].ID != 1 {
|
||
t.Errorf("expected segment[0].id = 1, got %d", transcript.Segments[0].ID)
|
||
}
|
||
if transcript.Segments[1].ID != 2 {
|
||
t.Errorf("expected segment[1].id = 2, got %d", transcript.Segments[1].ID)
|
||
}
|
||
}
|
||
|
||
func TestParseSourceTranscriptJSON_UnsupportedTopLevelShape(t *testing.T) {
|
||
raw := []byte(`"just a string"`)
|
||
|
||
_, err := ParseSourceTranscriptJSON(raw)
|
||
if err == nil {
|
||
t.Fatal("expected error for unsupported top-level shape, got nil")
|
||
}
|
||
|
||
parseErr, ok := err.(*ParseError)
|
||
if !ok {
|
||
t.Errorf("expected *ParseError, got %T", err)
|
||
} else if parseErr.Message != "transcript must be a JSON array or an object with a segments array" {
|
||
t.Errorf("expected unsupported shape message, got %q", parseErr.Message)
|
||
}
|
||
}
|
||
|
||
func TestParseSourceTranscriptJSON_ObjectWithoutSegments(t *testing.T) {
|
||
raw := []byte(`{"metadata": {"version": "1.0"}}`)
|
||
|
||
_, err := ParseSourceTranscriptJSON(raw)
|
||
if err == nil {
|
||
t.Fatal("expected error for object without segments, got nil")
|
||
}
|
||
|
||
parseErr, ok := err.(*ParseError)
|
||
if !ok {
|
||
t.Errorf("expected *ParseError, got %T", err)
|
||
} else if parseErr.Message != "transcript object must contain a segments array" {
|
||
t.Errorf("expected 'transcript object must contain a segments array', got %q", parseErr.Message)
|
||
}
|
||
}
|
||
|
||
func TestTranscriptToJSON(t *testing.T) {
|
||
transcript := &Transcript{
|
||
Segments: []Segment{
|
||
{ID: 1, Speaker: "Alice", Start: 0.0, End: 1.5, Text: "Hello world."},
|
||
{ID: 2, Speaker: "Bob", Start: 2.0, End: 3.5, Text: "Hi there.", Categories: []string{"greeting"}},
|
||
},
|
||
}
|
||
|
||
raw, err := TranscriptToJSON(transcript)
|
||
if err != nil {
|
||
t.Fatalf("TranscriptToJSON failed: %v", err)
|
||
}
|
||
|
||
parsed, err := ParseSourceTranscriptJSON(raw)
|
||
if err != nil {
|
||
t.Fatalf("failed to parse round-trip JSON: %v", err)
|
||
}
|
||
|
||
if len(parsed.Segments) != 2 {
|
||
t.Fatalf("expected 2 segments after round-trip, got %d", len(parsed.Segments))
|
||
}
|
||
|
||
if parsed.Segments[0].Speaker != "Alice" {
|
||
t.Errorf("expected segment[0].speaker = Alice, got %q", parsed.Segments[0].Speaker)
|
||
}
|
||
if len(parsed.Segments[1].Categories) != 1 || parsed.Segments[1].Categories[0] != "greeting" {
|
||
t.Errorf("expected segment[1].categories = [greeting], got %v", parsed.Segments[1].Categories)
|
||
}
|
||
}
|
||
|
||
func assertValidationError(t *testing.T, err error, fieldContains, messageContains string) {
|
||
t.Helper()
|
||
|
||
valErr, ok := err.(*ValidationError)
|
||
if !ok {
|
||
t.Errorf("expected *ValidationError, got %T", err)
|
||
return
|
||
}
|
||
|
||
if fieldContains != "" && valErr.Field == "" {
|
||
t.Errorf("expected field to contain %q, got empty field", fieldContains)
|
||
}
|
||
if messageContains != "" && valErr.Message == "" {
|
||
t.Errorf("expected message to contain %q, got empty message", messageContains)
|
||
}
|
||
}
|
||
|
||
func assertValidationErrorContains(t *testing.T, err error, fieldContains, messageContains string) {
|
||
t.Helper()
|
||
|
||
valErr, ok := err.(*ValidationError)
|
||
if !ok {
|
||
t.Errorf("expected *ValidationError, got %T", err)
|
||
return
|
||
}
|
||
|
||
if fieldContains != "" && valErr.Field == "" {
|
||
t.Errorf("expected field to contain %q, got empty field", fieldContains)
|
||
}
|
||
if messageContains != "" && valErr.Message == "" {
|
||
t.Errorf("expected message to contain %q, got empty message", messageContains)
|
||
}
|
||
}
|
||
|
||
func TestSourceIDsAreDecodedExactly(t *testing.T) {
|
||
first, second := "1", "2"
|
||
if strconv.IntSize == 64 {
|
||
first, second = "9007199254740992", "9007199254740993"
|
||
}
|
||
array := `[{"id":FIRST,"speaker":"A","text":"one","ignored":1e1000},{"id":SECONDe0,"speaker":"B","text":"two"},{"id":null,"speaker":"C","text":"three","categories":[]}]`
|
||
array = strings.NewReplacer("FIRST", first, "SECOND", second).Replace(array)
|
||
wantFirst, _ := strconv.Atoi(first)
|
||
wantSecond, _ := strconv.Atoi(second)
|
||
for _, raw := range []string{array, `{"segments":` + array + `,"ignored":1e1000}`} {
|
||
source, err := ParseSourceTranscriptJSON([]byte(raw))
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
segments := source.Canonical().Segments
|
||
if len(segments) != 3 || segments[0].ID != wantFirst || segments[1].ID != wantSecond || segments[2].ID != 3 || source.Segments[2].ID != nil {
|
||
t.Fatalf("source IDs and canonical segments: %+v, %+v", source.Segments, segments)
|
||
}
|
||
if segments[0].Categories != nil || segments[2].Categories == nil || len(segments[2].Categories) != 0 {
|
||
t.Fatalf("category presence: %+v", segments)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestSourceIDRejectsFractionAndOverflow(t *testing.T) {
|
||
for _, id := range []string{`"1"`, "0.1", "1e1000", "1e-1000", "9223372036854775808"} {
|
||
t.Run(id, func(t *testing.T) {
|
||
_, err := ParseSourceTranscriptJSON([]byte(`[{"speaker":"A","text":"one","id":` + id + `}]`))
|
||
if _, ok := err.(*ParseError); !ok {
|
||
t.Fatalf("expected a parse error for %s, got %v", id, err)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestSourceTranscriptUsesEffectiveMemberValues(t *testing.T) {
|
||
for _, tc := range []struct {
|
||
name, speaker, text, wantText string
|
||
wantError bool
|
||
}{
|
||
{"superseded type mismatch", `"speaker":17,"speaker":"A"`, `"text":"hello"`, "hello", false},
|
||
{"effective type mismatch", `"speaker":"A","speaker":17`, `"text":"hello"`, "", true},
|
||
{"case variant after exact key", `"speaker":"A"`, `"text":"hello","TEXT":"other"`, "hello", false},
|
||
{"case variant before exact key", `"speaker":"A"`, `"TEXT":"other","text":"hello"`, "hello", false},
|
||
} {
|
||
t.Run(tc.name, func(t *testing.T) {
|
||
raw := `[{` + tc.speaker + "," + tc.text + `,"start":0,"end":1}]`
|
||
transcript, err := ParseSourceTranscriptJSON([]byte(raw))
|
||
if tc.wantError {
|
||
if err == nil {
|
||
t.Fatal("expected a type mismatch in the effective member")
|
||
}
|
||
return
|
||
}
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
segment := transcript.Segments[0]
|
||
if segment.Speaker != "A" || segment.Text != tc.wantText {
|
||
t.Errorf("effective speaker = %q, text = %q", segment.Speaker, segment.Text)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestSourceTranscriptCombinesOrdinaryAndAliasedMembers(t *testing.T) {
|
||
id := "7"
|
||
if strconv.IntSize == 64 {
|
||
id = "9007199254740993"
|
||
}
|
||
wantID, _ := strconv.Atoi(id)
|
||
segments := `[
|
||
{"speaker":"A","text":"He said \"{hello, world}\".","id":SOURCE_ID,
|
||
"extra":{"speaker":12,"TEXT":"metadata"}},
|
||
{"speaker":17,"spea\u006ber":"B","text":"expected","TEXT":"other","categories":[]},
|
||
{"speaker":"C","ſpeaker":"D","text":"last"}
|
||
]`
|
||
segments = strings.Replace(segments, "SOURCE_ID", id, 1)
|
||
for _, raw := range []string{segments, `{"segments":` + segments + "}"} {
|
||
transcript, err := ParseSourceTranscriptJSON([]byte(raw))
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
got := transcript.Canonical().Segments
|
||
if len(got) != 3 || got[0].Speaker != "A" || got[0].ID != wantID ||
|
||
got[0].Text != `He said "{hello, world}".` ||
|
||
got[1].Speaker != "B" || got[1].Text != "expected" || got[1].Categories == nil ||
|
||
got[2].Speaker != "D" || got[2].Text != "last" {
|
||
t.Fatalf("unexpected segments: %+v", got)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestTranscriptClonePreservesPresenceAndIndependence(t *testing.T) {
|
||
original := &Transcript{Segments: []Segment{
|
||
{ID: 40, Text: "original", Categories: []string{"keep"}},
|
||
{ID: 41, Categories: []string{}},
|
||
{ID: 42},
|
||
}}
|
||
clone := original.Clone()
|
||
clone.Segments[0].Text = "changed"
|
||
clone.Segments[0].Categories[0] = "changed"
|
||
if original.Segments[0].Text != "original" || original.Segments[0].Categories[0] != "keep" {
|
||
t.Fatalf("clone mutated original: %+v", original)
|
||
}
|
||
if clone.Segments[1].Categories == nil || clone.Segments[2].Categories != nil ||
|
||
(&Transcript{}).Clone().Segments != nil || (&Transcript{Segments: []Segment{}}).Clone().Segments == nil {
|
||
t.Fatalf("clone changed slice presence: %+v", clone)
|
||
}
|
||
}
|