<commit_before>//
//  MockHNAPIClient.swift
//  HackerNewsAPI
//
//  Created by <NAME> on 30/06/2020.
//  Copyright © 2020 <NAME>. All rights reserved.
//

import Foundation

public struct MockHNAPIClient {
    private let decoder: JSONDecoder

    public init() {
        decoder = JSONDecoder()
        decoder.dateDecodingStrategy = .secondsSince1970
    }

    private func loadTestData<T: Decodable>(from filename: String) -> T {
        guard
            let bundle = Bundle(identifier: "fr.ngquerol.HackerNewsAPI"),
            let file = bundle.url(forResource: filename, withExtension: "json")
        else {
            fatalError("Couldn't find \(filename) in main bundle.")
        }

        let data: Data

        do {
            data = try Data(contentsOf: file)
        } catch {
            fatalError("Couldn't load \(filename) from main bundle: \(error)")
        }

        do {
            return try decoder.decode(T.self, from: data)
        } catch {
            fatalError("Couldn't parse \(filename) as \(T.self): \(error)")
        }
    }
}

// MARK: - HNAPIClient

extension MockHNAPIClient: HNAPIClient {
    public var requestTimeout: TimeInterval {
        get { .infinity }
        set {}
    }

    public func fetchStories(of type: StoryType, count: Int, completion: @escaping HNAPIResultsCallback<Story>) {
        let stories: [Story] = loadTestData(from: "\(type.rawValue.lowercased())_stories")

        completion(stories.prefix(count).map { .success($0) })
    }

    public func fetchComments(of story: Story, completion: @escaping HNAPIResultsCallback<Comment>) {
        let stories: [Story] = loadTestData(from: "top_stories")

        if let comments = stories.first(where: { $0 == story })?.kids {
            completion(comments.map { .success($0) })
        } else {
            completion([])
        }
    }

    public func fetchUser(with _: String, completion: @escaping HNAPIResultCallback<User>) {
        let user: User = loadTestData(from: "user")

        completion(.success(user))
    }
}<commit_msg>Use pre-fetched comments for mock api client<commit_after>//
//  MockHNAPIClient.swift
//  HackerNewsAPI
//
//  Created by <NAME> on 30/06/2020.
//  Copyright © 2020 <NAME>. All rights reserved.
//

import Foundation

public struct MockHNAPIClient {
    private let decoder: JSONDecoder

    public init() {
        decoder = JSONDecoder()
        decoder.dateDecodingStrategy = .secondsSince1970
    }

    private func loadTestData<T: Decodable>(from filename: String) -> T {
        guard
            let bundle = Bundle(identifier: "fr.ngquerol.HackerNewsAPI"),
            let file = bundle.url(forResource: filename, withExtension: "json")
        else {
            fatalError("Couldn't find \(filename) in main bundle.")
        }

        let data: Data

        do {
            data = try Data(contentsOf: file)
        } catch {
            fatalError("Couldn't load \(filename) from main bundle: \(error)")
        }

        do {
            return try decoder.decode(T.self, from: data)
        } catch {
            fatalError("Couldn't parse \(filename) as \(T.self): \(error)")
        }
    }
}

// MARK: - HNAPIClient

extension MockHNAPIClient: HNAPIClient {
    public var requestTimeout: TimeInterval {
        get { .infinity }
        set {}
    }

    public func fetchStories(of type: StoryType, count: Int, completion: @escaping HNAPIResultsCallback<Story>) {
        let stories: [Story] = loadTestData(from: "\(type.rawValue.lowercased())_stories")

        completion(stories.prefix(count).map { .success($0) })
    }

    public func fetchComments(of story: Story, completion: @escaping HNAPIResultsCallback<Comment>) {
        guard let kids = story.kids else { return completion([]) }
        
        completion(kids.map { .success($0) })
    }

    public func fetchUser(with _: String, completion: @escaping HNAPIResultCallback<User>) {
        let user: User = loadTestData(from: "user")

        completion(.success(user))
    }
}
{'fraction_non_alphanumeric': 0.12006946167204167, 'fraction_numerical': 0.009178863805507318, 'mean_word_length': 2.391084945332212, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2878758', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1158, 'n_words': 299}