import XCTest @testable import LChatCore // 🤖 The safe-filename derivation for downloading/previewing attachments: the // sender's name is untrusted, and the extension (which Quick Look and the Files // app route on) may be missing. These pin the sanitisation + MIME fallback. final class AttachmentDownloadTests: XCTestCase { func testKeepsGoodNameAndExtension() { XCTAssertEqual( AttachmentDownload.safeFilename(name: "report.pdf", mime: "application/pdf"), "report.pdf") XCTAssertEqual( AttachmentDownload.safeFilename(name: "photo.jpeg", mime: "image/jpeg"), "photo.jpeg") } func testDerivesExtensionFromMIMEWhenNameHasNone() { XCTAssertEqual( AttachmentDownload.safeFilename(name: "report", mime: "application/pdf"), "report.pdf") XCTAssertEqual( AttachmentDownload.safeFilename(name: "notes", mime: "text/plain"), "notes.txt") } func testFallsBackToFileWhenNameMissingOrEmpty() { XCTAssertEqual( AttachmentDownload.safeFilename(name: nil, mime: "application/pdf"), "file.pdf") XCTAssertEqual( AttachmentDownload.safeFilename(name: " ", mime: "image/png"), "file.png") } func testStripsDirectoryPathFromSenderName() { // lastPathComponent defuses a path-traversal-shaped name. XCTAssertEqual( AttachmentDownload.safeFilename(name: "../../etc/passwd", mime: nil), "passwd") XCTAssertEqual( AttachmentDownload.safeFilename(name: "a/b/c/secret.docx", mime: nil), "secret.docx") } func testNoExtensionWhenNeitherNameNorMIMEProvidesOne() { // No name extension and no usable MIME → leave it bare rather than invent one. XCTAssertEqual(AttachmentDownload.safeFilename(name: "notes", mime: nil), "notes") XCTAssertEqual(AttachmentDownload.safeFilename(name: "notes", mime: ""), "notes") XCTAssertEqual(AttachmentDownload.safeFilename(name: nil, mime: nil), "file") } }