textmachine/backend/internal/pipeline/seedlint_test.go

66 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

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 pipeline
import (
"strings"
"testing"
"textmachine/backend/internal/store"
)
// seedlint_test.go: WS3 (д) `tmctl seed-lint` — the dry-run of the REAL loadGlossarySeed fail-louds over
// a seed YAML (the mined delta or a manual seed). Both directions: a clean seed lints OK; a defect fails
// loud; and the emitted mined delta (MinedDeltaYAML) round-trips through the real loader with 0 fails.
func TestSeedLintCleanPasses(t *testing.T) {
p := writeSeed(t, `
terms:
- src: 方源
dst: Фан Юань
type: name
status: approved
- src: 蛊
status: auto
`)
if err := SeedLint(p); err != nil {
t.Fatalf("a clean seed must lint OK, got: %v", err)
}
}
func TestSeedLintCatchesDefects(t *testing.T) {
cases := []struct {
name, yaml, want string
}{
{"approved without dst", "terms:\n - src: 方源\n status: approved\n", "non-empty dst"},
{"unknown status", "terms:\n - src: 方源\n dst: X\n status: bogus\n", "status must be"},
{"duplicate key", "terms:\n - src: 方源\n dst: A\n - src: 方源\n dst: B\n", "duplicate"},
{"shared-key collision", "terms:\n - src: 老赵\n dst: Старина Чжао\n status: approved\n - src: 赵大\n dst: Чжао Да\n status: approved\n aliases:\n - alias: 老赵\n", "shared"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
p := writeSeed(t, tc.yaml)
err := SeedLint(p)
if err == nil {
t.Fatalf("%s: seed-lint must fail loud", tc.name)
}
if !strings.Contains(err.Error(), tc.want) {
t.Fatalf("%s: error %q must mention %q", tc.name, err.Error(), tc.want)
}
})
}
}
func TestSeedLintEmittedMinedDelta(t *testing.T) {
// The emitted mined delta must be loadable by the REAL loader (0 fail-louds) — the pre-condition for
// the bank-mining reseed.
chunks := mchunks(strings.Repeat("龙公来到青茅山。龙公很强。青茅山很高。", 4))
seed := []store.GlossaryEntry{{Src: "青茅山", Dst: "гора Цинмао", Type: "place", Status: "approved", Source: "seed"}}
mined := MineBank(chunks, smallContrast(t), seed, frozenMinerConfig(), testLangPack(t))
yamlStr, err := MinedDeltaYAML(mined)
if err != nil {
t.Fatal(err)
}
p := writeSeed(t, yamlStr)
if err := SeedLint(p); err != nil {
t.Fatalf("the emitted mined delta must lint clean (loadable), got:\n%v\n--- delta ---\n%s", err, yamlStr)
}
}