Reject exactly negative transcript timestamps

This commit is contained in:
2026-11-26 01:27:39 +00:00
parent 4026e26e76
commit 08a9bea23b
4 changed files with 48 additions and 7 deletions

View File

@@ -183,13 +183,13 @@ func validTimestamp(value fmt.Stringer, label string) (*big.Rat, error) {
if math.IsInf(parsed, 0) || math.IsNaN(parsed) {
return nil, inputErrorf("%s must be finite", label)
}
if parsed < 0 {
return nil, inputErrorf("%s must not be negative", label)
}
rat, ok := new(big.Rat).SetString(raw)
if !ok {
return nil, inputErrorf("%s must be a valid number", label)
}
if rat.Sign() < 0 {
return nil, inputErrorf("%s must not be negative", label)
}
return rat, nil
}

View File

@@ -111,6 +111,38 @@ func TestParseAcceptsNumericSegmentIDs(t *testing.T) {
}
}
func TestParseTimestampPrecisionAndSign(t *testing.T) {
for _, tt := range []struct {
name, start, end string
wantError bool
}{
{"tiny negative start", "-1e-1000", "0", true},
{"tiny negative end", "0", "-1e-1000", true},
{"tiny positive", "1e-1000", "2e-1000", false},
{"negative zero", "-0", "0", false},
{"ordinary", "1.25", "2.5", false},
{"precise reversed", "2e-1000", "1e-1000", true},
} {
t.Run(tt.name, func(t *testing.T) {
raw := validJSONWithSegment(`"id":1,"start":` + tt.start + `,"end":` + tt.end + `,"speaker":"Narrator","text":"Text"`)
doc, err := New().Parse(context.Background(), contracts.ParseRequest{Raw: raw})
if (err != nil) != tt.wantError {
t.Fatalf("Parse() error = %v, want error %t", err, tt.wantError)
}
if strings.HasPrefix(tt.name, "tiny negative") && !strings.Contains(err.Error(), "negative") {
t.Fatalf("Parse() error = %v, want negative timestamp error", err)
}
if err == nil {
start, _ := Start(doc.Units[0])
end, _ := End(doc.Units[0])
if start != json.Number(tt.start) || end != json.Number(tt.end) {
t.Fatalf("timestamps = %q, %q, want %q, %q", start, end, tt.start, tt.end)
}
}
})
}
}
func TestParseRequestSourceIDOverridesMetadataIDs(t *testing.T) {
raw := readFixture(t, "testdata/valid_minimal.json")