Repository navigation
Expand file tree
/
Copy pathgmailapi_write_test.go
More file actions
387 lines (359 loc) · 12.8 KB
/
Copy pathgmailapi_write_test.go
File metadata and controls
387 lines (359 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package sync
import (
"context"
"errors"
"io"
"log/slog"
"testing"
"time"
"golang.org/x/oauth2"
"github.com/CaffeinatedTech/jmap-bridge/internal/gmailapi"
"github.com/CaffeinatedTech/jmap-bridge/internal/jmapapi"
mb "github.com/CaffeinatedTech/jmap-bridge/internal/mailbackend"
"github.com/CaffeinatedTech/jmap-bridge/internal/store"
"github.com/CaffeinatedTech/jmap-bridge/test/fixturegmail"
)
// gapiEnv is the API-mode engine harness the write-path tests share.
type gapiEnv struct {
fx *fixturegmail.Server
st *store.Store
eng *Engine
}
func newGmailAPIEnv(t *testing.T, fx *fixturegmail.Server) *gapiEnv {
t.Helper()
return newGmailAPIEnvWrapped(t, fx, nil)
}
// newGmailAPIEnvWrapped is newGmailAPIEnv with an optional wrapper around
// every backend session the engine builds (tests that need to observe a
// specific provider call).
func newGmailAPIEnvWrapped(t *testing.T, fx *fixturegmail.Server, wrap func(mb.Backend) mb.Backend) *gapiEnv {
t.Helper()
st, err := store.Open(context.Background(), store.Options{
DataDir: t.TempDir(),
Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
})
if err != nil {
t.Fatalf("store: %v", err)
}
t.Cleanup(func() { _ = st.Close() })
client, err := gmailapi.New(context.Background(), gmailapi.Options{
Endpoint: fx.URL(),
HTTPClient: oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: fx.Token()})),
QuotaUnitsPerSecond: -1,
})
if err != nil {
t.Fatalf("gmailapi.New: %v", err)
}
ni := storeNative{st: st, account: "gapi"}
cfg := Config{
Account: "gapi",
NewBackend: func() mb.Backend {
b := mb.Backend(gmailapi.NewBackend(gmailapi.Config{Account: "gapi", Client: client, Native: ni}))
if wrap != nil {
b = wrap(b)
}
return b
},
Interval: 150 * time.Millisecond,
BatchSize: 50,
Concurrency: 2,
}
eng := New(cfg, st, slog.New(slog.NewTextHandler(io.Discard, nil)))
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go eng.Run(ctx)
waitUntil(t, 5*time.Second, "engine ready", eng.Ready)
return &gapiEnv{fx: fx, st: st, eng: eng}
}
func (env *gapiEnv) emails(t *testing.T) []*jmapapi.Email {
t.Helper()
list, _, notFound, err := env.st.EmailsByID(context.Background(), "gapi", nil, false)
if err != nil {
t.Fatalf("emails: %v", err)
}
if len(notFound) > 0 {
t.Fatalf("unexpected notFound: %v", notFound)
}
return list
}
func (env *gapiEnv) mailboxID(t *testing.T, name string) string {
t.Helper()
mbs, _, err := env.st.Mailboxes(context.Background(), "gapi")
if err != nil {
t.Fatalf("mailboxes: %v", err)
}
for _, m := range mbs {
if m.Name == name {
return m.ID
}
}
t.Fatalf("mailbox %q not found", name)
return ""
}
func gapiSeedLabels(fx *fixturegmail.Server) {
fx.SeedLabel(fixturegmail.LabelInbox, "INBOX", "system")
fx.SeedLabel(fixturegmail.LabelSent, "SENT", "system")
fx.SeedLabel(fixturegmail.LabelDraft, "Drafts", "system")
fx.SeedLabel(fixturegmail.LabelTrash, "Trash", "system")
fx.SeedLabel(fixturegmail.LabelSpam, "Spam", "system")
fx.SeedLabel("Label_work", "Work", "user")
}
func TestGmailAPIEngineWritePath(t *testing.T) {
fx := fixturegmail.Start(t, fixturegmail.Options{})
gapiSeedLabels(fx)
fx.SeedMessage(fixturegmail.SeedMessage{
ID: "m1", ThreadID: "thr1", LabelIDs: []string{fixturegmail.LabelInbox, fixturegmail.LabelUnread},
InternalDate: time.Now().UnixMilli(),
Headers: []fixturegmail.Header{
{Name: "From", Value: "Alice <alice@example.test>"},
{Name: "To", Value: "me@example.test"},
{Name: "Subject", Value: "Hello API"},
{Name: "Date", Value: "Mon, 02 Jan 2026 15:04:05 -0700"},
{Name: "Message-ID", Value: "<api1@x>"},
},
Raw: raw("Hello API", "<api1@x>", "body"),
})
env := newGmailAPIEnv(t, fx)
ctx := context.Background()
waitUntil(t, 5*time.Second, "email ingested", func() bool { return len(env.emails(t)) == 1 })
emailID := env.emails(t)[0].ID
// Mark read ($seen add removes UNREAD) and flagged in one patch.
if err := env.eng.ApplyEmailPatch(ctx, "gapi", emailID, jmapapi.EmailPatch{
KeywordAdd: []string{"$seen", "$flagged"},
}); err != nil {
t.Fatalf("ApplyEmailPatch keywords: %v", err)
}
labels, _ := fx.MessageLabels("m1")
if !contains(labels, fixturegmail.LabelStarred) || contains(labels, fixturegmail.LabelUnread) {
t.Fatalf("fixture labels after keyword patch = %v", labels)
}
got := env.emails(t)[0]
if !got.Keywords["$seen"] || !got.Keywords["$flagged"] {
t.Fatalf("store keywords after patch = %v", got.Keywords)
}
// File into the user label (a membership add on the label model).
workID := env.mailboxID(t, "Work")
if err := env.eng.ApplyEmailPatch(ctx, "gapi", emailID, jmapapi.EmailPatch{
MailboxAdd: []string{workID},
}); err != nil {
t.Fatalf("ApplyEmailPatch mailbox: %v", err)
}
labels, _ = fx.MessageLabels("m1")
if !contains(labels, "Label_work") {
t.Fatalf("fixture labels after membership = %v", labels)
}
// Destroy permanently removes the message (no trash routing in the API).
if err := env.eng.DestroyEmails(ctx, "gapi", emailID); err != nil {
t.Fatalf("DestroyEmails: %v", err)
}
if _, ok := fx.MessageLabels("m1"); ok {
t.Fatal("fixture still holds the destroyed message")
}
waitUntil(t, 5*time.Second, "email tombstoned", func() bool { return len(env.emails(t)) == 0 })
}
func TestGmailAPIEngineRefusesUnsupportedKeyword(t *testing.T) {
fx := fixturegmail.Start(t, fixturegmail.Options{})
gapiSeedLabels(fx)
fx.SeedMessage(fixturegmail.SeedMessage{
ID: "m1", ThreadID: "thr1", LabelIDs: []string{fixturegmail.LabelInbox},
InternalDate: time.Now().UnixMilli(),
Headers: []fixturegmail.Header{{Name: "Message-ID", Value: "<api1@x>"}, {Name: "Subject", Value: "Hello"}},
Raw: raw("Hello", "<api1@x>", "body"),
})
env := newGmailAPIEnv(t, fx)
ctx := context.Background()
waitUntil(t, 5*time.Second, "email ingested", func() bool { return len(env.emails(t)) == 1 })
emailID := env.emails(t)[0].ID
err := env.eng.ApplyEmailPatch(ctx, "gapi", emailID, jmapapi.EmailPatch{KeywordAdd: []string{"$answered"}})
var kwErr *jmapapi.KeywordError
if !errors.As(err, &kwErr) {
t.Fatalf("err = %v, want KeywordError", err)
}
if len(kwErr.Keywords) != 1 || kwErr.Keywords[0] != "$answered" {
t.Fatalf("blamed keywords = %v", kwErr.Keywords)
}
}
func TestGmailAPIEngineCreateDraft(t *testing.T) {
fx := fixturegmail.Start(t, fixturegmail.Options{})
gapiSeedLabels(fx)
env := newGmailAPIEnv(t, fx)
ctx := context.Background()
created, err := env.eng.CreateDraft(ctx, "gapi", jmapapi.DraftSpec{
To: []jmapapi.Address{{Email: "you@example.test"}},
Subject: "A draft",
Parts: []jmapapi.DraftPart{{Type: "text/plain", Text: "draft body"}},
})
if err != nil {
t.Fatalf("CreateDraft: %v", err)
}
if created.ID == "" {
t.Fatal("no created id")
}
drafts := fx.DraftIDs()
if len(drafts) != 1 {
t.Fatalf("fixture drafts = %v, want 1", drafts)
}
// The draft handle is persisted against the message's synthetic uid.
copies, err := env.st.EmailCopies(ctx, "gapi", created.ID)
if err != nil {
t.Fatalf("EmailCopies: %v", err)
}
if len(copies) == 0 {
t.Fatal("draft has no stored copy")
}
draftID, err := env.st.DraftIDByUID(ctx, "gapi", copies[0].UID)
if err != nil {
t.Fatalf("DraftIDByUID: %v", err)
}
if draftID != drafts[0] {
t.Fatalf("stored draft id = %q, fixture = %q", draftID, drafts[0])
}
}
func TestGmailAPIEngineCreateDraftMultiMailbox(t *testing.T) {
fx := fixturegmail.Start(t, fixturegmail.Options{})
gapiSeedLabels(fx)
env := newGmailAPIEnv(t, fx)
ctx := context.Background()
inbox := env.mailboxID(t, "INBOX")
work := env.mailboxID(t, "Work")
created, err := env.eng.CreateDraft(ctx, "gapi", jmapapi.DraftSpec{
MailboxIDs: []string{inbox, work},
To: []jmapapi.Address{{Email: "you@example.test"}},
Subject: "multi mailbox",
Parts: []jmapapi.DraftPart{{Type: "text/plain", Text: "body"}},
})
if err != nil {
t.Fatalf("CreateDraft: %v", err)
}
copies, err := env.st.EmailCopies(ctx, "gapi", created.ID)
if err != nil {
t.Fatalf("EmailCopies: %v", err)
}
if len(copies) != 2 {
t.Fatalf("copies = %d, want 2 (INBOX + Work)", len(copies))
}
native, ok, err := env.st.NativeByUID(ctx, "gapi", copies[0].UID)
if err != nil || !ok {
t.Fatalf("native for uid %d: %q ok=%v err=%v", copies[0].UID, native, ok, err)
}
labels, ok := fx.MessageLabels(native)
if !ok {
t.Fatalf("created message %s missing on the server", native)
}
if !contains(labels, fixturegmail.LabelInbox) || !contains(labels, "Label_work") {
t.Fatalf("labels = %v, want INBOX + Work", labels)
}
}
func TestGmailAPIEngineImportEmail(t *testing.T) {
fx := fixturegmail.Start(t, fixturegmail.Options{})
gapiSeedLabels(fx)
env := newGmailAPIEnv(t, fx)
ctx := context.Background()
blobID, err := env.st.PutBlob(ctx, "gapi", "message/rfc822", raw("Imported API", "<api-imp@x>", "imported body"))
if err != nil {
t.Fatalf("PutBlob: %v", err)
}
inbox := env.mailboxID(t, "INBOX")
work := env.mailboxID(t, "Work")
created, err := env.eng.ImportEmail(ctx, "gapi", jmapapi.ImportSpec{
BlobID: blobID,
MailboxIDs: []string{inbox, work},
Keywords: map[string]bool{"$seen": true, "$flagged": true},
})
if err != nil {
t.Fatalf("ImportEmail: %v", err)
}
if created.ID == "" || created.BlobID != blobID {
t.Fatalf("created = %+v", created)
}
// The one Gmail message carries both labels, seen and flagged.
copies, err := env.st.EmailCopies(ctx, "gapi", created.ID)
if err != nil {
t.Fatalf("EmailCopies: %v", err)
}
if len(copies) != 2 {
t.Fatalf("copies = %d, want 2 (INBOX + Work)", len(copies))
}
for _, c := range copies {
native, ok, err := env.st.NativeByUID(ctx, "gapi", c.UID)
if err != nil || !ok || native == "" {
t.Fatalf("native for uid %d: %q ok=%v err=%v", c.UID, native, ok, err)
}
labels, ok := fx.MessageLabels(native)
if !ok {
t.Fatalf("imported message %s missing on the server", native)
}
if contains(labels, fixturegmail.LabelUnread) || !contains(labels, fixturegmail.LabelStarred) {
t.Fatalf("labels = %v, want $seen + $flagged", labels)
}
if !contains(labels, fixturegmail.LabelInbox) || !contains(labels, "Label_work") {
t.Fatalf("labels = %v, want INBOX + Work", labels)
}
}
}
func TestGmailAPIEngineMailboxSet(t *testing.T) {
fx := fixturegmail.Start(t, fixturegmail.Options{})
gapiSeedLabels(fx)
env := newGmailAPIEnv(t, fx)
ctx := context.Background()
id, err := env.eng.CreateMailbox(ctx, "gapi", "Projects", "", 0)
if err != nil {
t.Fatalf("CreateMailbox: %v", err)
}
if _, ok := fx.LabelIDByName("Projects"); !ok {
t.Fatal("created label not on the server")
}
if err := env.eng.RenameMailbox(ctx, "gapi", id, "Archive", ""); err != nil {
t.Fatalf("RenameMailbox: %v", err)
}
if _, ok := fx.LabelIDByName("Archive"); !ok {
t.Fatal("renamed label not on the server")
}
// onDestroyRemoveEmails=true is refused by name in API mode.
if err := env.eng.DestroyMailbox(ctx, "gapi", id, true); !errors.Is(err, jmapapi.ErrOnDestroyRemoveEmails) {
t.Fatalf("DestroyMailbox(removeEmails=true) err = %v, want ErrOnDestroyRemoveEmails", err)
}
if err := env.eng.DestroyMailbox(ctx, "gapi", id, false); err != nil {
t.Fatalf("DestroyMailbox(removeEmails=false): %v", err)
}
if _, ok := fx.LabelIDByName("Archive"); ok {
t.Fatal("label still on the server after destroy")
}
}
// TestGmailAPIEngineHydrationBuildsStructure pins the API-mode hydration
// fix: the header pass carries no MIME structure, so hydration must build
// bodyStructure from the raw bytes or textBody/htmlBody come back empty.
func TestGmailAPIEngineHydrationBuildsStructure(t *testing.T) {
fx := fixturegmail.Start(t, fixturegmail.Options{})
gapiSeedLabels(fx)
fx.SeedMessage(fixturegmail.SeedMessage{
ID: "m1", ThreadID: "thr1", LabelIDs: []string{fixturegmail.LabelInbox},
InternalDate: time.Now().UnixMilli(),
Headers: []fixturegmail.Header{
{Name: "From", Value: "Alice <alice@example.test>"},
{Name: "To", Value: "me@example.test"},
{Name: "Subject", Value: "Body test"},
{Name: "Date", Value: "Mon, 02 Jan 2026 15:04:05 -0700"},
{Name: "Message-ID", Value: "<body1@x>"},
},
Raw: raw("Body test", "<body1@x>", "the body text"),
})
env := newGmailAPIEnv(t, fx)
ctx := context.Background()
waitUntil(t, 5*time.Second, "email ingested", func() bool { return len(env.emails(t)) == 1 })
id := env.emails(t)[0].ID
list, _, notFound, err := env.st.EmailsByID(ctx, "gapi", []string{id}, true)
if err != nil || len(notFound) > 0 || len(list) != 1 {
t.Fatalf("EmailsByID: list=%d notFound=%v err=%v", len(list), notFound, err)
}
e := list[0]
if len(e.Structure) == 0 || string(e.Structure) == "{}" {
t.Fatalf("bodyStructure = %s, want a tree (API-mode hydration must build it)", e.Structure)
}
if len(e.BodyValues) == 0 {
t.Fatal("bodyValues empty after hydration")
}
if len(e.TextParts) == 0 {
t.Fatal("textBody empty after hydration")
}
}