mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Migrate AI settings and git settings to the VFS
This commit is contained in:
7 files changed
+597
-91
No files matched your search
@@ -0,0 +1,231 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import {
|
||||
readAISettings,
|
||||
writeAISettings,
|
||||
readGitSettings,
|
||||
writeGitSettings
|
||||
} from './configUtils';
|
||||
import type { JSRuntimeFS } from './JSRuntime';
|
||||
import type { AISettings } from '@/contexts/AISettingsContext';
|
||||
import type { GitSettings } from '@/contexts/GitSettingsContext';
|
||||
|
||||
// Mock filesystem
|
||||
const createMockFS = (): JSRuntimeFS => ({
|
||||
readFile: vi.fn(),
|
||||
writeFile: vi.fn(),
|
||||
mkdir: vi.fn(),
|
||||
stat: vi.fn(),
|
||||
readdir: vi.fn(),
|
||||
rmdir: vi.fn(),
|
||||
unlink: vi.fn(),
|
||||
lstat: vi.fn(),
|
||||
rename: vi.fn(),
|
||||
readlink: vi.fn(),
|
||||
symlink: vi.fn(),
|
||||
});
|
||||
|
||||
// Mock localStorage
|
||||
const createMockLocalStorage = () => {
|
||||
const storage: Record<string, string> = {};
|
||||
return {
|
||||
getItem: vi.fn((key: string) => storage[key] || null),
|
||||
setItem: vi.fn((key: string, value: string) => { storage[key] = value; }),
|
||||
removeItem: vi.fn((key: string) => { delete storage[key]; }),
|
||||
clear: vi.fn(() => { Object.keys(storage).forEach(key => delete storage[key]); }),
|
||||
length: 0,
|
||||
key: vi.fn(),
|
||||
};
|
||||
};
|
||||
|
||||
describe('configUtils', () => {
|
||||
let mockFS: JSRuntimeFS;
|
||||
let mockLocalStorage: ReturnType<typeof createMockLocalStorage>;
|
||||
|
||||
beforeEach(() => {
|
||||
mockFS = createMockFS();
|
||||
mockLocalStorage = createMockLocalStorage();
|
||||
|
||||
// Replace global localStorage
|
||||
Object.defineProperty(window, 'localStorage', {
|
||||
value: mockLocalStorage,
|
||||
writable: true,
|
||||
});
|
||||
});
|
||||
|
||||
describe('AI Settings', () => {
|
||||
const sampleAISettings: AISettings = {
|
||||
providers: {
|
||||
'openai': {
|
||||
baseURL: 'https://api.openai.com/v1',
|
||||
apiKey: 'sk-test123',
|
||||
},
|
||||
},
|
||||
recentlyUsedModels: ['gpt-4', 'gpt-3.5-turbo'],
|
||||
};
|
||||
|
||||
describe('readAISettings', () => {
|
||||
it('should read AI settings from VFS', async () => {
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(sampleAISettings));
|
||||
|
||||
const result = await readAISettings(mockFS);
|
||||
|
||||
expect(mockFS.readFile).toHaveBeenCalledWith('/config/ai.json', 'utf8');
|
||||
expect(result).toEqual(sampleAISettings);
|
||||
});
|
||||
|
||||
it('should return default settings if file does not exist and no localStorage data', async () => {
|
||||
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('File not found'));
|
||||
mockLocalStorage.getItem.mockReturnValue(null);
|
||||
|
||||
const result = await readAISettings(mockFS);
|
||||
|
||||
expect(result).toEqual({
|
||||
providers: {},
|
||||
recentlyUsedModels: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('should return default settings if JSON is invalid and no localStorage data', async () => {
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue('invalid json');
|
||||
mockLocalStorage.getItem.mockReturnValue(null);
|
||||
|
||||
const result = await readAISettings(mockFS);
|
||||
|
||||
expect(result).toEqual({
|
||||
providers: {},
|
||||
recentlyUsedModels: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('should migrate from localStorage if VFS file does not exist', async () => {
|
||||
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('File not found'));
|
||||
vi.mocked(mockFS.stat).mockRejectedValue(new Error('Directory not found'));
|
||||
vi.mocked(mockFS.mkdir).mockResolvedValue(undefined);
|
||||
vi.mocked(mockFS.writeFile).mockResolvedValue(undefined);
|
||||
|
||||
mockLocalStorage.getItem.mockReturnValue(JSON.stringify(sampleAISettings));
|
||||
|
||||
const result = await readAISettings(mockFS);
|
||||
|
||||
expect(result).toEqual(sampleAISettings);
|
||||
expect(mockLocalStorage.getItem).toHaveBeenCalledWith('ai-settings');
|
||||
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('ai-settings');
|
||||
expect(mockFS.writeFile).toHaveBeenCalledWith(
|
||||
'/config/ai.json',
|
||||
JSON.stringify(sampleAISettings, null, 2),
|
||||
'utf8'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('writeAISettings', () => {
|
||||
it('should write AI settings to VFS', async () => {
|
||||
vi.mocked(mockFS.stat).mockRejectedValue(new Error('Directory not found'));
|
||||
vi.mocked(mockFS.mkdir).mockResolvedValue(undefined);
|
||||
vi.mocked(mockFS.writeFile).mockResolvedValue(undefined);
|
||||
|
||||
await writeAISettings(mockFS, sampleAISettings);
|
||||
|
||||
expect(mockFS.mkdir).toHaveBeenCalledWith('/config', { recursive: true });
|
||||
expect(mockFS.writeFile).toHaveBeenCalledWith(
|
||||
'/config/ai.json',
|
||||
JSON.stringify(sampleAISettings, null, 2),
|
||||
'utf8'
|
||||
);
|
||||
});
|
||||
|
||||
it('should not create directory if it already exists', async () => {
|
||||
vi.mocked(mockFS.stat).mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
isFile: () => false,
|
||||
});
|
||||
vi.mocked(mockFS.writeFile).mockResolvedValue(undefined);
|
||||
|
||||
await writeAISettings(mockFS, sampleAISettings);
|
||||
|
||||
expect(mockFS.mkdir).not.toHaveBeenCalled();
|
||||
expect(mockFS.writeFile).toHaveBeenCalledWith(
|
||||
'/config/ai.json',
|
||||
JSON.stringify(sampleAISettings, null, 2),
|
||||
'utf8'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('Git Settings', () => {
|
||||
const sampleGitSettings: GitSettings = {
|
||||
credentials: {
|
||||
'github.com': {
|
||||
username: 'user',
|
||||
password: 'token123',
|
||||
},
|
||||
},
|
||||
corsProxy: 'https://cors.isomorphic-git.org',
|
||||
};
|
||||
|
||||
describe('readGitSettings', () => {
|
||||
it('should read Git settings from VFS', async () => {
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(sampleGitSettings));
|
||||
|
||||
const result = await readGitSettings(mockFS);
|
||||
|
||||
expect(mockFS.readFile).toHaveBeenCalledWith('/config/git.json', 'utf8');
|
||||
expect(result).toEqual(sampleGitSettings);
|
||||
});
|
||||
|
||||
it('should return default settings if file does not exist and no localStorage data', async () => {
|
||||
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('File not found'));
|
||||
mockLocalStorage.getItem.mockReturnValue(null);
|
||||
|
||||
const result = await readGitSettings(mockFS);
|
||||
|
||||
expect(result).toEqual({
|
||||
credentials: {},
|
||||
corsProxy: 'https://cors.isomorphic-git.org',
|
||||
});
|
||||
});
|
||||
|
||||
it('should migrate from localStorage if VFS file does not exist', async () => {
|
||||
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('File not found'));
|
||||
vi.mocked(mockFS.stat).mockRejectedValue(new Error('Directory not found'));
|
||||
vi.mocked(mockFS.mkdir).mockResolvedValue(undefined);
|
||||
vi.mocked(mockFS.writeFile).mockResolvedValue(undefined);
|
||||
|
||||
mockLocalStorage.getItem.mockReturnValue(JSON.stringify(sampleGitSettings));
|
||||
|
||||
const result = await readGitSettings(mockFS);
|
||||
|
||||
expect(result).toEqual(sampleGitSettings);
|
||||
expect(mockLocalStorage.getItem).toHaveBeenCalledWith('git-settings');
|
||||
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('git-settings');
|
||||
expect(mockFS.writeFile).toHaveBeenCalledWith(
|
||||
'/config/git.json',
|
||||
JSON.stringify(sampleGitSettings, null, 2),
|
||||
'utf8'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('writeGitSettings', () => {
|
||||
it('should write Git settings to VFS', async () => {
|
||||
vi.mocked(mockFS.stat).mockRejectedValue(new Error('Directory not found'));
|
||||
vi.mocked(mockFS.mkdir).mockResolvedValue(undefined);
|
||||
vi.mocked(mockFS.writeFile).mockResolvedValue(undefined);
|
||||
|
||||
await writeGitSettings(mockFS, sampleGitSettings);
|
||||
|
||||
expect(mockFS.mkdir).toHaveBeenCalledWith('/config', { recursive: true });
|
||||
expect(mockFS.writeFile).toHaveBeenCalledWith(
|
||||
'/config/git.json',
|
||||
JSON.stringify(sampleGitSettings, null, 2),
|
||||
'utf8'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,159 @@
|
||||
import type { JSRuntimeFS } from '@/lib/JSRuntime';
|
||||
import type { AISettings } from '@/contexts/AISettingsContext';
|
||||
import type { GitSettings } from '@/contexts/GitSettingsContext';
|
||||
|
||||
const CONFIG_DIR = '/config';
|
||||
const AI_CONFIG_PATH = `${CONFIG_DIR}/ai.json`;
|
||||
const GIT_CONFIG_PATH = `${CONFIG_DIR}/git.json`;
|
||||
|
||||
/**
|
||||
* Ensures the config directory exists
|
||||
*/
|
||||
async function ensureConfigDir(fs: JSRuntimeFS): Promise<void> {
|
||||
try {
|
||||
await fs.stat(CONFIG_DIR);
|
||||
} catch {
|
||||
await fs.mkdir(CONFIG_DIR, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate AI settings from localStorage to VFS (internal function)
|
||||
*/
|
||||
async function migrateAISettingsFromLocalStorage(fs: JSRuntimeFS): Promise<AISettings | null> {
|
||||
// If not in VFS, try to migrate from localStorage
|
||||
const stored = localStorage.getItem('ai-settings');
|
||||
if (stored) {
|
||||
try {
|
||||
const parsed = JSON.parse(stored);
|
||||
if (parsed && typeof parsed === 'object' && 'providers' in parsed) {
|
||||
const settings: AISettings = {
|
||||
providers: parsed.providers || {},
|
||||
recentlyUsedModels: parsed.recentlyUsedModels || [],
|
||||
};
|
||||
|
||||
// Write to VFS and remove from localStorage
|
||||
await writeAISettings(fs, settings);
|
||||
localStorage.removeItem('ai-settings');
|
||||
|
||||
return settings;
|
||||
}
|
||||
} catch {
|
||||
// If parsing fails, return null
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read AI settings from VFS (with automatic migration from localStorage)
|
||||
*/
|
||||
export async function readAISettings(fs: JSRuntimeFS): Promise<AISettings> {
|
||||
const defaultSettings: AISettings = {
|
||||
providers: {},
|
||||
recentlyUsedModels: [],
|
||||
};
|
||||
|
||||
// Try to read from VFS first
|
||||
try {
|
||||
const content = await fs.readFile(AI_CONFIG_PATH, 'utf8');
|
||||
const parsed = JSON.parse(content);
|
||||
|
||||
if (parsed && typeof parsed === 'object' && 'providers' in parsed) {
|
||||
return {
|
||||
providers: parsed.providers || {},
|
||||
recentlyUsedModels: parsed.recentlyUsedModels || [],
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// File doesn't exist or is invalid, try migration
|
||||
}
|
||||
|
||||
// If not in VFS, try to migrate from localStorage
|
||||
const migratedSettings = await migrateAISettingsFromLocalStorage(fs);
|
||||
if (migratedSettings) {
|
||||
return migratedSettings;
|
||||
}
|
||||
|
||||
return defaultSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write AI settings to VFS
|
||||
*/
|
||||
export async function writeAISettings(fs: JSRuntimeFS, settings: AISettings): Promise<void> {
|
||||
await ensureConfigDir(fs);
|
||||
await fs.writeFile(AI_CONFIG_PATH, JSON.stringify(settings, null, 2), 'utf8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate Git settings from localStorage to VFS (internal function)
|
||||
*/
|
||||
async function migrateGitSettingsFromLocalStorage(fs: JSRuntimeFS): Promise<GitSettings | null> {
|
||||
// If not in VFS, try to migrate from localStorage
|
||||
const stored = localStorage.getItem('git-settings');
|
||||
if (stored) {
|
||||
try {
|
||||
const parsed = JSON.parse(stored);
|
||||
if (parsed && typeof parsed === 'object' && 'credentials' in parsed) {
|
||||
const settings: GitSettings = {
|
||||
credentials: parsed.credentials || {},
|
||||
corsProxy: parsed.corsProxy || 'https://cors.isomorphic-git.org',
|
||||
};
|
||||
|
||||
// Write to VFS and remove from localStorage
|
||||
await writeGitSettings(fs, settings);
|
||||
localStorage.removeItem('git-settings');
|
||||
|
||||
return settings;
|
||||
}
|
||||
} catch {
|
||||
// If parsing fails, return null
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read Git settings from VFS (with automatic migration from localStorage)
|
||||
*/
|
||||
export async function readGitSettings(fs: JSRuntimeFS): Promise<GitSettings> {
|
||||
const defaultSettings: GitSettings = {
|
||||
credentials: {},
|
||||
corsProxy: 'https://cors.isomorphic-git.org',
|
||||
};
|
||||
|
||||
// Try to read from VFS first
|
||||
try {
|
||||
const content = await fs.readFile(GIT_CONFIG_PATH, 'utf8');
|
||||
const parsed = JSON.parse(content);
|
||||
|
||||
if (parsed && typeof parsed === 'object' && 'credentials' in parsed) {
|
||||
return {
|
||||
credentials: parsed.credentials || {},
|
||||
corsProxy: parsed.corsProxy || 'https://cors.isomorphic-git.org',
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// File doesn't exist or is invalid, try migration
|
||||
}
|
||||
|
||||
// If not in VFS, try to migrate from localStorage
|
||||
const migratedSettings = await migrateGitSettingsFromLocalStorage(fs);
|
||||
if (migratedSettings) {
|
||||
return migratedSettings;
|
||||
}
|
||||
|
||||
return defaultSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write Git settings to VFS
|
||||
*/
|
||||
export async function writeGitSettings(fs: JSRuntimeFS, settings: GitSettings): Promise<void> {
|
||||
await ensureConfigDir(fs);
|
||||
await fs.writeFile(GIT_CONFIG_PATH, JSON.stringify(settings, null, 2), 'utf8');
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user