Keep transcript decoding and encoding on typed data
This commit is contained in:
@@ -2,6 +2,8 @@ package schema
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -163,30 +165,33 @@ func TestParseSourceTranscriptJSON_EmptyArray(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTranscriptJSON_NonSequentialID(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
|
||||
_, err = ParseTranscriptJSON(raw)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for non-sequential ID, got nil")
|
||||
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)
|
||||
}
|
||||
|
||||
assertValidationErrorContains(t, err, "id", "must be sequential")
|
||||
}
|
||||
|
||||
func TestParseTranscriptJSONLenient_AssignsSequentialIDs(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
|
||||
transcript, err := ParseTranscriptJSONLenient(raw)
|
||||
source, err := ParseSourceTranscriptJSON(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseTranscriptJSONLenient failed: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
transcript := source.Canonical()
|
||||
|
||||
if len(transcript.Segments) != 2 {
|
||||
t.Fatalf("expected 2 segments, got %d", len(transcript.Segments))
|
||||
@@ -245,7 +250,7 @@ func TestTranscriptToJSON(t *testing.T) {
|
||||
t.Fatalf("TranscriptToJSON failed: %v", err)
|
||||
}
|
||||
|
||||
parsed, err := ParseTranscriptJSON(raw)
|
||||
parsed, err := ParseSourceTranscriptJSON(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse round-trip JSON: %v", err)
|
||||
}
|
||||
@@ -295,3 +300,56 @@ func assertValidationErrorContains(t *testing.T, err error, fieldContains, messa
|
||||
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 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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user