]> git.giacomolli.uk - onechat/blobdiff - whatsapp_test.go
Move whatsapp_send to its own file
[onechat] / whatsapp_test.go
index e0018e5ba2a744d99f6130ed03b269dd366ebfc1..1ca0a52318d6cf85a8cf141d87773a74013ada0b 100644 (file)
@@ -134,6 +134,14 @@ func TestWhatsAppPairsWithACode(t *testing.T) {
        items <- whatsmeow.QRChannelSuccess
        asking.WaitFor(t, "WhatsApp is linked to +15550001111.")
 
        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())
        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 +322,7 @@ func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) {
        whatsappTestSession(t, dir, true)
        clock := newTestClock()
        whatsapp, fake, _ := whatsappStartTest(t, dir, clock)
        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]")
        socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
        client := terminalStartTestClient(t, socket)
        client.WaitFor(t, "[connected to OneChat]")
@@ -323,7 +331,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{}))
        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)
        require.Equal(t, "[connected to OneChat]\n", client.Output())
 
        clock.now = clock.now.Add(time.Hour)
@@ -331,7 +339,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{}))
        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")
 
        require.True(t, fake.Emit(&events.Connected{}))
        client.WaitFor(t, "WhatsApp is connected again.\n")
@@ -448,22 +456,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"}`))
 }
 }