|
1 | 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { GatewayRequestError } from "../../api/gateway.ts"; |
2 | 3 | import type { ApplicationContext } from "../../app/context.ts"; |
3 | 4 | import type { BoardWidget } from "../../lib/board/types.ts"; |
4 | 5 | import { createApplicationContextProvider } from "../../test-helpers/application-context.ts"; |
@@ -124,6 +125,184 @@ describe("plugin board widget cells", () => { |
124 | 125 | expect(cell.querySelector('[data-test-id="board-widget-error"]')).toBeNull(); |
125 | 126 | }); |
126 | 127 |
|
| 128 | + it("renders an advertised session progress card and its empty state", async () => { |
| 129 | + const dashboardSessionKey = "agent:main:dashboard"; |
| 130 | + const targetSessionKey = "agent:main:target"; |
| 131 | + const responses = [ |
| 132 | + { |
| 133 | + props: { sessionKey: targetSessionKey }, |
| 134 | + response: { |
| 135 | + card: { |
| 136 | + sessionKey: targetSessionKey, |
| 137 | + revision: 1, |
| 138 | + updatedAt: 1, |
| 139 | + markdown: "**Release** validation is active.", |
| 140 | + steps: [{ step: "Run focused checks", status: "in_progress" }], |
| 141 | + }, |
| 142 | + }, |
| 143 | + text: "Run focused checks", |
| 144 | + requestedSessionKey: targetSessionKey, |
| 145 | + }, |
| 146 | + { |
| 147 | + props: undefined, |
| 148 | + response: { card: null }, |
| 149 | + text: "No progress card yet", |
| 150 | + requestedSessionKey: dashboardSessionKey, |
| 151 | + }, |
| 152 | + ] as const; |
| 153 | + |
| 154 | + for (const [index, scenario] of responses.entries()) { |
| 155 | + const request = vi.fn(async () => scenario.response); |
| 156 | + const context = { |
| 157 | + gateway: { |
| 158 | + snapshot: { |
| 159 | + phase: "connected", |
| 160 | + client: { request }, |
| 161 | + hello: { |
| 162 | + features: { methods: ["progressCard.get"] }, |
| 163 | + controlUiWidgetKinds: [ |
| 164 | + { pluginId: "session", kind: "session:progress", label: "Session progress" }, |
| 165 | + ], |
| 166 | + }, |
| 167 | + }, |
| 168 | + subscribe: () => () => undefined, |
| 169 | + subscribeEvents: () => () => undefined, |
| 170 | + }, |
| 171 | + } as unknown as ApplicationContext; |
| 172 | + const widget: BoardWidget = { |
| 173 | + name: `session-progress-${index}`, |
| 174 | + tabId: "main", |
| 175 | + title: "Session progress", |
| 176 | + contentKind: "plugin", |
| 177 | + pluginKind: "session:progress", |
| 178 | + ...(scenario.props ? { props: scenario.props } : {}), |
| 179 | + sizeW: 6, |
| 180 | + sizeH: 4, |
| 181 | + position: index, |
| 182 | + grantState: "none", |
| 183 | + revision: 1, |
| 184 | + }; |
| 185 | + const provider = createApplicationContextProvider(context); |
| 186 | + const cell = document.createElement("openclaw-board-widget-cell"); |
| 187 | + cell.widget = widget; |
| 188 | + cell.rect = { name: widget.name, x: 0, y: index * 4, w: 6, h: 4 }; |
| 189 | + cell.sessionKey = dashboardSessionKey; |
| 190 | + cell.active = index !== 0; |
| 191 | + cell.callbacks = callbacks(); |
| 192 | + provider.append(cell); |
| 193 | + document.body.append(provider); |
| 194 | + |
| 195 | + if (index === 0) { |
| 196 | + await cell.updateComplete; |
| 197 | + expect(request).not.toHaveBeenCalled(); |
| 198 | + cell.active = true; |
| 199 | + } |
| 200 | + |
| 201 | + await vi.waitFor( |
| 202 | + () => |
| 203 | + expect(cell.querySelector("openclaw-session-progress-widget")?.textContent).toContain( |
| 204 | + scenario.text, |
| 205 | + ), |
| 206 | + CHUNK_LOAD_WAIT, |
| 207 | + ); |
| 208 | + expect(request).toHaveBeenCalledWith("progressCard.get", { |
| 209 | + sessionKey: scenario.requestedSessionKey, |
| 210 | + }); |
| 211 | + } |
| 212 | + }); |
| 213 | + |
| 214 | + it("surfaces a failed session progress read and retries it", async () => { |
| 215 | + const sessionKey = "agent:main:protected"; |
| 216 | + let attempts = 0; |
| 217 | + const deniedSessionKey = "agent:main:private"; |
| 218 | + const request = vi.fn(async (_method: string, params: { sessionKey: string }) => { |
| 219 | + if (params.sessionKey === deniedSessionKey) { |
| 220 | + throw new GatewayRequestError({ |
| 221 | + code: "INVALID_REQUEST", |
| 222 | + message: "session is private for this connection", |
| 223 | + details: { code: "SESSION_PARTICIPATION_REQUIRED" }, |
| 224 | + }); |
| 225 | + } |
| 226 | + attempts += 1; |
| 227 | + if (attempts === 1) { |
| 228 | + throw new Error("session not shared"); |
| 229 | + } |
| 230 | + return { |
| 231 | + card: { |
| 232 | + sessionKey, |
| 233 | + revision: 1, |
| 234 | + updatedAt: 1, |
| 235 | + steps: [{ step: "Recovered progress", status: "in_progress" }], |
| 236 | + }, |
| 237 | + }; |
| 238 | + }); |
| 239 | + const context = { |
| 240 | + gateway: { |
| 241 | + snapshot: { |
| 242 | + phase: "connected", |
| 243 | + client: { request }, |
| 244 | + hello: { |
| 245 | + features: { methods: ["progressCard.get"] }, |
| 246 | + controlUiWidgetKinds: [ |
| 247 | + { pluginId: "session", kind: "session:progress", label: "Session progress" }, |
| 248 | + ], |
| 249 | + }, |
| 250 | + }, |
| 251 | + subscribe: () => () => undefined, |
| 252 | + subscribeEvents: () => () => undefined, |
| 253 | + }, |
| 254 | + } as unknown as ApplicationContext; |
| 255 | + const widget: BoardWidget = { |
| 256 | + name: "protected-session-progress", |
| 257 | + tabId: "main", |
| 258 | + title: "Session progress", |
| 259 | + contentKind: "plugin", |
| 260 | + pluginKind: "session:progress", |
| 261 | + props: { sessionKey }, |
| 262 | + sizeW: 6, |
| 263 | + sizeH: 4, |
| 264 | + position: 0, |
| 265 | + grantState: "none", |
| 266 | + revision: 1, |
| 267 | + }; |
| 268 | + const provider = createApplicationContextProvider(context); |
| 269 | + const cell = document.createElement("openclaw-board-widget-cell"); |
| 270 | + cell.widget = widget; |
| 271 | + cell.rect = { name: widget.name, x: 0, y: 0, w: 6, h: 4 }; |
| 272 | + cell.sessionKey = "agent:main:dashboard"; |
| 273 | + cell.callbacks = callbacks(); |
| 274 | + provider.append(cell); |
| 275 | + document.body.append(provider); |
| 276 | + |
| 277 | + await vi.waitFor( |
| 278 | + () => expect(cell.querySelector('[data-test-id="session-progress-error"]')).not.toBeNull(), |
| 279 | + CHUNK_LOAD_WAIT, |
| 280 | + ); |
| 281 | + cell |
| 282 | + .querySelector<HTMLButtonElement>('[data-test-id="session-progress-error"] button') |
| 283 | + ?.click(); |
| 284 | + |
| 285 | + await vi.waitFor( |
| 286 | + () => |
| 287 | + expect(cell.querySelector("openclaw-session-progress-widget")?.textContent).toContain( |
| 288 | + "Recovered progress", |
| 289 | + ), |
| 290 | + CHUNK_LOAD_WAIT, |
| 291 | + ); |
| 292 | + expect(request).toHaveBeenCalledTimes(2); |
| 293 | + |
| 294 | + cell.widget = { ...widget, props: { sessionKey: deniedSessionKey } }; |
| 295 | + await vi.waitFor( |
| 296 | + () => |
| 297 | + expect( |
| 298 | + cell.querySelector('[data-test-id="session-progress-error"]')?.textContent, |
| 299 | + ).toContain("Select a session you can access or change sharing for this session."), |
| 300 | + CHUNK_LOAD_WAIT, |
| 301 | + ); |
| 302 | + expect(cell.querySelector('[data-test-id="session-progress-error"] button')).toBeNull(); |
| 303 | + expect(request).toHaveBeenCalledTimes(3); |
| 304 | + }); |
| 305 | + |
127 | 306 | it("passes activity to a retained Workboard plugin element", async () => { |
128 | 307 | const context = { |
129 | 308 | gateway: { |
|
0 commit comments