import type { Did } from "@atcute/lexicons/syntax"; import type { OAuthUserAgent } from "@atcute/oauth-browser-client"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { getOrCreateDelegatedSession } from "$lib/api/actAs"; import { awaitIndexedOrSaved } from "$lib/api/awaitRecord"; import { createBobbinClient } from "$lib/api/client"; import { saveOrganizationAvatar, saveOrganizationProfile } from "$lib/api/orgProfile"; import { putProfile, uploadProfileAvatar } from "$lib/api/profile"; vi.mock("$lib/api/actAs", () => ({ getOrCreateDelegatedSession: vi.fn() })); vi.mock("$lib/api/profile", () => ({ PROFILE_COLLECTION: "sh.tangled.actor.profile", putProfile: vi.fn(), uploadProfileAvatar: vi.fn() })); vi.mock("$lib/api/awaitRecord", () => ({ awaitIndexedOrSaved: vi.fn() })); const orgDid = "did:plc:org" as Did; const controller = { sub: "did:plc:controller" } as unknown as OAuthUserAgent; const orgAgent = { sub: orgDid } as unknown as OAuthUserAgent; const bobbin = createBobbinClient({ serviceUrl: "https://bobbin.test" }); const written = { uri: `at://${orgDid}/sh.tangled.actor.profile/self`, cid: "bafynew" }; beforeEach(() => { vi.resetAllMocks(); vi.mocked(getOrCreateDelegatedSession).mockResolvedValue(orgAgent); vi.mocked(putProfile).mockResolvedValue(written as never); }); describe("saveOrganizationProfile", () => { it("writes through a delegated session and keeps the organization flag", async () => { const result = await saveOrganizationProfile(bobbin, controller, orgDid, { $type: "sh.tangled.actor.profile", bluesky: false, description: "Updated." }); expect(getOrCreateDelegatedSession).toHaveBeenCalledWith(controller, orgDid); expect(putProfile).toHaveBeenCalledWith(orgAgent, { $type: "sh.tangled.actor.profile", bluesky: false, description: "Updated.", isOrganization: true }); expect(awaitIndexedOrSaved).toHaveBeenCalledWith(bobbin, written); expect(result).toEqual(expect.objectContaining({ isOrganization: true })); }); }); describe("saveOrganizationAvatar", () => { it("uploads with the organization's session, starting a record if there is none", async () => { const image = new Blob(["png"], { type: "image/png" }); const avatar = { $type: "blob", ref: { $link: "bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a" }, mimeType: "image/png", size: 3 } as const; vi.mocked(uploadProfileAvatar).mockResolvedValue(avatar); const result = await saveOrganizationAvatar(bobbin, controller, orgDid, image, null); expect(uploadProfileAvatar).toHaveBeenCalledWith(orgAgent, image); expect(putProfile).toHaveBeenCalledWith(orgAgent, { $type: "sh.tangled.actor.profile", bluesky: false, isOrganization: true, avatar }); expect(result).toEqual(expect.objectContaining({ avatar, isOrganization: true })); }); });