package migration import ( "context" "net/http" "strings" "testing" ) func TestGraphWellKnownFolder(t *testing.T) { remote, ftype := graphWellKnownFolder("inbox", "Inbox") if remote != "INBOX" || ftype != "inbox" { t.Fatalf("got %q / %q", remote, ftype) } remote, ftype = graphWellKnownFolder("", "Projects") if remote != "PROJECTS" || ftype != "custom" { t.Fatalf("custom folder: got %q / %q", remote, ftype) } } func TestGraphFlags(t *testing.T) { flags := graphFlags(true, "notFlagged") if len(flags) != 1 || flags[0] != "\\Seen" { t.Fatalf("read flags: %v", flags) } flags = graphFlags(false, "flagged") if len(flags) != 1 || flags[0] != "\\Flagged" { t.Fatalf("flagged: %v", flags) } } func TestGraphRecipientsJSON(t *testing.T) { raw := graphRecipientsJSON([]graphRecipient{ {EmailAddress: graphEmailAddress{Name: "Bob", Address: "bob@example.com"}}, }) if string(raw) != `[{"name":"Bob","email":"bob@example.com"}]` { t.Fatalf("unexpected json: %s", raw) } } func TestParseGraphTime(t *testing.T) { tm := parseGraphTime("2024-05-01T12:34:56Z") if tm.IsZero() { t.Fatal("expected parsed time") } } func TestRemoteMessageUIDMatchesGmailUID(t *testing.T) { id := "abc123" if remoteMessageUID(id) != gmailUID(id) { t.Fatal("uid helpers diverged") } } func TestGraphFolderQueueSortedAndCached(t *testing.T) { g := NewGraphImporter(nil) g.folders = map[string]graphFolderMeta{ "sent-folder": {RemoteName: "SENT", FolderType: "sent"}, "inbox-folder": {RemoteName: "INBOX", FolderType: "inbox"}, } cursor := map[string]any{} queue := g.folderQueue(cursor) if len(queue) != 2 { t.Fatalf("queue len = %d", len(queue)) } if queue[0] != "inbox-folder" || queue[1] != "sent-folder" { t.Fatalf("queue order = %v", queue) } cached := readGraphFolderQueue(cursor) if len(cached) != 2 || cached[0] != "inbox-folder" { t.Fatalf("cached queue = %v", cached) } } func TestGraphFolderMessagesURLUsesMailFoldersPath(t *testing.T) { g := NewGraphImporter(nil).WithBaseURL("https://graph.test") listURL := g.folderMessagesURL("folder-abc") if !strings.Contains(listURL, "/mailFolders/folder-abc/messages") { t.Fatalf("url = %q", listURL) } if strings.Contains(listURL, "/me/messages") { t.Fatalf("flat messages path should not be used: %q", listURL) } } func TestGraphEnsureFoldersPaginates(t *testing.T) { pages := 0 client := mockGraphHTTPClient(t, func(w http.ResponseWriter, r *http.Request) { if !strings.HasSuffix(r.URL.Path, "/mailFolders") { http.NotFound(w, r) return } pages++ if pages == 1 { _, _ = w.Write([]byte(`{ "value":[{"id":"inbox-id","displayName":"Inbox","wellKnownName":"inbox"}], "@odata.nextLink":"https://graph.microsoft.com/v1.0/me/mailFolders?$top=100&$skip=100" }`)) return } _, _ = w.Write([]byte(`{"value":[{"id":"sent-id","displayName":"Sent","wellKnownName":"sentitems"}]}`)) }) g := NewGraphImporter(nil).WithHTTPClient(client) if err := g.ensureGraphFolders(context.Background(), "token"); err != nil { t.Fatalf("ensure folders: %v", err) } if pages != 2 { t.Fatalf("pages = %d, want 2", pages) } if len(g.folders) != 2 { t.Fatalf("folders = %d", len(g.folders)) } } func TestGraphInitFolderDeltaLink(t *testing.T) { client := mockGraphHTTPClient(t, func(w http.ResponseWriter, r *http.Request) { if strings.Contains(r.URL.Path, "/mailFolders/inbox-id/messages/delta") { _, _ = w.Write([]byte(`{"@odata.deltaLink":"https://graph.microsoft.com/v1.0/me/mailFolders/inbox-id/messages/delta?token=done"}`)) return } http.NotFound(w, r) }) g := NewGraphImporter(nil).WithHTTPClient(client) link, err := g.initFolderDeltaLink(context.Background(), "token", "inbox-id") if err != nil { t.Fatalf("init delta: %v", err) } if !strings.Contains(link, "/mailFolders/inbox-id/messages/delta") { t.Fatalf("delta link = %q", link) } } func TestGraphFolderDeltaLinkHelpers(t *testing.T) { cursor := map[string]any{} setGraphFolderDeltaLink(cursor, "inbox-id", "https://delta/inbox") links := graphFolderDeltaLinks(cursor) if links["inbox-id"] != "https://delta/inbox" { t.Fatalf("links = %v", links) } }