@testable import BaseKit import Foundation import XCTest final class AskViewTests: XCTestCase { /// "Started {when}", in the web's words (ask.js startedText). func testStartedText() { let now = Date(timeIntervalSince1970: 1_768_473_000) // 2026-01-15 10:30 UTC let zone = TimeZone(identifier: "Europe/Dublin") func at(_ seconds: Double) -> String { StartedText.text(now.addingTimeInterval(-seconds), now: now, timeZone: zone) } XCTAssertEqual(at(20), "Started just now") XCTAssertEqual(at(60), "Started 1 minute ago") XCTAssertEqual(at(5 * 60 + 30), "Started 5 minutes ago") XCTAssertEqual(at(3600), "Started 1 hour ago") XCTAssertEqual(at(18 * 3600), "Started 18 hours ago") XCTAssertEqual(at(3 * 86400), "Started on 12 January") } /// Only absolute http(s) links act, as ask.js's webURL; Equipo's own /// paths and mailto are text in Base. func testOnlyWebLinksAct() { XCTAssertTrue(ChatMarkdown.allowsLink("https://fennel.example/menu")) XCTAssertTrue(ChatMarkdown.allowsLink("http://fennel.example")) XCTAssertFalse(ChatMarkdown.allowsLink("/tasks/tk123")) XCTAssertFalse(ChatMarkdown.allowsLink("mailto:ana@fennel.example")) XCTAssertFalse(ChatMarkdown.allowsLink("javascript:alert(1)")) XCTAssertFalse(ChatMarkdown.allowsLink("https://user:pw@fennel.example")) } /// The Assistant's little Markdown: a list is a list, bold is bold, and /// a draft holds back what its next words could change. func testTheAssistantsMarkdownParses() { let tree = ChatMarkdown.parse("Three things:\n- **08:00** Flour\n- [Menu](https://fennel.example/menu)", draft: false) XCTAssertEqual(tree.map(\.kind), ["paragraph", "unordered"]) XCTAssertEqual(tree[1].children.count, 2) let draft = ChatMarkdown.parse("Three things:\n- ", draft: true) XCTAssertEqual(draft.map(\.kind), ["paragraph"], "a marker alone waits for its words") } func testTheWebsCopyIsInTheCatalog() { XCTAssertEqual(BaseText.text("ask.placeholder"), "Ask Base anything") XCTAssertEqual(BaseText.format("ask.pill_a11y", ["title": "Christmas order form", "stage": "One question for you"]), "Christmas order form, One question for you") } } extension AskViewTests { /// Review finding 8 (P2): not_linked offers the web's set-up, and only /// the states Oficina ID can set up offer to set up here. func testNotLinkedOffersTheWebsSetUp() { XCTAssertEqual(AskActions.note(.notLinked).way, .web("native.ask.not_linked.button")) XCTAssertEqual(AskActions.note(.connect).way, .here("ask.connect.button")) XCTAssertEqual(AskActions.note(.reconnect).way, .here("ask.reconnect.button")) XCTAssertNil(AskActions.note(.unavailable).way) XCTAssertEqual(BaseText.text("native.ask.not_linked.button"), "Set up Ask Base on the web") } } extension AskViewTests { /// The web's norunner and oldrunner notes keep their Markdown link, and /// the app draws it as a link to Equipo, never as brackets. func testTheRunnerNotesLinkToEquipo() throws { let equipo = URL(string: "https://equipo.fennel.example")! for (key, words) in [("ask.norunner", "Finish setting it up"), ("ask.oldrunner", "Update it")] { XCTAssertTrue(BaseText.text(key).contains("[\(words)]({equipo})"), "the catalog keeps the web's link") let note = AskView.runnerNote(key, equipo: equipo) let plain = String(note.characters) XCTAssertFalse(plain.contains("[") || plain.contains("]") || plain.contains("("), plain) let links = note.runs.compactMap { run in run.link.map { (String(note[run.range].characters), $0) } } XCTAssertEqual(links.count, 1, plain) XCTAssertEqual(links.first?.0, words) XCTAssertEqual(links.first?.1.host, "equipo.fennel.example") let unlinked = String(AskView.runnerNote(key, equipo: nil).characters) XCTAssertFalse(unlinked.contains("[") || unlinked.contains("{"), unlinked) XCTAssertTrue(unlinked.contains(words)) } } } extension AskViewTests { /// The person's messages take the web's little Markdown (2026-11-26); /// words with none are drawn exactly as written (Fable's S4). func testThePersonsPlainWordsStayPlain() { 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)", "Two lines\nhere", "# Not a heading", "> not a quote", "Gap\n\nbetween", " indented", "1986. A great year", "+ item", "AT&T \\*x"] { XCTAssertTrue(PersonMarkdown.isPlain(PersonMarkdown.parse(plain)), plain) } for rich in ["**Bold**", "*it*", "_it_", "- a\n- b", "1. One", "[Menu](https://fennel.example/menu)", "https://fennel.example/menu"] { XCTAssertFalse(PersonMarkdown.isPlain(PersonMarkdown.parse(rich)), rich) } } /// The web's server draws each of these (TestPersonText); the app's /// tree, written out the same way, must be the same. func testThePersonsMarkdownMatchesTheWeb() 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/person.json"))) XCTAssertGreaterThan(cases.count, 10) 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(PersonMarkdown.parse(c.text)), c.html, c.text) } // A numbered list counts from its first number, the Assistant's too. XCTAssertEqual(MarkdownPresentation.rows(PersonMarkdown.parse("3. Third\n4. Fourth")).map(\.marker), ["3.", "4."]) XCTAssertEqual(MarkdownPresentation.rows(ChatMarkdown.parse("3. Third\n4. Fourth", draft: false)).map(\.marker), ["3.", "4."]) } } extension AskViewTests { /// A message's files decode (an unknown kind is a file, one without an /// id is dropped), and a message without them has none. func testMessageFilesDecode() throws { let raw = """ {"id":"m_1","role":"person","text":"","turn_id":"t_1","created_at":"2026-11-26T10:00:00Z", "attachments":[{"id":"at_abcdefgh12","kind":"image","name":"shot.png","media_type":"image/png","size":2048,"width":640,"height":480}, {"id":"at_zzzzzzzz99","kind":"video","name":"notes.md","media_type":"text/markdown","size":12,"width":0,"height":0}, {"kind":"file","name":"nameless"}]} """ let m = try JSONDecoder().decode(AskMessage.self, from: Data(raw.utf8)) XCTAssertEqual(m.attachments.map(\.name), ["shot.png", "notes.md"]) XCTAssertEqual(m.attachments.map(\.kind), ["image", "file"]) XCTAssertEqual(m.attachments[0].width, 640) XCTAssertEqual(m.attachments[1].mediaType, "text/markdown") let plain = try JSONDecoder().decode(AskMessage.self, from: Data(#"{"id":"m_2","role":"person","text":"hi"}"#.utf8)) XCTAssertEqual(plain.attachments, []) } /// A person's bubble with files and words shows the words, then the /// names line; with files alone, only the names; with words alone, no /// line (plan P9). func testThePersonsBubbleNamesItsFiles() { let files = [AskAttachment(id: "at_abcdefgh12", kind: "image", name: "shot.png"), AskAttachment(id: "at_abcdefgh13", name: "q3.csv")] XCTAssertEqual(PersonBubble.parts(AskMessage(id: "m", role: "person", text: "Look", attachments: files)), .init(showsText: true, names: "shot.png, q3.csv")) XCTAssertEqual(PersonBubble.parts(AskMessage(id: "m", role: "person", text: " ", attachments: files)), .init(showsText: false, names: "shot.png, q3.csv")) XCTAssertEqual(PersonBubble.parts(AskMessage(id: "m", role: "person", text: "Look")), .init(showsText: true, names: nil)) XCTAssertEqual(BaseText.format("ask.files_line_a11y", ["names": "shot.png, q3.csv"]), "Attached: shot.png, q3.csv") } }