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

IntegrationsSettings.logic.test.ts

64 lines2.7 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import type { EnvironmentId } from "@t3tools/contracts";
3​
4import { clearBrowserProfileData, importFailureReason } from "./IntegrationsSettings";
5​
6const environmentId = "environment-a" as EnvironmentId;
7const secondEnvironmentId = "environment-b" as EnvironmentId;
8​
9describe("clearBrowserProfileData", () => {
10 it("does not report success without an environment or bridge", async () => {
11 const bridge = {
12 clearCookies: vi.fn().mockResolvedValue(undefined),
13 clearCache: vi.fn().mockResolvedValue(undefined),
14 };
15​
16 await expect(clearBrowserProfileData(bridge, [], "profile-a")).rejects.toThrow();
17 await expect(clearBrowserProfileData(null, [environmentId], "profile-a")).rejects.toThrow();
18 expect(bridge.clearCookies).not.toHaveBeenCalled();
19 expect(bridge.clearCache).not.toHaveBeenCalled();
20 });
21});
22​
23describe("clearBrowserProfileData with server browsers", () => {
24 it("clears server-hosted profiles without a desktop bridge", async () => {
25 const clear = vi.fn().mockResolvedValue(undefined);
26 await clearBrowserProfileData(null, [environmentId, secondEnvironmentId], "profile-a", {
27 environmentIds: [secondEnvironmentId],
28 clear,
29 });
30 expect(clear).toHaveBeenCalledExactlyOnceWith(secondEnvironmentId, "profile-a");
31 });
32​
33 it("clears both copies and fails when the server copy fails", async () => {
34 const bridge = {
35 clearCookies: vi.fn().mockResolvedValue(undefined),
36 clearCache: vi.fn().mockResolvedValue(undefined),
37 };
38 const failure = new Error("server failed");
39 await expect(
40 clearBrowserProfileData(bridge, [environmentId], "profile-a", {
41 environmentIds: [environmentId],
42 clear: vi.fn().mockRejectedValue(failure),
43 }),
44 ).rejects.toBe(failure);
45 expect(bridge.clearCookies).toHaveBeenCalledWith(environmentId, "profile-a");
46 });
47});
48​
49// Mirrors `BrowserImportFailedError.message`, which IPC flattens to a string
50// before the renderer sees it.
51const failure = (reason: string) => ({
52 message: `Importing cookies from safari failed: ${reason}.`,
53});
54​
55describe("importFailureReason", () => {
56 it("recovers the reason token from the flattened message", () => {
57 expect(importFailureReason(failure("needsFullDiskAccess"))).toBe("needsFullDiskAccess");
58 expect(importFailureReason(failure("browserRunning"))).toBe("browserRunning");
59 expect(importFailureReason(failure("readFailed"))).toBe("readFailed");
60 expect(importFailureReason(failure("keychainUnavailable"))).toBe("keychainUnavailable");
61 expect(importFailureReason(failure("profileNotSaved"))).toBe("profileNotSaved");
62 expect(importFailureReason(failure("profileLimitReached"))).toBe("profileLimitReached");
63 });
64});