@testable import BaseKit import Foundation import RastrilloApple import RastrilloTestSupport import SwiftUI import XCTest /// The Assistant's abilities in the app: what it looked up for a reply /// (docs/api/base-native-v1.md, Lookup), drawn as the web draws it /// (chat.html `ask-lookup`), and a held job's Carry on now. final class AbilitiesTests: XCTestCase { /// A reply's lookups decode with https sources only; one left without /// a source, or without an answer, is dropped; a person's message has /// none. The Sources line is the label, then each title (a link) and /// its host. func testLookupAnswerAndSources() throws { let raw = #""" {"id":"m1","role":"assistant","text":"Here it is.","turn_id":"t1","lookups":[ {"answer":"Rain later.","sources":[{"url":"https://www.met.test/dublin","title":"Met"},{"url":"http://plain.test/","title":"Plain"}, {"url":"javascript:alert(1)","title":"Bad"},{"url":"https://b.test/","title":""}]}, {"answer":"No sources.","sources":[{"url":"http://plain.test/","title":"Plain"}]}, {"answer":"","sources":[{"url":"https://c.test/","title":"C"}]}]} """# let m = try JSONDecoder().decode(AskMessage.self, from: Data(raw.utf8)) XCTAssertEqual(m.lookups.count, 1) let l = try XCTUnwrap(m.lookups.first) XCTAssertEqual(l.answer, "Rain later.") XCTAssertEqual(l.sources.map { $0.url?.absoluteString }, ["https://www.met.test/dublin", "https://b.test/"]) XCTAssertEqual(l.sources.map(\.host), ["met.test", "b.test"]) XCTAssertEqual(l.sources.map(\.label), ["Met", "b.test"]) let person = try JSONDecoder().decode(AskMessage.self, from: Data(#"{"id":"m2","role":"person","text":"Hi","lookups":[{"answer":"x","sources":[{"url":"https://a.test/","title":"A"}]}]}"#.utf8)) XCTAssertEqual(person.lookups, []) let none = try JSONDecoder().decode(AskMessage.self, from: Data(#"{"id":"m3","role":"assistant","text":"Hi"}"#.utf8)) XCTAssertEqual(none.lookups, []) let line = LookupView.sources(l.sources, label: BaseText.text("ask.sources"), palette: Palette(.light)) XCTAssertEqual(String(line.characters), "Sources Met met.test b.test b.test") let links = line.runs.compactMap(\.link) XCTAssertEqual(links.map(\.absoluteString), ["https://www.met.test/dublin", "https://b.test/"]) XCTAssertEqual(BaseText.text("ask.sources"), "Sources") } /// The web's server draws each of these (TestLookupMarkdown): no link /// at all, whatever the answer says. func testLookupMarkdownNoLinks() throws { struct Case: Decodable { let text: String; let html: String } let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent() .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent() let cases = try JSONDecoder().decode([Case].self, from: Data(contentsOf: root.appendingPathComponent("internal/web/testdata/lookup.json"))) XCTAssertGreaterThan(cases.count, 4) func escape(_ s: String) -> String { s.replacingOccurrences(of: "&", with: "&").replacingOccurrences(of: "<", with: "<").replacingOccurrences(of: ">", with: ">") .replacingOccurrences(of: "'", with: "'").replacingOccurrences(of: "\"", with: """) } func html(_ nodes: [ChatMarkdownNode]) -> String { nodes.map { n in switch n.kind { case "text": escape(n.text ?? "") case "strong": "\(html(n.children))" case "emphasis": "\(html(n.children))" case "link": "\(html(n.children))" case "unordered": "" case "ordered": (n.start.map { "
    " } ?? "
      ") + html(n.children) + "
    " case "item": "
  1. \(html(n.children))
  2. " default: html(n.children) } }.joined() } for c in cases { XCTAssertEqual(html(LookupMarkdown.parse(c.text)), c.html, c.text) } // The person's own words keep their links. XCTAssertTrue(html(PersonMarkdown.parse("[Met](https://met.test/)")).hasPrefix("")) } /// The app says it draws lookups on Ask's two reads, as a query /// parameter (the kit sets no header), and on nothing else. func testClientCapabilityLookup() async throws { XCTAssertEqual(CapabilityTransport.path("/ask/state"), "/ask/state?client_caps=lookup_answers") XCTAssertEqual(CapabilityTransport.path("/ask/events?after=42"), "/ask/events?after=42&client_caps=lookup_answers") XCTAssertEqual(CapabilityTransport.path("/ask/messages"), "/ask/messages") XCTAssertEqual(CapabilityTransport.path("/api/v1/me"), "/api/v1/me") let inner = ScriptedTransport([json(200, #"{"state":"connect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0}"#), json(200, #"{"message":{"id":"m1","role":"person","text":"Hi","turn_id":"t1"},"turn":{"id":"t1","kind":"chat","state":"queued"}}"#), .respond(status: 200, body: Data())]) let client = BaseClient(transport: CapabilityTransport(inner)) {} _ = try await client.askState() _ = try await client.send("Hi", key: "k1") try? await client.events(after: 7) { _, _ in } XCTAssertEqual(inner.calls.map(\.path), ["/ask/state?client_caps=lookup_answers", "/ask/messages", "/ask/events?after=7&client_caps=lookup_answers"]) } /// A job held while building was off, once it is on again, offers /// Carry on now as a paused one does (plan AD4); without carry_on, or /// in any other state, it doesn't. func testStoppedCarryOn() throws { func card(_ state: String, _ carryOn: Bool) throws -> AskCard { try JSONDecoder().decode(AskCard.self, from: Data(#"{"id":"c1","revision":3,"kind":"job","state":"\#(state)","question":"Bakery website","result":"Stopped","carry_on":\#(carryOn)}"#.utf8)) } XCTAssertTrue(try card("stopped", true).offersCarryOn) XCTAssertTrue(try card("paused", true).offersCarryOn) XCTAssertFalse(try card("stopped", false).offersCarryOn) XCTAssertFalse(try card("ready", true).offersCarryOn) XCTAssertFalse(try card("working", true).offersCarryOn) } }