package db

import (
	"testing"
	"time"
)

func TestCountMessagesByFolder(t *testing.T) {
	d := openTestDB(t)
	const mailboxID = int64(1)

	insert := func(folder, flags string) {
		t.Helper()
		if _, err := d.InsertMessage(mailboxID, folder, "", flags, time.Now(), 10, "/dev/null", []byte("nonce"), "a@example.com", "b@example.com", "subj", ""); err != nil {
			t.Fatal(err)
		}
	}
	insert("INBOX", "")
	insert("INBOX", "")
	insert("INBOX", `\Seen`)
	insert("Sent", `\Seen`)

	totals, err := d.CountMessagesByFolder(mailboxID)
	if err != nil {
		t.Fatal(err)
	}
	if totals["INBOX"] != 3 {
		t.Errorf("INBOX total = %d, want 3", totals["INBOX"])
	}
	if totals["Sent"] != 1 {
		t.Errorf("Sent total = %d, want 1", totals["Sent"])
	}

	unread, err := d.CountUnreadByFolder(mailboxID)
	if err != nil {
		t.Fatal(err)
	}
	if unread["INBOX"] != 2 {
		t.Errorf("INBOX unread = %d, want 2", unread["INBOX"])
	}
	if _, ok := unread["Sent"]; ok {
		t.Errorf("expected Sent to have no unread entry, got %d", unread["Sent"])
	}
}
