Files
Reims-Go/internal/app/tasks_test.go
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

109 lines
3.3 KiB
Go

package app
import (
"context"
"errors"
"fmt"
"testing"
"github.com/Nwflower/reims-go/internal/macos/installer"
)
func newTestTasks(t *testing.T) *[]Task {
t.Helper()
sent := &[]Task{}
old := tasks
tasks = &taskManager{tasks: map[string]*taskEntry{}, emit: func(x Task) { *sent = append(*sent, x) }}
t.Cleanup(func() { tasks = old })
return sent
}
func TestTaskProgressIsThrottledButStepsAndEndsAlwaysGoOut(t *testing.T) {
sent := newTestTasks(t)
id := tasks.start("installer-download", "26.7.1", "", func() {})
tasks.update(id, installer.Update{Kind: installer.KindStep, Message: "downloading", Percent: -1})
for i := 1; i <= 50; i++ {
tasks.update(id, installer.Update{Kind: installer.KindProgress, Done: int64(i), Total: 100, Percent: float64(i)})
}
tasks.update(id, installer.Update{Kind: installer.KindProgress, Done: 100, Total: 100, Percent: 100})
tasks.finish(id, "C:/x.img", nil)
// start, step, first progress (step unchanged so throttled after), 100%, done.
if n := len(*sent); n < 4 || n > 6 {
t.Fatalf("sent %d events: %+v", n, *sent)
}
last := (*sent)[len(*sent)-1]
if last.Status != TaskDone || last.Result != "C:/x.img" || last.Percent != 100 || last.Cancelable {
t.Fatalf("final = %+v", last)
}
if got := tasks.list(); len(got) != 1 || got[0].ID != id {
t.Fatalf("list = %+v", got)
}
tasks.dismiss(id)
if len(tasks.list()) != 0 {
t.Fatal("dismissed task still listed")
}
}
func TestTaskCancelAndFailure(t *testing.T) {
newTestTasks(t)
ctx, cancel := context.WithCancel(context.Background())
id := tasks.start("installer-download", "tahoe", "", cancel)
if err := tasks.cancel(id); err != nil {
t.Fatal(err)
}
if ctx.Err() == nil {
t.Fatal("cancel did not reach the context")
}
tasks.finish(id, "", fmt.Errorf("reims-util: %w", context.Canceled))
if got := tasks.list()[0]; got.Status != TaskCanceled {
t.Fatalf("status = %s", got.Status)
}
id2 := tasks.start("vm-create", "m1", "m1", nil)
if err := tasks.cancel(id2); err == nil {
t.Fatal("a create can't be cancelled")
}
tasks.finish(id2, "", errors.New("disk full"))
for _, x := range tasks.list() {
if x.ID == id2 && (x.Status != TaskFailed || x.Error != "disk full") {
t.Fatalf("failed task = %+v", x)
}
}
}
func TestCtlTaskWriterRoutesProgressToTheTask(t *testing.T) {
sent := newTestTasks(t)
a := &App{} // no Wails context: emitLog is a no-op
_, w, finish := a.ctlTaskWriter("vm-create", "macos-tahoe", nil)
fmt.Fprintln(w, "macos-tahoe: creating the disk")
for p := 0; p <= 100; p += 5 {
fmt.Fprintf(w, "macos-tahoe: downloading OpenCore 1.0.8: %d%% of 11 MB\n", p)
}
finish(nil)
var steps []string
for _, x := range *sent {
if len(steps) == 0 || steps[len(steps)-1] != x.Step {
steps = append(steps, x.Step)
}
}
want := []string{"", "creating the disk", "downloading OpenCore 1.0.8"}
if fmt.Sprint(steps) != fmt.Sprint(want) {
t.Fatalf("steps = %q, want %q", steps, want)
}
final := (*sent)[len(*sent)-1]
if final.Status != TaskDone || final.Total != 11_000_000 || final.Percent != 100 || final.Subject != "macos-tahoe" {
t.Fatalf("final = %+v", final)
}
}
// dismiss drops a finished task, so one test does not see another's.
func (m *taskManager) dismiss(id string) {
m.mu.Lock()
if e, ok := m.tasks[id]; ok && e.task.Status != TaskRunning {
delete(m.tasks, id)
}
m.mu.Unlock()
}