<commit_before>import XCTest
import Cocoa
@testable import ToyboxKit

struct TestingStorage: Workspace {
    static var rootURL: URL {
        return URL(fileURLWithPath: NSTemporaryDirectory().appending(".toybox"),
                   isDirectory: true)
    }
}

struct AssertOpener: PlaygroundOpener {
    static var opened = false

    static func open(at path: URL, with xcodePath: URL?) {
        opened = true
    }
}

struct TestingDateProvider: DateProvider {
    static var date: Date = .init(timeIntervalSince1970: 1554336125.712433)
}

typealias TestingPlaygroundHandler = PlaygroundHandler<TestingStorage, TestingDateProvider, AssertOpener>

class HandlerTests: XCTestCase {
    let handler = TestingPlaygroundHandler()
    let manager = FileManager()

    override func setUp() {
        super.setUp()

        _ = handler.bootstrap()
    }

    func playgroundURL(for name: String) -> URL {
        return handler.rootURL.appendingPathComponent("\(name).playground")
    }

    func temporaryPlaygroundURL(for name: String) -> URL {
        if #available(OSX 10.12, *) {
            return manager.temporaryDirectory.appendingPathComponent("\(name).playground")
        } else {
            return URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(name).playground")
        }
    }

    func testList() {
        _ = handler.bootstrap()
        guard case let .success(list0) = handler.list() else {
            XCTFail("handler.list should be success")
            return
        }
        XCTAssertTrue(list0.isEmpty)

        _ = handler.create(.named("ios"), for: .iOS)
        _ = handler.create(.named("mac"), for: .macOS)

        guard case let .success(list1) = handler.list() else {
            XCTFail("handler.list should be success")
            return
        }
        XCTAssertEqual(list1.count, 2)

        guard case let .success(list2) = handler.list(for: .macOS) else {
            XCTFail("handler.list should be success")
            return
        }
        XCTAssertEqual(list2.count, 1)
    }

    func testCreate() {
        XCTAssertFalse(manager.fileExists(atPath: playgroundURL(for: "hello").path))
        let result = handler.create(.named("hello"), for: .iOS)
        if case .failure(_) = result {
            XCTFail("handler.list should be failed")
        }
        XCTAssertTrue(manager.fileExists(atPath: playgroundURL(for: "hello").path))
    }

    func testCreateAnonymous() {
        XCTAssertFalse(manager.fileExists(atPath: playgroundURL(for: "hello").path))
        let result = handler.create(.anonymous, for: .iOS)
        switch result {
        case .success(let playground):
            XCTAssertTrue(manager.fileExists(atPath: playgroundURL(for: playground.name).path))
            XCTAssertEqual(playground.name, "20190404090205")
        case .failure:
            XCTFail("handler.list should be failed")
        }
    }

    func testCreateWithTemporaryOption() {
        _ = handler.create(.temporary, for: .iOS)
        XCTAssertTrue(manager.fileExists(atPath: temporaryPlaygroundURL(for: "hello").path))
        XCTAssertFalse(manager.fileExists(atPath: playgroundURL(for: "hello").path))
    }

    func testListDoesNotShowTemporaryFile() {
        _ = handler.create(.named("foo"), for: .iOS)<commit_msg>Fix timezone issue on CI<commit_after>import XCTest
import Cocoa
@testable import ToyboxKit

struct TestingStorage: Workspace {
    static var rootURL: URL {
        return URL(fileURLWithPath: NSTemporaryDirectory().appending(".toybox"),
                   isDirectory: true)
    }
}

struct AssertOpener: PlaygroundOpener {
    static var opened = false

    static func open(at path: URL, with xcodePath: URL?) {
        opened = true
    }
}

struct TestingDateProvider: DateProvider {
    static var date: Date = .init(timeIntervalSince1970: 1554336125.712433)
}

typealias TestingPlaygroundHandler = PlaygroundHandler<TestingStorage, TestingDateProvider, AssertOpener>

class HandlerTests: XCTestCase {
    let handler = TestingPlaygroundHandler()
    let manager = FileManager()

    override func setUp() {
        super.setUp()

        _ = handler.bootstrap()
    }

    func playgroundURL(for name: String) -> URL {
        return handler.rootURL.appendingPathComponent("\(name).playground")
    }

    func temporaryPlaygroundURL(for name: String) -> URL {
        if #available(OSX 10.12, *) {
            return manager.temporaryDirectory.appendingPathComponent("\(name).playground")
        } else {
            return URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(name).playground")
        }
    }

    func testList() {
        _ = handler.bootstrap()
        guard case let .success(list0) = handler.list() else {
            XCTFail("handler.list should be success")
            return
        }
        XCTAssertTrue(list0.isEmpty)

        _ = handler.create(.named("ios"), for: .iOS)
        _ = handler.create(.named("mac"), for: .macOS)

        guard case let .success(list1) = handler.list() else {
            XCTFail("handler.list should be success")
            return
        }
        XCTAssertEqual(list1.count, 2)

        guard case let .success(list2) = handler.list(for: .macOS) else {
            XCTFail("handler.list should be success")
            return
        }
        XCTAssertEqual(list2.count, 1)
    }

    func testCreate() {
        XCTAssertFalse(manager.fileExists(atPath: playgroundURL(for: "hello").path))
        let result = handler.create(.named("hello"), for: .iOS)
        if case .failure(_) = result {
            XCTFail("handler.list should be failed")
        }
        XCTAssertTrue(manager.fileExists(atPath: playgroundURL(for: "hello").path))
    }

    func name(from date: Date) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyyMMddHHmmss"
        return formatter.string(from: date)
    }

    func testCreateAnonymous() {
        XCTAssertFalse(manager.fileExists(atPath: playgroundURL(for: "hello").path))
        let result = handler.create(.anonymous, for: .iOS)
        switch result {
        case .success(let playground):
            XCTAssertTrue(manager.fileExists(atPath: playgroundURL(for: playground.name).path))
            XCTAssertEqual(playground.name, name(from: TestingDateProvider.date))
        case .failure:
            XCTFail("handler.list should be failed")
        }
    }

    func testCreateWithTemporaryOption() {
        _ = handler.create(.temporary, for: .iOS)
        XCTAssertTrue(manager.fileExists(atPath: temporaryPlaygroundURL(for: "hello").path))
        XCTAssertFalse(manager.fileExists(atPath: playgroundURL(for: "hello").path))
    }

    func testListDoesNotShowTemporaryFile() {
        _ = handler.create(.named("foo"), for: .iOS)
{'fraction_non_alphanumeric': 0.11187784466304507, 'fraction_numerical': 0.0114520628395243, 'mean_word_length': 2.7163120567375887, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '2963779', 'n_tokens_mistral': 2041, 'n_tokens_neox': 1908, 'n_words': 409}