package mailfetch import ( "errors" "strings" "testing" ) func TestCreateMailboxIgnoresAlreadyExists(t *testing.T) { c, _ := newFakeClient("A0001 NO [ALREADYEXISTS] Mailbox already exists\r\n") if err := c.CreateMailbox("Delete"); err != nil { t.Fatalf("CreateMailbox() error = %v, want nil for an existing mailbox", err) } } func TestCreateMailboxReturnsOtherError(t *testing.T) { c, _ := newFakeClient("A0001 NO [CANNOT] permission denied\r\n") err := c.CreateMailbox("Delete") if err == nil { t.Fatal("CreateMailbox() error = nil, want failure") } var se *ServerError if !errors.As(err, &se) { t.Fatalf("CreateMailbox() error = %T, want *ServerError", err) } } func TestMoveUsesMoveExtension(t *testing.T) { c, fc := newFakeClient("A0001 OK MOVE completed\r\n") if err := c.Move(5, "Delete"); err != nil { t.Fatalf("Move() error = %v", err) } if !strings.Contains(fc.w.String(), `UID MOVE 5 "Delete"`) { t.Errorf("command = %q, want UID MOVE", fc.w.String()) } } func TestMoveFallsBackToCopyStoreExpunge(t *testing.T) { script := "A0001 NO [BAD] MOVE not supported\r\n" + "A0002 OK COPY completed\r\n" + "A0003 OK STORE completed\r\n" + "A0004 OK EXPUNGE completed\r\n" c, fc := newFakeClient(script) if err := c.Move(9, "Delete"); err != nil { t.Fatalf("Move() fallback error = %v", err) } got := fc.w.String() for _, want := range []string{ `UID MOVE 9 "Delete"`, `UID COPY 9 "Delete"`, `UID STORE 9 +FLAGS (\Deleted)`, "UID EXPUNGE 9", } { if !strings.Contains(got, want) { t.Errorf("writes = %q, missing %q", got, want) } } } func TestStoreFlags(t *testing.T) { c, fc := newFakeClient("A0001 OK STORE completed\r\n") if err := c.StoreFlags(4, "+", `\Seen`); err != nil { t.Fatalf("StoreFlags() error = %v", err) } if !strings.Contains(fc.w.String(), `UID STORE 4 +FLAGS (\Seen)`) { t.Errorf("command = %q", fc.w.String()) } } func TestStoreFlagsRejectsBadOp(t *testing.T) { c, _ := newFakeClient("") if err := c.StoreFlags(4, "*", `\Seen`); err == nil { t.Fatal("StoreFlags() error = nil, want failure") } } func TestSearchGmailID(t *testing.T) { c, fc := newFakeClient("* SEARCH 3 4\r\nA0001 OK SEARCH completed\r\n") uids, err := c.SearchGmailID("1234000000000000000") if err != nil { t.Fatalf("SearchGmailID() error = %v", err) } if len(uids) != 2 || uids[0] != 3 || uids[1] != 4 { t.Fatalf("uids = %v, want [3 4]", uids) } if !strings.Contains(fc.w.String(), "UID SEARCH X-GM-MSGID 1234000000000000000") { t.Errorf("command = %q", fc.w.String()) } } func TestSearchGmailIDRejectsNonNumeric(t *testing.T) { c, _ := newFakeClient("") if _, err := c.SearchGmailID(`1234") DELETE`); err == nil { t.Fatal("SearchGmailID() error = nil, want failure for a non-numeric id") } }