| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import EquipoKit |
| 4 | #if os(macOS) |
| 5 | import AppKit |
| 6 | import SwiftUI |
| 7 | @testable import EquipoMac |
| 8 | #endif |
| 9 | |
| 10 | @MainActor @Suite(.serialized) struct ProjectsReviewTests { |
| 11 | func session(_ t: ScriptedTransport) throws -> EquipoSession { |
| 12 | var account = DeviceAccount.new(origin: t.origin, label: "Test") |
| 13 | account.credential = try Fixture.decode("device-credential") |
| 14 | return EquipoSession(account: account, accounts: DeviceAccounts(store: MemorySecretStore()), transport: t) |
| 15 | } |
| 16 | func body(_ request: APIRequest) throws -> [String: String] { |
| 17 | try #require(JSONSerialization.jsonObject(with: request.body!) as? [String: String]) |
| 18 | } |
| 19 | @Test func messageFailuresOnlyShowKnownServerCopy() async throws { |
| 20 | for project in [nil, "p_star"] as [String?] { |
| 21 | for failure in [0, 500, 422] { |
| 22 | let t = ScriptedTransport(); let s = try session(t) |
| 23 | if failure == 0 { t.on("POST /messages", .failure(.offline)) } |
| 24 | else { t.on("POST /messages", .success(APIResponse(status: failure, data: Data("{\"code\":\"refused\",\"copy_key\":\"\(failure == 422 ? "api.repo_not_allowed" : "unrecognised")\",\"request_id\":\"r\"}".utf8)))) } |
| 25 | await s.send("Fix it", projectID: project) |
| 26 | #expect(s.outgoing.first?.notice == (failure == 422 ? EquipoStrings.text("api.repo_not_allowed") : nil)) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | @Test func taskThreadNeverCarriesProject() async throws { |
| 31 | let t = ScriptedTransport(); let s = try session(t) |
| 32 | await s.send("More detail", inThreadOf: "tk1", projectID: "p_star") |
| 33 | let request = try #require(t.requests.first { $0.method == "POST" }) |
| 34 | #expect(try body(request)["project_id"] == nil) |
| 35 | #expect(s.outgoing.first?.projectName == nil) |
| 36 | } |
| 37 | @Test func removedSelectionClearsFromListAndEvent() async throws { |
| 38 | for event in [false, true] { |
| 39 | let t = ScriptedTransport(); let s = try session(t) |
| 40 | t.on("GET /projects", fixture: "project-list"); await s.projectCatalog.refresh() |
| 41 | var p = try #require(s.projectCatalog.items.first); s.projectCatalog.selection = p |
| 42 | if event { p.state = "removed"; p.revision += 1; s.projectCatalog.applyEvent(p, cursor: 100) } |
| 43 | else { t.replace("GET /projects", with: .success(APIResponse(status: 200, data: Data("{\"items\":[],\"cursor\":100}".utf8)))); await s.projectCatalog.refresh() } |
| 44 | #expect(s.projectCatalog.selection == nil) |
| 45 | #expect(s.projectCatalog.selectionNotice == EquipoStrings.text("projects.selection_lost")) |
| 46 | } |
| 47 | } |
| 48 | @Test func listCursorRejectsOlderViewerEventsAndResurrection() async throws { |
| 49 | let t = ScriptedTransport(); let s = try session(t) |
| 50 | var json = try #require(JSONSerialization.jsonObject(with: Fixture.data("project-list")) as? [String: Any]) |
| 51 | json["cursor"] = 100 |
| 52 | t.on("GET /projects", .success(APIResponse(status: 200, data: try JSONSerialization.data(withJSONObject: json)))) |
| 53 | await s.projectCatalog.refresh() |
| 54 | var p = try #require(s.projectCatalog.items.first); p.personalNames = ["stale"] |
| 55 | s.projectCatalog.applyEvent(p, cursor: 99) |
| 56 | s.projectCatalog.applySnapshot([p], cursor: 99) |
| 57 | #expect(s.projectCatalog.projects[p.id]?.personalNames != ["stale"]) |
| 58 | p.id = "removed_project"; s.projectCatalog.applyEvent(p, cursor: 99) |
| 59 | #expect(s.projectCatalog.projects[p.id] == nil) |
| 60 | } |
| 61 | @Test func questionMarkerAndRememberChoiceSurviveRevision() async throws { |
| 62 | let t = ScriptedTransport(); let s = try session(t) |
| 63 | var card: ThinCard = try Fixture.decode("project-card") |
| 64 | #expect(card.isTypedProjectChoice) |
| 65 | #expect(card.answerFieldKey == "projects.other") |
| 66 | let draft = s.questionDraft(for: card) |
| 67 | draft.remembers = false; draft.name = "my edited name" |
| 68 | card.revision += 1 |
| 69 | #expect(s.questionDraft(for: card) === draft) |
| 70 | #expect(!draft.remembers && draft.name == "my edited name") |
| 71 | _ = await draft.submit("a repo", card: card, session: s) |
| 72 | let request = try #require(t.sent("POST /cards/\(card.id)/answer").first) |
| 73 | let fields = try #require(JSONSerialization.jsonObject(with: request.body!) as? [String: Any]) |
| 74 | #expect(fields["remember_name"] as? String == "") |
| 75 | #expect(fields["revision"] as? Int64 == card.revision) |
| 76 | card.placementByName = nil; card.rememberName = nil |
| 77 | #expect(!card.isTypedProjectChoice) |
| 78 | #expect(card.answerFieldKey == "chat.card.answer") |
| 79 | } |
| 80 | @Test func phoneComposerConsumesSelection() async throws { |
| 81 | let t = ScriptedTransport(); let s = try session(t) |
| 82 | let list: ProjectList = try Fixture.decode("project-list") |
| 83 | s.projectCatalog.selection = list.items?.first |
| 84 | let screen = ConversationScreen(session: s, openTask: { _ in }) |
| 85 | let send = screen.sendFromComposer("Hello") |
| 86 | #expect(s.projectCatalog.selection == nil) |
| 87 | await send.value |
| 88 | #expect(try body(#require(t.sent("POST /messages").first))["project_id"] == "p_star") |
| 89 | } |
| 90 | #if os(macOS) |
| 91 | @Test func macComposerConsumesSelectionAndChipIsAboveIt() async throws { |
| 92 | let t = ScriptedTransport(); let s = try session(t) |
| 93 | let list: ProjectList = try Fixture.decode("project-list") |
| 94 | let view = LiveTranscript(session: s, openTask: { _ in }) |
| 95 | view.frame = NSRect(x: 0, y: 0, width: 700, height: 800) |
| 96 | s.projectCatalog.selection = list.items?.first |
| 97 | view.layout() |
| 98 | #expect(view.selectionFrame.maxY <= view.composer.frame.minY) |
| 99 | #expect(view.selectionFrame.height > 0) |
| 100 | view.composer.onSend?("Hello") |
| 101 | #expect(s.projectCatalog.selection == nil) |
| 102 | for _ in 0..<200 where t.sent("POST /messages").isEmpty { await Task.yield() } |
| 103 | #expect(try body(#require(t.sent("POST /messages").first))["project_id"] == "p_star") |
| 104 | view.layout() |
| 105 | #expect(view.selectionFrame.height == 0) |
| 106 | } |
| 107 | @Test func ordinaryQuestionStaysAppKitButPlacementUsesSwiftUI() throws { |
| 108 | let s = try session(ScriptedTransport()) |
| 109 | var card: ThinCard = try Fixture.decode("project-card") |
| 110 | let view = LiveTranscript(session: s, openTask: { _ in }) |
| 111 | view.frame = NSRect(x: 0, y: 0, width: 700, height: 800) |
| 112 | let byline = RosterByline(who: .coordinator, verb: "asks", project: nil, time: nil) |
| 113 | view.update(items: [.question(card, byline)]) |
| 114 | #expect(view.hostedQuestionIDs == [card.id]) |
| 115 | card.id = "ordinary"; card.placementByName = nil; card.rememberName = nil |
| 116 | view.update(items: [.question(card, byline)]) |
| 117 | #expect(view.hostedQuestionIDs.isEmpty) |
| 118 | func fields(_ v: NSView) -> [NSTextField] { (v as? NSTextField).map { [$0] } ?? v.subviews.flatMap(fields) } |
| 119 | #expect(fields(view).contains { $0.isEditable && $0.placeholderString == EquipoStrings.text("chat.card.answer") }) |
| 120 | } |
| 121 | #endif |
| 122 | } |
| 123 | |
| 124 | extension ProjectsReviewTests { |
| 125 | @Test func phoneAndMacControlsUseTestedActionsAndRosterTheme() throws { |
| 126 | let sources = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("Sources") |
| 127 | func source(_ path: String) throws -> String { try String(contentsOf: sources.appendingPathComponent(path), encoding: .utf8) } |
| 128 | let card = try source("EquipoKit/UI/LiveCards.swift") |
| 129 | #expect(card.contains("draft.submit(choice, card: card, session: session)")) |
| 130 | #expect(card.contains("card.answerFieldKey")) |
| 131 | #expect(card.contains("$draft.remembers")) |
| 132 | #expect(!card.contains("remembers = true")) |
| 133 | #expect(!card.contains(".roundedBorder")) |
| 134 | #expect(card.contains("RosterField(")) |
| 135 | #expect(card.contains("RosterCheck(")) |
| 136 | let phone = try source("EquipoKit/UI/Screens/ConversationScreen.swift") |
| 137 | #expect(phone.contains("_ = sendFromComposer(text)")) |
| 138 | let mac = try source("EquipoMac/LiveTranscript.swift") |
| 139 | #expect(mac.contains("_ = session.sendFromComposer(text)")) |
| 140 | #expect(mac.contains(".id(card.id)")) |
| 141 | #expect(!mac.contains(".id(card.revision)")) |
| 142 | let projects = try source("EquipoKit/UI/Screens/ProjectsScreen.swift") |
| 143 | #expect(!projects.contains(".borderedProminent")) |
| 144 | for key in ["projects.new_request", "projects.add_reachable", "projects.forget_name"] { |
| 145 | #expect(projects.contains("RosterButton(title: EquipoStrings.text(\"\(key)\")")) |
| 146 | } |
| 147 | #expect(!(try source("EquipoMac/MacMainView.swift")).contains("ProjectSelectionChip(")) |
| 148 | } |
| 149 | } |
| 150 | |