Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import { type EnvironmentId, managedPolicyDeniedMessage } from "@t3tools/contracts";
3
4import {
5 clearBrowserProfileData,
6 importFailureReason,
7 importPolicyRefusal,
8} from "./IntegrationsSettings";
9
10const environmentId = "environment-a" as EnvironmentId;
11const secondEnvironmentId = "environment-b" as EnvironmentId;
12
13describe("clearBrowserProfileData", () => {
14 it("does not report success without an environment or bridge", async () => {
15 const bridge = {
16 clearCookies: vi.fn().mockResolvedValue(undefined),
17 clearCache: vi.fn().mockResolvedValue(undefined),
18 };
19
20 await expect(clearBrowserProfileData(bridge, [], "profile-a")).rejects.toThrow();
21 await expect(clearBrowserProfileData(null, [environmentId], "profile-a")).rejects.toThrow();
22 expect(bridge.clearCookies).not.toHaveBeenCalled();
23 expect(bridge.clearCache).not.toHaveBeenCalled();
24 });
25});
26
27describe("clearBrowserProfileData with server browsers", () => {
28 it("clears server-hosted profiles without a desktop bridge", async () => {
29 const clear = vi.fn().mockResolvedValue(undefined);
30 await clearBrowserProfileData(null, [environmentId, secondEnvironmentId], "profile-a", {
31 environmentIds: [secondEnvironmentId],
32 clear,
33 });
34 expect(clear).toHaveBeenCalledExactlyOnceWith(secondEnvironmentId, "profile-a");
35 });
36
37 it("clears both copies and fails when the server copy fails", async () => {
38 const bridge = {
39 clearCookies: vi.fn().mockResolvedValue(undefined),
40 clearCache: vi.fn().mockResolvedValue(undefined),
41 };
42 const failure = new Error("server failed");
43 await expect(
44 clearBrowserProfileData(bridge, [environmentId], "profile-a", {
45 environmentIds: [environmentId],
46 clear: vi.fn().mockRejectedValue(failure),
47 }),
48 ).rejects.toBe(failure);
49 expect(bridge.clearCookies).toHaveBeenCalledWith(environmentId, "profile-a");
50 });
51});
52
53// Mirrors `BrowserImportFailedError.message`, which IPC flattens to a string
54// before the renderer sees it.
55const failure = (reason: string) => ({
56 message: `Importing cookies from safari failed: ${reason}.`,
57});
58
59describe("importFailureReason", () => {
60 it("recovers the reason token from the flattened message", () => {
61 expect(importFailureReason(failure("needsFullDiskAccess"))).toBe("needsFullDiskAccess");
62 expect(importFailureReason(failure("browserRunning"))).toBe("browserRunning");
63 expect(importFailureReason(failure("readFailed"))).toBe("readFailed");
64 expect(importFailureReason(failure("keychainUnavailable"))).toBe("keychainUnavailable");
65 expect(importFailureReason(failure("profileNotSaved"))).toBe("profileNotSaved");
66 expect(importFailureReason(failure("profileLimitReached"))).toBe("profileLimitReached");
67 });
68});
69
70describe("importPolicyRefusal", () => {
71 it("recovers a managed policy's refusal from the IPC-wrapped message", () => {
72 const denied = managedPolicyDeniedMessage("Example Corp", "importing browser cookies");
73 expect(
74 importPolicyRefusal({
75 message: `Error invoking remote method 'desktop:preview-import-cookies': BrowserImportPolicyError: ${denied}`,
76 }),
77 ).toBe(denied);
78 const unreadable =
79 "The managed policy at C:\\Program Files\\T3Code\\policy.json could not be read, so importing browser cookies is not allowed.";
80 expect(importPolicyRefusal({ message: `Error: ${unreadable}` })).toBe(unreadable);
81 });
82
83 it("leaves other failures to their reason token", () => {
84 expect(importPolicyRefusal(failure("readFailed"))).toBeUndefined();
85 });
86});