textmachine/backend/internal/seed/normalize_test.go

57 lines
2.3 KiB
Go
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package seed
import (
"reflect"
"testing"
)
// normalize_test.go: pins on the canonical form — each against a mutation the acceptance planted and
// the battery survived.
// TestNormalizeTrimsTheWholeWhitespaceClass pins the RULE, not its most famous trigger. The emitter
// defect that bought this file fires on a LEADING NEWLINE, and a normalization narrowed to exactly that
// (`TrimSpace` → `TrimLeft(s, "\n")`) survived the battery: every fixture led with the newline. The rule
// is surrounding whitespace as a CLASS — the bank loader keys on trimmed values, so an untrimmed tail is
// a silently inert term, newline or not.
func TestNormalizeTrimsTheWholeWhitespaceClass(t *testing.T) {
f := File{Terms: []Term{{
Src: " 方源\t", Dst: "\tФан Юань ", Sense: " ", Status: " approved\n", Note: " x ",
}}}
got := f.Normalize().Terms[0]
want := Term{Src: "方源", Dst: "Фан Юань", Sense: "", Status: "approved", Note: "x"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("Normalize must trim the whole Unicode whitespace class on every field:\ngot %+v\nwant %+v", got, want)
}
}
// TestSignatureMapEnvelopeRoundTrips pins the §4.10 envelope: the version and the content id survive
// the strict decoder, a legacy map without them still loads, and the id is a function of CONTENT — the
// same document yields the same id, a different one a different id.
func TestSignatureMapEnvelopeRoundTrips(t *testing.T) {
content := File{Terms: []Term{{Src: "方源", Dst: "Фан Юань", Status: "auto"}}}
id1, err := SignatureMapID(content)
if err != nil {
t.Fatal(err)
}
id2, err := SignatureMapID(content)
if err != nil {
t.Fatal(err)
}
if id1 == "" || id1 != id2 {
t.Fatalf("the id must be a deterministic function of content: %q vs %q", id1, id2)
}
other, err := SignatureMapID(File{Terms: []Term{{Src: "花月", Dst: "Хуа Юэ", Status: "auto"}}})
if err != nil {
t.Fatal(err)
}
if other == id1 {
t.Fatal("two different documents must not share an id")
}
legacy, err := DecodeSignatureMap([]byte("terms:\n - src: 方源\n status: auto\n"))
if err != nil {
t.Fatalf("a pre-envelope map must still load (a book mid-signature across the upgrade): %v", err)
}
if legacy.Version != "" || legacy.ID != "" || len(legacy.Terms) != 1 {
t.Fatalf("legacy decode: %+v", legacy)
}
}