@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": "