Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import type { EnvironmentId } from "@t3tools/contracts";
3
4import { browserProfileRemovalAvailable, clearBrowserProfileData } from "./IntegrationsSettings";
5
6const environmentId = "environment-a" as EnvironmentId;
7const secondEnvironmentId = "environment-b" as EnvironmentId;
8
9describe("clearBrowserProfileData", () => {
10 it("waits for cookie and cache cleanup", async () => {
11 const clearCookies = vi.fn().mockResolvedValue(undefined);
12 const clearCache = vi.fn().mockResolvedValue(undefined);
13
14 await clearBrowserProfileData({ clearCookies, clearCache }, [environmentId], "profile-a");
15
16 expect(clearCookies).toHaveBeenCalledWith(environmentId, "profile-a");
17 expect(clearCache).toHaveBeenCalledWith(environmentId, "profile-a");
18 });
19
20 it("clears every known environment before succeeding", async () => {
21 const clearCookies = vi.fn().mockResolvedValue(undefined);
22 const clearCache = vi.fn().mockResolvedValue(undefined);
23
24 await clearBrowserProfileData(
25 { clearCookies, clearCache },
26 [environmentId, secondEnvironmentId],
27 "profile-a",
28 );
29
30 expect(clearCookies.mock.calls).toEqual([
31 [environmentId, "profile-a"],
32 [secondEnvironmentId, "profile-a"],
33 ]);
34 expect(clearCache.mock.calls).toEqual([
35 [environmentId, "profile-a"],
36 [secondEnvironmentId, "profile-a"],
37 ]);
38 });
39
40 it("propagates cleanup failures", async () => {
41 const failure = new Error("clear failed");
42 await expect(
43 clearBrowserProfileData(
44 {
45 clearCookies: vi.fn().mockRejectedValue(failure),
46 clearCache: vi.fn().mockResolvedValue(undefined),
47 },
48 [environmentId],
49 "profile-a",
50 ),
51 ).rejects.toBe(failure);
52 });
53
54 it("does not report success without an environment or bridge", async () => {
55 const bridge = {
56 clearCookies: vi.fn().mockResolvedValue(undefined),
57 clearCache: vi.fn().mockResolvedValue(undefined),
58 };
59
60 await expect(clearBrowserProfileData(bridge, [], "profile-a")).rejects.toThrow();
61 await expect(clearBrowserProfileData(null, [environmentId], "profile-a")).rejects.toThrow();
62 expect(bridge.clearCookies).not.toHaveBeenCalled();
63 expect(bridge.clearCache).not.toHaveBeenCalled();
64 });
65});
66
67describe("browserProfileRemovalAvailable", () => {
68 it("requires a ready non-empty catalog and desktop bridge", () => {
69 expect(browserProfileRemovalAvailable(true, true, 1)).toBe(true);
70 expect(browserProfileRemovalAvailable(true, true, 0)).toBe(false);
71 expect(browserProfileRemovalAvailable(true, false, 1)).toBe(false);
72 expect(browserProfileRemovalAvailable(false, true, 1)).toBe(false);
73 });
74});