|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import type { ProgressCard } from "@openclaw/gateway-protocol"; |
| 4 | +import { render } from "lit"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import type { ControlUiSessionPullRequestSnapshot } from "../../../src/gateway/control-ui-contract.js"; |
| 7 | +import type { SidebarRecentSession } from "./app-sidebar-session-types.ts"; |
| 8 | +import { renderSessionHovercard } from "./session-hovercard.ts"; |
| 9 | + |
| 10 | +function row(overrides: Partial<SidebarRecentSession> = {}): SidebarRecentSession { |
| 11 | + return { |
| 12 | + key: "agent:main:work", |
| 13 | + label: "Ship the release", |
| 14 | + startedAt: Date.now() - 2 * 60 * 60_000, |
| 15 | + updatedAt: Date.now() - 5 * 60_000, |
| 16 | + owner: { actor: { type: "human", id: "alice", label: "Alice Baker" } }, |
| 17 | + children: [], |
| 18 | + ...overrides, |
| 19 | + } as SidebarRecentSession; |
| 20 | +} |
| 21 | + |
| 22 | +function snapshot( |
| 23 | + overrides: Partial<ControlUiSessionPullRequestSnapshot> = {}, |
| 24 | +): ControlUiSessionPullRequestSnapshot { |
| 25 | + return { status: "ready", pullRequests: [], rateLimited: false, ...overrides }; |
| 26 | +} |
| 27 | + |
| 28 | +function progressCard(): ProgressCard { |
| 29 | + return { |
| 30 | + sessionKey: "agent:main:work", |
| 31 | + revision: 1, |
| 32 | + updatedAt: Date.now(), |
| 33 | + markdown: "**Release** is ready.", |
| 34 | + steps: [{ step: "Verify", status: "in_progress" }], |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +describe("renderSessionHovercard", () => { |
| 39 | + beforeEach(() => { |
| 40 | + vi.useFakeTimers(); |
| 41 | + vi.setSystemTime(new Date("2026-08-17T12:00:00Z")); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + vi.useRealTimers(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("renders synchronous row metadata without inventing a progress section", () => { |
| 49 | + const container = document.createElement("div"); |
| 50 | + render(renderSessionHovercard({ row: row() }), container); |
| 51 | + |
| 52 | + expect(container.querySelector(".session-hovercard__title")?.textContent).toBe( |
| 53 | + "Ship the release", |
| 54 | + ); |
| 55 | + expect(container.querySelector(".session-hovercard__avatar")?.textContent).toBe("AB"); |
| 56 | + const metadata = container.querySelector(".session-hovercard__meta")?.textContent ?? ""; |
| 57 | + expect(metadata).toContain("Alice Baker"); |
| 58 | + expect(metadata).toContain("created"); |
| 59 | + expect(metadata).toContain("updated"); |
| 60 | + expect(container.querySelector(".session-progress-card")).toBeNull(); |
| 61 | + expect(container.querySelector(".session-hovercard__excerpt")).toBeNull(); |
| 62 | + expect(container.querySelector(".session-hovercard__divider")).toBeNull(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("renders bounded linked PR chips with state, CI, and diff facts", () => { |
| 66 | + const container = document.createElement("div"); |
| 67 | + render( |
| 68 | + renderSessionHovercard({ |
| 69 | + pullRequests: snapshot({ |
| 70 | + pullRequests: [ |
| 71 | + { |
| 72 | + number: 101, |
| 73 | + owner: "openclaw", |
| 74 | + repo: "openclaw", |
| 75 | + branch: "feature", |
| 76 | + title: "First", |
| 77 | + url: "https://github.com/openclaw/openclaw/pull/101", |
| 78 | + state: "open", |
| 79 | + changedFiles: 2, |
| 80 | + additions: 7, |
| 81 | + deletions: 3, |
| 82 | + checks: { state: "passing", passed: 2, failed: 0, skipped: 0, running: 0 }, |
| 83 | + }, |
| 84 | + { |
| 85 | + number: 102, |
| 86 | + owner: "openclaw", |
| 87 | + repo: "openclaw", |
| 88 | + branch: "feature", |
| 89 | + title: "Second", |
| 90 | + url: "https://github.com/openclaw/openclaw/pull/102", |
| 91 | + state: "draft", |
| 92 | + }, |
| 93 | + { |
| 94 | + number: 103, |
| 95 | + owner: "openclaw", |
| 96 | + repo: "openclaw", |
| 97 | + branch: "feature", |
| 98 | + title: "Third", |
| 99 | + url: "https://github.com/openclaw/openclaw/pull/103", |
| 100 | + state: "merged", |
| 101 | + }, |
| 102 | + { |
| 103 | + number: 104, |
| 104 | + owner: "openclaw", |
| 105 | + repo: "openclaw", |
| 106 | + branch: "feature", |
| 107 | + title: "Fourth", |
| 108 | + url: "https://github.com/openclaw/openclaw/pull/104", |
| 109 | + state: "closed", |
| 110 | + }, |
| 111 | + ], |
| 112 | + }), |
| 113 | + }), |
| 114 | + container, |
| 115 | + ); |
| 116 | + |
| 117 | + const links = [...container.querySelectorAll<HTMLAnchorElement>(".session-hovercard__pr-chip")]; |
| 118 | + expect(links).toHaveLength(3); |
| 119 | + expect(links[0]?.href).toBe("https://github.com/openclaw/openclaw/pull/101"); |
| 120 | + expect(links[0]?.target).toBe("_blank"); |
| 121 | + expect(links[0]?.rel).toContain("noopener"); |
| 122 | + expect(links[0]?.querySelector(".session-hovercard__pr-number")?.textContent).toBe("#101"); |
| 123 | + expect(links[0]?.querySelector(".session-hovercard__pr-state")?.textContent).toBe("Open"); |
| 124 | + expect(links[0]?.querySelector("[data-checks='passing']")?.textContent).toBe("✓"); |
| 125 | + expect(links[0]?.querySelector(".session-hovercard__files")?.textContent).toBe("2 files"); |
| 126 | + expect(links[0]?.querySelector(".session-hovercard__additions")?.textContent).toBe("+7"); |
| 127 | + expect(links[0]?.querySelector(".session-hovercard__deletions")?.textContent).toBe("−3"); |
| 128 | + expect(container.querySelector(".session-hovercard__more")?.textContent).toBe("+1 more"); |
| 129 | + }); |
| 130 | + |
| 131 | + it("falls back to branch and diff chips with a create-PR link", () => { |
| 132 | + const container = document.createElement("div"); |
| 133 | + render( |
| 134 | + renderSessionHovercard({ |
| 135 | + row: row({ workSession: true, subtitle: "openclaw/openclaw · feature" }), |
| 136 | + pullRequests: snapshot({ |
| 137 | + branch: { |
| 138 | + owner: "openclaw", |
| 139 | + repo: "openclaw", |
| 140 | + branch: "feature", |
| 141 | + changedFiles: 3, |
| 142 | + additions: 12, |
| 143 | + deletions: 4, |
| 144 | + createUrl: "https://github.com/openclaw/openclaw/pull/new/feature", |
| 145 | + }, |
| 146 | + }), |
| 147 | + }), |
| 148 | + container, |
| 149 | + ); |
| 150 | + |
| 151 | + expect(container.querySelector(".session-hovercard__branch-chip")?.textContent).toBe( |
| 152 | + "openclaw/openclaw · feature", |
| 153 | + ); |
| 154 | + expect(container.querySelector(".session-hovercard__files")?.textContent).toBe("3 files"); |
| 155 | + expect(container.querySelector(".session-hovercard__additions")?.textContent).toBe("+12"); |
| 156 | + expect(container.querySelector(".session-hovercard__deletions")?.textContent).toBe("−4"); |
| 157 | + const createLink = container.querySelector<HTMLAnchorElement>(".session-hovercard__no-pr a"); |
| 158 | + expect(createLink?.textContent).toBe("No PR yet"); |
| 159 | + expect(createLink?.href).toBe("https://github.com/openclaw/openclaw/pull/new/feature"); |
| 160 | + }); |
| 161 | + |
| 162 | + it("renders the latest turn as plain text when progress is absent", () => { |
| 163 | + const container = document.createElement("div"); |
| 164 | + render( |
| 165 | + renderSessionHovercard({ |
| 166 | + row: row({ lastMessagePreview: " Finished <strong>without markup</strong>. " }), |
| 167 | + }), |
| 168 | + container, |
| 169 | + ); |
| 170 | + |
| 171 | + expect(container.querySelector(".session-hovercard__excerpt")?.textContent).toBe( |
| 172 | + "Finished <strong>without markup</strong>.", |
| 173 | + ); |
| 174 | + expect(container.querySelector(".session-hovercard__excerpt strong")).toBeNull(); |
| 175 | + expect(container.querySelector(".session-progress-card")).toBeNull(); |
| 176 | + }); |
| 177 | + |
| 178 | + it("renders progress instead of the latest-turn excerpt", () => { |
| 179 | + const container = document.createElement("div"); |
| 180 | + render( |
| 181 | + renderSessionHovercard({ |
| 182 | + row: row({ lastMessagePreview: "This must not appear." }), |
| 183 | + progressCard: progressCard(), |
| 184 | + }), |
| 185 | + container, |
| 186 | + ); |
| 187 | + |
| 188 | + expect(container.querySelector(".session-progress-card")?.textContent).toContain("Release"); |
| 189 | + expect(container.querySelector(".session-hovercard__excerpt")).toBeNull(); |
| 190 | + expect(container.textContent).not.toContain("This must not appear."); |
| 191 | + }); |
| 192 | + |
| 193 | + it("renders nothing when no session facts are known", () => { |
| 194 | + const container = document.createElement("div"); |
| 195 | + render(renderSessionHovercard({}), container); |
| 196 | + |
| 197 | + expect(container.childElementCount).toBe(0); |
| 198 | + }); |
| 199 | +}); |
0 commit comments