|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "database/sql" |
| 6 | + "errors" |
| 7 | + "go/ast" |
| 8 | + "go/parser" |
| 9 | + "go/token" |
| 10 | + "log" |
| 11 | + "os" |
| 12 | + "os/exec" |
| 13 | + "path/filepath" |
| 14 | + "runtime" |
| 15 | + "strings" |
| 16 | + "testing" |
| 17 | + |
| 18 | + "github.com/midagedev/gadak/internal/config" |
| 19 | + "github.com/midagedev/gadak/internal/store" |
| 20 | + |
| 21 | + _ "modernc.org/sqlite" // pure-Go driver: same one internal/store registers |
| 22 | +) |
| 23 | + |
| 24 | +// TestGDK1243MainRoutesBootErrorToBootFatal is the GDK-1243 wiring half: a |
| 25 | +// boot failure returned from run() (mirror schema refusal, locked DB, any |
| 26 | +// pre-window error) must reach bootFatal — the exit that shows a native |
| 27 | +// dialog — not a bare log.Fatal, which a double-clicked app user never sees. |
| 28 | +// AST pin for the same reason gdk658_test.go parses main.go: main() itself is |
| 29 | +// not callable from a test. |
| 30 | +// |
| 31 | +// FAIL-first: against the pre-fix main (log.Fatal on run's error) the |
| 32 | +// bootFatal lookup fails and log.Fatal is still present. |
| 33 | +func TestGDK1243MainRoutesBootErrorToBootFatal(t *testing.T) { |
| 34 | + fset := token.NewFileSet() |
| 35 | + f, err := parser.ParseFile(fset, "main.go", nil, 0) |
| 36 | + if err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + var mainFn *ast.FuncDecl |
| 40 | + for _, d := range f.Decls { |
| 41 | + fn, ok := d.(*ast.FuncDecl) |
| 42 | + if !ok || fn.Name == nil || fn.Name.Name != "main" || fn.Recv != nil { |
| 43 | + continue |
| 44 | + } |
| 45 | + mainFn = fn |
| 46 | + break |
| 47 | + } |
| 48 | + if mainFn == nil || mainFn.Body == nil { |
| 49 | + t.Fatal("func main() not found in main.go") |
| 50 | + } |
| 51 | + calls := topLevelCallNames(mainFn.Body) |
| 52 | + hasBootFatal, hasLogFatal := false, false |
| 53 | + for _, name := range calls { |
| 54 | + switch name { |
| 55 | + case "bootFatal": |
| 56 | + hasBootFatal = true |
| 57 | + case "log.Fatal": |
| 58 | + hasLogFatal = true |
| 59 | + } |
| 60 | + } |
| 61 | + if !hasBootFatal { |
| 62 | + t.Fatalf("GDK-1243: main() never calls bootFatal — a run() error still dies without a dialog. calls=%v", calls) |
| 63 | + } |
| 64 | + if hasLogFatal { |
| 65 | + t.Fatalf("GDK-1243: main() still calls log.Fatal directly — that path has no dialog. calls=%v", calls) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// writeFutureMirror builds the GDK-1243 repro home: a scratch GADAK_HOME |
| 70 | +// whose gadak.db carries a user_version no gadak build can read (999 > any |
| 71 | +// len(migrations)). Same technique as internal/store's too-new test. |
| 72 | +func writeFutureMirror(t *testing.T, home string) { |
| 73 | + t.Helper() |
| 74 | + raw, err := sql.Open("sqlite", "file:"+filepath.Join(home, "gadak.db")) |
| 75 | + if err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + if _, err := raw.Exec("PRAGMA user_version = 999"); err != nil { |
| 79 | + raw.Close() |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + if err := raw.Close(); err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// TestGDK1243FutureMirrorReachesDialogSeam is the runtime half: the exact |
| 88 | +// boot the v0.18.1 measurement saw die silently (scratch home, mirror from a |
| 89 | +// newer gadak) must return run()'s error, and reporting it must hand the |
| 90 | +// store's sentence to the dialog seam verbatim. Headless by construction — |
| 91 | +// the seam is swapped, so no modal runs (the CI macos-14 runner has no user |
| 92 | +// session to dismiss one). The real ~/.gadak is never touched: GADAK_HOME |
| 93 | +// points at t.TempDir(). |
| 94 | +func TestGDK1243FutureMirrorReachesDialogSeam(t *testing.T) { |
| 95 | + home := t.TempDir() |
| 96 | + writeFutureMirror(t, home) |
| 97 | + t.Setenv("GADAK_HOME", home) |
| 98 | + config.SetProfile("") |
| 99 | + t.Cleanup(func() { config.SetProfile("") }) |
| 100 | + |
| 101 | + err := run() |
| 102 | + if err == nil { |
| 103 | + t.Fatal("run() with a future mirror returned nil — it would have opened a window; the refusal is gone") |
| 104 | + } |
| 105 | + var tooNew *store.SchemaTooNewError |
| 106 | + if !errors.As(err, &tooNew) { |
| 107 | + t.Fatalf("boot failure must be the typed store refusal, got %T: %v", err, err) |
| 108 | + } |
| 109 | + |
| 110 | + var gotTitle, gotText string |
| 111 | + shown := 0 |
| 112 | + orig := showBootErrorDialog |
| 113 | + showBootErrorDialog = func(title, text string) { |
| 114 | + shown++ |
| 115 | + gotTitle, gotText = title, text |
| 116 | + } |
| 117 | + defer func() { showBootErrorDialog = orig }() |
| 118 | + |
| 119 | + reportBootFailure(err) |
| 120 | + |
| 121 | + if shown != 1 { |
| 122 | + t.Fatalf("dialog seam called %d times, want 1", shown) |
| 123 | + } |
| 124 | + if gotTitle != "Gadak" { |
| 125 | + t.Fatalf("dialog title = %q, want Gadak", gotTitle) |
| 126 | + } |
| 127 | + if gotText != tooNew.Error() { |
| 128 | + t.Fatalf("dialog text must be the store's sentence verbatim — no translation, no summary:\n got: %s\nwant: %s", gotText, tooNew.Error()) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// TestGDK1243ReportKeepsStderrTrail pins the CLI-parity half: the dialog is |
| 133 | +// additive, and the stderr/log line the old log.Fatal wrote still happens. |
| 134 | +func TestGDK1243ReportKeepsStderrTrail(t *testing.T) { |
| 135 | + var buf bytes.Buffer |
| 136 | + log.SetOutput(&buf) |
| 137 | + defer log.SetOutput(os.Stderr) |
| 138 | + |
| 139 | + orig := showBootErrorDialog |
| 140 | + showBootErrorDialog = func(string, string) {} |
| 141 | + defer func() { showBootErrorDialog = orig }() |
| 142 | + |
| 143 | + reportBootFailure(errors.New("boot boom")) |
| 144 | + |
| 145 | + if !strings.Contains(buf.String(), "boot boom") { |
| 146 | + t.Fatalf("stderr/log trail lost; log wrote: %q", buf.String()) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// TestGDK1243BootDialogScriptEscaping: the dialog body is arbitrary error |
| 151 | +// prose — quotes, backslashes (Windows paths in the store's recovery |
| 152 | +// command), line breaks. The AppleScript literal must carry all of them. |
| 153 | +func TestGDK1243BootDialogScriptEscaping(t *testing.T) { |
| 154 | + script := bootDialogScript("Gad\"ak", "mirror \"x\" at C:\\Users\\a\\b.db\nsecond line") |
| 155 | + want := "display dialog \"mirror \\\"x\\\" at C:\\\\Users\\\\a\\\\b.db\nsecond line\" with title \"Gad\\\"ak\" with icon stop" |
| 156 | + if script != want { |
| 157 | + t.Fatalf("script:\n got: %q\nwant: %q", script, want) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// TestGDK1243BootDialogScriptCompiles is the artifact/code detector for the |
| 162 | +// AppleScript string: bootDialogScript's output is consumed by an external |
| 163 | +// interpreter, so a Go-level string assertion alone cannot prove it is valid |
| 164 | +// AppleScript. osacompile compiles the production script — with a nasty body |
| 165 | +// and with the real store refusal sentence — without running or showing |
| 166 | +// anything. darwin-only; elsewhere the dialog is not the surface anyway. |
| 167 | +func TestGDK1243BootDialogScriptCompiles(t *testing.T) { |
| 168 | + if runtime.GOOS != "darwin" { |
| 169 | + t.Skip("osacompile is darwin-only; other GOOS keep stderr as the boot-failure surface") |
| 170 | + } |
| 171 | + tooNew := &store.SchemaTooNewError{Path: "/tmp/h/gadak.db", Have: 999, Supported: 40} |
| 172 | + for _, text := range []string{ |
| 173 | + tooNew.Error(), |
| 174 | + "quote \" backslash \\ and\nnewline", |
| 175 | + } { |
| 176 | + out := filepath.Join(t.TempDir(), "probe.scpt") |
| 177 | + cmd := exec.Command("osacompile", "-o", out, "-e", bootDialogScript("Gadak", text)) |
| 178 | + if err := cmd.Run(); err != nil { |
| 179 | + t.Fatalf("boot dialog script does not compile as AppleScript (text=%q): %v", text, err) |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments