mirror of
https://github.com/MengMengCode/VoCat.git
synced 2026-08-13 03:13:43 +08:00
- Added `automatic_tasks.go` and `automatic_tasks_test.go` for backend logic and testing of automatic tasks. - Created `automatic_tasks_test.go` to validate task claiming and deletion behavior. - Developed `AutomaticTasksPage.tsx` for frontend management of automatic tasks, including task creation, editing, and execution. - Integrated device and eSIM profile selection for task configuration. - Implemented automatic task scheduling and retry logic in the backend.
73 lines
2.4 KiB
Go
73 lines
2.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAutomaticTasksAreClaimedInDeviceQueueOrderAndAdvanceSchedule(t *testing.T) {
|
|
ctx := context.Background()
|
|
database := openTestStore(t, filepath.Join(t.TempDir(), "automatic-tasks.db"))
|
|
mustSaveDevice(t, database, "ec20", "EC20")
|
|
now := time.Now().UTC().Truncate(time.Second)
|
|
for index := 0; index < 2; index++ {
|
|
payload, _ := json.Marshal(map[string]any{"phone": "10086", "message": "test"})
|
|
if _, err := database.SaveAutomaticTask(ctx, AutomaticTask{
|
|
Name: "task", Enabled: true, DeviceID: "ec20", ProfileICCID: "8944100000000000000",
|
|
TaskType: "sms", Environment: "vowifi", IntervalDays: 2,
|
|
StartDate: "2026-08-10", RunTime: "12:00", Timezone: "Asia/Shanghai", Payload: payload,
|
|
NextRunAt: now.Add(time.Duration(index-2) * time.Minute),
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
runs, err := database.ClaimDueAutomaticTasks(ctx, now, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(runs) != 2 || runs[0].DeviceID != "ec20" || runs[1].DeviceID != "ec20" || runs[0].TaskID >= runs[1].TaskID {
|
|
t.Fatalf("claimed runs = %+v", runs)
|
|
}
|
|
tasks, err := database.ListAutomaticTasks(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, task := range tasks {
|
|
if !task.NextRunAt.After(now) {
|
|
t.Fatalf("task %d next run was not advanced: %v", task.ID, task.NextRunAt)
|
|
}
|
|
}
|
|
second, err := database.ClaimDueAutomaticTasks(ctx, now, 10)
|
|
if err != nil || len(second) != 0 {
|
|
t.Fatalf("same schedule claimed twice: %+v, %v", second, err)
|
|
}
|
|
}
|
|
|
|
func TestDeletingAutomaticTaskRemovesRunHistory(t *testing.T) {
|
|
ctx := context.Background()
|
|
database := openTestStore(t, filepath.Join(t.TempDir(), "automatic-task-delete.db"))
|
|
mustSaveDevice(t, database, "ec20", "EC20")
|
|
task, err := database.SaveAutomaticTask(ctx, AutomaticTask{
|
|
Name: "task", Enabled: true, DeviceID: "ec20", ProfileICCID: "one",
|
|
TaskType: "call", Environment: "cellular", IntervalDays: 1,
|
|
StartDate: "2026-08-10", RunTime: "12:00", Timezone: "Asia/Shanghai", Payload: []byte(`{"phone":"10086","duration_seconds":10}`),
|
|
NextRunAt: time.Now().Add(time.Hour),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := database.QueueAutomaticTaskNow(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := database.DeleteAutomaticTask(ctx, task.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runs, err := database.ListAutomaticTaskRuns(ctx, 10)
|
|
if err != nil || len(runs) != 0 {
|
|
t.Fatalf("orphan runs = %+v, %v", runs, err)
|
|
}
|
|
}
|