package archtest import ( "go/ast" "io/fs" "os" "path/filepath" "regexp" "strings" "testing" ) // TestWindowBindingsMatch keeps the methods App exposes to the window in step // with the frontend: Wails exposes every exported method, so each one needs a // caller, and frontend/wailsjs must be regenerated after a change // (wails generate module). func TestWindowBindingsMatch(t *testing.T) { generated := map[string]bool{} raw, err := os.ReadFile("../../frontend/wailsjs/go/app/App.d.ts") if err != nil { t.Fatal(err) } for _, m := range regexp.MustCompile(`export function (\w+)\(`).FindAllStringSubmatch(string(raw), -1) { generated[m[1]] = true } used := map[string]bool{} call := regexp.MustCompile(`\bapi\.(\w+)`) err = filepath.WalkDir("../../frontend/src", func(p string, d fs.DirEntry, err error) error { if err != nil || d.IsDir() || !strings.Contains(filepath.Ext(p), ".ts") { return err } src, err := os.ReadFile(p) for _, m := range call.FindAllStringSubmatch(string(src), -1) { used[m[1]] = true } return err }) if err != nil { t.Fatal(err) } exported := map[string]bool{} for _, s := range sources(t) { if s.rel == "main.go" { ast.Inspect(s.file, func(n ast.Node) bool { if sel, ok := n.(*ast.SelectorExpr); ok { used[sel.Sel.Name] = true } return true }) } if s.isTest() || s.dir() != "internal/app" { continue } for _, decl := range s.file.Decls { fn, ok := decl.(*ast.FuncDecl) if !ok || fn.Recv == nil || !fn.Name.IsExported() { continue } star, ok := fn.Recv.List[0].Type.(*ast.StarExpr) if !ok { continue } if id, ok := star.X.(*ast.Ident); ok && id.Name == "App" { exported[fn.Name.Name] = true } } } if len(exported) < 100 || len(generated) < 100 { t.Fatalf("found only %d App methods and %d generated bindings", len(exported), len(generated)) } for name := range exported { if !used[name] { t.Errorf("the window never calls App.%s: delete or unexport it", name) } if !generated[name] { t.Errorf("App.%s is missing from frontend/wailsjs: run wails generate module", name) } } for name := range generated { if !exported[name] { t.Errorf("frontend/wailsjs still has App.%s: run wails generate module", name) } } }