A Telegram Mini App served by the core under /app/, embedded in the binary. - API under /app/api/ with initData validated (HMAC, age) on every call - Pages: Home, Queue, Buy (target, product, invoice), History, Groups, group detail (Overview, Settings, Wallet, Usage) and Me - Every action runs through the same service, billing and scheduler code as the chat, so limits, prices and admin checks are decided on the server - Texts come from the catalog over the API; layout mirrors for RTL - Front end in web/miniapp (Vite, React, TypeScript); the build is committed and guarded by a source hash test; the Docker image rebuilds it - MINIAPP_ENABLED with an https PUBLIC_URL; entry points from /app, group settings and the over-limit offer - Browser test (make miniapp-e2e) against a core in test mode - Fix: each download attempt gets its own scratch directory, so a retry cannot lose its file to the cleanup of the failed attempt Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
203 lines
5.6 KiB
Go
203 lines
5.6 KiB
Go
package i18n_test
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
"time"
|
|
|
|
"github.com/noderunners/dlbot/internal/i18n"
|
|
"github.com/noderunners/dlbot/internal/ytdlp"
|
|
"github.com/noderunners/dlbot/locales"
|
|
)
|
|
|
|
func load(t *testing.T) *i18n.Bundle {
|
|
t.Helper()
|
|
b, err := i18n.Load(locales.FS, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
func TestEveryErrorCodeHasAMessage(t *testing.T) {
|
|
b := load(t)
|
|
for _, code := range ytdlp.KnownCodes {
|
|
if !b.Has("en", "error."+string(code)) {
|
|
t.Errorf("the en catalog has no message for error.%s", code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPluralsAndArgs(t *testing.T) {
|
|
b := load(t)
|
|
if got := b.T("en", "unit.minutes", i18n.Args{"Count": 1}); got != "1 minute" {
|
|
t.Errorf("one = %q", got)
|
|
}
|
|
if got := b.T("en", "unit.minutes", i18n.Args{"Count": 5}); got != "5 minutes" {
|
|
t.Errorf("other = %q", got)
|
|
}
|
|
if got := b.T("en", "btn.res", i18n.Args{"Height": 720}); got != "720p" {
|
|
t.Errorf("btn.res = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestHTMLEscapesArguments(t *testing.T) {
|
|
b := load(t)
|
|
got := b.HTML("en", "start.private", i18n.Args{"Name": "<script>alert(1)</script>"})
|
|
if strings.Contains(got, "<script>") {
|
|
t.Fatalf("user text was not escaped: %q", got)
|
|
}
|
|
if !strings.Contains(got, "<b>") {
|
|
t.Fatalf("catalog markup must survive: %q", got)
|
|
}
|
|
raw := b.HTML("en", "info.line", i18n.Args{"Label": i18n.Raw("<i>x</i>"), "Size": "1 & 2"})
|
|
if raw != "<i>x</i>: 1 & 2" {
|
|
t.Fatalf("Raw handling = %q", raw)
|
|
}
|
|
}
|
|
|
|
func TestMissingKeyReturnsID(t *testing.T) {
|
|
b := load(t)
|
|
if got := b.T("en", "no.such.key", nil); got != "no.such.key" {
|
|
t.Fatalf("missing key = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveAndFallbackChain(t *testing.T) {
|
|
fsys := fstest.MapFS{
|
|
"en.toml": {Data: []byte("hello = \"Hello\"\nbye = \"Bye\"\nonly_en = \"Only English\"\n")},
|
|
"es.toml": {Data: []byte("hello = \"Hola\"\nbye = \"Chau\"\n")},
|
|
"nl.toml": {Data: []byte("hello = \"Hallo\"\n")},
|
|
}
|
|
b, err := i18n.Load(fsys, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resolve := map[string]string{
|
|
"": "en", "en": "en", "en-GB": "en", "es": "es", "es-PY": "es", "es-419": "es",
|
|
"nl": "nl", "nl-BE": "nl", "de": "en", "zz": "en", "not a tag!": "en",
|
|
}
|
|
for in, want := range resolve {
|
|
if got := b.Resolve(in); got != want {
|
|
t.Errorf("Resolve(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
// es-PY falls back to es, then to en for keys es does not have.
|
|
if got := b.T("es-PY", "bye", nil); got != "Chau" {
|
|
t.Errorf("es-PY bye = %q, want the es text", got)
|
|
}
|
|
if got := b.T("es-PY", "only_en", nil); got != "Only English" {
|
|
t.Errorf("es-PY only_en = %q, want the en text", got)
|
|
}
|
|
if got := b.T("nl", "bye", nil); got != "Bye" {
|
|
t.Errorf("nl bye = %q, want the en fallback", got)
|
|
}
|
|
if b.Has("nl", "bye") || !b.Has("nl", "hello") {
|
|
t.Error("Has must not count fallbacks")
|
|
}
|
|
if langs := b.Langs(); len(langs) != 3 || langs[0] != "en" {
|
|
t.Errorf("Langs = %v", langs)
|
|
}
|
|
}
|
|
|
|
func TestLoadNeedsEnglish(t *testing.T) {
|
|
if _, err := i18n.Load(fstest.MapFS{"es.toml": {Data: []byte("a = \"b\"\n")}}, nil); err == nil {
|
|
t.Fatal("loading without en.toml must fail")
|
|
}
|
|
}
|
|
|
|
func TestFormatting(t *testing.T) {
|
|
b := load(t)
|
|
sizes := map[int64]string{
|
|
512: "512 B",
|
|
2048: "2 KB",
|
|
5 << 20: "5.0 MB",
|
|
64_900_000: "61.9 MB",
|
|
150 << 20: "150 MB",
|
|
3 << 29: "1.50 GB",
|
|
}
|
|
for in, want := range sizes {
|
|
if got := b.Size("en", in); got != want {
|
|
t.Errorf("Size(%d) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
if got := b.Size("es", 64_900_000); got != "61,9 MB" {
|
|
t.Errorf("Spanish decimal separator: %q", got)
|
|
}
|
|
clocks := map[int64]string{0: "0:00", 59: "0:59", 61: "1:01", 3600: "1:00:00", 3723: "1:02:03"}
|
|
for in, want := range clocks {
|
|
if got := i18n.Clock(in); got != want {
|
|
t.Errorf("Clock(%d) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
spans := map[time.Duration]string{
|
|
0: "1 second", 30 * time.Second: "30 seconds", 60 * time.Second: "1 minute",
|
|
10 * time.Minute: "10 minutes", 3 * time.Hour: "3 hours",
|
|
}
|
|
for in, want := range spans {
|
|
if got := b.Span("en", in); got != want {
|
|
t.Errorf("Span(%s) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
if got := b.Percent("en", 0.426); got != "43%" {
|
|
t.Errorf("Percent = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestSectionExportsForTheMiniApp(t *testing.T) {
|
|
fsys := fstest.MapFS{
|
|
"en.toml": {Data: []byte(`
|
|
[app]
|
|
hello = "Hi {{.Name}}"
|
|
only_english = "English only"
|
|
[app.tab]
|
|
home = "Home"
|
|
[app.files]
|
|
one = "{{.Count}} file"
|
|
other = "{{.Count}} files"
|
|
[other]
|
|
secret = "not exported"
|
|
`)},
|
|
"es.toml": {Data: []byte(`
|
|
[app]
|
|
hello = "Hola {{.Name}}"
|
|
[app.tab]
|
|
home = "Inicio"
|
|
[app.files]
|
|
one = "{{.Count}} archivo"
|
|
other = "{{.Count}} archivos"
|
|
`)},
|
|
}
|
|
b, err := i18n.Load(fsys, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := b.Section("es-PY", "app")
|
|
if got["hello"] != "Hola {Name}" || got["tab.home"] != "Inicio" {
|
|
t.Fatalf("section = %v", got)
|
|
}
|
|
// A message missing in the language comes from English, like in chat.
|
|
if got["only_english"] != "English only" {
|
|
t.Fatalf("no fallback to English: %v", got["only_english"])
|
|
}
|
|
forms, ok := got["files"].(map[string]string)
|
|
if !ok || forms["one"] != "{Count} archivo" || forms["other"] != "{Count} archivos" {
|
|
t.Fatalf("plural forms = %v", got["files"])
|
|
}
|
|
if _, leaked := got["secret"]; leaked || len(b.Section("es", "missing")) != 0 {
|
|
t.Fatal("a section must hold its own table only")
|
|
}
|
|
}
|
|
|
|
func TestDir(t *testing.T) {
|
|
for lang, want := range map[string]string{
|
|
"en": "ltr", "es-PY": "ltr", "nl": "ltr", "en-XA": "ltr",
|
|
"ar": "rtl", "ar-EG": "rtl", "he": "rtl", "fa-IR": "rtl", "ur": "rtl", "": "ltr",
|
|
} {
|
|
if got := i18n.Dir(lang); got != want {
|
|
t.Errorf("Dir(%q) = %q, want %q", lang, got, want)
|
|
}
|
|
}
|
|
}
|