| 1 | @testable import BaseKit |
| 2 | import Foundation |
| 3 | import RastrilloApple |
| 4 | import RastrilloTestSupport |
| 5 | import SwiftUI |
| 6 | import XCTest |
| 7 | |
| 8 | /// The Assistant's abilities in the app: what it looked up for a reply |
| 9 | /// (docs/api/base-native-v1.md, Lookup), drawn as the web draws it |
| 10 | /// (chat.html `ask-lookup`), and a held job's Carry on now. |
| 11 | final class AbilitiesTests: XCTestCase { |
| 12 | /// A reply's lookups decode with https sources only; one left without |
| 13 | /// a source, or without an answer, is dropped; a person's message has |
| 14 | /// none. The Sources line is the label, then each title (a link) and |
| 15 | /// its host. |
| 16 | func testLookupAnswerAndSources() throws { |
| 17 | let raw = #""" |
| 18 | {"id":"m1","role":"assistant","text":"Here it is.","turn_id":"t1","lookups":[ |
| 19 | {"answer":"Rain later.","sources":[{"url":"https://www.met.test/dublin","title":"Met"},{"url":"http://plain.test/","title":"Plain"}, |
| 20 | {"url":"javascript:alert(1)","title":"Bad"},{"url":"https://b.test/","title":""}]}, |
| 21 | {"answer":"No sources.","sources":[{"url":"http://plain.test/","title":"Plain"}]}, |
| 22 | {"answer":"","sources":[{"url":"https://c.test/","title":"C"}]}]} |
| 23 | """# |
| 24 | let m = try JSONDecoder().decode(AskMessage.self, from: Data(raw.utf8)) |
| 25 | XCTAssertEqual(m.lookups.count, 1) |
| 26 | let l = try XCTUnwrap(m.lookups.first) |
| 27 | XCTAssertEqual(l.answer, "Rain later.") |
| 28 | XCTAssertEqual(l.sources.map { $0.url?.absoluteString }, ["https://www.met.test/dublin", "https://b.test/"]) |
| 29 | XCTAssertEqual(l.sources.map(\.host), ["met.test", "b.test"]) |
| 30 | XCTAssertEqual(l.sources.map(\.label), ["Met", "b.test"]) |
| 31 | |
| 32 | 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)) |
| 33 | XCTAssertEqual(person.lookups, []) |
| 34 | let none = try JSONDecoder().decode(AskMessage.self, from: Data(#"{"id":"m3","role":"assistant","text":"Hi"}"#.utf8)) |
| 35 | XCTAssertEqual(none.lookups, []) |
| 36 | |
| 37 | let line = LookupView.sources(l.sources, label: BaseText.text("ask.sources"), palette: Palette(.light)) |
| 38 | XCTAssertEqual(String(line.characters), "Sources Met met.test b.test b.test") |
| 39 | let links = line.runs.compactMap(\.link) |
| 40 | XCTAssertEqual(links.map(\.absoluteString), ["https://www.met.test/dublin", "https://b.test/"]) |
| 41 | XCTAssertEqual(BaseText.text("ask.sources"), "Sources") |
| 42 | } |
| 43 | |
| 44 | /// The web's server draws each of these (TestLookupMarkdown): no link |
| 45 | /// at all, whatever the answer says. |
| 46 | func testLookupMarkdownNoLinks() throws { |
| 47 | struct Case: Decodable { let text: String; let html: String } |
| 48 | let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent() |
| 49 | .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent() |
| 50 | let cases = try JSONDecoder().decode([Case].self, from: Data(contentsOf: root.appendingPathComponent("internal/web/testdata/lookup.json"))) |
| 51 | XCTAssertGreaterThan(cases.count, 4) |
| 52 | func escape(_ s: String) -> String { |
| 53 | s.replacingOccurrences(of: "&", with: "&").replacingOccurrences(of: "<", with: "<").replacingOccurrences(of: ">", with: ">") |
| 54 | .replacingOccurrences(of: "'", with: "'").replacingOccurrences(of: "\"", with: """) |
| 55 | } |
| 56 | func html(_ nodes: [ChatMarkdownNode]) -> String { |
| 57 | nodes.map { n in |
| 58 | switch n.kind { |
| 59 | case "text": escape(n.text ?? "") |
| 60 | case "strong": "<strong>\(html(n.children))</strong>" |
| 61 | case "emphasis": "<em>\(html(n.children))</em>" |
| 62 | case "link": "<a>\(html(n.children))</a>" |
| 63 | case "unordered": "<ul>\(html(n.children))</ul>" |
| 64 | case "ordered": (n.start.map { "<ol start=\"\($0)\">" } ?? "<ol>") + html(n.children) + "</ol>" |
| 65 | case "item": "<li>\(html(n.children))</li>" |
| 66 | default: html(n.children) |
| 67 | } |
| 68 | }.joined() |
| 69 | } |
| 70 | for c in cases { |
| 71 | XCTAssertEqual(html(LookupMarkdown.parse(c.text)), c.html, c.text) |
| 72 | } |
| 73 | // The person's own words keep their links. |
| 74 | XCTAssertTrue(html(PersonMarkdown.parse("[Met](https://met.test/)")).hasPrefix("<a>")) |
| 75 | } |
| 76 | |
| 77 | /// The app says it draws lookups on Ask's two reads, as a query |
| 78 | /// parameter (the kit sets no header), and on nothing else. |
| 79 | func testClientCapabilityLookup() async throws { |
| 80 | XCTAssertEqual(CapabilityTransport.path("/ask/state"), "/ask/state?client_caps=lookup_answers") |
| 81 | XCTAssertEqual(CapabilityTransport.path("/ask/events?after=42"), "/ask/events?after=42&client_caps=lookup_answers") |
| 82 | XCTAssertEqual(CapabilityTransport.path("/ask/messages"), "/ask/messages") |
| 83 | XCTAssertEqual(CapabilityTransport.path("/api/v1/me"), "/api/v1/me") |
| 84 | |
| 85 | let inner = ScriptedTransport([json(200, #"{"state":"connect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0}"#), |
| 86 | json(200, #"{"message":{"id":"m1","role":"person","text":"Hi","turn_id":"t1"},"turn":{"id":"t1","kind":"chat","state":"queued"}}"#), |
| 87 | .respond(status: 200, body: Data())]) |
| 88 | let client = BaseClient(transport: CapabilityTransport(inner)) {} |
| 89 | _ = try await client.askState() |
| 90 | _ = try await client.send("Hi", key: "k1") |
| 91 | try? await client.events(after: 7) { _, _ in } |
| 92 | XCTAssertEqual(inner.calls.map(\.path), ["/ask/state?client_caps=lookup_answers", "/ask/messages", "/ask/events?after=7&client_caps=lookup_answers"]) |
| 93 | } |
| 94 | |
| 95 | /// A job held while building was off, once it is on again, offers |
| 96 | /// Carry on now as a paused one does (plan AD4); without carry_on, or |
| 97 | /// in any other state, it doesn't. |
| 98 | func testStoppedCarryOn() throws { |
| 99 | func card(_ state: String, _ carryOn: Bool) throws -> AskCard { |
| 100 | try JSONDecoder().decode(AskCard.self, from: Data(#"{"id":"c1","revision":3,"kind":"job","state":"\#(state)","question":"Bakery website","result":"Stopped","carry_on":\#(carryOn)}"#.utf8)) |
| 101 | } |
| 102 | XCTAssertTrue(try card("stopped", true).offersCarryOn) |
| 103 | XCTAssertTrue(try card("paused", true).offersCarryOn) |
| 104 | XCTAssertFalse(try card("stopped", false).offersCarryOn) |
| 105 | XCTAssertFalse(try card("ready", true).offersCarryOn) |
| 106 | XCTAssertFalse(try card("working", true).offersCarryOn) |
| 107 | } |
| 108 | } |
| 109 | |