123 lines
3.8 KiB
Go
123 lines
3.8 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 config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDocumentPreservesSectionOrderAndPrivateAtomicSave(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "settings.toml")
|
|
if err := os.WriteFile(path, []byte("version=1\n[ 'speech' ]\nrate=40\n[reading]\nautomatic=false\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
doc, err := Open_document(path, Defaults())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(doc.Order, []string{"presentation", "presentation.speech", "reading"}) {
|
|
t.Fatal(doc.Order)
|
|
}
|
|
settings := doc.Settings
|
|
settings.Presentation.Speech.Rate = 41
|
|
if err := doc.Save(settings); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := Open_document(path, Defaults())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loaded.Settings.Presentation.Speech.Rate != 41 || !reflect.DeepEqual(loaded.Order, []string{"presentation", "presentation.speech", "presentation.sound", "presentation.progress", "presentation.time_and_date", "reading", "keyboard", "internal_ui", "review", "screen", "clipboard", "general", "scripts", "remote", "prompt"}) {
|
|
t.Fatal(loaded)
|
|
}
|
|
info, err := os.Stat(path)
|
|
if err != nil || info.Mode().Perm() != 0600 {
|
|
t.Fatal(info, err)
|
|
}
|
|
files, err := filepath.Glob(filepath.Join(filepath.Dir(path), ".settings-*"))
|
|
if err != nil || len(files) != 0 {
|
|
t.Fatal(files, err)
|
|
}
|
|
}
|
|
|
|
func TestDocumentRejectsExternalEditsAndInvalidSettings(t *testing.T) {
|
|
for _, mode := range []string{"changed", "created", "invalid"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "settings.toml")
|
|
if mode != "created" {
|
|
if err := Save(path, Defaults()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
doc, err := Open_document(path, Defaults())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
draft := doc.Settings
|
|
if mode == "invalid" {
|
|
draft.Presentation.Sound.Volume = 2
|
|
} else {
|
|
other := Defaults()
|
|
other.Presentation.Speech.Rate = 3
|
|
if err := Save(path, other); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
before, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := doc.Save(draft); err == nil {
|
|
t.Fatal("unsafe save accepted")
|
|
}
|
|
after, err := os.ReadFile(path)
|
|
if err != nil || string(before) != string(after) {
|
|
t.Fatal("failed save changed file", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDocumentDefaultsAndDottedTables(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "settings.toml")
|
|
doc, err := Open_document(path, Defaults())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := doc.Save(doc.Settings); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte("reading.automatic=false\nspeech = {rate=30}\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
doc, err = Open_document(path, Defaults())
|
|
if err != nil || !reflect.DeepEqual(doc.Order, []string{"reading", "presentation", "presentation.speech"}) {
|
|
t.Fatal(doc, err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(strings.Repeat("#", (1<<20)+1)), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Open_document(path, Defaults()); err == nil {
|
|
t.Fatal("oversize document accepted")
|
|
}
|
|
}
|