Files
Reims-Go/internal/archtest/bindings_test.go
T
NwflowerandClaude Opus 5.5 565577589a refactor: give each VM action one window binding
Every VM action had two bindings: one by name and one by driver and name.
The window used only the second, so the first is gone and the WithDriver
suffix with it. Unused bindings and frontend exports are removed too.

Tests now reject bindings the window never calls, stale generated
bindings and unused frontend exports. TypeScript rejects unused locals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:16:28 -07:00

87 lines
2.2 KiB
Go

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(`\b(?:api|boundGo)\.(\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)
}
}
}