| 1 | @testable import BaseKit |
| 2 | import Foundation |
| 3 | import XCTest |
| 4 | |
| 5 | final class AskViewTests: XCTestCase { |
| 6 | /// "Started {when}", in the web's words (ask.js startedText). |
| 7 | func testStartedText() { |
| 8 | let now = Date(timeIntervalSince1970: 1_768_473_000) // 2026-01-15 10:30 UTC |
| 9 | let zone = TimeZone(identifier: "Europe/Dublin") |
| 10 | func at(_ seconds: Double) -> String { StartedText.text(now.addingTimeInterval(-seconds), now: now, timeZone: zone) } |
| 11 | XCTAssertEqual(at(20), "Started just now") |
| 12 | XCTAssertEqual(at(60), "Started 1 minute ago") |
| 13 | XCTAssertEqual(at(5 * 60 + 30), "Started 5 minutes ago") |
| 14 | XCTAssertEqual(at(3600), "Started 1 hour ago") |
| 15 | XCTAssertEqual(at(18 * 3600), "Started 18 hours ago") |
| 16 | XCTAssertEqual(at(3 * 86400), "Started on 12 January") |
| 17 | } |
| 18 | |
| 19 | /// Only absolute http(s) links act, as ask.js's webURL; Equipo's own |
| 20 | /// paths and mailto are text in Base. |
| 21 | func testOnlyWebLinksAct() { |
| 22 | XCTAssertTrue(ChatMarkdown.allowsLink("https://fennel.example/menu")) |
| 23 | XCTAssertTrue(ChatMarkdown.allowsLink("http://fennel.example")) |
| 24 | XCTAssertFalse(ChatMarkdown.allowsLink("/tasks/tk123")) |
| 25 | XCTAssertFalse(ChatMarkdown.allowsLink("mailto:ana@fennel.example")) |
| 26 | XCTAssertFalse(ChatMarkdown.allowsLink("javascript:alert(1)")) |
| 27 | XCTAssertFalse(ChatMarkdown.allowsLink("https://user:pw@fennel.example")) |
| 28 | } |
| 29 | |
| 30 | /// The Assistant's little Markdown: a list is a list, bold is bold, and |
| 31 | /// a draft holds back what its next words could change. |
| 32 | func testTheAssistantsMarkdownParses() { |
| 33 | let tree = ChatMarkdown.parse("Three things:\n- **08:00** Flour\n- [Menu](https://fennel.example/menu)", draft: false) |
| 34 | XCTAssertEqual(tree.map(\.kind), ["paragraph", "unordered"]) |
| 35 | XCTAssertEqual(tree[1].children.count, 2) |
| 36 | let draft = ChatMarkdown.parse("Three things:\n- ", draft: true) |
| 37 | XCTAssertEqual(draft.map(\.kind), ["paragraph"], "a marker alone waits for its words") |
| 38 | } |
| 39 | |
| 40 | func testTheWebsCopyIsInTheCatalog() { |
| 41 | XCTAssertEqual(BaseText.text("ask.placeholder"), "Ask Base anything") |
| 42 | XCTAssertEqual(BaseText.format("ask.pill_a11y", ["title": "Christmas order form", "stage": "One question for you"]), |
| 43 | "Christmas order form, One question for you") |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | extension AskViewTests { |
| 48 | /// Review finding 8 (P2): not_linked offers the web's set-up, and only |
| 49 | /// the states Oficina ID can set up offer to set up here. |
| 50 | func testNotLinkedOffersTheWebsSetUp() { |
| 51 | XCTAssertEqual(AskActions.note(.notLinked).way, .web("native.ask.not_linked.button")) |
| 52 | XCTAssertEqual(AskActions.note(.connect).way, .here("ask.connect.button")) |
| 53 | XCTAssertEqual(AskActions.note(.reconnect).way, .here("ask.reconnect.button")) |
| 54 | XCTAssertNil(AskActions.note(.unavailable).way) |
| 55 | XCTAssertEqual(BaseText.text("native.ask.not_linked.button"), "Set up Ask Base on the web") |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | extension AskViewTests { |
| 60 | /// The web's norunner and oldrunner notes keep their Markdown link, and |
| 61 | /// the app draws it as a link to Equipo, never as brackets. |
| 62 | func testTheRunnerNotesLinkToEquipo() throws { |
| 63 | let equipo = URL(string: "https://equipo.fennel.example")! |
| 64 | for (key, words) in [("ask.norunner", "Finish setting it up"), ("ask.oldrunner", "Update it")] { |
| 65 | XCTAssertTrue(BaseText.text(key).contains("[\(words)]({equipo})"), "the catalog keeps the web's link") |
| 66 | let note = AskView.runnerNote(key, equipo: equipo) |
| 67 | let plain = String(note.characters) |
| 68 | XCTAssertFalse(plain.contains("[") || plain.contains("]") || plain.contains("("), plain) |
| 69 | let links = note.runs.compactMap { run in run.link.map { (String(note[run.range].characters), $0) } } |
| 70 | XCTAssertEqual(links.count, 1, plain) |
| 71 | XCTAssertEqual(links.first?.0, words) |
| 72 | XCTAssertEqual(links.first?.1.host, "equipo.fennel.example") |
| 73 | let unlinked = String(AskView.runnerNote(key, equipo: nil).characters) |
| 74 | XCTAssertFalse(unlinked.contains("[") || unlinked.contains("{"), unlinked) |
| 75 | XCTAssertTrue(unlinked.contains(words)) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | extension AskViewTests { |
| 81 | /// The person's messages take the web's little Markdown (2026-11-26); |
| 82 | /// words with none are drawn exactly as written (Fable's S4). |
| 83 | func testThePersonsPlainWordsStayPlain() { |
| 84 | for plain in ["What's on tomorrow?", "2*3 and a_b_c", "2*3*4", "a*b + c*d", "x**2 + y**2", "__init__.py", "f(*args) and g(*rest)", |
| 85 | "Two lines\nhere", "# Not a heading", "> not a quote", "Gap\n\nbetween", " indented", "1986. A great year", "+ item", "AT&T \\*x"] { |
| 86 | XCTAssertTrue(PersonMarkdown.isPlain(PersonMarkdown.parse(plain)), plain) |
| 87 | } |
| 88 | for rich in ["**Bold**", "*it*", "_it_", "- a\n- b", "1. One", "[Menu](https://fennel.example/menu)", "https://fennel.example/menu"] { |
| 89 | XCTAssertFalse(PersonMarkdown.isPlain(PersonMarkdown.parse(rich)), rich) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// The web's server draws each of these (TestPersonText); the app's |
| 94 | /// tree, written out the same way, must be the same. |
| 95 | func testThePersonsMarkdownMatchesTheWeb() throws { |
| 96 | struct Case: Decodable { let text: String; let html: String } |
| 97 | let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent() |
| 98 | .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent() |
| 99 | let cases = try JSONDecoder().decode([Case].self, from: Data(contentsOf: root.appendingPathComponent("internal/web/testdata/person.json"))) |
| 100 | XCTAssertGreaterThan(cases.count, 10) |
| 101 | func escape(_ s: String) -> String { |
| 102 | s.replacingOccurrences(of: "&", with: "&").replacingOccurrences(of: "<", with: "<").replacingOccurrences(of: ">", with: ">") |
| 103 | .replacingOccurrences(of: "'", with: "'").replacingOccurrences(of: "\"", with: """) |
| 104 | } |
| 105 | func html(_ nodes: [ChatMarkdownNode]) -> String { |
| 106 | nodes.map { n in |
| 107 | switch n.kind { |
| 108 | case "text": escape(n.text ?? "") |
| 109 | case "strong": "<strong>\(html(n.children))</strong>" |
| 110 | case "emphasis": "<em>\(html(n.children))</em>" |
| 111 | case "link": "<a href=\"\(escape(n.target ?? ""))\" target=\"_blank\" rel=\"noopener\">\(html(n.children))</a>" |
| 112 | case "unordered": "<ul>\(html(n.children))</ul>" |
| 113 | case "ordered": (n.start.map { "<ol start=\"\($0)\">" } ?? "<ol>") + html(n.children) + "</ol>" |
| 114 | case "item": "<li>\(html(n.children))</li>" |
| 115 | default: html(n.children) |
| 116 | } |
| 117 | }.joined() |
| 118 | } |
| 119 | for c in cases { |
| 120 | XCTAssertEqual(html(PersonMarkdown.parse(c.text)), c.html, c.text) |
| 121 | } |
| 122 | // A numbered list counts from its first number, the Assistant's too. |
| 123 | XCTAssertEqual(MarkdownPresentation.rows(PersonMarkdown.parse("3. Third\n4. Fourth")).map(\.marker), ["3.", "4."]) |
| 124 | XCTAssertEqual(MarkdownPresentation.rows(ChatMarkdown.parse("3. Third\n4. Fourth", draft: false)).map(\.marker), ["3.", "4."]) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | extension AskViewTests { |
| 129 | /// A message's files decode (an unknown kind is a file, one without an |
| 130 | /// id is dropped), and a message without them has none. |
| 131 | func testMessageFilesDecode() throws { |
| 132 | let raw = """ |
| 133 | {"id":"m_1","role":"person","text":"","turn_id":"t_1","created_at":"2026-11-26T10:00:00Z", |
| 134 | "attachments":[{"id":"at_abcdefgh12","kind":"image","name":"shot.png","media_type":"image/png","size":2048,"width":640,"height":480}, |
| 135 | {"id":"at_zzzzzzzz99","kind":"video","name":"notes.md","media_type":"text/markdown","size":12,"width":0,"height":0}, |
| 136 | {"kind":"file","name":"nameless"}]} |
| 137 | """ |
| 138 | let m = try JSONDecoder().decode(AskMessage.self, from: Data(raw.utf8)) |
| 139 | XCTAssertEqual(m.attachments.map(\.name), ["shot.png", "notes.md"]) |
| 140 | XCTAssertEqual(m.attachments.map(\.kind), ["image", "file"]) |
| 141 | XCTAssertEqual(m.attachments[0].width, 640) |
| 142 | XCTAssertEqual(m.attachments[1].mediaType, "text/markdown") |
| 143 | let plain = try JSONDecoder().decode(AskMessage.self, from: Data(#"{"id":"m_2","role":"person","text":"hi"}"#.utf8)) |
| 144 | XCTAssertEqual(plain.attachments, []) |
| 145 | } |
| 146 | |
| 147 | /// A person's bubble with files and words shows the words, then the |
| 148 | /// names line; with files alone, only the names; with words alone, no |
| 149 | /// line (plan P9). |
| 150 | func testThePersonsBubbleNamesItsFiles() { |
| 151 | let files = [AskAttachment(id: "at_abcdefgh12", kind: "image", name: "shot.png"), AskAttachment(id: "at_abcdefgh13", name: "q3.csv")] |
| 152 | XCTAssertEqual(PersonBubble.parts(AskMessage(id: "m", role: "person", text: "Look", attachments: files)), |
| 153 | .init(showsText: true, names: "shot.png, q3.csv")) |
| 154 | XCTAssertEqual(PersonBubble.parts(AskMessage(id: "m", role: "person", text: " ", attachments: files)), |
| 155 | .init(showsText: false, names: "shot.png, q3.csv")) |
| 156 | XCTAssertEqual(PersonBubble.parts(AskMessage(id: "m", role: "person", text: "Look")), .init(showsText: true, names: nil)) |
| 157 | XCTAssertEqual(BaseText.format("ask.files_line_a11y", ["names": "shot.png, q3.csv"]), "Attached: shot.png, q3.csv") |
| 158 | } |
| 159 | } |
| 160 | |