import { describe, it, expect, vi } from "vitest"; import { resolveIdentity } from "../resolve-identity.js"; import { AtpAgent } from "@atproto/api"; vi.mock("@atproto/api", () => ({ AtpAgent: vi.fn(), })); describe("resolveIdentity", () => { it("returns DID directly when input starts with 'did:'", async () => { const result = await resolveIdentity("did:plc:abc123", "https://bsky.social"); expect(result).toEqual({ did: "did:plc:abc123" }); // AtpAgent should NOT be instantiated for DID input expect(AtpAgent).not.toHaveBeenCalled(); }); it("resolves a handle to a DID via PDS", async () => { const mockResolveHandle = vi.fn().mockResolvedValue({ data: { did: "did:plc:resolved123" }, }); (AtpAgent as any).mockImplementation(function () { return { resolveHandle: mockResolveHandle }; }); const result = await resolveIdentity("alice.bsky.social", "https://bsky.social"); expect(result).toEqual({ did: "did:plc:resolved123", handle: "alice.bsky.social", }); expect(AtpAgent).toHaveBeenCalledWith({ service: "https://bsky.social" }); expect(mockResolveHandle).toHaveBeenCalledWith({ handle: "alice.bsky.social" }); }); it("throws when handle resolution fails", async () => { (AtpAgent as any).mockImplementation(function () { return { resolveHandle: vi.fn().mockRejectedValue(new Error("Unable to resolve handle")), }; }); await expect( resolveIdentity("nonexistent.bsky.social", "https://bsky.social") ).rejects.toThrow("Unable to resolve handle"); }); });