import Foundation import XCTest @testable import CottonValues final class ShellLinesTests: XCTestCase { /// RULE: A face says the kit's words with no core: every line set Loom bundles (`CottonValues/Resources/lines`) answers its lines before any core has sent one, so a face drawn in Loom reads as it does in the app. func testEverySetIsSaidWithNoCore() throws { let urls = try XCTUnwrap(CottonValuesResources.bundle?.urls(forResourcesWithExtension: "json", subdirectory: "lines")) XCTAssertGreaterThan(urls.count, 10, "the bundle carries the kit's line sets") for url in urls { let set = url.deletingPathExtension().lastPathComponent let tree = try XCTUnwrap(JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any]) guard let (path, words) = firstLine(tree) else { continue } XCTAssertEqual(ShellLines.shipped(set, path), ShellLines.said(words, as: ShellLines.speaker), "\(set).\(path)") } XCTAssertEqual(ShellLines.line("no-such-set", "nowhere"), "no-such-set.nowhere", "a missing line shows where it is missing") } /// RULE: A line the core sent stands in place of the kit's, and a business says its own name where a line says Cotton. func testTheCoresLinesAndTheSpeakersName() { ShellLines.receive(["loom-probe": ["greeting": ["hello": "Cotton is open"]]]) XCTAssertEqual(ShellLines.said("Cotton is open", as: "Sharmila"), "Sharmila is open") XCTAssertEqual(ShellLines.said("Cotton is open", as: nil), "Cotton is open") XCTAssertEqual(ShellLines.said("Cotton Voice hears you; Cotton keeps it", as: "Cotton Books"), "Cotton Voice hears you; Cotton Books keeps it") XCTAssertEqual(ShellLines.shipped("loom-probe", "greeting.hello"), ShellLines.said("Cotton is open", as: ShellLines.speaker)) } private func firstLine(_ node: [String: Any], _ prefix: String = "") -> (String, String)? { for key in node.keys.sorted() { let path = prefix.isEmpty ? key : "\(prefix).\(key)" if let words = node[key] as? String, !words.contains("{") { return (path, words) } if let inner = node[key] as? [String: Any], let found = firstLine(inner, path) { return found } } return nil } }