2025-08-08 11:02:27 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { vi } from 'vitest';
|
2025-08-13 11:06:31 -07:00
|
|
|
import { renderHook, act } from '@testing-library/react';
|
2025-08-08 11:02:27 -07:00
|
|
|
import { useFolderTrust } from './useFolderTrust.js';
|
2025-08-26 00:04:53 +02:00
|
|
|
import type { LoadedSettings } from '../../config/settings.js';
|
2025-08-08 11:02:27 -07:00
|
|
|
import { FolderTrustChoice } from '../components/FolderTrustDialog.js';
|
2025-08-26 00:04:53 +02:00
|
|
|
import type { LoadedTrustedFolders } from '../../config/trustedFolders.js';
|
|
|
|
|
import { TrustLevel } from '../../config/trustedFolders.js';
|
2025-08-25 22:11:27 +02:00
|
|
|
import * as process from 'node:process';
|
2025-08-13 11:06:31 -07:00
|
|
|
|
|
|
|
|
import * as trustedFolders from '../../config/trustedFolders.js';
|
|
|
|
|
|
|
|
|
|
vi.mock('process', () => ({
|
|
|
|
|
cwd: vi.fn(),
|
|
|
|
|
platform: 'linux',
|
|
|
|
|
}));
|
2025-08-08 11:02:27 -07:00
|
|
|
|
|
|
|
|
describe('useFolderTrust', () => {
|
2025-08-13 11:06:31 -07:00
|
|
|
let mockSettings: LoadedSettings;
|
2025-09-06 01:39:02 -04:00
|
|
|
let mockConfig: unknown;
|
2025-08-13 11:06:31 -07:00
|
|
|
let mockTrustedFolders: LoadedTrustedFolders;
|
|
|
|
|
let loadTrustedFoldersSpy: vi.SpyInstance;
|
2025-08-14 11:15:48 -07:00
|
|
|
let isWorkspaceTrustedSpy: vi.SpyInstance;
|
|
|
|
|
let onTrustChange: (isTrusted: boolean | undefined) => void;
|
2025-09-08 16:41:39 +00:00
|
|
|
let refreshStatic: () => void;
|
2025-08-13 11:06:31 -07:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
mockSettings = {
|
2025-08-08 11:02:27 -07:00
|
|
|
merged: {
|
2025-09-08 16:41:39 +00:00
|
|
|
security: {
|
|
|
|
|
folderTrust: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-08-08 11:02:27 -07:00
|
|
|
},
|
|
|
|
|
setValue: vi.fn(),
|
|
|
|
|
} as unknown as LoadedSettings;
|
|
|
|
|
|
2025-09-06 01:39:02 -04:00
|
|
|
mockConfig = {} as unknown;
|
|
|
|
|
|
2025-08-13 11:06:31 -07:00
|
|
|
mockTrustedFolders = {
|
|
|
|
|
setValue: vi.fn(),
|
|
|
|
|
} as unknown as LoadedTrustedFolders;
|
|
|
|
|
|
|
|
|
|
loadTrustedFoldersSpy = vi
|
|
|
|
|
.spyOn(trustedFolders, 'loadTrustedFolders')
|
|
|
|
|
.mockReturnValue(mockTrustedFolders);
|
2025-08-14 11:15:48 -07:00
|
|
|
isWorkspaceTrustedSpy = vi.spyOn(trustedFolders, 'isWorkspaceTrusted');
|
2025-08-13 11:06:31 -07:00
|
|
|
(process.cwd as vi.Mock).mockReturnValue('/test/path');
|
2025-08-14 11:15:48 -07:00
|
|
|
onTrustChange = vi.fn();
|
2025-09-08 16:41:39 +00:00
|
|
|
refreshStatic = vi.fn();
|
2025-08-08 11:02:27 -07:00
|
|
|
});
|
|
|
|
|
|
2025-08-13 11:06:31 -07:00
|
|
|
afterEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
2025-08-08 11:02:27 -07:00
|
|
|
|
2025-08-13 11:06:31 -07:00
|
|
|
it('should not open dialog when folder is already trusted', () => {
|
2025-08-14 11:15:48 -07:00
|
|
|
isWorkspaceTrustedSpy.mockReturnValue(true);
|
2025-08-13 11:06:31 -07:00
|
|
|
const { result } = renderHook(() =>
|
2025-09-08 16:41:39 +00:00
|
|
|
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
|
2025-08-13 11:06:31 -07:00
|
|
|
);
|
|
|
|
|
expect(result.current.isFolderTrustDialogOpen).toBe(false);
|
2025-08-14 11:15:48 -07:00
|
|
|
expect(onTrustChange).toHaveBeenCalledWith(true);
|
2025-08-13 11:06:31 -07:00
|
|
|
});
|
2025-08-08 11:02:27 -07:00
|
|
|
|
2025-08-13 11:06:31 -07:00
|
|
|
it('should not open dialog when folder is already untrusted', () => {
|
2025-08-14 11:15:48 -07:00
|
|
|
isWorkspaceTrustedSpy.mockReturnValue(false);
|
2025-08-13 11:06:31 -07:00
|
|
|
const { result } = renderHook(() =>
|
2025-09-08 16:41:39 +00:00
|
|
|
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
|
2025-08-13 11:06:31 -07:00
|
|
|
);
|
2025-08-08 11:02:27 -07:00
|
|
|
expect(result.current.isFolderTrustDialogOpen).toBe(false);
|
2025-08-14 11:15:48 -07:00
|
|
|
expect(onTrustChange).toHaveBeenCalledWith(false);
|
2025-08-08 11:02:27 -07:00
|
|
|
});
|
|
|
|
|
|
2025-08-13 11:06:31 -07:00
|
|
|
it('should open dialog when folder trust is undefined', () => {
|
2025-08-14 11:15:48 -07:00
|
|
|
isWorkspaceTrustedSpy.mockReturnValue(undefined);
|
2025-08-13 11:06:31 -07:00
|
|
|
const { result } = renderHook(() =>
|
2025-09-08 16:41:39 +00:00
|
|
|
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
|
2025-08-13 11:06:31 -07:00
|
|
|
);
|
|
|
|
|
expect(result.current.isFolderTrustDialogOpen).toBe(true);
|
2025-08-14 11:15:48 -07:00
|
|
|
expect(onTrustChange).toHaveBeenCalledWith(undefined);
|
2025-08-13 11:06:31 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle TRUST_FOLDER choice', () => {
|
2025-08-21 00:38:12 -07:00
|
|
|
isWorkspaceTrustedSpy
|
|
|
|
|
.mockReturnValueOnce(undefined)
|
|
|
|
|
.mockReturnValueOnce(true);
|
2025-08-13 11:06:31 -07:00
|
|
|
const { result } = renderHook(() =>
|
2025-09-08 16:41:39 +00:00
|
|
|
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
|
2025-08-13 11:06:31 -07:00
|
|
|
);
|
2025-08-08 11:02:27 -07:00
|
|
|
|
2025-08-14 11:15:48 -07:00
|
|
|
isWorkspaceTrustedSpy.mockReturnValue(true);
|
2025-08-13 11:06:31 -07:00
|
|
|
act(() => {
|
|
|
|
|
result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_FOLDER);
|
|
|
|
|
});
|
2025-08-08 11:02:27 -07:00
|
|
|
|
2025-08-13 11:06:31 -07:00
|
|
|
expect(loadTrustedFoldersSpy).toHaveBeenCalled();
|
|
|
|
|
expect(mockTrustedFolders.setValue).toHaveBeenCalledWith(
|
|
|
|
|
'/test/path',
|
|
|
|
|
TrustLevel.TRUST_FOLDER,
|
|
|
|
|
);
|
2025-08-08 11:02:27 -07:00
|
|
|
expect(result.current.isFolderTrustDialogOpen).toBe(false);
|
2025-08-14 11:15:48 -07:00
|
|
|
expect(onTrustChange).toHaveBeenLastCalledWith(true);
|
2025-08-08 11:02:27 -07:00
|
|
|
});
|
|
|
|
|
|
2025-08-13 11:06:31 -07:00
|
|
|
it('should handle TRUST_PARENT choice', () => {
|
2025-08-21 00:38:12 -07:00
|
|
|
isWorkspaceTrustedSpy
|
|
|
|
|
.mockReturnValueOnce(undefined)
|
|
|
|
|
.mockReturnValueOnce(true);
|
2025-08-13 11:06:31 -07:00
|
|
|
const { result } = renderHook(() =>
|
2025-09-08 16:41:39 +00:00
|
|
|
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
|
2025-08-13 11:06:31 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_PARENT);
|
|
|
|
|
});
|
2025-08-08 11:02:27 -07:00
|
|
|
|
2025-08-13 11:06:31 -07:00
|
|
|
expect(mockTrustedFolders.setValue).toHaveBeenCalledWith(
|
|
|
|
|
'/test/path',
|
|
|
|
|
TrustLevel.TRUST_PARENT,
|
|
|
|
|
);
|
|
|
|
|
expect(result.current.isFolderTrustDialogOpen).toBe(false);
|
2025-08-14 11:15:48 -07:00
|
|
|
expect(onTrustChange).toHaveBeenLastCalledWith(true);
|
2025-08-13 11:06:31 -07:00
|
|
|
});
|
|
|
|
|
|
2025-08-21 00:38:12 -07:00
|
|
|
it('should handle DO_NOT_TRUST choice and trigger restart', () => {
|
|
|
|
|
isWorkspaceTrustedSpy
|
|
|
|
|
.mockReturnValueOnce(undefined)
|
|
|
|
|
.mockReturnValueOnce(false);
|
2025-08-13 11:06:31 -07:00
|
|
|
const { result } = renderHook(() =>
|
2025-09-08 16:41:39 +00:00
|
|
|
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
|
2025-08-13 11:06:31 -07:00
|
|
|
);
|
2025-08-08 11:02:27 -07:00
|
|
|
|
|
|
|
|
act(() => {
|
2025-08-13 11:06:31 -07:00
|
|
|
result.current.handleFolderTrustSelect(FolderTrustChoice.DO_NOT_TRUST);
|
2025-08-08 11:02:27 -07:00
|
|
|
});
|
|
|
|
|
|
2025-08-13 11:06:31 -07:00
|
|
|
expect(mockTrustedFolders.setValue).toHaveBeenCalledWith(
|
|
|
|
|
'/test/path',
|
|
|
|
|
TrustLevel.DO_NOT_TRUST,
|
2025-08-08 11:02:27 -07:00
|
|
|
);
|
2025-08-14 11:15:48 -07:00
|
|
|
expect(onTrustChange).toHaveBeenLastCalledWith(false);
|
2025-09-06 01:39:02 -04:00
|
|
|
expect(result.current.isRestarting).toBe(false);
|
|
|
|
|
expect(result.current.isFolderTrustDialogOpen).toBe(false);
|
2025-08-08 11:02:27 -07:00
|
|
|
});
|
2025-08-13 11:06:31 -07:00
|
|
|
|
|
|
|
|
it('should do nothing for default choice', () => {
|
2025-08-14 11:15:48 -07:00
|
|
|
isWorkspaceTrustedSpy.mockReturnValue(undefined);
|
2025-08-13 11:06:31 -07:00
|
|
|
const { result } = renderHook(() =>
|
2025-09-08 16:41:39 +00:00
|
|
|
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
|
2025-08-13 11:06:31 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleFolderTrustSelect(
|
|
|
|
|
'invalid_choice' as FolderTrustChoice,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(mockTrustedFolders.setValue).not.toHaveBeenCalled();
|
|
|
|
|
expect(mockSettings.setValue).not.toHaveBeenCalled();
|
|
|
|
|
expect(result.current.isFolderTrustDialogOpen).toBe(true);
|
2025-08-14 11:15:48 -07:00
|
|
|
expect(onTrustChange).toHaveBeenCalledWith(undefined);
|
2025-08-13 11:06:31 -07:00
|
|
|
});
|
2025-08-21 00:38:12 -07:00
|
|
|
|
|
|
|
|
it('should set isRestarting to true when trust status changes from false to true', () => {
|
|
|
|
|
isWorkspaceTrustedSpy.mockReturnValueOnce(false).mockReturnValueOnce(true); // Initially untrusted, then trusted
|
|
|
|
|
const { result } = renderHook(() =>
|
2025-09-08 16:41:39 +00:00
|
|
|
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
|
2025-08-21 00:38:12 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_FOLDER);
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-06 01:39:02 -04:00
|
|
|
expect(result.current.isRestarting).toBe(false);
|
|
|
|
|
expect(result.current.isFolderTrustDialogOpen).toBe(false); // Dialog should close after selection
|
2025-08-21 00:38:12 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not set isRestarting to true when trust status does not change', () => {
|
|
|
|
|
isWorkspaceTrustedSpy
|
|
|
|
|
.mockReturnValueOnce(undefined)
|
|
|
|
|
.mockReturnValueOnce(true); // Initially undefined, then trust
|
|
|
|
|
const { result } = renderHook(() =>
|
2025-09-08 16:41:39 +00:00
|
|
|
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
|
2025-08-21 00:38:12 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_FOLDER);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.current.isRestarting).toBe(false);
|
|
|
|
|
expect(result.current.isFolderTrustDialogOpen).toBe(false); // Dialog should close
|
|
|
|
|
});
|
2025-09-08 16:41:39 +00:00
|
|
|
|
|
|
|
|
it('should call refreshStatic when dialog opens and closes', () => {
|
|
|
|
|
isWorkspaceTrustedSpy.mockReturnValue(undefined);
|
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
|
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// The hook runs, isFolderTrustDialogOpen becomes true, useEffect triggers.
|
|
|
|
|
// It's called once on mount, and once when the dialog state changes.
|
|
|
|
|
expect(refreshStatic).toHaveBeenCalledTimes(2);
|
|
|
|
|
expect(result.current.isFolderTrustDialogOpen).toBe(true);
|
|
|
|
|
|
|
|
|
|
// Now, simulate closing the dialog
|
|
|
|
|
isWorkspaceTrustedSpy.mockReturnValue(true); // So the state update works
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_FOLDER);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// The state isFolderTrustDialogOpen becomes false, useEffect triggers again
|
|
|
|
|
expect(refreshStatic).toHaveBeenCalledTimes(3);
|
|
|
|
|
expect(result.current.isFolderTrustDialogOpen).toBe(false);
|
|
|
|
|
});
|
2025-08-08 11:02:27 -07:00
|
|
|
});
|