Add Gmail --query to mail/sync (default in:inbox) (#4)
* Add --query to Gmail mail/sync instead of always listing in:inbox. Callers keep the search string; default remains in:inbox. * Document Gmail --query on the mail/sync pipeline. * test(mail): assert Gmail --query reaches ListIDs, not only the CLI flag. ParseCLI coverage left a hole: an empty query still has to become in:inbox and a custom q has to be the string the client lists with.
This commit is contained in:
@@ -221,6 +221,71 @@ func TestCollectParts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type fakeGmailAPI struct {
|
||||
lastQ string
|
||||
lastLimit int
|
||||
ids []string
|
||||
}
|
||||
|
||||
func (f *fakeGmailAPI) ListIDs(_ context.Context, q string, maxIDs int, _ string) ([]string, string, error) {
|
||||
f.lastQ = q
|
||||
f.lastLimit = maxIDs
|
||||
return f.ids, "", nil
|
||||
}
|
||||
func (f *fakeGmailAPI) GetMessage(context.Context, string) (*Message, error) {
|
||||
return nil, errors.New("unused")
|
||||
}
|
||||
func (f *fakeGmailAPI) DownloadAttachment(context.Context, string, string) ([]byte, error) {
|
||||
return nil, errors.New("unused")
|
||||
}
|
||||
|
||||
func TestGmailSourcePassesQueryToListIDs(t *testing.T) {
|
||||
fake := &fakeGmailAPI{ids: []string{"m1"}}
|
||||
src := &gmailSource{c: fake, query: "from:alice@example.com"}
|
||||
ids, _, err := src.ListIDs(context.Background(), 10, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fake.lastQ != "from:alice@example.com" {
|
||||
t.Fatalf("ListIDs q=%q, want from:alice@example.com", fake.lastQ)
|
||||
}
|
||||
if fake.lastLimit != 10 {
|
||||
t.Fatalf("ListIDs limit=%d, want 10", fake.lastLimit)
|
||||
}
|
||||
if len(ids) != 1 || ids[0] != "m1" {
|
||||
t.Fatalf("ids=%v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGmailSourceEmptyQueryDefaultsToInbox(t *testing.T) {
|
||||
fake := &fakeGmailAPI{}
|
||||
src := &gmailSource{c: fake, query: ""}
|
||||
if _, _, err := src.ListIDs(context.Background(), 5, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fake.lastQ != "in:inbox" {
|
||||
t.Fatalf("empty query q=%q, want in:inbox", fake.lastQ)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCLIGmailQuery(t *testing.T) {
|
||||
cli, code, err := ParseCLI([]string{
|
||||
"--source", "gmail",
|
||||
"--query", "from:letrado@example.com",
|
||||
"--out", t.TempDir(),
|
||||
"--dry-run",
|
||||
})
|
||||
if err != nil || code != 0 {
|
||||
t.Fatalf("ParseCLI: code=%d err=%v", code, err)
|
||||
}
|
||||
if cli.Sync.Query != "from:letrado@example.com" {
|
||||
t.Fatalf("query=%q", cli.Sync.Query)
|
||||
}
|
||||
if cli.Sync.Gmail == nil {
|
||||
t.Fatal("gmail source not configured")
|
||||
}
|
||||
}
|
||||
|
||||
func b64(s string) string {
|
||||
return base64.URLEncoding.EncodeToString([]byte(s))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user