package sync import ( "context" "net/http" "net/http/httptest" "os" "path/filepath" "testing" ) // newM365TestClient serves the Graph delta + message endpoints against a fake // token endpoint, so unit tests never touch the network. func newM365TestClient(t *testing.T, graph http.Handler) *M365Client { t.Helper() tok := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"access_token":"test-token","expires_in":3600}`)) })) gr := httptest.NewServer(graph) t.Cleanup(func() { tok.Close() gr.Close() }) client, err := NewM365Client(M365Credentials{Tenant: "t.onmicrosoft.com", ClientID: "c", ClientSecret: "s"}) if err != nil { t.Fatal(err) } client.base = gr.URL client.tokenEndpoint = tok.URL return client } func TestM365AccessToken(t *testing.T) { c := newM365TestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) got, err := c.accessToken(context.Background()) if err != nil { t.Fatalf("accessToken: %v", err) } if got != "test-token" { t.Errorf("token = %q", got) } // Second call must reuse the cached token (no token request). again, err := c.accessToken(context.Background()) if err != nil || again != "test-token" { t.Fatalf("cached token: %q, %v", again, err) } } func TestM365DeltaSkipsTombstones(t *testing.T) { first := true graph := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if first { first = false w.Write([]byte(`{"value":[ {"id":"m1"}, {"id":"m2","@odata.removedReason":"deleted"}, {"id":"m3"} ],"@odata.deltaLink":"` + deltaNext + `"}`)) return } // Second call must use the stored deltaLink (points at this server). if r.URL.Path != "/v1.0/delta-next" { w.WriteHeader(500) w.Write([]byte(`{"error":{"message":"unexpected path"}}`)) return } w.Write([]byte(`{"value":[{"id":"m4"}],"@odata.deltaLink":"` + deltaFinal + `"}`)) }) c := newM365TestClient(t, graph) deltaNext = c.base + "/v1.0/delta-next" deltaFinal = c.base + "/v1.0/delta-final" ids, link, err := c.ListDeltaIDs(context.Background(), "a@x.de", "", 0) if err != nil { t.Fatalf("delta: %v", err) } if len(ids) != 2 || ids[0] != "m1" || ids[1] != "m3" { t.Errorf("ids = %v", ids) } if link == "" { t.Error("expected new deltaLink") } // Incremental: pass the deltaLink, get only the new id. ids2, link2, err := c.ListDeltaIDs(context.Background(), "a@x.de", link, 0) if err != nil { t.Fatalf("delta incremental: %v", err) } if len(ids2) != 1 || ids2[0] != "m4" { t.Errorf("ids2 = %v", ids2) } if link2 == "" { t.Error("expected updated deltaLink") } } func TestM365GetMessageNormalizes(t *testing.T) { graph := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if pathLast(r.URL.Path) == "messages" { w.Write([]byte(`{"value":[{"id":"m1"}]}`)) return } w.Write([]byte(`{ "id":"m1", "subject":"Hallo", "from":{"emailAddress":{"name":"Max","address":"max@x.de"}}, "toRecipients":[{"emailAddress":{"address":"a@x.de"}}], "receivedDateTime":"2026-08-14T08:15:00Z", "body":{"contentType":"html","content":"
body
"}, "bodyPreview":"body", "internetMessageId":"body
" { t.Errorf("html = %q", m.HTMLBody) } if m.ReceivedAt.IsZero() { t.Error("receivedAt zero") } if m.MimeMessageID != "