import Testing
import Foundation
@testable import MIMEParser

@Suite("RFC2047Decoder")
struct RFC2047DecoderTests {

	@Test("plain ASCII filename passes through unchanged")
	func plainAscii() {
		let result = RFC2047Decoder.decode("report.pdf")
		#expect(result == "report.pdf")
	}

	@Test("base64 encoded UTF-8 filename decoded correctly")
	func base64Utf8() {
		// "Bericht.pdf" in base64
		let encoded = "=?utf-8?B?QmVyaWNodC5wZGY=?="
		let result = RFC2047Decoder.decode(encoded)
		#expect(result == "Bericht.pdf")
	}

	@Test("quoted-printable encoded UTF-8 filename decoded correctly")
	func quotedPrintableUtf8() {
		// "Grüße.txt" — ü = =C3=BC, ß = =C3=9F
		let encoded = "=?utf-8?Q?Gr=C3=BC=C3=9Fe.txt?="
		let result = RFC2047Decoder.decode(encoded)
		#expect(result == "Grüße.txt")
	}

	@Test("multiple encoded words concatenated")
	func multipleEncodedWords() {
		let encoded = "=?utf-8?B?SGVsbG8=?= =?utf-8?B?V29ybGQ=?="
		let result = RFC2047Decoder.decode(encoded)
		#expect(result == "HelloWorld")
	}

	@Test("ISO-8859-1 encoded filename decoded correctly")
	func iso88591() {
		// "café" — é = 0xE9 in ISO-8859-1, base64 of "café" in ISO-8859-1 is "Y2Fm6Q=="
		let encoded = "=?iso-8859-1?B?Y2Fm6Q==?="
		let result = RFC2047Decoder.decode(encoded)
		#expect(result == "café")
	}

	@Test("underscores in Q-encoding replaced with spaces")
	func qEncodingUnderscores() {
		let encoded = "=?utf-8?Q?my_file_name.pdf?="
		let result = RFC2047Decoder.decode(encoded)
		#expect(result == "my file name.pdf")
	}
}
