Question: I search in my IMAP folder Archive/2016 for the term "foo". There are several messages containing this word, but the search (ctrl-shift-f) tells me that there are not messages with this word in the body.

I use "search on server".

This looks like the IMAP server does return the wrong results.

How can I debug this?

Is there a way in thunderbird that I can see the IMAP commands that get send to the server and the responses which I get?

Since the communication is encrypted wireshark does not help :-(

Answer: 
If you suspect the IMAP server is giving you the erroneous output, why don't you test it from command line and see for yourself?

Establisth a TLS connection with the IMAP server:

```
$ openssl s_client -connect imap.server.com:993

```

Log in and go to your inbox:

```
>>a1 LOGIN username password
<<a1 OK Logged in.
>>a2 SELECT INBOX
<<a2 OK [READ-ONLY] Select completed.

```

Search the body of your inbox messages for "foo"

```
>>a3 SEARCH BODY foo
<< [search results]
<<a3 OK Search completed.

```

Refer to [IMAP RFC](https://www.rfc-editor.org/rfc/rfc1730) for additional commands. You didn't specify your OS, but `openssl` builds exist for most of them.
You can setup <https://www.stunnel.org> on your host so it connects to real IMAPS (TLS) server, and then configure thunderbird to make unencrypted IMAP connection to localhost port on which `stunnel` listens. After that you can use wireshark to sniff localhost traffic, and see it unencrypted.