]> git.giacomolli.uk - onechat/blobdiff - whatsapp_test.go
Shorten the daemon log lines
[onechat] / whatsapp_test.go
index e0018e5ba2a744d99f6130ed03b269dd366ebfc1..7001ea1145b543d317a986d007e51a4f99e2b18e 100644 (file)
@@ -37,12 +37,38 @@ func TestWhatsAppStatusFollowsTheConnection(t *testing.T) {
 func TestWhatsAppStatusOfAFirstConnectionThatFailed(t *testing.T) {
        dir := t.TempDir()
        whatsappTestSession(t, dir, true)
-       whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
+       whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
        daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
 
        <-fake.connects
        require.True(t, fake.Emit(&events.Disconnected{}))
        require.Equal(t, "WhatsApp is linked and disconnected since 2026-01-01 12:00.", whatsapp.Status())
+       require.Equal(t, "first connect failed, retrying\n", logs.String())
+}
+
+func TestWhatsAppLogsEachChangeOfTheConnectionOnce(t *testing.T) {
+       dir := t.TempDir()
+       whatsappTestSession(t, dir, true)
+       clock := newTestClock()
+       whatsapp, fake, logs := whatsappStartTest(t, dir, clock)
+       daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
+
+       <-fake.connects
+       require.True(t, fake.Emit(&events.Connected{}))
+       clock.now = clock.now.Add(time.Minute)
+       require.True(t, fake.Emit(&events.Disconnected{}))
+       clock.now = clock.now.Add(time.Minute)
+       require.True(t, fake.Emit(&events.Disconnected{}))
+       clock.now = clock.now.Add(25 * time.Second)
+       require.True(t, fake.Emit(&events.Connected{}))
+
+       require.Equal(t, []string{
+               "connected",
+               "disconnected, retrying",
+               "connected again after 1m25s",
+       }, strings.Split(strings.TrimSuffix(logs.String(), "\n"), "\n"))
+       require.NotContains(t, logs.String(), whatsappTestNumber)
+       require.NotContains(t, logs.String(), whatsappTestLID)
 }
 
 func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) {
@@ -134,6 +160,14 @@ func TestWhatsAppPairsWithACode(t *testing.T) {
        items <- whatsmeow.QRChannelSuccess
        asking.WaitFor(t, "WhatsApp is linked to +15550001111.")
 
+       // The library connects again after the link with the context of the
+       // connection, so the end of the pairing must not end it.
+       connections := fake.Connections()
+       require.Len(t, connections, 1)
+       require.Never(t, func() bool {
+               return connections[0].Err() != nil
+       }, 200*time.Millisecond, time.Millisecond)
+
        require.Equal(t, []string{whatsappTestNumber}, fake.Phones())
        require.Equal(t, "WhatsApp is linked and not connected yet.", whatsapp.Status())
        require.Equal(t, "[connected to OneChat]\n", other.Output())
@@ -314,7 +348,7 @@ func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) {
        whatsappTestSession(t, dir, true)
        clock := newTestClock()
        whatsapp, fake, _ := whatsappStartTest(t, dir, clock)
-       whatsapp.noticeDelay = 50 * time.Millisecond
+       whatsapp.noticeDelay = 300 * time.Millisecond
        socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
        client := terminalStartTestClient(t, socket)
        client.WaitFor(t, "[connected to OneChat]")
@@ -323,7 +357,7 @@ func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) {
        require.True(t, fake.Emit(&events.Connected{}))
        require.True(t, fake.Emit(&events.Disconnected{}))
        require.True(t, fake.Emit(&events.Connected{}))
-       time.Sleep(100 * time.Millisecond)
+       time.Sleep(400 * time.Millisecond)
        require.Equal(t, "[connected to OneChat]\n", client.Output())
 
        clock.now = clock.now.Add(time.Hour)
@@ -331,7 +365,7 @@ func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) {
        require.True(t, fake.Emit(&events.Disconnected{}))
        client.WaitFor(t, "WhatsApp is disconnected since 2026-01-01 13:00. OneChat tries again.\n")
        require.True(t, fake.Emit(&events.Disconnected{}))
-       time.Sleep(100 * time.Millisecond)
+       time.Sleep(400 * time.Millisecond)
 
        require.True(t, fake.Emit(&events.Connected{}))
        client.WaitFor(t, "WhatsApp is connected again.\n")
@@ -362,7 +396,7 @@ func TestWhatsAppLogsNoOwnNumber(t *testing.T) {
        require.Eventually(t, func() bool {
                return logs.String() != ""
        }, 10*time.Second, time.Millisecond)
-       require.Equal(t, "connect: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String())
+       require.Equal(t, "connect failed, restart to retry: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String())
 }
 
 func TestWhatsAppPhone(t *testing.T) {
@@ -448,22 +482,8 @@ func TestWhatsAppCommandArgs(t *testing.T) {
        }
 }
 
-// whatsappTestLogTexts returns the texts of the messages in the log of memory,
-// after the log holds at least one message.
-func whatsappTestLogTexts(t *testing.T, memory *Memory) []string {
-       t.Helper()
-
-       require.Eventually(t, func() bool {
-               return memory.Len() > 0
-       }, 10*time.Second, time.Millisecond)
-
-       var texts []string
-       for id := range memory.Len() {
-               message, err := memory.Message(id)
-               require.NoError(t, err)
-
-               texts = append(texts, message.Text)
-       }
-
-       return texts
+func TestSubagentHasNoWhatsAppTools(t *testing.T) {
+       box := &Toolbox{}
+       require.Equal(t, `error: unknown tool "whatsapp_find"`, box.run(t.Context(), "whatsapp_find", `{"name":"ana"}`))
+       require.Equal(t, `error: unknown tool "whatsapp_send"`, box.run(t.Context(), "whatsapp_send", `{"id":"a","chat":"15550002222@s.whatsapp.net","text":"hi"}`))
 }