|
1 | 1 | package sync |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "os" |
6 | 7 | "path/filepath" |
7 | 8 | "runtime" |
| 9 | + "strings" |
8 | 10 | "testing" |
9 | 11 |
|
10 | 12 | "github.com/wesm/msgvault/internal/gmail" |
11 | 13 | "github.com/wesm/msgvault/internal/store" |
12 | 14 | testemail "github.com/wesm/msgvault/internal/testutil/email" |
13 | 15 | ) |
14 | 16 |
|
| 17 | +// panicOnBatchAPI wraps a MockAPI and panics when GetMessagesRawBatch is called. |
| 18 | +// Used to test that Full() recovers from panics gracefully. |
| 19 | +type panicOnBatchAPI struct { |
| 20 | + *gmail.MockAPI |
| 21 | +} |
| 22 | + |
| 23 | +func (p *panicOnBatchAPI) GetMessagesRawBatch(_ context.Context, _ []string) ([]*gmail.RawMessage, error) { |
| 24 | + panic("unexpected nil pointer in batch processing") |
| 25 | +} |
| 26 | + |
| 27 | +func TestFullSync_PanicReturnsError(t *testing.T) { |
| 28 | + env := newTestEnv(t) |
| 29 | + seedMessages(env, 1, 12345, "msg1") |
| 30 | + |
| 31 | + // Replace the client with one that panics during batch fetch |
| 32 | + env.Syncer = New(&panicOnBatchAPI{MockAPI: env.Mock}, env.Store, nil) |
| 33 | + |
| 34 | + // Should return an error, NOT panic and crash the program |
| 35 | + _, err := env.Syncer.Full(env.Context, testEmail) |
| 36 | + if err == nil { |
| 37 | + t.Fatal("expected error from panic recovery, got nil") |
| 38 | + } |
| 39 | + if !strings.Contains(err.Error(), "panic") { |
| 40 | + t.Errorf("expected error to mention panic, got: %v", err) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// panicOnHistoryAPI wraps a MockAPI and panics when ListHistory is called. |
| 45 | +// Used to test that Incremental() recovers from panics gracefully. |
| 46 | +type panicOnHistoryAPI struct { |
| 47 | + *gmail.MockAPI |
| 48 | +} |
| 49 | + |
| 50 | +func (p *panicOnHistoryAPI) ListHistory(_ context.Context, _ uint64, _ string) (*gmail.HistoryResponse, error) { |
| 51 | + panic("unexpected nil pointer in history processing") |
| 52 | +} |
| 53 | + |
| 54 | +func TestIncrementalSync_PanicReturnsError(t *testing.T) { |
| 55 | + env := newTestEnv(t) |
| 56 | + env.CreateSourceWithHistory(t, "12340") |
| 57 | + |
| 58 | + env.Mock.Profile.MessagesTotal = 10 |
| 59 | + env.Mock.Profile.HistoryID = 12350 |
| 60 | + |
| 61 | + // Replace the client with one that panics during history fetch |
| 62 | + env.Syncer = New(&panicOnHistoryAPI{MockAPI: env.Mock}, env.Store, nil) |
| 63 | + |
| 64 | + // Should return an error, NOT panic and crash the program |
| 65 | + _, err := env.Syncer.Incremental(env.Context, testEmail) |
| 66 | + if err == nil { |
| 67 | + t.Fatal("expected error from panic recovery, got nil") |
| 68 | + } |
| 69 | + if !strings.Contains(err.Error(), "panic") { |
| 70 | + t.Errorf("expected error to mention panic, got: %v", err) |
| 71 | + } |
| 72 | +} |
| 73 | + |
15 | 74 | func TestFullSync(t *testing.T) { |
16 | 75 | env := newTestEnv(t) |
17 | 76 | seedMessages(env, 3, 12345, "msg1", "msg2", "msg3") |
|
0 commit comments