Files
Media-download-telegram-bot/web/miniapp/embed_test.go
T
MichilisandClaude Fable 5.2 4f11f939a0 Phase 6: Mini App
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>
2026-11-18 10:27:20 +00:00

133 lines
3.9 KiB
Go

package miniapp_test
import (
"crypto/sha256"
"encoding/hex"
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"testing"
"github.com/noderunners/dlbot/internal/i18n"
"github.com/noderunners/dlbot/locales"
"github.com/noderunners/dlbot/web/miniapp"
)
// sourceFiles are the files the build is made from, as paths relative to
// this directory. The list matches sourceStamp in vite.config.ts.
func sourceFiles(t *testing.T) []string {
t.Helper()
files := []string{"index.html", "package.json", "package-lock.json", "tsconfig.json", "vite.config.ts"}
err := filepath.WalkDir("src", func(path string, d fs.DirEntry, err error) error {
if err == nil && !d.IsDir() {
files = append(files, filepath.ToSlash(path))
}
return err
})
if err != nil {
t.Fatal(err)
}
sort.Strings(files)
return files
}
// TestBuildIsCurrent fails when the committed build was not made from the
// committed sources. Fix it with "make miniapp" and commit the result.
func TestBuildIsCurrent(t *testing.T) {
h := sha256.New()
for _, f := range sourceFiles(t) {
data, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
h.Write([]byte(f + "\x00"))
h.Write(data)
h.Write([]byte{0})
}
want := hex.EncodeToString(h.Sum(nil))
got, err := fs.ReadFile(miniapp.FS(), "source.sha256")
if err != nil {
t.Fatalf("the build has no source stamp, run \"make miniapp\": %v", err)
}
if strings.TrimSpace(string(got)) != want {
t.Fatal("web/miniapp/dist is older than the sources. Run \"make miniapp\" and commit dist.")
}
}
func TestBuildIsComplete(t *testing.T) {
index, err := fs.ReadFile(miniapp.FS(), "index.html")
if err != nil {
t.Fatal(err)
}
refs := regexp.MustCompile(`(?:src|href)="/app/([^"]+)"`).FindAllStringSubmatch(string(index), -1)
if len(refs) < 2 {
t.Fatalf("index.html names %d files of its own, want a script and a style sheet", len(refs))
}
for _, r := range refs {
if st, err := fs.Stat(miniapp.FS(), r[1]); err != nil || st.Size() == 0 {
t.Errorf("index.html names %s, which is not in the build", r[1])
}
}
if !strings.Contains(string(index), "https://telegram.org/js/telegram-web-app.js") {
t.Error("index.html does not load Telegram's bridge script")
}
}
var (
literalKey = regexp.MustCompile(`\bt\("([a-z0-9_.]+)"[,)]`)
prefixKey = regexp.MustCompile(`\bt\("([a-z0-9_.]+\.)" \+`)
rawText = regexp.MustCompile(`>\s*([A-Z][a-z]+(?: [a-z]+)+)\s*<`)
)
// TestEveryTextIsInTheCatalog checks that each message the app asks for
// exists in the English catalog, and that no sentence is written into the
// markup directly.
func TestEveryTextIsInTheCatalog(t *testing.T) {
bundle, err := i18n.Load(locales.FS, nil)
if err != nil {
t.Fatal(err)
}
// The tables the API hands to the app, see appTables in internal/api.
exported := map[string]any{}
for _, table := range []string{"app", "product", "unit", "btn", "settings", "quality", "language"} {
for k := range bundle.Section("en", table) {
exported[table+"."+k] = true
}
}
seen := 0
for _, f := range sourceFiles(t) {
if !strings.HasPrefix(f, "src/") || !(strings.HasSuffix(f, ".ts") || strings.HasSuffix(f, ".tsx")) {
continue
}
data, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
src := string(data)
for _, m := range literalKey.FindAllStringSubmatch(src, -1) {
seen++
if exported[m[1]] == nil {
t.Errorf("%s uses %q, which the app does not get from the catalog", f, m[1])
}
}
for _, m := range prefixKey.FindAllStringSubmatch(src, -1) {
found := false
for k := range exported {
found = found || strings.HasPrefix(k, m[1])
}
if !found {
t.Errorf("%s builds keys from %q, and the catalog has none", f, m[1])
}
}
for _, m := range rawText.FindAllStringSubmatch(src, -1) {
t.Errorf("%s has literal text %q in its markup; it belongs in the catalog", f, m[1])
}
}
if seen < 100 {
t.Fatalf("found only %d message lookups; the pattern no longer matches the code", seen)
}
}