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", "referral"} { 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) } }