// ─────────────────────────────────────────────────────────────────────────────
// `TQDownloadResponseHandler.isDownloadable:` 纯函数单测。
// ─────────────────────────────────────────────────────────────────────────────

#import <XCTest/XCTest.h>
#import "TQDownloadResponseHandler.h"

@interface TQDownloadResponseHandlerTests : XCTestCase
@end

@implementation TQDownloadResponseHandlerTests

#pragma mark - 辅助构造

/// 构造一个带指定 MIME 和 Content-Disposition 的 HTTPURLResponse
- (NSHTTPURLResponse *)makeHTTPResponseWithURL:(NSString *)urlString
                                       headers:(NSDictionary<NSString *, NSString *> *)headers {
    NSURL *url = [NSURL URLWithString:urlString];
    return [[NSHTTPURLResponse alloc] initWithURL:url
                                       statusCode:200
                                      HTTPVersion:@"HTTP/1.1"
                                     headerFields:headers];
}

/// 构造一个只有 mimeType 的 URLResponse(非 HTTP)
- (NSURLResponse *)makeURLResponseWithMime:(nullable NSString *)mimeType {
    NSURL *url = [NSURL URLWithString:@"https://example.com/file"];
    return [[NSURLResponse alloc] initWithURL:url
                                     MIMEType:mimeType
                        expectedContentLength:-1
                             textEncodingName:nil];
}

#pragma mark - MIME 白名单命中

- (void)test_pdf_mime_is_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"application/pdf"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_docx_mime_is_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_xlsx_mime_is_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_pptx_mime_is_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"application/vnd.openxmlformats-officedocument.presentationml.presentation"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_legacy_doc_mime_is_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"application/msword"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_legacy_xls_mime_is_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"application/vnd.ms-excel"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_legacy_ppt_mime_is_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"application/vnd.ms-powerpoint"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_zip_mime_is_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"application/zip"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_x_zip_compressed_mime_is_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"application/x-zip-compressed"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_octet_stream_mime_is_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"application/octet-stream"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

#pragma mark - MIME 大小写不敏感

- (void)test_mime_matching_is_case_insensitive {
    NSURLResponse *r = [self makeURLResponseWithMime:@"Application/PDF"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_mime_mixed_case_zip {
    NSURLResponse *r = [self makeURLResponseWithMime:@"Application/Zip"];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

#pragma mark - Content-Disposition: attachment

- (void)test_content_disposition_attachment_triggers_download {
    NSHTTPURLResponse *r = [self makeHTTPResponseWithURL:@"https://example.com/file"
                                                  headers:@{@"Content-Disposition": @"attachment; filename=\"report.pdf\""}];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_content_disposition_attachment_case_insensitive {
    NSHTTPURLResponse *r = [self makeHTTPResponseWithURL:@"https://example.com/file"
                                                  headers:@{@"Content-Disposition": @"ATTACHMENT; filename=\"x.zip\""}];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_content_disposition_inline_does_not_trigger {
    NSHTTPURLResponse *r = [self makeHTTPResponseWithURL:@"https://example.com/file"
                                                  headers:@{@"Content-Disposition": @"inline"}];
    XCTAssertFalse([TQDownloadResponseHandler isDownloadable:r]);
}

#pragma mark - 不触发的 MIME

- (void)test_html_mime_is_not_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"text/html"];
    XCTAssertFalse([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_json_mime_is_not_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"application/json"];
    XCTAssertFalse([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_plain_text_is_not_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"text/plain"];
    XCTAssertFalse([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_image_png_is_not_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@"image/png"];
    XCTAssertFalse([TQDownloadResponseHandler isDownloadable:r]);
}

#pragma mark - 边界

- (void)test_nil_mime_no_headers_is_downloadable_due_to_system_default {
    // iOS 在 NSURLResponse 创建时若 mimeType 传 nil,系统会默认为 "application/octet-stream",
    // 该 MIME 在白名单中,因此返回 YES 是平台正确行为。
    NSURLResponse *r = [self makeURLResponseWithMime:nil];
    XCTAssertTrue([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_empty_mime_is_not_downloadable {
    NSURLResponse *r = [self makeURLResponseWithMime:@""];
    XCTAssertFalse([TQDownloadResponseHandler isDownloadable:r]);
}

- (void)test_non_http_response_without_matching_mime_is_not_downloadable {
    // URLResponse(非 HTTP)没有 Content-Disposition header,只看 MIME
    NSURLResponse *r = [self makeURLResponseWithMime:@"text/css"];
    XCTAssertFalse([TQDownloadResponseHandler isDownloadable:r]);
}

@end
