Merge branch 'main' of https://git.ruixininfo.com/wqy/tgk-touch
commit
38bf981652
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestShouldAutoInstallFirefox(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
installed bool
|
||||
markerExists bool
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "missing without marker can install",
|
||||
installed: false,
|
||||
markerExists: false,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "installed must not reinstall even when unusable",
|
||||
installed: true,
|
||||
markerExists: false,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "missing with marker must not reinstall",
|
||||
installed: false,
|
||||
markerExists: true,
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := shouldAutoInstallFirefox(tt.installed, tt.markerExists); got != tt.want {
|
||||
t.Fatalf("shouldAutoInstallFirefox(%v, %v) = %v, want %v", tt.installed, tt.markerExists, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 1.5 MiB |
@ -0,0 +1,138 @@
|
||||
package m9zApi
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"tgk-touch/internal/library/m9z"
|
||||
)
|
||||
|
||||
const automaticModeUseDemoAPI = false
|
||||
|
||||
type automaticModeTaskReq struct {
|
||||
CommUid string `json:"comm_uid"`
|
||||
Mode uint8 `json:"mode"`
|
||||
}
|
||||
|
||||
type automaticModeDemoStore struct {
|
||||
mu sync.RWMutex
|
||||
currentMode uint8
|
||||
startTasks [3]m9z.ControlTask
|
||||
stopTasks [3]m9z.ControlTask
|
||||
middleTasks [3]m9z.MiddleControlTask
|
||||
}
|
||||
|
||||
var automaticModeDemo = newAutomaticModeDemoStore()
|
||||
|
||||
func newAutomaticModeDemoStore() *automaticModeDemoStore {
|
||||
store := &automaticModeDemoStore{}
|
||||
for mode := uint8(0); mode < 3; mode++ {
|
||||
store.startTasks[mode] = buildAutomaticModeDemoTask(6+int(mode), 30, true, uint8(60+mode*10))
|
||||
store.stopTasks[mode] = buildAutomaticModeDemoTask(22, int(mode)*10, false, 0)
|
||||
store.middleTasks[mode] = m9z.MiddleControlTask{
|
||||
ControlTaskList: []m9z.ControlTask{
|
||||
buildAutomaticModeDemoTask(12, int(mode)*10, true, uint8(45+mode*5)),
|
||||
},
|
||||
}
|
||||
}
|
||||
return store
|
||||
}
|
||||
|
||||
func buildAutomaticModeDemoTask(hour int, minute int, turnOn bool, pwm uint8) m9z.ControlTask {
|
||||
loops := make([]m9z.TaskLoop, 10)
|
||||
for i := range loops {
|
||||
loops[i] = m9z.TaskLoop{
|
||||
Index: uint(i),
|
||||
TurnOn: turnOn,
|
||||
Pwm: pwm,
|
||||
}
|
||||
}
|
||||
return m9z.ControlTask{
|
||||
ExecTime: time.Date(2025, 6, 7, hour, minute, 0, 0, time.Local),
|
||||
TimeType: 0,
|
||||
Loops: loops,
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeAutomaticMode(mode uint8) uint8 {
|
||||
if mode > 2 {
|
||||
return 0
|
||||
}
|
||||
return mode
|
||||
}
|
||||
|
||||
func cloneAutomaticControlTask(task m9z.ControlTask) m9z.ControlTask {
|
||||
task.Loops = append([]m9z.TaskLoop(nil), task.Loops...)
|
||||
return task
|
||||
}
|
||||
|
||||
func cloneAutomaticMiddleTask(task m9z.MiddleControlTask) m9z.MiddleControlTask {
|
||||
task.ControlTaskList = append([]m9z.ControlTask(nil), task.ControlTaskList...)
|
||||
for i := range task.ControlTaskList {
|
||||
task.ControlTaskList[i] = cloneAutomaticControlTask(task.ControlTaskList[i])
|
||||
}
|
||||
return task
|
||||
}
|
||||
|
||||
func (s *automaticModeDemoStore) getMode() uint8 {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.currentMode
|
||||
}
|
||||
|
||||
func (s *automaticModeDemoStore) setMode(mode uint8) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.currentMode = normalizeAutomaticMode(mode)
|
||||
}
|
||||
|
||||
func (s *automaticModeDemoStore) getStartTask(mode uint8) m9z.ControlTask {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return cloneAutomaticControlTask(s.startTasks[normalizeAutomaticMode(mode)])
|
||||
}
|
||||
|
||||
func (s *automaticModeDemoStore) getStartTaskProgram(mode uint8) (*m9z.TaskProgram, error) {
|
||||
normalizedMode := normalizeAutomaticMode(mode)
|
||||
return m9z.ControlTaskToTaskProgram(s.getStartTask(normalizedMode), normalizedMode)
|
||||
}
|
||||
|
||||
func (s *automaticModeDemoStore) setStartTask(mode uint8, task m9z.ControlTask) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.startTasks[normalizeAutomaticMode(mode)] = cloneAutomaticControlTask(task)
|
||||
}
|
||||
|
||||
func (s *automaticModeDemoStore) getStopTask(mode uint8) m9z.ControlTask {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return cloneAutomaticControlTask(s.stopTasks[normalizeAutomaticMode(mode)])
|
||||
}
|
||||
|
||||
func (s *automaticModeDemoStore) getStopTaskProgram(mode uint8) (*m9z.TaskProgram, error) {
|
||||
normalizedMode := normalizeAutomaticMode(mode)
|
||||
return m9z.ControlTaskToTaskProgram(s.getStopTask(normalizedMode), normalizedMode)
|
||||
}
|
||||
|
||||
func (s *automaticModeDemoStore) setStopTask(mode uint8, task m9z.ControlTask) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.stopTasks[normalizeAutomaticMode(mode)] = cloneAutomaticControlTask(task)
|
||||
}
|
||||
|
||||
func (s *automaticModeDemoStore) getMiddleTask(mode uint8) m9z.MiddleControlTask {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return cloneAutomaticMiddleTask(s.middleTasks[normalizeAutomaticMode(mode)])
|
||||
}
|
||||
|
||||
func (s *automaticModeDemoStore) getMiddleTaskProgram(mode uint8) (*m9z.TaskProgram, error) {
|
||||
normalizedMode := normalizeAutomaticMode(mode)
|
||||
return m9z.MiddleControlTaskToTaskProgram(s.getMiddleTask(normalizedMode), normalizedMode)
|
||||
}
|
||||
|
||||
func (s *automaticModeDemoStore) setMiddleTask(mode uint8, task m9z.MiddleControlTask) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.middleTasks[normalizeAutomaticMode(mode)] = cloneAutomaticMiddleTask(task)
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
package m9zApi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"tgk-touch/internal/library/m9z"
|
||||
)
|
||||
|
||||
func TestAutomaticModeDemoStoreReturnsCopies(t *testing.T) {
|
||||
store := newAutomaticModeDemoStore()
|
||||
|
||||
start := store.getStartTask(0)
|
||||
if len(start.Loops) == 0 {
|
||||
t.Fatal("demo start task should include loops")
|
||||
}
|
||||
start.Loops[0].TurnOn = !start.Loops[0].TurnOn
|
||||
start.Loops[0].Pwm = 1
|
||||
|
||||
got := store.getStartTask(0)
|
||||
if got.Loops[0].Pwm == 1 {
|
||||
t.Fatal("demo start task loop was mutated through returned value")
|
||||
}
|
||||
|
||||
middle := store.getMiddleTask(0)
|
||||
if len(middle.ControlTaskList) == 0 || len(middle.ControlTaskList[0].Loops) == 0 {
|
||||
t.Fatal("demo middle task should include control tasks and loops")
|
||||
}
|
||||
middle.ControlTaskList[0].Loops[0].Pwm = 1
|
||||
|
||||
gotMiddle := store.getMiddleTask(0)
|
||||
if gotMiddle.ControlTaskList[0].Loops[0].Pwm == 1 {
|
||||
t.Fatal("demo middle task loop was mutated through returned value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeAutomaticMode(t *testing.T) {
|
||||
if got := normalizeAutomaticMode(2); got != 2 {
|
||||
t.Fatalf("normalizeAutomaticMode(2) = %d, want 2", got)
|
||||
}
|
||||
if got := normalizeAutomaticMode(3); got != 0 {
|
||||
t.Fatalf("normalizeAutomaticMode(3) = %d, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomaticModeDemoStoreBuildsTaskPrograms(t *testing.T) {
|
||||
store := newAutomaticModeDemoStore()
|
||||
|
||||
programs := map[string]func(uint8) (*m9z.TaskProgram, error){
|
||||
"start": store.getStartTaskProgram,
|
||||
"middle": store.getMiddleTaskProgram,
|
||||
"stop": store.getStopTaskProgram,
|
||||
}
|
||||
|
||||
for name, build := range programs {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
program, err := build(1)
|
||||
if err != nil {
|
||||
t.Fatalf("build %s demo task program failed: %v", name, err)
|
||||
}
|
||||
if program == nil {
|
||||
t.Fatalf("%s demo task program is nil", name)
|
||||
}
|
||||
if program.Index != 1 {
|
||||
t.Fatalf("%s demo task program index = %d, want 1", name, program.Index)
|
||||
}
|
||||
if len(program.Instructions) == 0 {
|
||||
t.Fatalf("%s demo task program should include instructions", name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue