X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/56d959f2d573eef1be5c04d0bf1659fa30d36e7c..38a9b0502a1ccefc6ccb3472c4a21805b222caa6:/whatsapp_test.go?ds=inline diff --git a/whatsapp_test.go b/whatsapp_test.go index 38a7b8d..7c893cc 100644 --- a/whatsapp_test.go +++ b/whatsapp_test.go @@ -2,6 +2,7 @@ package main import ( "errors" + "fmt" "net/http" "os" "path/filepath" @@ -485,7 +486,7 @@ func TestWhatsAppPrivateBatchStartsATurnWithAnEmptyReply(t *testing.T) { socket, memory := daemonStartTestWithWhatsApp(t, server, bot, whatsapp) client := terminalStartTestClient(t, socket) client.WaitFor(t, "[connected to OneChat]") - <-fake.connects + whatsappTestConnect(t, fake) at := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) require.True(t, fake.Emit(whatsappTestMessage(whatsappTestAna, whatsappTestAna, "m1", "Are you at home?", at))) @@ -563,7 +564,7 @@ func TestWhatsAppGroupTurns(t *testing.T) { socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp) client := terminalStartTestClient(t, socket) client.WaitFor(t, "[connected to OneChat]") - <-fake.connects + whatsappTestConnect(t, fake) event := whatsappTestMessage(whatsappTestFamily, whatsappTestBruno, "m1", "Who brings the cake?", time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)) event.Info.PushName = "Bruno Lima" @@ -670,3 +671,216 @@ func whatsappStartTestLinked(t *testing.T) (*WhatsApp, *WhatsAppFake) { return whatsapp, fake } + +func TestWhatsAppCatchupGivesOneTurn(t *testing.T) { + whatsapp, fake := whatsappStartTestLinked(t) + server := newModelFake(t) + socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp) + client := terminalStartTestClient(t, socket) + client.WaitFor(t, "[connected to OneChat]") + <-fake.connects + + require.True(t, fake.Emit(&events.Connected{})) + at := time.Date(2026, 1, 1, 8, 0, 0, 0, time.UTC) + for index, event := range []*events.Message{ + whatsappTestMessage(whatsappTestAna, whatsappTestAna, "m1", "Good morning", at), + whatsappTestMessage(whatsappTestBruno, whatsappTestBruno, "m2", "Call me", at.Add(time.Hour)), + whatsappTestMessage(whatsappTestAna, whatsappTestAna, "m3", "Are you there?", at.Add(2*time.Hour)), + } { + event.Info.PushName = []string{"Ana", "Bruno", "Ana"}[index] + require.True(t, fake.Emit(event)) + } + + time.Sleep(100 * time.Millisecond) + require.Zero(t, memory.Len()) + + require.True(t, fake.Emit(&events.OfflineSyncCompleted{})) + client.WaitFor(t, "[done]") + require.Equal(t, []string{ + "[WhatsApp] Ana, private chat 15550002222@s.whatsapp.net, Thursday 2026-01-01\n08:00 Ana: Good morning\n" + whatsappBatchEmptyReply, + "[WhatsApp] Bruno, private chat 15550003333@s.whatsapp.net, Thursday 2026-01-01\n09:00 Bruno: Call me\n" + whatsappBatchEmptyReply, + "[WhatsApp] Ana, private chat 15550002222@s.whatsapp.net, Thursday 2026-01-01\n10:00 Ana: Are you there?\n" + whatsappBatchEmptyReply, + }, whatsappTestLogTexts(t, memory)[:3]) + + time.Sleep(100 * time.Millisecond) + require.Equal(t, 1, server.Calls()) + for _, text := range []string{"Good morning", "Call me", "Are you there?"} { + require.Contains(t, string(server.Call(0).raw), text) + } +} + +func TestWhatsAppCatchupEndsAfterItsLimit(t *testing.T) { + whatsapp, fake := whatsappStartTestLinked(t) + whatsapp.catchupLimit = 100 * time.Millisecond + socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) + client := terminalStartTestClient(t, socket) + client.WaitFor(t, "[connected to OneChat]") + <-fake.connects + + require.True(t, fake.Emit(&events.Connected{})) + require.True(t, fake.Emit(whatsappTestMessage(whatsappTestAna, whatsappTestOwn, "m1", "On my way.", time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)))) + client.WaitFor(t, "12:00 You: On my way.") + require.Len(t, whatsappTestLogTexts(t, memory), 1) +} + +func TestWhatsAppBatchesOfTheStateFileWaitForTheCatchup(t *testing.T) { + dir := t.TempDir() + whatsappTestSession(t, dir, true) + before, _, _ := whatsappStartTest(t, dir, newTestClock()) + _, err := before.batches.Add(whatsappTestChat, WhatsAppMessage{ + ID: "m1", + Sender: "Ana Souza", + Time: time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC), + Text: "Did you see my message?", + }, time.Now().Add(-time.Hour)) + require.NoError(t, err) + + whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock()) + socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) + client := terminalStartTestClient(t, socket) + client.WaitFor(t, "[connected to OneChat]") + <-fake.connects + + time.Sleep(100 * time.Millisecond) + require.Zero(t, memory.Len()) + + require.True(t, fake.Emit(&events.Connected{})) + require.True(t, fake.Emit(&events.OfflineSyncCompleted{})) + client.WaitFor(t, "12:00 Ana Souza: Did you see my message?") + client.WaitFor(t, "[done]") +} + +// whatsappTestConnect waits for the connection of the daemon and gives it the +// events of a connection with an empty catch-up. +func whatsappTestConnect(t *testing.T, fake *WhatsAppFake) { + t.Helper() + + <-fake.connects + require.True(t, fake.Emit(&events.Connected{})) + require.True(t, fake.Emit(&events.OfflineSyncCompleted{})) +} + +func TestWhatsAppFindMatchesNamesWithNoRegardToCaseAndAccents(t *testing.T) { + whatsapp, fake := whatsappStartTestLinked(t) + fake.groups = []*types.GroupInfo{ + { + JID: whatsappTestFamily, + GroupName: types.GroupName{ + Name: "Família Souza", + }, + }, + } + + whatsappTestContacts(t, whatsapp) + daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) + whatsappTestConnect(t, fake) + + testCases := []struct { + name string + query string + answer string + }{ + { + name: "no accent finds an accent", + query: "joao", + answer: "15550004444@s.whatsapp.net | João Silva | contact", + }, + { + name: "a part of a name finds contacts and groups", + query: "SOUZA", + answer: "15550002222@s.whatsapp.net | Ana Souza | contact\n120363000000000001@g.us | Família Souza | group", + }, + { + name: "a business name", + query: "bakery", + answer: "15550005555@s.whatsapp.net | Corner Bakery | contact", + }, + { + name: "no match", + query: "Zed", + answer: "No contact or group has a name with \"Zed\".", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + answer, err := whatsapp.Find(t.Context(), tc.query) + require.NoError(t, err) + require.Equal(t, tc.answer, answer) + }) + } +} + +func TestWhatsAppFindGivesAtMostTwentyMatches(t *testing.T) { + whatsapp, _ := whatsappStartTestLinked(t) + for index := range 25 { + jid := types.NewJID(fmt.Sprintf("1555001%04d", index), types.DefaultUserServer) + require.NoError(t, whatsapp.device.Contacts.PutContactName(t.Context(), jid, "", fmt.Sprintf("Member %02d", index))) + } + + answer, err := whatsapp.Find(t.Context(), "member") + require.NoError(t, err) + + lines := strings.Split(answer, "\n") + require.Len(t, lines, 22) + require.Equal(t, "15550010000@s.whatsapp.net | Member 00 | contact", lines[0]) + require.Equal(t, "5 more: give a longer part of the name.", lines[20]) + require.Equal(t, "The groups are not in this answer, because WhatsApp is disconnected.", lines[21]) +} + +func TestWhatsAppFindBeforeTheNamesArrive(t *testing.T) { + whatsapp, fake := whatsappStartTestLinked(t) + daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) + whatsappTestConnect(t, fake) + + answer, err := whatsapp.Find(t.Context(), "ana") + require.NoError(t, err) + require.Equal(t, whatsappNamesNotReady, answer) +} + +func TestWhatsAppFindWithoutALink(t *testing.T) { + whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), newTestClock()) + + answer, err := whatsapp.Find(t.Context(), "ana") + require.NoError(t, err) + require.Equal(t, whatsappNotLinked, answer) +} + +func TestAgentFindsAWhatsAppContact(t *testing.T) { + whatsapp, fake := whatsappStartTestLinked(t) + whatsappTestContacts(t, whatsapp) + server := newModelFake(t) + server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + if call.number == 0 { + modelWriteEvents(w, modelCompletedEvent([]any{modelFunctionCallItem("fc_1", "call_1", "whatsapp_find", `{"name":"ana"}`)}, 100, 0, 5)) + return + } + + modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("Found Ana.")}, 100, 0, 5)) + } + + socket, _ := daemonStartTestWithWhatsApp(t, server, nil, whatsapp) + client := terminalStartTestClient(t, socket) + whatsappTestConnect(t, fake) + + client.Type("Find Ana on WhatsApp.") + client.WaitFor(t, "[result] 15550002222@s.whatsapp.net | Ana Souza | contact\nFound Ana.\n[done]") +} + +func TestSubagentHasNoWhatsAppTools(t *testing.T) { + box := &Toolbox{} + require.Equal(t, `error: unknown tool "whatsapp_find"`, box.run(t.Context(), "whatsapp_find", `{"name":"ana"}`)) +} + +// whatsappTestContacts puts the contacts of the tests into the session of +// whatsapp: Ana Souza, Joao Silva with an accent, a business, and the user. +func whatsappTestContacts(t *testing.T, whatsapp *WhatsApp) { + t.Helper() + + contacts := whatsapp.device.Contacts + require.NoError(t, contacts.PutContactName(t.Context(), whatsappTestAna, "Ana", "Ana Souza")) + require.NoError(t, contacts.PutContactName(t.Context(), types.NewJID("15550004444", types.DefaultUserServer), "João", "João Silva")) + _, _, err := contacts.PutBusinessName(t.Context(), types.NewJID("15550005555", types.DefaultUserServer), "Corner Bakery") + require.NoError(t, err) + require.NoError(t, contacts.PutContactName(t.Context(), whatsappTestOwn, "Me", "Me Souza")) +}