~bigbes/sr-ht-spec

ref: 36b9e1830fa09548dc188dcf7cfed7750021ad2e sr-ht-spec/search/lang_test.go -rw-r--r-- 2.5 KiB
36b9e183 — Eugene Blikh feat: search — one global bleve index with per-line ru/en routing 27 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package search

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestDetectClassifiesByScript(t *testing.T) {
	cases := []struct {
		name string
		text string
		want Lang
		ok   bool
	}{
		{"english prose", "The approved revision is the one a bot reads.", LangEN, true},
		{"russian prose", "Одобренная ревизия — это та, которую читает бот.", LangRU, true},
		{
			"russian prose with latin identifiers",
			"Ревизия хранится в ветке approved, а черновик — в proposals/<id>.",
			LangRU, true,
		},
		{
			"english prose with one russian word",
			"The reviewer approves the merge, sometimes annotated проверено, before it lands.",
			LangEN, true,
		},
		{"too short to classify", "## API", "", false},
		{"digits and punctuation only", "1234-5678 | --- | 9.0", "", false},
		{"empty", "", "", false},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got, ok := Detect(c.text)
			require.Equal(t, c.ok, ok)
			require.Equal(t, c.want, got)
		})
	}
}

func TestDetectInFallsBackWhenUndecidable(t *testing.T) {
	require.Equal(t, LangRU, DetectIn("## API", LangRU))
	require.Equal(t, LangEN, DetectIn("## API", LangEN))
	// A decidable text ignores the fallback entirely.
	require.Equal(t, LangRU, DetectIn("Ревизия хранится в ветке approved.", LangEN))
}

func TestRouteSplitsAMixedDocumentByLine(t *testing.T) {
	text := "Одобренная ревизия — это та, которую читает бот.\n" +
		"## API\n" +
		"The approved revision is the one a bot reads and pins.\n" +
		"Черновики живут в отдельной ветке предложений.\n"

	ru, en := route(text, LangRU)
	require.Contains(t, ru, "Одобренная ревизия")
	require.Contains(t, ru, "Черновики живут")
	require.NotContains(t, ru, "approved revision is the one")
	require.Contains(t, en, "The approved revision is the one a bot reads")
	require.NotContains(t, en, "Одобренная")
	// The undecidable heading follows the document's language.
	require.Contains(t, ru, "## API")
	require.NotContains(t, en, "## API")
}

func TestRouteLeavesASingleLanguageDocumentWhole(t *testing.T) {
	text := "The approved revision is the one a bot reads.\nProposals live on their own branch.\n"
	ru, en := route(text, LangEN)
	require.Empty(t, ru)
	require.Equal(t, "The approved revision is the one a bot reads.\nProposals live on their own branch.", en)
}