package migration

import "testing"

func TestExtractGmailAttachmentPartsInlineData(t *testing.T) {
	payload := gmailPayload{
		MimeType: "multipart/mixed",
		Parts: []gmailPayload{
			{
				MimeType: "text/plain",
				Body:     gmailBody{Data: "dGV4dA=="},
			},
			{
				MimeType: "image/png",
				Headers: []gmailHeader{
					{Name: "Content-Disposition", Value: `attachment; filename="logo.png"`},
				},
				Body: gmailBody{Data: "aW1n", Size: 3},
			},
		},
	}
	parts := extractGmailAttachmentParts(payload)
	if len(parts) != 1 {
		t.Fatalf("expected 1 attachment, got %d", len(parts))
	}
	if parts[0].Filename != "logo.png" || parts[0].MimeType != "image/png" {
		t.Fatalf("unexpected part: %#v", parts[0])
	}
}

func TestExtractGmailAttachmentPartsAttachmentID(t *testing.T) {
	payload := gmailPayload{
		MimeType: "multipart/mixed",
		Parts: []gmailPayload{
			{
				MimeType: "application/pdf",
				Headers: []gmailHeader{
					{Name: "Content-Disposition", Value: `attachment; filename="report.pdf"`},
				},
				Body: gmailBody{AttachmentID: "ANGjdJ_test", Size: 4096},
			},
		},
	}
	parts := extractGmailAttachmentParts(payload)
	if len(parts) != 1 || parts[0].AttachmentID != "ANGjdJ_test" {
		t.Fatalf("unexpected parts: %#v", parts)
	}
}

func TestDecodeGmailBodyBytes(t *testing.T) {
	got := decodeGmailBody("aGVsbG8=")
	if got != "hello" {
		t.Fatalf("decode = %q", got)
	}
}
