|
| 1 | +import * as fs from 'node:fs/promises'; |
| 2 | + |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { DEFAULT_SETTINGS } from '../defaultSettings'; |
| 6 | +import { ClaudeCodeInstallationInfo, TweakccConfig } from '../types'; |
| 7 | +import { updateConfigFile } from '../config'; |
| 8 | +import { replaceFileBreakingHardLinks } from '../utils'; |
| 9 | +import { restoreClijsFromBackup } from '../installationBackup'; |
| 10 | +import { writeModelCustomizations } from './modelSelector'; |
| 11 | +import { writeShowMoreItemsInSelectMenus } from './showMoreItemsInSelectMenus'; |
| 12 | +import { applySystemPrompts } from './systemPrompts'; |
| 13 | +import { applyCustomization } from './index'; |
| 14 | + |
| 15 | +const mockReadFile = vi.hoisted(() => vi.fn()); |
| 16 | + |
| 17 | +vi.mock('node:fs/promises', () => ({ |
| 18 | + readFile: mockReadFile, |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock('../config', () => ({ |
| 22 | + CONFIG_DIR: '/tmp/tweakcc-test-config', |
| 23 | + NATIVE_BINARY_BACKUP_FILE: '/tmp/tweakcc-test-config/native.backup', |
| 24 | + updateConfigFile: vi.fn(async updateFn => { |
| 25 | + const config = { changesApplied: false } as TweakccConfig; |
| 26 | + updateFn(config); |
| 27 | + return config; |
| 28 | + }), |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock('../utils', () => ({ |
| 32 | + debug: vi.fn(), |
| 33 | + replaceFileBreakingHardLinks: vi.fn(), |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock('../installationBackup', () => ({ |
| 37 | + restoreNativeBinaryFromBackup: vi.fn(), |
| 38 | + restoreClijsFromBackup: vi.fn(async () => true), |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock('../nativeInstallationLoader', () => ({ |
| 42 | + extractClaudeJsFromNativeInstallation: vi.fn(), |
| 43 | + repackNativeInstallation: vi.fn(), |
| 44 | +})); |
| 45 | + |
| 46 | +vi.mock('./modelSelector', () => ({ |
| 47 | + writeModelCustomizations: vi.fn((content: string) => `${content}|model`), |
| 48 | +})); |
| 49 | + |
| 50 | +vi.mock('./showMoreItemsInSelectMenus', () => ({ |
| 51 | + writeShowMoreItemsInSelectMenus: vi.fn( |
| 52 | + (content: string) => `${content}|show` |
| 53 | + ), |
| 54 | +})); |
| 55 | + |
| 56 | +vi.mock('./systemPrompts', () => ({ |
| 57 | + applySystemPrompts: vi.fn(async (content: string) => ({ |
| 58 | + newContent: content, |
| 59 | + results: [], |
| 60 | + })), |
| 61 | +})); |
| 62 | + |
| 63 | +const baseConfig = (): TweakccConfig => ({ |
| 64 | + ccVersion: '', |
| 65 | + ccInstallationPath: null, |
| 66 | + lastModified: '2026-01-01T00:00:00.000Z', |
| 67 | + changesApplied: false, |
| 68 | + settings: { |
| 69 | + ...DEFAULT_SETTINGS, |
| 70 | + misc: { |
| 71 | + ...DEFAULT_SETTINGS.misc, |
| 72 | + }, |
| 73 | + }, |
| 74 | +}); |
| 75 | + |
| 76 | +const ccInstInfo: ClaudeCodeInstallationInfo = { |
| 77 | + cliPath: '/tmp/claude-cli.js', |
| 78 | + version: '2.1.63', |
| 79 | + source: 'search-paths', |
| 80 | +}; |
| 81 | + |
| 82 | +describe('model customization toggle patch conditions', () => { |
| 83 | + beforeEach(() => { |
| 84 | + vi.clearAllMocks(); |
| 85 | + vi.mocked(fs.readFile).mockResolvedValue('base-content'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('skips both model customization patches when disabled', async () => { |
| 89 | + const config = baseConfig(); |
| 90 | + config.settings.misc.enableModelCustomizations = false; |
| 91 | + |
| 92 | + const { results } = await applyCustomization(config, ccInstInfo, [ |
| 93 | + 'model-customizations', |
| 94 | + 'show-more-items-in-select-menus', |
| 95 | + ]); |
| 96 | + |
| 97 | + const modelResult = results.find(r => r.id === 'model-customizations'); |
| 98 | + const showMoreResult = results.find( |
| 99 | + r => r.id === 'show-more-items-in-select-menus' |
| 100 | + ); |
| 101 | + |
| 102 | + expect(modelResult).toMatchObject({ applied: false, skipped: true }); |
| 103 | + expect(showMoreResult).toMatchObject({ applied: false, skipped: true }); |
| 104 | + expect(vi.mocked(writeModelCustomizations)).not.toHaveBeenCalled(); |
| 105 | + expect(vi.mocked(writeShowMoreItemsInSelectMenus)).not.toHaveBeenCalled(); |
| 106 | + expect(vi.mocked(replaceFileBreakingHardLinks)).toHaveBeenCalledWith( |
| 107 | + '/tmp/claude-cli.js', |
| 108 | + 'base-content', |
| 109 | + 'patch' |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it('applies both model customization patches when enabled', async () => { |
| 114 | + const config = baseConfig(); |
| 115 | + config.settings.misc.enableModelCustomizations = true; |
| 116 | + |
| 117 | + const { results } = await applyCustomization(config, ccInstInfo, [ |
| 118 | + 'model-customizations', |
| 119 | + 'show-more-items-in-select-menus', |
| 120 | + ]); |
| 121 | + |
| 122 | + const modelResult = results.find(r => r.id === 'model-customizations'); |
| 123 | + const showMoreResult = results.find( |
| 124 | + r => r.id === 'show-more-items-in-select-menus' |
| 125 | + ); |
| 126 | + |
| 127 | + expect(modelResult).toMatchObject({ applied: true, failed: false }); |
| 128 | + expect(showMoreResult).toMatchObject({ applied: true, failed: false }); |
| 129 | + expect(vi.mocked(writeModelCustomizations)).toHaveBeenCalledTimes(1); |
| 130 | + expect(vi.mocked(writeShowMoreItemsInSelectMenus)).toHaveBeenCalledTimes(1); |
| 131 | + expect(vi.mocked(replaceFileBreakingHardLinks)).toHaveBeenCalledWith( |
| 132 | + '/tmp/claude-cli.js', |
| 133 | + expect.stringContaining('base-content'), |
| 134 | + 'patch' |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it('runs plumbing required for apply customization', async () => { |
| 139 | + await applyCustomization(baseConfig(), ccInstInfo, [ |
| 140 | + 'model-customizations', |
| 141 | + 'show-more-items-in-select-menus', |
| 142 | + ]); |
| 143 | + |
| 144 | + expect(vi.mocked(restoreClijsFromBackup)).toHaveBeenCalledTimes(1); |
| 145 | + expect(vi.mocked(applySystemPrompts)).toHaveBeenCalledTimes(1); |
| 146 | + expect(vi.mocked(updateConfigFile)).toHaveBeenCalledTimes(1); |
| 147 | + }); |
| 148 | +}); |
0 commit comments