This commit is contained in:
2026-10-19 02:33:10 +07:00
parent e96199afba
commit 6d69bedfbc
11 changed files with 123 additions and 32 deletions
@@ -118,7 +118,7 @@ func normalizePlan(steps []Step, currentStep int) ([]Step, error) {
}
normalized := make([]Step, 0, len(steps))
for _, step := range steps {
if step.Mode <= 0 || step.Mode > 8 {
if !screen_service.ValidMode(step.Mode) {
return nil, ErrInvalidPlan
}
step.Text = strings.TrimSpace(step.Text)
@@ -1,6 +1,9 @@
package screen_plan_service
import "testing"
import (
"errors"
"testing"
)
func TestNormalizePlan(t *testing.T) {
tests := []struct {
@@ -16,6 +19,8 @@ func TestNormalizePlan(t *testing.T) {
{Mode: 7, TimerDurationSeconds: 60},
},
},
{name: "rejects removed intro mode", steps: []Step{{Mode: 3}}, wantErr: true},
{name: "rejects removed conclusion mode", steps: []Step{{Mode: 5}}, wantErr: true},
{
name: "rejects empty text",
steps: []Step{{Mode: 8}},
@@ -36,6 +41,9 @@ func TestNormalizePlan(t *testing.T) {
t.Fatalf("normalizePlan() error = %v, want error: %v", err, tt.wantErr)
}
if tt.wantErr {
if !errors.Is(err, ErrInvalidPlan) {
t.Fatalf("want ErrInvalidPlan, got %v", err)
}
return
}
if steps[0].Text != "Внимание" || steps[1].TimerLabel != "Перерыв" {
@@ -7,6 +7,9 @@ import (
"evening_detective_server/internal/repos/screen_sessions_repo"
)
// ErrInvalidMode indicates an unsupported display mode.
var ErrInvalidMode = errors.New("некорректный режим экрана")
// ErrScreenNotFound indicates that a screen session was not found.
var ErrScreenNotFound = errors.New("экран не найден")
@@ -47,8 +50,21 @@ func (s *ScreenService) ListByGame(ctx context.Context, gameID int) ([]*ScreenSe
return result, nil
}
// ValidMode reports whether mode is a supported display mode.
func ValidMode(mode int32) bool {
switch mode {
case 1, 2, 4, 6, 7, 8:
return true
default:
return false
}
}
// SetMode updates the display mode for a game.
func (s *ScreenService) SetMode(ctx context.Context, gameID int, mode int32) error {
if !ValidMode(mode) {
return ErrInvalidMode
}
return s.repo.SetMode(ctx, gameID, mode)
}
@@ -0,0 +1,16 @@
package screen_service
import (
"context"
"errors"
"testing"
)
func TestSetModeRejectsUnsupportedModes(t *testing.T) {
s := NewScreenService(nil)
for _, mode := range []int32{-1, 0, 3, 5, 9} {
if err := s.SetMode(context.Background(), 1, mode); !errors.Is(err, ErrInvalidMode) {
t.Errorf("SetMode(%d) = %v, want ErrInvalidMode", mode, err)
}
}
}