diff --git a/.github/scripts/test-windows.ps1 b/.github/scripts/test-windows.ps1 index d8ee57d..3d67532 100644 --- a/.github/scripts/test-windows.ps1 +++ b/.github/scripts/test-windows.ps1 @@ -75,6 +75,7 @@ $required = @( 'TestAnalyzeRefusalsDoNotWriteState', 'TestAnalyzeDoesNotRedirectAnUnreadableExactGame', 'TestPlayNowCutsAMaxSearchShort', + 'TestPlayNowIsOfferedOnTheSmallestTerminal', 'TestReplayAnalysisFromTheMenu', 'TestStudyNoteSurvivesLeavingAndResizing' ) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a8e61e..b0667fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,19 @@ project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed + +- In a terminal too small for the panel, `esc play now` and the search's time + and completed depth were hidden for the whole search behind the announcement + of the move just played, until another key was pressed. They now lead the + status line, and the announcement follows them. +- In a terminal too small for the panel, pressing `m` again after a bookmark + change refused because another window had saved first toggled a stored state + that had never been shown. The refusal now says whether the entry is + bookmarked now, at every size. +- The manual described notes attached to a changed record, or kept in a newer + or damaged file, as read-only; they are not shown at all. + ## [0.8.0] - 2026-09-28 ### Added diff --git a/docs/MANUAL.md b/docs/MANUAL.md index 0848b1e..627f40b 100644 --- a/docs/MANUAL.md +++ b/docs/MANUAL.md @@ -990,11 +990,13 @@ another program keeps them unless you edit them away. Notes are attached to the record's digest as well as to the game ID, and the file is limited to 1 MiB. If the record has changed since the notes were written, or the file was written by a newer twixtui or is damaged, the viewer -says so and the notes are read-only; the file is left exactly as it was, never -moved onto the new record or rewritten. Notes can be saved on finished games -only, so an unfinished imported game can be watched but `m` and `e` explain why -they do nothing. In a terminal with no room for the panel, the status line -gives the reason for a refused `m`, `e` or save in a few words. +does not show those notes — they belong to another record or cannot be read — +and says why instead. They cannot be edited either, so `m` and `e` change +nothing, and the file is left exactly as it was, never moved onto the new +record or rewritten. Notes can be saved on finished games only, so an +unfinished imported game can be watched but `m` and `e` explain why they do +nothing. In a terminal with no room for the panel, the status line gives the +reason for a refused `m`, `e` or save in a few words. Every save is checked against the revision the viewer last read, and a successful save advances it. If another window saved first, the save is refused @@ -1004,10 +1006,15 @@ stored one. Saving again is taken only while the whole stored note is on screen. In a terminal too small to show it — with no panel, or a panel too short or narrow for all of it — the save is refused again and nothing is written until the terminal is enlarged; `esc` still keeps the stored note. A -bookmark refused the same way shows what is stored, and pressing `m` again -decides. A file another program has left at the highest revision there is -still shows its notes but can take no further save: saving is refused as for a -damaged file, and the file is left as it was. +bookmark refused the same way changes nothing either: the viewer shows what is +stored, and the refusal says whether the entry is bookmarked now, on the status +line (`marked elsewhere` or `unmarked elsewhere`) as well as in the panel, so +pressing `m` again is a decision about the state you have read. In a terminal +too small to draw the board, where the refusal cannot be shown, `m` on that +entry does nothing until the terminal is enlarged. A file another program has +left at the highest revision there is still shows its notes but can take no +further save: saving is refused as for a damaged file, and the file is left as +it was. ## Position analysis diff --git a/internal/app/gamescreen.go b/internal/app/gamescreen.go index 6c2aa31..3a65a42 100644 --- a/internal/app/gamescreen.go +++ b/internal/app/gamescreen.go @@ -1464,11 +1464,13 @@ func (s *gameScreen) lastSearchForms() []string { } } -// fitMessage is the message as a status line width cells wide can show it. -// The finished search's figures are the one message with shorter forms of its -// own, and the forms stand only while the message is still the sentence they -// were made with: whatever writes the message next replaces the sentence, and -// so retires the forms without having to know they exist. +// fitMessage is the message as width cells of the status line can show it: the +// whole line, or what is left of it once the play-now key and the search have +// led a line with no panel. The finished search's figures are the one message +// with shorter forms of its own, and the forms stand only while the message is +// still the sentence they were made with: whatever writes the message next +// replaces the sentence, and so retires the forms without having to know they +// exist. func (s *gameScreen) fitMessage(width int) string { if len(s.figures) == 0 || s.message != s.figures[0] { return s.message @@ -2584,12 +2586,34 @@ func (s *gameScreen) openingPeg() string { // because there is nowhere else for that to go, and advice once it has been // asked for, which leads the line ahead of the turn. While the engine searches, // the key that cuts the search short and how far the search has got lead even -// that. +// that, and on a line with no panel they lead the answer to a keypress too. func (s *gameScreen) statusLine(arr ui.Arrangement) string { + phase := s.keyPhase() + playNow := "" + if s.actionAvailable(phase, gaPlayNow, ui.ActNone) { + playNow = s.gameKeyLabel(gaPlayNow) + " play now" + } if s.message != "" { - return s.style(s.styles.Status, gsTruncate(s.fitMessage(arr.Width), arr.Width)) + if playNow == "" || arr.Panel != ui.PanelNone { + return s.style(s.styles.Status, gsTruncate(s.fitMessage(arr.Width), arr.Width)) + } + // The move that starts a search is announced here, and the + // announcement stands until the next key, which a player waiting for + // the engine has no reason to press. Given the whole line, it hid the + // key that cuts the search short, and the search, for as long as the + // search ran, in the one layout where this line is the only place + // either can be seen. So they lead a message as they lead the line + // without one, and the message has the width they leave: it is what + // is cut, or dropped whole, when there is not room for everything. + // Nothing is cleared, so once play now is no longer offered the + // message has the line to itself again. With a panel the thinking + // line shows the search and the help lists the key, and a message + // keeps the line as it always has. + parts := s.searchLead(playNow, arr.Width) + room := arr.Width - ansi.StringWidth(strings.Join(parts, " · ")+" · ") + parts = append(parts, s.fitMessage(room)) + return s.style(s.styles.Status, fitLead(parts, len(parts)-1, arr.Width)) } - phase := s.keyPhase() if phase == phasePlay && s.hint.shown && s.notice == "" { // A short panel, or the one-turn swap notice above it, can hide the // advice. Keep its complete coordinate and policy on the status line @@ -2597,12 +2621,8 @@ func (s *gameScreen) statusLine(arr ui.Arrangement) string { return s.style(s.styles.Status, s.hint.statusText(arr.Width)) } var parts []string - playNow := "" - if s.actionAvailable(phase, gaPlayNow, ui.ActNone) { - playNow = s.gameKeyLabel(gaPlayNow) + " play now" - } // lead counts the parts at the front of the line that have to reach the - // screen whole; see the end of this function. + // screen whole; see fitLead. lead := 0 if arr.Panel == ui.PanelNone { if s.notice != "" { @@ -2612,14 +2632,9 @@ func (s *gameScreen) statusLine(arr ui.Arrangement) string { // With no panel this line is the only place the search and the // key that cuts it short can be seen. The headline used to come // first, and at the minimum width it filled the line and cut both - // off. The key leads now, then the search, cut down to the room - // the key leaves, and the headline has whatever width is left. - room := arr.Width - if playNow != "" { - parts = append(parts, playNow) - room -= ansi.StringWidth(playNow + " · ") - } - parts = append(parts, s.searchProgressShort(room)) + // off. The key leads now, then the search, and the headline has + // whatever width is left. + parts = s.searchLead(playNow, arr.Width) lead = len(parts) } parts = append(parts, gsPlain(s.headlineText())) @@ -2677,20 +2692,38 @@ func (s *gameScreen) statusLine(arr ui.Arrangement) string { } parts = append(parts, s.gameKeyLabel(gaDraw)+" draw", s.gameKeyLabel(gaResign)+" resign") } + return s.style(s.styles.Status, fitLead(parts, lead, arr.Width)) +} + +// searchLead is the front of the status line of a layout with no panel while +// the engine searches: the play-now key while it is offered, then how far the +// search has got, cut down to the room the key leaves. +func (s *gameScreen) searchLead(playNow string, width int) []string { + room := width + var lead []string + if playNow != "" { + lead = append(lead, playNow) + room -= ansi.StringWidth(playNow + " · ") + } + return append(lead, s.searchProgressShort(room)) +} + +// fitLead joins a status line's parts and cuts the line to width, with the +// first lead of them, which have to reach the screen whole, kept whole. A cut +// line ends in a mark, and ui.Compose shortens such a line to the last item it +// can be sure is whole: an item with the mark straight after it may have been +// cut through, so it goes. Where the width leaves no cell past the lead for the +// separator after it, the mark lands straight after the search's figures, and +// they would go. The lead is shown alone then, unmarked: the figures are worth +// more than a mark saying that the rest of the line did not fit. +func fitLead(parts []string, lead, width int) string { line := strings.Join(parts, " · ") - if lead > 0 && ansi.StringWidth(line) > arr.Width { - // A cut line ends in a mark, and ui.Compose shortens such a line to - // the last item it can be sure is whole: an item with the mark - // straight after it may have been cut through, so it goes. Where the - // width leaves no cell past the lead for the separator after it, the - // mark lands straight after the search's figures, and they would go. - // The lead is shown alone then, unmarked: the figures are worth more - // than a mark saying that the headline and the other keys did not fit. - if head := strings.Join(parts[:lead], " · "); ansi.StringWidth(head)+2 > arr.Width { + if lead > 0 && ansi.StringWidth(line) > width { + if head := strings.Join(parts[:lead], " · "); ansi.StringWidth(head)+2 > width { line = head } } - return s.style(s.styles.Status, gsTruncate(line, arr.Width)) + return gsTruncate(line, width) } // quitHint is the terse form of the quit key for the status line. A game opened diff --git a/internal/app/gamescreen_progress_test.go b/internal/app/gamescreen_progress_test.go index 6dc9426..a57ec28 100644 --- a/internal/app/gamescreen_progress_test.go +++ b/internal/app/gamescreen_progress_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "regexp" + "slices" "strings" "sync" "testing" @@ -15,6 +16,7 @@ import ( "github.com/BAKocska/twixtui/internal/bot" "github.com/BAKocska/twixtui/internal/game" "github.com/BAKocska/twixtui/internal/gamestore" + "github.com/BAKocska/twixtui/internal/ui" ) // --- a search the test runs by hand ----------------------------------------- @@ -241,6 +243,17 @@ func gpClaimsDepth(frame string, depth int) bool { // included. var gpDepthClaim = regexp.MustCompile(`\bdepth \d`) +// gpCompactDepth2 is the completed depth 2 in the short form a status line +// with no panel gives it. +var gpCompactDepth2 = regexp.MustCompile(`\bd2\b`) + +// gpBottom is the bottom row of a composed frame, where ui.Compose pins the +// status line, as the player reads it. +func gpBottom(frame string) string { + lines := strings.Split(frame, "\n") + return ansi.Strip(lines[len(lines)-1]) +} + // --- the thinking line -------------------------------------------------------- // TestTheThinkingLineShowsOnlyThisSearchsFinishedWork reads the engine while @@ -385,8 +398,10 @@ func TestPlayNowAndTheSearchShowAtEverySize(t *testing.T) { h := newGSHarness(t, gsTestDeps(t), gsVersusBot(6, engine), size[0], size[1]) h.playTurn(game.Point{Col: 1, Row: 0}) gpEntered(t, engine) - // The commit's own line answers the key that made it, and holds - // the status line until the next key. + // The commit's own line answers the key that made it, and where + // there is a panel it holds the status line until the next key. + // TestPlayNowLeadsTheMovesAnnouncementWithoutAPanel looks before + // any such key. h.press("j") key := h.s.gameKeyLabel(gaPlayNow) + " play now" @@ -410,6 +425,87 @@ func TestPlayNowAndTheSearchShowAtEverySize(t *testing.T) { } } +// TestPlayNowLeadsTheMovesAnnouncementWithoutAPanel commits a move from the +// keyboard and then only lets the search run. Nothing else is pressed, so the +// commit's announcement stays the status line's message for the whole search, +// as it does for a player who sits back and waits for the engine. Without a +// panel that line is the only place for the play-now key and the search, and +// the announcement used to take all of it: neither was on screen until some +// other key cleared the message. TestPlayNowAndTheSearchShowAtEverySize +// presses such a key before it looks, which is why it never saw this. With a +// panel the announcement keeps the line to itself, as it always has, and once +// the move arrives its own announcement takes the line at every size. +// +// Both boards are played because the layouts differ: at 40x12 the six-hole +// board leaves room for a panel below it and the full-size board does not. +// 70x12 is added to the supported sizes as a line with no panel beside the +// full-size board that is wide enough for the announcement to follow the key +// and the search whole, which is where it can be seen not to have been lost. +func TestPlayNowLeadsTheMovesAnnouncementWithoutAPanel(t *testing.T) { + sizes := append(slices.Clone(shellSizes), [2]int{70, 12}) + var panelless, paneled, followed int + for _, n := range []int{6, 24} { + for _, size := range sizes { + w, ht := size[0], size[1] + panel := ui.Arrange(w, ht, n).Panel + if panel == ui.PanelNone { + panelless++ + } else { + paneled++ + } + t.Run(fmt.Sprintf("board %d at %dx%d", n, w, ht), func(t *testing.T) { + engine := newGPBot(t) + h := newGSHarness(t, gsTestDeps(t), gsVersusBot(n, engine), w, ht) + h.playTurn(game.Point{Col: 1, Row: 0}) + gpEntered(t, engine) + key := h.s.gameKeyLabel(gaPlayNow) + " play now" + announced := h.s.toMoveText() + + gpTick(t, h, engine.started.Add(1500*time.Millisecond)) + bottom := gpBottom(h.frame()) + if panel == ui.PanelNone { + if !strings.HasPrefix(bottom, key+" · 1.5s") { + t.Errorf("with the move's announcement standing, the bottom row %q does not lead with %q and the search's time", bottom, key) + } + } else if bottom != announced { + t.Errorf("with a panel, the bottom row %q is not the move's announcement %q alone", bottom, announced) + } + + engine.complete(2, 4321, game.Point{Col: 4, Row: 2}) + gpTick(t, h, engine.started.Add(2500*time.Millisecond)) + frame := h.frame() + bottom = gpBottom(frame) + if panel == ui.PanelNone { + if !strings.HasPrefix(bottom, key) || !gpCompactDepth2.MatchString(bottom) { + t.Errorf("with the move's announcement standing, the bottom row %q does not lead with %q and the completed depth d2", bottom, key) + } + if whole := key + " · 2.5s d2 · " + announced; ansi.StringWidth(whole) <= w { + if bottom != whole { + t.Errorf("the bottom row %q has room for the announcement after the key and the search, want %q", bottom, whole) + } + followed++ + } + } else if bottom != announced { + t.Errorf("with a panel, the bottom row %q is not the move's announcement %q alone", bottom, announced) + } + gsCheckFrame(t, "thinking with the announcement standing", frame, w, ht) + + move := game.Point{Col: 4, Row: 2} + engine.release <- move + h.waitFor("the engine's move", func() bool { return h.s.g.At(move) == game.Horizontal }) + bottom = gpBottom(h.frame()) + if strings.Contains(bottom, key) || !strings.Contains(bottom, "played "+move.String()) { + t.Errorf("once the move has arrived, the bottom row %q is not its announcement", bottom) + } + }) + } + } + if panelless == 0 || paneled == 0 || followed == 0 { + t.Fatalf("the sizes covered %d layouts with no panel, %d with one, and %d with room for the announcement after the search; each needs one", + panelless, paneled, followed) + } +} + // --- play now ----------------------------------------------------------------- // TestPlayNowPlaysTheSearchsOwnMoveOnce presses escape during a search that has diff --git a/internal/app/replay_study.go b/internal/app/replay_study.go index cad52b3..cfeb475 100644 --- a/internal/app/replay_study.go +++ b/internal/app/replay_study.go @@ -56,9 +56,11 @@ type replayNotes struct { } // openNotes finds the study of the game being replayed. Nothing about it stops -// the replay: a study that cannot be read leaves the review as it was and the -// notes read-only, with the reason in the panel, and the file exactly as it was -// found, so the program that can read it still finds it there. +// the replay: a study that cannot be read leaves the review as it was, shows +// none of its notes, since they belong to another record or cannot be read, +// and takes no bookmark or note in their place. The reason is in the panel, and +// the file is left exactly as it was found, so the program that can read it +// still finds it there. func openNotes(store *study.Store, saved gamestore.Saved, record game.Record, entries int) replayNotes { n := replayNotes{ store: store, @@ -156,6 +158,27 @@ func studyRefusal(err error) studyNotice { return studyNotice{replayLabel(why), brief} } +// staleBookmark is the refusal of a bookmark pressed against notes another +// window has saved since. The next press toggles what is stored now, which +// need not be what this window showed when the player pressed, so every form +// of the refusal says what it is: whether the entry is bookmarked. The brief +// form fits the narrowest status line whole, because at the sizes without a +// panel that line is the only place it can be read. +func staleBookmark(marked bool) studyNotice { + if marked { + return studyNotice{ + "the notes were changed elsewhere and are shown as they are now: " + + "this entry is bookmarked, and pressing again unmarks it", + "marked elsewhere", + }.of("bookmark unchanged") + } + return studyNotice{ + "the notes were changed elsewhere and are shown as they are now: " + + "this entry is not bookmarked, and pressing again marks it", + "unmarked elsewhere", + }.of("bookmark unchanged") +} + // refuse records what a request on entry ran into. func (n *replayNotes) refuse(entry int, why studyNotice) { n.problem, n.problemAt = why, entry } @@ -181,6 +204,17 @@ func (n *replayNotes) flags(entry int) string { // toggleBookmark puts a bookmark on the entry on screen, or takes it off, and // saves at once: a bookmark is one keypress, and a second one to confirm it // would be a form around a single bit. +// +// A press refused because the notes changed elsewhere is where that bit needs +// care. The next press toggles what is stored now, and the other window may +// have made it what this press was about to, so a player who presses again +// without being told undoes the change they meant to make. The refusal says +// whether the entry is bookmarked now (staleBookmark), and a press on an entry +// is not taken while the refusal standing on it is off screen, so the next one +// is a decision about the state the player has read. A stale note needs a +// measure of the panel for the same guarantee (storedOnScreen); a bookmark's +// state is a word, which the status line carries whole in every frame that has +// a board, so only a terminal too small for one keeps it off screen. func (s *ReplayScreen) toggleBookmark() { n := &s.notes entry := s.step() @@ -188,6 +222,11 @@ func (s *ReplayScreen) toggleBookmark() { n.refuse(entry, n.locked.of("bookmark unchanged")) return } + if n.problem != nil && n.problemAt == entry && !s.refusalOnScreen() { + // Nothing is written, and the refusal stands until the terminal is + // large enough for it to be read. + return + } // The note travels with the bookmark because a mark is saved whole. It is // the note of the revision the write is made against, so a note saved // elsewhere since cannot be put back to what this screen remembered: that @@ -198,13 +237,10 @@ func (s *ReplayScreen) toggleBookmark() { switch { case errors.Is(err, study.ErrStale): // The press is not repeated on the player's behalf. What is stored - // now is shown, and the player decides whether it still wants the - // change. + // now is shown, the refusal says whether this entry is bookmarked in + // it, and the player decides whether it still wants the change. n.doc = doc - n.refuse(entry, studyNotice{ - "the notes were changed elsewhere and are shown as they are now; press again to change it", - "changed elsewhere", - }.of("bookmark unchanged")) + n.refuse(entry, staleBookmark(doc.At(entry).Bookmark)) case err != nil: n.refuse(entry, studyRefusal(err).of("bookmark unchanged")) default: @@ -433,6 +469,17 @@ func (s *ReplayScreen) storedOnScreen() bool { return s.notes.storedFits(arr.PanelW, s.studyRoom(arr.PanelW, arr.PanelH)) } +// refusalOnScreen reports whether the frame the terminal's size gives draws +// the refusal standing on the entry on screen. The status line of every frame +// that has a board draws it first, ahead of everything a narrow line gives up, +// and whole, since every brief form fits the narrowest such line; a terminal +// too small for a board is drawn as the notice saying so and nothing else. +// Like storedOnScreen, it is judged on the size the screen was last given, so +// a press is judged on the frame the player is looking at. +func (s *ReplayScreen) refusalOnScreen() bool { + return !ui.Arrange(s.width, s.height, s.current().Size()).TooSmall +} + // noteInput is the note input as the status line draws it. At the widths that // have no room for a panel it is the whole of the interface, as it is for the // entry input, so the field is drawn here too, after what the input says about diff --git a/internal/app/replay_study_test.go b/internal/app/replay_study_test.go index c9a19c3..5a74999 100644 --- a/internal/app/replay_study_test.go +++ b/internal/app/replay_study_test.go @@ -381,6 +381,95 @@ func TestReplayStaleSaveKeepsBothVersions(t *testing.T) { } } +// TestReplayStaleBookmarkIsToggledOnlyOnceShown is a bookmark pressed in a +// window that another window has saved past, at the sizes a player may press +// it in. The press is refused, and the next one toggles what is stored now, +// which the other window may have made what this press was about to: a player +// who presses again without being told undoes the change they meant to make. +// So the frame after the refusal has to say whether the entry is bookmarked +// now, and the next press has to make it the other. Both directions are tried +// — stored bookmarked after this window saw the entry bare, and stored bare +// after it saw a bookmark — at each supported size, the smallest of which has +// no panel, only the status line to say it on. A terminal too small for a +// board says nothing at all, so there the next press writes nothing, and the +// press after the terminal is enlarged is the decision. +func TestReplayStaleBookmarkIsToggledOnlyOnceShown(t *testing.T) { + sv := rpConnectionRecord(t) + target := snTarget(t, sv) + // stated is whether a frame says the entry is bookmarked, and whether it + // says either. The words are read whole, since one is inside the other, + // and a frame that says both has said neither. + stated := func(frame string) (marked, said bool) { + words := strings.FieldsFunc(ansi.Strip(frame), func(r rune) bool { return !unicode.IsLetter(r) }) + marked = slices.Contains(words, "marked") + return marked, marked != slices.Contains(words, "unmarked") + } + tooSmall := [2]int{ui.MinWidth - 1, ui.MinHeight} + for _, size := range append(slices.Clone(shellSizes), tooSmall) { + for _, stored := range []bool{true, false} { + w, h := size[0], size[1] + d := snDeps(t) + path := snFile(d, sv.ID) + other := study.Open(d.ConfigDir) + // This window reads the entry as the opposite of what the other + // one then stores, so its press is the one the other has made. + base := int64(0) + if !stored { + doc, err := other.Set(target, 0, study.Mark{Entry: 9, Bookmark: true}) + if err != nil { + t.Fatal(err) + } + base = doc.Revision + } + s := rpSized(t, d, sv, w, h) + rpJump(t, s, "9") + theirs, err := other.Set(target, base, study.Mark{Entry: 9, Bookmark: stored}) + if err != nil { + t.Fatal(err) + } + before := snBytes(t, path) + + s.Update(tutorialKeyMsg("m")) + if after := snBytes(t, path); !bytes.Equal(after, before) { + t.Fatalf("at %dx%d a stale bookmark rewrote the study:\nbefore: %s\nafter: %s", w, h, before, after) + } + frame := s.View().Content + shellAssertFits(t, "a bookmark refused as stale", frame, w, h) + if ui.Arrange(w, h, s.current().Size()).TooSmall { + // The frame is the notice that the terminal is too small, so + // pressing again is not a decision about anything on it. + s.Update(tutorialKeyMsg("m")) + if after := snBytes(t, path); !bytes.Equal(after, before) { + t.Errorf("at %dx%d only the too-small notice is drawn, and a second m toggled the bookmark stored elsewhere:\n%s", + w, h, after) + continue + } + w, h = shellSizes[0][0], shellSizes[0][1] + s.Update(tea.WindowSizeMsg{Width: w, Height: h}) + frame = s.View().Content + } + shown, said := stated(frame) + if !said || shown != stored { + t.Errorf("at %dx%d entry 9 is stored bookmarked=%v, and after the refused m the frame says bookmarked=%v, said=%v:\n%s", + w, h, stored, shown, said, frame) + continue + } + + // Pressing again is a decision about what was shown, made against + // the revision the other window stored. + s.Update(tutorialKeyMsg("m")) + var want []study.Mark + if !shown { + want = []study.Mark{{Entry: 9, Bookmark: true}} + } + if doc := snLoad(t, d, target); !slices.Equal(doc.Marks, want) || doc.Revision != theirs.Revision+1 { + t.Errorf("at %dx%d the frame said bookmarked=%v, and m left the study at revision %d holding %+v, want %+v at revision %d", + w, h, shown, doc.Revision, doc.Marks, want, theirs.Revision+1) + } + } + } +} + // TestReplayStaleNoteIsReplacedOnlyWhenShownWhole is the refused save at the // sizes a player may be reading it in. A second confirm is a choice between // two versions only while both are on screen, so at each size either every diff --git a/internal/e2e/progress_test.go b/internal/e2e/progress_test.go index 938d33e..63091dd 100644 --- a/internal/e2e/progress_test.go +++ b/internal/e2e/progress_test.go @@ -21,6 +21,13 @@ var ( // playedEarly is the announcement of a move play now cut short, in both // its forms: from a completed iteration, or before one had finished. playedEarly = regexp.MustCompile(`played ([A-Z]{1,2}\d{1,2}) early, (?:from its completed depth (\d+)|before any depth was complete)`) + // playNowLead is the front of the status line of a terminal with no panel + // while the engine searches: the play-now key, then the search's time or, + // once an iteration has finished and the two do not fit, its depth alone. + playNowLead = regexp.MustCompile(`^esc play now · (?:\d+\.\ds|d\d+)\b`) + // playedMove is the engine's move as its announcement names it, which at + // the smallest width is all of the announcement that fits. + playedMove = regexp.MustCompile(`played ([A-Z]{1,2}\d{1,2})\b`) ) // TestPlayNowCutsAMaxSearchShort plays against the max engine on a full-size @@ -157,3 +164,107 @@ func TestPlayNowCutsAMaxSearchShort(t *testing.T) { t.Errorf("the stored reply is %v at %s, want horizontal at %s", reply.Player, reply.Peg, move) } } + +// TestPlayNowIsOfferedOnTheSmallestTerminal commits a move against the max +// engine at the minimum size, where there is no panel and the status line is +// the only place the play-now key can be seen, and presses nothing else. The +// commit's announcement stands until the next key, and it used to take the +// whole line: the key and the search stayed hidden for as long as the engine +// searched, which is exactly when a player waiting on it has no reason to press +// anything. TestPlayNowCutsAMaxSearchShort plays at 120x40, where the panel +// shows the search and names the key, so it could not see this. The key and +// the search have to lead the bottom row while the engine searches, escape has +// to cut the search short from there, and the move's own announcement has to +// take the row once the move is played. The stored record is the judge of the +// move. +func TestPlayNowIsOfferedOnTheSmallestTerminal(t *testing.T) { + t.Parallel() + cfg := t.TempDir() + tm := sessionIn(t, cfg, 20, 8, "play", "bot", "--tier", "max", "--side", "vertical", "--seed", "9") + + // Positive control: the game is up and waiting for the player. At this + // width the turn line is cut to its first words. + frame := tm.MustWaitFor("vertical to", 20*time.Second) + if !tm.Alive() { + t.Fatalf("the program exited instead of starting the game:\n%s", frame) + } + frame = tm.WaitSettled(10 * time.Second) + if status := lbStatusLine(frame); strings.Contains(status, "play now") { + t.Fatalf("play now is offered before the engine has anything to play: %q\n%s", status, frame) + } + + // The cursor starts on the centre hole: place there and commit, and press + // nothing else until play now shows. + tm.SendKeys("Space") + tm.WaitChanged(frame, 10*time.Second) + committed := time.Now() + tm.SendKeys("Enter") + for { + screen := tm.Capture() + if playNowLead.MatchString(lbStatusLine(screen)) { + break + } + if playedMove.MatchString(screen) { + t.Fatalf("the engine moved before the bottom row offered play now:\n%s", screen) + } + // The engine still has most of its budget left by then, so the + // search it was on is still running. + if time.Since(committed) > maxBudget*2/5 { + t.Fatalf("%s after the commit, with no other key pressed, the bottom row does not lead with play now and the search:\n%s", + maxBudget*2/5, screen) + } + time.Sleep(pollInterval) + } + tm.AssertFits() + + pressed := time.Now() + tm.SendKeys("Escape") + for { + screen := tm.Capture() + if playedMove.MatchString(lbStatusLine(screen)) { + break + } + if time.Since(pressed) > maxBudget/2 { + t.Fatalf("the engine had not played %s after play now was pressed:\n%s", maxBudget/2, screen) + } + time.Sleep(pollInterval) + } + // The move is read from a settled frame: the first frame it shows in can + // be one the terminal has only part of, and play now must not still be + // offered on the finished one. The move is also waited for before the next + // key, since escape and a key sent straight after it can arrive as one + // alt-modified key. + after := tm.WaitSettled(10 * time.Second) + status := lbStatusLine(after) + found := playedMove.FindStringSubmatch(status) + if found == nil || strings.Contains(status, "play now") { + t.Fatalf("once the engine has moved, the bottom row %q is not its announcement:\n%s", status, after) + } + move := found[1] + tm.AssertFits() + + tm.SendKeys("q") + if code, exited := tm.WaitExit(20 * time.Second); !exited || code != 0 { + t.Fatalf("the program did not leave cleanly: exited=%v status=%d\n%s", exited, code, tm.Capture()) + } + + store, err := gamestore.Open(cfg) + if err != nil { + t.Fatal(err) + } + saved := store.List() + if len(saved) != 1 { + t.Fatalf("%d games stored, want 1", len(saved)) + } + g, err := saved[0].Game() + if err != nil { + t.Fatalf("the stored game does not replay: %v", err) + } + if g.Entries() != 2 || g.Turn() != game.Vertical { + t.Fatalf("the stored game holds %d entries with %v to move, want the player's move and one reply:\n%s", + g.Entries(), g.Turn(), g) + } + if reply := g.History()[1]; reply.Player != game.Horizontal || reply.Peg.String() != move { + t.Errorf("the stored reply is %v at %s, want horizontal at %s", reply.Player, reply.Peg, move) + } +}