|
2 | 2 |
|
3 | 3 | import { render } from "lit"; |
4 | 4 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { createDeferred } from "../../../../test/helpers/promise.js"; |
5 | 6 | import { setAvatarGatewayOrigin } from "../../lib/identity-avatar-context.ts"; |
6 | | -import { invalidateChatAvatarCache, refreshChatAvatar, renderChatAvatar } from "./chat-avatar.ts"; |
| 7 | +import { |
| 8 | + invalidateChatAvatarCache, |
| 9 | + refreshChatAvatar, |
| 10 | + refreshSenderAgentAvatars, |
| 11 | + renderChatAvatar, |
| 12 | +} from "./chat-avatar.ts"; |
7 | 13 | import { makeChatHost } from "./chat-host.test-support.ts"; |
8 | 14 |
|
9 | 15 | function renderAvatar(params: Parameters<typeof renderChatAvatar>) { |
@@ -356,6 +362,133 @@ describe("refreshChatAvatar", () => { |
356 | 362 | }); |
357 | 363 | }); |
358 | 364 |
|
| 365 | +describe("refreshSenderAgentAvatars", () => { |
| 366 | + function forwardedMessages(...agentIds: string[]) { |
| 367 | + return agentIds.map((agentId) => ({ |
| 368 | + role: "assistant", |
| 369 | + content: "report", |
| 370 | + senderSession: { agentId }, |
| 371 | + })); |
| 372 | + } |
| 373 | + function senderHost() { |
| 374 | + return { |
| 375 | + ...makeChatHost({ sessionKey: "agent:main:main", settings: { token: "test-token" } }), |
| 376 | + agentsList: { defaultId: "main", agents: [{ id: "main" }, { id: "research" }] }, |
| 377 | + senderAgentAvatars: undefined as ReadonlyMap<string, string | null> | undefined, |
| 378 | + }; |
| 379 | + } |
| 380 | + |
| 381 | + it("shares authenticated sender snapshots with the current-agent cache and skips unknown agents", async () => { |
| 382 | + const fetchMock = vi |
| 383 | + .spyOn(globalThis, "fetch") |
| 384 | + .mockResolvedValueOnce(new Response(JSON.stringify({ avatarUrl: "/avatar/research" }))) |
| 385 | + .mockResolvedValueOnce(new Response(new Blob(["avatar"]))); |
| 386 | + vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:research-avatar"); |
| 387 | + const host = senderHost(); |
| 388 | + |
| 389 | + host.chatMessages = forwardedMessages("main", "unknown", "research", "research"); |
| 390 | + await Promise.all([refreshSenderAgentAvatars(host), refreshSenderAgentAvatars(host)]); |
| 391 | + expect(host.senderAgentAvatars).toEqual(new Map([["research", "blob:research-avatar"]])); |
| 392 | + expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([ |
| 393 | + "/avatar/research?meta=1", |
| 394 | + "/avatar/research", |
| 395 | + ]); |
| 396 | + for (const [, init] of fetchMock.mock.calls) { |
| 397 | + expect(init?.headers).toEqual({ Authorization: "Bearer test-token" }); |
| 398 | + } |
| 399 | + host.sessionKey = "agent:research:main"; |
| 400 | + await refreshChatAvatar(host); |
| 401 | + expect(host.chatAvatarUrl).toBe("blob:research-avatar"); |
| 402 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 403 | + invalidateChatAvatarCache(host); |
| 404 | + expect(host.senderAgentAvatars).toBeUndefined(); |
| 405 | + }); |
| 406 | + |
| 407 | + it("loads newly committed forwards and refreshes them after cache invalidation", async () => { |
| 408 | + const avatarUrl = "data:image/png;base64,YQ=="; |
| 409 | + const fetchMock = vi |
| 410 | + .spyOn(globalThis, "fetch") |
| 411 | + .mockImplementation(async () => new Response(JSON.stringify({ avatarUrl }))); |
| 412 | + const host = { ...senderHost(), requestUpdate: vi.fn() }; |
| 413 | + await refreshSenderAgentAvatars(host); |
| 414 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 415 | + expect(host.requestUpdate).not.toHaveBeenCalled(); |
| 416 | + |
| 417 | + host.chatMessages = [ |
| 418 | + { role: "assistant", content: "report", senderSession: { agentId: "research" } }, |
| 419 | + ]; |
| 420 | + await refreshSenderAgentAvatars(host); |
| 421 | + await vi.waitFor(() => expect(host.senderAgentAvatars?.get("research")).toBe(avatarUrl)); |
| 422 | + await refreshSenderAgentAvatars(host); |
| 423 | + expect(fetchMock).toHaveBeenCalledOnce(); |
| 424 | + expect(host.requestUpdate).toHaveBeenCalledOnce(); |
| 425 | + |
| 426 | + invalidateChatAvatarCache(host); |
| 427 | + await refreshSenderAgentAvatars(host); |
| 428 | + await vi.waitFor(() => expect(host.senderAgentAvatars?.get("research")).toBe(avatarUrl)); |
| 429 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 430 | + host.chatMessages = []; |
| 431 | + await refreshSenderAgentAvatars(host); |
| 432 | + expect(host.senderAgentAvatars?.size).toBe(0); |
| 433 | + invalidateChatAvatarCache(host); |
| 434 | + }); |
| 435 | + |
| 436 | + it.each(["session", "request", "connection", "roster", "invalidation"])( |
| 437 | + "does not publish a sender avatar after its %s changes", |
| 438 | + async (change) => { |
| 439 | + const response = createDeferred<Response>(); |
| 440 | + vi.spyOn(globalThis, "fetch").mockReturnValue(response.promise); |
| 441 | + const host = senderHost(); |
| 442 | + host.chatMessages = forwardedMessages("research"); |
| 443 | + const pending = refreshSenderAgentAvatars(host); |
| 444 | + if (change === "session") { |
| 445 | + host.sessionKey = "agent:main:other"; |
| 446 | + } |
| 447 | + if (change === "request") { |
| 448 | + host.chatMessages = []; |
| 449 | + await refreshSenderAgentAvatars(host); |
| 450 | + } |
| 451 | + if (change === "connection") { |
| 452 | + host.connectionEpoch += 1; |
| 453 | + } |
| 454 | + if (change === "roster") { |
| 455 | + host.agentsList = { defaultId: "main", agents: [{ id: "main" }] }; |
| 456 | + } |
| 457 | + if (change === "invalidation") { |
| 458 | + invalidateChatAvatarCache(host); |
| 459 | + } |
| 460 | + response.resolve(new Response(JSON.stringify({ avatarUrl: "data:image/png;base64,YQ==" }))); |
| 461 | + await pending; |
| 462 | + expect(host.senderAgentAvatars?.size ?? 0).toBe(0); |
| 463 | + }, |
| 464 | + ); |
| 465 | + |
| 466 | + it("bounds sender loads without evicting the current agent's blob", async () => { |
| 467 | + const agents = Array.from({ length: 30 }, (_, i) => ({ id: `sender-${i}` })); |
| 468 | + const host = senderHost(); |
| 469 | + host.agentsList.agents.push(...agents); |
| 470 | + const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (input) => { |
| 471 | + const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; |
| 472 | + return url.endsWith("?meta=1") |
| 473 | + ? new Response(JSON.stringify({ avatarUrl: url.replace("?meta=1", "") })) |
| 474 | + : new Response(new Blob(["avatar"])); |
| 475 | + }); |
| 476 | + let sequence = 0; |
| 477 | + vi.spyOn(URL, "createObjectURL").mockImplementation(() => `blob:avatar-${sequence++}`); |
| 478 | + const revoke = vi.spyOn(URL, "revokeObjectURL"); |
| 479 | + await refreshChatAvatar(host); |
| 480 | + const currentAvatar = host.chatAvatarUrl; |
| 481 | + host.chatMessages = forwardedMessages(...agents.map((agent) => agent.id)); |
| 482 | + await refreshSenderAgentAvatars(host); |
| 483 | + expect(host.senderAgentAvatars?.size).toBe(23); |
| 484 | + expect(fetchMock).toHaveBeenCalledTimes(48); |
| 485 | + expect(host.chatAvatarUrl).toBe(currentAvatar); |
| 486 | + expect(revoke).not.toHaveBeenCalled(); |
| 487 | + invalidateChatAvatarCache(host); |
| 488 | + expect(revoke).toHaveBeenCalledTimes(24); |
| 489 | + }); |
| 490 | +}); |
| 491 | + |
359 | 492 | describe("attributed sender avatars", () => { |
360 | 493 | it("restores pending initials when the authenticated sender avatar changes", async () => { |
361 | 494 | setAvatarGatewayOrigin("https://gateway.example.test", "Bearer profile-token"); |
|
0 commit comments