2026-05-10 23:52:36 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
2026-11-16 03:56:54 +00:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2026-05-10 23:52:36 +00:00
|
|
|
"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")
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 00:41:29 +00:00
|
|
|
parseErr, ok := err.(*ParseError)
|
|
|
|
|
if !ok {
|
2026-05-10 23:52:36 +00:00
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 00:41:29 +00:00
|
|
|
parseErr, ok := err.(*ParseError)
|
|
|
|
|
if !ok {
|
2026-05-10 23:52:36 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 03:56:54 +00:00
|
|
|
func TestCanonicalPreservesNonSequentialSourceIDs(t *testing.T) {
|
2026-05-10 23:52:36 +00:00
|
|
|
raw, err := os.ReadFile("testdata/transcript_non_sequential_id.json")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to read test fixture: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 03:56:54 +00:00
|
|
|
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)
|
2026-05-10 23:52:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 03:56:54 +00:00
|
|
|
func TestCanonicalAssignsMissingIDs(t *testing.T) {
|
2026-05-10 23:52:36 +00:00
|
|
|
raw, err := os.ReadFile("testdata/transcript_no_ids.json")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to read test fixture: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 03:56:54 +00:00
|
|
|
source, err := ParseSourceTranscriptJSON(raw)
|
2026-05-10 23:52:36 +00:00
|
|
|
if err != nil {
|
2026-11-16 03:56:54 +00:00
|
|
|
t.Fatal(err)
|
2026-05-10 23:52:36 +00:00
|
|
|
}
|
2026-11-16 03:56:54 +00:00
|
|
|
transcript := source.Canonical()
|
2026-05-10 23:52:36 +00:00
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 00:41:29 +00:00
|
|
|
parseErr, ok := err.(*ParseError)
|
|
|
|
|
if !ok {
|
2026-05-10 23:52:36 +00:00
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 00:41:29 +00:00
|
|
|
parseErr, ok := err.(*ParseError)
|
|
|
|
|
if !ok {
|
2026-05-10 23:52:36 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 03:56:54 +00:00
|
|
|
parsed, err := ParseSourceTranscriptJSON(raw)
|
2026-05-10 23:52:36 +00:00
|
|
|
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()
|
|
|
|
|
|
2026-05-11 00:41:29 +00:00
|
|
|
valErr, ok := err.(*ValidationError)
|
|
|
|
|
if !ok {
|
2026-05-10 23:52:36 +00:00
|
|
|
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()
|
|
|
|
|
|
2026-05-11 00:41:29 +00:00
|
|
|
valErr, ok := err.(*ValidationError)
|
|
|
|
|
if !ok {
|
2026-05-10 23:52:36 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-16 03:56:54 +00:00
|
|
|
|
|
|
|
|
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 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)
|
|
|
|
|
}
|
|
|
|
|
}
|