Phase 1: core bot
Link in, card, download, send, in private chats and groups. - cmd/core with an embedded download worker - Telegram front end on telego v1.13.0 (Bot API 10.4): link card with thumbnail and format buttons, one live status message, /dl /audio /thumb /info /queue /cancel, group support with requester-only buttons - Database backed queue: paid and free lanes, leases with heartbeats, one automatic retry, cancel, queue position and wait estimate - yt-dlp and ffprobe behind a Runner interface with classified, redacted errors mapped to friendly messages - SQLite via modernc with goose migrations, portable schema - Local file storage behind a Storage interface - go-i18n with an English catalog, no literal text in handlers - Free tier limits enforced before queueing and again on the real file - Bot token and webhook secret are stripped from all log output - Tests: URL parsing, job lifecycle, worker, yt-dlp wrapper, and end to end flows against a fake Bot API server; opt-in test with the real yt-dlp Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user