Files

176 lines
5.1 KiB
Go

// Fenrir console screen reader.
// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
package review
import (
"strings"
"testing"
"git.stormux.org/storm/fenrir/internal/command"
"git.stormux.org/storm/fenrir/internal/screen"
)
func fixture(lines ...string) screen.Snapshot {
const width = 20
s := screen.Snapshot{Terminal: 2, Active: true, Rows: len(lines), Cols: width}
for _, text := range lines {
line := []rune(text)
s.Cells = append(s.Cells, line...)
s.Cells = append(s.Cells, []rune(strings.Repeat(" ", width-len(line)))...)
}
return s
}
func TestReviewDoesNotMoveApplicationCursor(t *testing.T) {
r := &Review{}
s := fixture("one two", "café 界", "last")
s.X, s.Y = 2, 1
r.Update(s)
if got := r.Execute(command.CurrentWord).Text; got != "café" {
t.Fatal(got)
}
if got := r.Execute(command.PreviousLine).Text; got != "one two" {
t.Fatal(got)
}
if got := r.Execute(command.NextWord).Text; got != "two" {
t.Fatal(got)
}
if got := r.Execute(command.PreviousWord).Text; got != "one" {
t.Fatal(got)
}
if got := r.Execute(command.NextChar).Text; got != "n" {
t.Fatal(got)
}
if r.screen.X != 2 || r.screen.Y != 1 {
t.Fatal("review moved application cursor")
}
s.X, s.Y = 0, 2
r.Update(s)
if r.x != 1 || r.y != 0 {
t.Fatal("review followed application cursor")
}
r.Execute(command.FollowCursor)
if r.x != 0 || r.y != 2 {
t.Fatal("follow did not reset review")
}
if got := r.Execute(command.ReadScreen).Text; got != "one two\ncafé 界\nlast" {
t.Fatal(got)
}
}
func TestReviewBoundariesAndResize(t *testing.T) {
r := &Review{}
r.Update(fixture(" alpha", "omega"))
if got := r.Execute(command.PreviousLine).Text; got != "start of screen" {
t.Fatal(got)
}
if got := r.Execute(command.FirstText).Text; got != "a" {
t.Fatal(got)
}
if got := r.Execute(command.LastText).Text; got != "a" {
t.Fatal(got)
}
r.Execute(command.Bottom)
if got := r.Execute(command.NextLine).Text; got != "end of screen" {
t.Fatal(got)
}
if got := r.Execute(command.Position).Text; !strings.Contains(got, "line 2, column") {
t.Fatal(got)
}
r.Update(fixture("new"))
if got := r.Execute(command.CurrentLine).Text; got != "new" {
t.Fatal(got)
}
r.Update(screen.Snapshot{Terminal: 1})
if got := r.Execute(command.CurrentLine).Text; got != "no text console" {
t.Fatal(got)
}
}
func TestReviewAcrossBlankRowsAndScreenEdges(t *testing.T) {
var r Review
r.Update(fixture("one two", "", "café 界"))
for _, step := range []struct {
id command.ID
text string
}{
{command.CurrentLine, "one two"},
{command.PreviousChar, "start of screen"},
{command.PreviousWord, "start of screen"},
{command.CurrentChar, "o"},
{command.NextWord, "two"},
{command.CurrentWord, "two"},
{command.NextChar, "w"},
{command.PreviousChar, "t"},
{command.NextWord, "café"},
{command.NextWord, "界"},
{command.NextWord, "end of screen"},
{command.PreviousWord, "café"},
{command.NextLine, "end of screen"},
{command.Top, "Top"},
{command.NextLine, "blank"},
{command.CurrentWord, "two"},
{command.LineEnd, "space"},
{command.NextChar, "space"},
{command.Position, "line 2, column 1, Terminal 2"},
{command.Bottom, "Bottom"},
{command.LastText, "界"},
{command.FirstText, "c"},
{command.LineStart, "c"},
{command.FollowCursor, "Exiting Review Mode"},
{command.ReadScreen, "one two\n\ncafé 界"},
} {
if got := r.Execute(step.id).Text; got != step.text {
t.Fatalf("%s: got %q, want %q", step.id, got, step.text)
}
}
}
func TestReviewAvailabilityAndSessionChanges(t *testing.T) {
var r Review
if got := r.Execute(command.NextChar).Text; got != "no text console" || r.Active() {
t.Fatalf("review without a screen: %q, active %v", got, r.Active())
}
s := fixture("alpha", "omega")
r.Update(s)
if got := r.Execute(command.ID("unknown")).Text; got != "unbound" || r.Active() {
t.Fatalf("unknown command: %q, active %v", got, r.Active())
}
r.Execute(command.Bottom)
s.Terminal = 3
if !r.Update(s) || r.Active() {
t.Fatal("VT switch did not reset review")
}
if got := r.Execute(command.CurrentLine).Text; got != "alpha" {
t.Fatal(got)
}
r.Update(screen.Snapshot{Terminal: 1})
if r.Active() {
t.Fatal("graphical VT retained review")
}
s.Y = 1
r.Update(s)
if got := r.Execute(command.CurrentLine).Text; got != "omega" {
t.Fatal("returning from graphics did not follow the application cursor", got)
}
r.Update(fixture(""))
if got := r.Execute(command.ReadScreen).Text; got != "screen is empty" {
t.Fatal(got)
}
}