|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import { html, render } from "lit"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { resolveAvatarInitials, setAvatarGatewayOrigin } from "../lib/identity-avatar.ts"; |
| 6 | +import { |
| 7 | + identityAvatarClass, |
| 8 | + renderIdentityAvatarImage, |
| 9 | + resolveIdentityAvatarView, |
| 10 | + type IdentityAvatarView, |
| 11 | +} from "./identity-avatar-view.ts"; |
| 12 | + |
| 13 | +function renderAvatar(view: IdentityAvatarView, container: HTMLElement): void { |
| 14 | + render( |
| 15 | + html`<span class=${identityAvatarClass("test-avatar", view)}> |
| 16 | + ${renderIdentityAvatarImage({ |
| 17 | + view, |
| 18 | + fallbackSelector: ".test-avatar", |
| 19 | + className: "test-avatar__image", |
| 20 | + alt: "Ada Lovelace", |
| 21 | + ariaHidden: true, |
| 22 | + })} |
| 23 | + </span>`, |
| 24 | + container, |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +afterEach(() => { |
| 29 | + document.body.replaceChildren(); |
| 30 | + setAvatarGatewayOrigin(null); |
| 31 | + vi.restoreAllMocks(); |
| 32 | +}); |
| 33 | + |
| 34 | +describe("shared identity avatar view", () => { |
| 35 | + it("derives fallback initials and colors from the canonical user identity", () => { |
| 36 | + const identity = { |
| 37 | + id: "profile-riley", |
| 38 | + name: "Riley", |
| 39 | + |
| 40 | + }; |
| 41 | + |
| 42 | + const view = resolveIdentityAvatarView(identity); |
| 43 | + |
| 44 | + expect(view.fallback).toEqual(resolveAvatarInitials(identity)); |
| 45 | + expect(view.fallback.initials).toBe("R"); |
| 46 | + expect(view.imageUrl).toBeNull(); |
| 47 | + expect(view.pending).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it("keeps hostile avatar origins out of the shared authenticated renderer", () => { |
| 51 | + setAvatarGatewayOrigin("https://gateway.example.test", "Bearer avatar-token"); |
| 52 | + const fetchAvatar = vi.spyOn(globalThis, "fetch"); |
| 53 | + |
| 54 | + const view = resolveIdentityAvatarView({ |
| 55 | + id: "profile-mallory", |
| 56 | + name: "Mallory", |
| 57 | + profileAvatarUrl: "https://evil.example/api/users/profile-mallory/avatar", |
| 58 | + }); |
| 59 | + |
| 60 | + expect(view.fallback.initials).toBe("M"); |
| 61 | + expect(view.imageUrl).toBeNull(); |
| 62 | + expect(view.pending).toBe(false); |
| 63 | + expect(fetchAvatar).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("shares authenticated loading and reconciles image fallback events", async () => { |
| 67 | + setAvatarGatewayOrigin("https://gateway.example.test", "Bearer avatar-token"); |
| 68 | + const fetchAvatar = vi.spyOn(globalThis, "fetch").mockResolvedValue( |
| 69 | + new Response(new Uint8Array([1, 2, 3]), { |
| 70 | + headers: { "content-type": "image/png" }, |
| 71 | + }), |
| 72 | + ); |
| 73 | + vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:shared-identity-avatar"); |
| 74 | + |
| 75 | + const view = resolveIdentityAvatarView({ |
| 76 | + id: "profile-ada", |
| 77 | + name: "Ada Lovelace", |
| 78 | + profileAvatarUrl: "/api/users/profile-ada/avatar?v=7", |
| 79 | + }); |
| 80 | + const container = document.createElement("div"); |
| 81 | + document.body.append(container); |
| 82 | + renderAvatar(view, container); |
| 83 | + |
| 84 | + const wrapper = container.querySelector<HTMLElement>(".test-avatar"); |
| 85 | + expect(view.pending).toBe(true); |
| 86 | + expect(wrapper?.classList.contains("is-fallback")).toBe(true); |
| 87 | + |
| 88 | + const image = await vi.waitFor(() => { |
| 89 | + const element = container.querySelector<HTMLImageElement>(".test-avatar__image"); |
| 90 | + expect(element?.getAttribute("src")).toBe("blob:shared-identity-avatar"); |
| 91 | + return element!; |
| 92 | + }); |
| 93 | + expect(image.getAttribute("alt")).toBe("Ada Lovelace"); |
| 94 | + expect(image.getAttribute("aria-hidden")).toBe("true"); |
| 95 | + expect(image.getAttribute("referrerpolicy")).toBe("no-referrer"); |
| 96 | + expect(fetchAvatar).toHaveBeenCalledOnce(); |
| 97 | + expect(fetchAvatar).toHaveBeenCalledWith( |
| 98 | + "https://gateway.example.test/api/users/profile-ada/avatar?v=7", |
| 99 | + expect.objectContaining({ headers: { Authorization: "Bearer avatar-token" } }), |
| 100 | + ); |
| 101 | + |
| 102 | + image.dispatchEvent(new Event("load")); |
| 103 | + expect(wrapper?.classList.contains("is-fallback")).toBe(false); |
| 104 | + |
| 105 | + image.dispatchEvent(new Event("error")); |
| 106 | + expect(wrapper?.classList.contains("is-fallback")).toBe(true); |
| 107 | + |
| 108 | + image.dispatchEvent(new Event("load")); |
| 109 | + expect(wrapper?.classList.contains("is-fallback")).toBe(false); |
| 110 | + }); |
| 111 | + |
| 112 | + it("retains its existing image while a newer avatar revision loads", async () => { |
| 113 | + setAvatarGatewayOrigin("https://gateway.example.test", "Bearer avatar-token"); |
| 114 | + vi.spyOn(globalThis, "fetch").mockImplementation( |
| 115 | + async () => |
| 116 | + new Response(new Uint8Array([1, 2, 3]), { |
| 117 | + headers: { "content-type": "image/png" }, |
| 118 | + }), |
| 119 | + ); |
| 120 | + vi.spyOn(URL, "createObjectURL") |
| 121 | + .mockReturnValueOnce("blob:identity-avatar-v1") |
| 122 | + .mockReturnValueOnce("blob:identity-avatar-v2"); |
| 123 | + |
| 124 | + const identity = { id: "profile-ada", name: "Ada Lovelace" }; |
| 125 | + const container = document.createElement("div"); |
| 126 | + document.body.append(container); |
| 127 | + renderAvatar( |
| 128 | + resolveIdentityAvatarView({ |
| 129 | + ...identity, |
| 130 | + profileAvatarUrl: "/api/users/profile-ada/avatar?v=1", |
| 131 | + }), |
| 132 | + container, |
| 133 | + ); |
| 134 | + |
| 135 | + const firstImage = await vi.waitFor(() => { |
| 136 | + const image = container.querySelector<HTMLImageElement>(".test-avatar__image"); |
| 137 | + expect(image?.getAttribute("src")).toBe("blob:identity-avatar-v1"); |
| 138 | + return image!; |
| 139 | + }); |
| 140 | + firstImage.dispatchEvent(new Event("load")); |
| 141 | + |
| 142 | + renderAvatar( |
| 143 | + resolveIdentityAvatarView({ |
| 144 | + ...identity, |
| 145 | + profileAvatarUrl: "/api/users/profile-ada/avatar?v=2", |
| 146 | + }), |
| 147 | + container, |
| 148 | + ); |
| 149 | + expect(container.querySelector(".test-avatar")?.classList.contains("is-fallback")).toBe(true); |
| 150 | + |
| 151 | + const secondImage = await vi.waitFor(() => { |
| 152 | + const image = container.querySelector<HTMLImageElement>(".test-avatar__image"); |
| 153 | + expect(image?.getAttribute("src")).toBe("blob:identity-avatar-v2"); |
| 154 | + return image!; |
| 155 | + }); |
| 156 | + expect(secondImage).toBe(firstImage); |
| 157 | + |
| 158 | + secondImage.dispatchEvent(new Event("load")); |
| 159 | + expect(container.querySelector(".test-avatar")?.classList.contains("is-fallback")).toBe(false); |
| 160 | + }); |
| 161 | +}); |
0 commit comments