import { beforeEach, describe, expect, it, vi } from "vitest"; import { getStoredWechatProfile, requestWechatProfile, saveWechatProfile, } from "@/lib/wechat-profile"; describe("wechat profile", () => { const storage = new Map(); const getUserProfile = vi.fn(); beforeEach(() => { storage.clear(); getUserProfile.mockReset(); vi.stubGlobal("uni", { getStorageSync: (key: string) => storage.get(key), setStorageSync: (key: string, value: unknown) => storage.set(key, value), getUserProfile, }); }); it("stores and restores a valid avatar profile", () => { saveWechatProfile({ avatarUrl: "https://example.com/avatar.jpg", nickName: "Damon" }); expect(getStoredWechatProfile()).toEqual({ avatarUrl: "https://example.com/avatar.jpg", nickName: "Damon", }); }); it("requests the profile only through the explicit profile API", async () => { getUserProfile.mockImplementation(({ success }: { success: (result: unknown) => void }) => { success({ userInfo: { avatarUrl: "https://example.com/wechat.jpg", nickName: "旅行者" } }); }); await expect(requestWechatProfile()).resolves.toEqual({ avatarUrl: "https://example.com/wechat.jpg", nickName: "旅行者", }); expect(getUserProfile).toHaveBeenCalledWith( expect.objectContaining({ provider: "weixin", lang: "zh_CN" }), ); }); it("returns null when the user declines profile permission", async () => { getUserProfile.mockImplementation(({ fail }: { fail: () => void }) => fail()); await expect(requestWechatProfile()).resolves.toBeNull(); }); });