import { describe, expect, it } from "vitest"; import { render } from "svelte/server"; import type { OAuthUserAgent } from "@atcute/oauth-browser-client"; import NotificationItem from "$lib/components/notifications/NotificationItem.svelte"; import { notifications } from "$lib/components/notifications/mock"; import type { NotificationSummary } from "$lib/components/notifications/types"; import { AUTH_KEY, type Auth } from "$lib/auth.svelte"; import oauthMetadata from "$lib/oauth-client-metadata"; const rowOf = (type: NotificationSummary["type"]): NotificationSummary => { const found = notifications.find((notification) => notification.type === type); if (!found) throw new Error(`no ${type} fixture to render`); return found; }; const signedIn = (scope: string): Map => new Map([ [ AUTH_KEY, { agent: { session: { token: { scope } } } as unknown as OAuthUserAgent, currentDid: "did:plc:limpet" } as unknown as Auth ] ]); const markup = (notification: NotificationSummary, context?: Map): string => render(NotificationItem, { props: { notification, onToggleRead: () => {} }, context }).body; const insideTheLink = (html: string): string => { const open = html.indexOf("", open); if (open < 0 || close < 0) throw new Error("the row rendered no link"); return html.slice(open, close); }; describe("a notification row", () => { it("no control sits inside the link, since anchors can't wrap buttons", () => { const html = markup(rowOf("collaborator_invited"), signedIn(oauthMetadata.scope)); expect(insideTheLink(html)).not.toContain(" { expect(insideTheLink(markup(rowOf("user_mentioned")))).toContain("after:inset-0"); }); it("an invite gets an accept button", () => { expect(markup(rowOf("collaborator_invited"), signedIn(oauthMetadata.scope))).toContain( "Accept" ); }); it("a session minted before the scopes goes back through authorization", () => { const html = markup( rowOf("collaborator_invited"), signedIn("atproto repo:sh.tangled.repo") ); expect(html).toContain("Sign in again to accept"); expect(html).not.toContain(">Accept<"); }); it("no session, no auth context, no accept button", () => { const signedOut = markup( rowOf("collaborator_invited"), new Map([[AUTH_KEY, { agent: null, currentDid: null } as unknown as Auth]]) ); expect(signedOut).not.toContain("Accept"); expect(signedOut.split(" { const html = markup(rowOf("pull_merged"), signedIn(oauthMetadata.scope)); expect(html).not.toContain("Accept"); expect(html.split("