Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

IntegrationsSettings.logic.test.ts

102 lines3.9 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import type { EnvironmentId } from "@t3tools/contracts";
3​
4import {
5 browserProfileRemovalAvailable,
6 clearBrowserProfileData,
7 importFailureReason,
8} from "./IntegrationsSettings";
9​
10const environmentId = "environment-a" as EnvironmentId;
11const secondEnvironmentId = "environment-b" as EnvironmentId;
12​
13describe("clearBrowserProfileData", () => {
14 it("waits for cookie and cache cleanup", async () => {
15 const clearCookies = vi.fn().mockResolvedValue(undefined);
16 const clearCache = vi.fn().mockResolvedValue(undefined);
17​
18 await clearBrowserProfileData({ clearCookies, clearCache }, [environmentId], "profile-a");
19​
20 expect(clearCookies).toHaveBeenCalledWith(environmentId, "profile-a");
21 expect(clearCache).toHaveBeenCalledWith(environmentId, "profile-a");
22 });
23​
24 it("clears every known environment before succeeding", async () => {
25 const clearCookies = vi.fn().mockResolvedValue(undefined);
26 const clearCache = vi.fn().mockResolvedValue(undefined);
27​
28 await clearBrowserProfileData(
29 { clearCookies, clearCache },
30 [environmentId, secondEnvironmentId],
31 "profile-a",
32 );
33​
34 expect(clearCookies.mock.calls).toEqual([
35 [environmentId, "profile-a"],
36 [secondEnvironmentId, "profile-a"],
37 ]);
38 expect(clearCache.mock.calls).toEqual([
39 [environmentId, "profile-a"],
40 [secondEnvironmentId, "profile-a"],
41 ]);
42 });
43​
44 it("propagates cleanup failures", async () => {
45 const failure = new Error("clear failed");
46 await expect(
47 clearBrowserProfileData(
48 {
49 clearCookies: vi.fn().mockRejectedValue(failure),
50 clearCache: vi.fn().mockResolvedValue(undefined),
51 },
52 [environmentId],
53 "profile-a",
54 ),
55 ).rejects.toBe(failure);
56 });
57​
58 it("does not report success without an environment or bridge", async () => {
59 const bridge = {
60 clearCookies: vi.fn().mockResolvedValue(undefined),
61 clearCache: vi.fn().mockResolvedValue(undefined),
62 };
63​
64 await expect(clearBrowserProfileData(bridge, [], "profile-a")).rejects.toThrow();
65 await expect(clearBrowserProfileData(null, [environmentId], "profile-a")).rejects.toThrow();
66 expect(bridge.clearCookies).not.toHaveBeenCalled();
67 expect(bridge.clearCache).not.toHaveBeenCalled();
68 });
69});
70​
71describe("browserProfileRemovalAvailable", () => {
72 it("requires a ready non-empty catalog and desktop bridge", () => {
73 expect(browserProfileRemovalAvailable(true, true, 1)).toBe(true);
74 expect(browserProfileRemovalAvailable(true, true, 0)).toBe(false);
75 expect(browserProfileRemovalAvailable(true, false, 1)).toBe(false);
76 expect(browserProfileRemovalAvailable(false, true, 1)).toBe(false);
77 });
78});
79​
80// Mirrors `BrowserImportFailedError.message`, which IPC flattens to a string
81// before the renderer sees it.
82const failure = (reason: string) => ({
83 message: `Importing cookies from safari failed: ${reason}.`,
84});
85​
86describe("importFailureReason", () => {
87 it("recovers the reason token from the flattened message", () => {
88 expect(importFailureReason(failure("needsFullDiskAccess"))).toBe("needsFullDiskAccess");
89 expect(importFailureReason(failure("browserRunning"))).toBe("browserRunning");
90 expect(importFailureReason(failure("readFailed"))).toBe("readFailed");
91 expect(importFailureReason(failure("keychainUnavailable"))).toBe("keychainUnavailable");
92 // A settings write that fails after the cookies landed is its own case,
93 // not a read failure over a database that was in fact read.
94 expect(importFailureReason(failure("profileNotSaved"))).toBe("profileNotSaved");
95 expect(importFailureReason(failure("profileLimitReached"))).toBe("profileLimitReached");
96 });
97​
98 it("falls back to readFailed for anything it cannot classify", () => {
99 expect(importFailureReason(new Error("something else entirely"))).toBe("readFailed");
100 expect(importFailureReason(undefined)).toBe("readFailed");
101 });
102});