"strings"
"sync"
"testing"
+ "testing/synctest"
"time"
"github.com/stretchr/testify/require"
require.NoError(t, err)
clock.Set(clock.Now().Add(5 * time.Minute))
+
+ schedules.changed <- struct{}{}
+
test.events.WaitFor(t, "idle", 1)
require.Equal(t, 1, server.Calls())
"[Schedule 1] Sunday 2026-11-08 09:00 -03:00, once\nRemind the user to call.\n" + scheduleLateLine + "\n" + scheduleEmptyReply,
"[Schedule 2] Sunday 2026-11-08 10:00 -03:00, repeats 0 * * * *\nTell the user the time.\n" + scheduleLateLine + "\n" + scheduleEmptyReply,
}, texts[len(texts)-2:])
+
require.Equal(t, "2 | Sunday 2026-11-08 21:00 -03:00 | 0 * * * * | Tell the user the time.", schedules.List())
require.Equal(t, "<6>schedule ran id=1 late=11h30m0s\n<6>schedule ran id=2 late=10h30m0s\n", logs.String())
}
schedules, err := openSchedules(filepath.Join(t.TempDir(), scheduleFileName), "OneChat", clock.Now, discardLog())
require.NoError(t, err)
- schedules.check = time.Millisecond
whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
socket, _ := daemonStartTestWithSchedules(t, newModelFake(t), nil, whatsapp, schedules, clock.Now)
client := terminalStartTestClient(t, socket)
require.NoError(t, err)
clock.Set(clock.Now().Add(5 * time.Minute))
+
+ schedules.changed <- struct{}{}
+
client.WaitFor(t, "[Schedule 1] Sunday 2026-11-08 20:05 -03:00, once\nRemind the user to drink water.\n"+scheduleEmptyReply+"\n[working]\na line\n[done]")
}
+func TestSchedulesCheckTheClockAfterItsWaitLimit(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
+ loop, events := newTestIdleTurnLoop(t, whatsapp)
+ clock := &ScheduleTestClock{now: time.Now()}
+ schedules, err := openSchedules(filepath.Join(t.TempDir(), scheduleFileName), "OneChat", clock.Now, discardLog())
+ require.NoError(t, err)
+
+ _, err = schedules.Set("Check the clock.", new(clock.Now().Add(5*time.Minute).Format(scheduleAtLayout)), nil)
+ require.NoError(t, err)
+
+ ctx, cancel := context.WithCancel(t.Context())
+ defer cancel()
+
+ done := make(chan error, 1)
+ go func() {
+ done <- schedules.Run(ctx, loop)
+ }()
+
+ synctest.Wait()
+ require.Zero(t, loop.memory.Len())
+ clock.Set(clock.Now().Add(5 * time.Minute))
+ time.Sleep(scheduleClockWaitTimeout)
+ synctest.Wait()
+ require.Equal(t, 1, loop.memory.Len())
+ require.Contains(t, events.Last("work"), "Check the clock.")
+ cancel()
+ require.NoError(t, <-done)
+ })
+}
+
func TestToolboxRunsTheScheduleTools(t *testing.T) {
schedules, _ := scheduleTestOpen(t)
box := &Toolbox{
}
// scheduleTestRun runs schedules in a new turn loop that calls server, until
-// the test ends. Run looks at the times every millisecond.
+// the test ends.
func scheduleTestRun(t *testing.T, schedules *Schedules, server *ModelFake) *TurnTestLoop {
t.Helper()
- schedules.check = time.Millisecond
test := newTestTurnLoop(t, server)
ctx, cancel := context.WithCancel(t.Context())
done := make(chan error)