File size: 4,829 Bytes
7421850 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | /**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect } from 'vitest';
import { renderHookActionFeedback } from './hookUtils.js';
import type { HookActionResult } from './hookSettings.js';
import { SettingScope } from '../config/settings.js';
describe('hookUtils', () => {
describe('renderHookActionFeedback', () => {
const mockFormatScope = (label: string, path: string) =>
`${label} (${path})`;
it('should render error message', () => {
const result: HookActionResult = {
status: 'error',
hookName: 'test-hook',
action: 'enable',
modifiedScopes: [],
alreadyInStateScopes: [],
error: 'Something went wrong',
};
const message = renderHookActionFeedback(result, mockFormatScope);
expect(message).toBe('Something went wrong');
});
it('should render default error message if error string is missing', () => {
const result: HookActionResult = {
status: 'error',
hookName: 'test-hook',
action: 'enable',
modifiedScopes: [],
alreadyInStateScopes: [],
};
const message = renderHookActionFeedback(result, mockFormatScope);
expect(message).toBe(
'An error occurred while attempting to enable hook "test-hook".',
);
});
it('should render no-op message for enable', () => {
const result: HookActionResult = {
status: 'no-op',
hookName: 'test-hook',
action: 'enable',
modifiedScopes: [],
alreadyInStateScopes: [],
};
const message = renderHookActionFeedback(result, mockFormatScope);
expect(message).toBe('Hook "test-hook" is already enabled.');
});
it('should render no-op message for disable', () => {
const result: HookActionResult = {
status: 'no-op',
hookName: 'test-hook',
action: 'disable',
modifiedScopes: [],
alreadyInStateScopes: [],
};
const message = renderHookActionFeedback(result, mockFormatScope);
expect(message).toBe('Hook "test-hook" is already disabled.');
});
it('should render success message for enable (single scope)', () => {
const result: HookActionResult = {
status: 'success',
hookName: 'test-hook',
action: 'enable',
modifiedScopes: [{ scope: SettingScope.User, path: '/path/user.json' }],
alreadyInStateScopes: [
{ scope: SettingScope.Workspace, path: '/path/workspace.json' },
],
};
const message = renderHookActionFeedback(result, mockFormatScope);
expect(message).toBe(
'Hook "test-hook" enabled by removing it from the disabled list in user (/path/user.json) and workspace (/path/workspace.json) settings.',
);
});
it('should render success message for enable (single scope only affected)', () => {
// E.g. Workspace doesn't exist or isn't loadable, so only User is affected.
const result: HookActionResult = {
status: 'success',
hookName: 'test-hook',
action: 'enable',
modifiedScopes: [{ scope: SettingScope.User, path: '/path/user.json' }],
alreadyInStateScopes: [],
};
const message = renderHookActionFeedback(result, mockFormatScope);
expect(message).toBe(
'Hook "test-hook" enabled by removing it from the disabled list in user (/path/user.json) settings.',
);
});
it('should render success message for disable (single scope)', () => {
const result: HookActionResult = {
status: 'success',
hookName: 'test-hook',
action: 'disable',
modifiedScopes: [
{ scope: SettingScope.Workspace, path: '/path/workspace.json' },
],
alreadyInStateScopes: [],
};
const message = renderHookActionFeedback(result, mockFormatScope);
expect(message).toBe(
'Hook "test-hook" disabled by adding it to the disabled list in workspace (/path/workspace.json) settings.',
);
});
it('should render success message for disable (two scopes)', () => {
// E.g. Disabled in Workspace, but ALREADY disabled in User.
const result: HookActionResult = {
status: 'success',
hookName: 'test-hook',
action: 'disable',
modifiedScopes: [
{ scope: SettingScope.Workspace, path: '/path/workspace.json' },
],
alreadyInStateScopes: [
{ scope: SettingScope.User, path: '/path/user.json' },
],
};
const message = renderHookActionFeedback(result, mockFormatScope);
expect(message).toBe(
'Hook "test-hook" is now disabled in both workspace (/path/workspace.json) and user (/path/user.json) settings.',
);
});
});
});
|