package installguide import ( "errors" "testing" "time" ) // Walk the same actions as the modeless dialog, including closing the old // process and discovering the newly booted VM before Restart returns. func TestGuideContinuesAcrossBoots(t *testing.T) { dir := t.TempDir() g := Guide{Name: "Tahoe", Kind: Offline, PID: 42} if !g.Resume(dir, "offline-installer.img", true) || g.initial != boot { t.Fatal("missing initial recovery guide") } now := time.Now() s := session{current: g.initial} click := func(id uintptr, want page) { t.Helper() now = now.Add(10 * time.Second) next := s.targetAt(g, id, now) if next != want { t.Fatalf("on %s: got %s, want %s", s.current, next, want) } s.enterAt(next, now) } click(idRight, terminal) click(idRight, command) click(idRight, typingCommand) g.TypeText = func(text string) error { if text != OfflineCommand { t.Fatalf("unexpected command %q", text) } return nil } res := g.perform(s.current, command, "") if res.err != nil || res.next != verifyCommand { t.Fatal(res) } s.enterAt(res.next, now) click(idRight, waitInstall) click(idFold, compact) if s.targetAt(g, 2, now) != compact { t.Fatal("Escape unexpectedly expanded folded compact pill") } click(idFold, waitInstall) if err := g.save(s.current, 0); err != nil { t.Fatal(err) } g.PID++ if !g.Resume(dir, "offline-installer.img", true) || g.initial != waitInstall { t.Fatal("lost install progress on a cold reboot") } click(idRight, confirm) // A repeated click within the Windows double-click interval must stay on confirmation. if s.targetAt(g, idRight, now) != confirm { t.Fatal("double-click immediately confirmed restart") } click(idRight, working) finished := false g.FinishRecovery = func() error { finished = true; return nil } var post Guide g.Restart = func() error { if !finished { t.Fatal("restart before FinishInstall") } old := Guide{Name: g.Name, PID: g.PID} if !old.Resume(dir, "", false) || old.initial != restartPending { t.Fatal("same boot shown post-install instructions") } post = Guide{Name: g.Name, PID: g.PID + 1} if !post.Resume(dir, "", false) || post.initial != setup { t.Fatal("missing post-restart guide") } return nil } if r := g.perform(s.current, confirm, ""); r.err != nil || r.next != "" { t.Fatal(r.err) } g = post s = session{current: g.initial} for _, p := range []page{network, account, localUser, preferences, desktop, done} { click(idRight, p) if err := g.save(p, 0); err != nil { t.Fatal(err) } } if g.Resume(dir, "", false) { t.Fatal("completed guide reappeared on normal boot") } } func TestRecoveryGuideBranches(t *testing.T) { g := Guide{Kind: Online} walk := func(start page, ids []uintptr, want []page) { t.Helper() now := time.Now() s := session{current: start} for i, id := range ids { now = now.Add(10 * time.Second) p := s.targetAt(g, id, now) if p != want[i] { t.Fatalf("on %s: got %s, want %s", s.current, p, want[i]) } s.enterAt(p, now) } } walk(boot, []uintptr{idRight, idRight, idRight, idRight, idRight}, []page{selectDisk, formatDisk, reinstall, waitInstall, confirm}) walk(selectDisk, []uintptr{idExtra}, []page{reinstall}) g.Kind = BuiltIn walk(boot, []uintptr{idRight, idExtra, idExtra}, []page{recoverMenu, mediaChoice, pickImage}) walk(recoverMenu, []uintptr{idExtra, idRight, idRight}, []page{mediaChoice, reinstall, waitInstall}) walk(recoverMenu, []uintptr{idRight, idRight}, []page{confirmRepair, working}) walk(boot, []uintptr{idJump, idLeft}, []page{confirm, boot}) // Escape on confirm after jumping from boot must also return to boot, not waitInstall. walk(boot, []uintptr{idJump, 2}, []page{confirm, boot}) g.FinishRecovery = func() error { return errors.New("eject failed") } g.Restart = func() error { t.Fatal("restarted after failed finish"); return nil } if r := g.perform(working, confirm, ""); r.err == nil { t.Fatal("lost finish error") } // If AttachImage succeeds and Restart fails, offer restartPending instead of mediaChoice. g.AttachImage = func(string) error { return nil } g.Restart = func() error { return errors.New("stop timed out") } if r := g.perform(mounting, confirmImage, "offline-installer.img"); r.err == nil || r.next != restartPending { t.Fatalf("mounting restart failure = %+v, want next=%s with error", r, restartPending) } } func TestGuideKeepsMediaAndVMProgressSeparate(t *testing.T) { dir := t.TempDir() g := Guide{Name: "first", Kind: Online, PID: 33} g.Resume(dir, "recovery.img", true) if err := g.save(formatDisk, 0); err != nil { t.Fatal(err) } g.Resume(dir, "other.img", true) if g.initial != boot { t.Fatal("progress crossed media") } if g.Resume(dir, "", false) { t.Fatal("normal boot inherited recovery instructions") } g.Name = "clone" g.Resume(dir, "recovery.img", true) if g.initial != boot { t.Fatal("clone inherited source disk preparation") } }