|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { test, expect, beforeEach } from 'vitest'; |
| 5 | +import { ConfigManager } from './config-manager'; |
| 6 | + |
| 7 | +const extensionContext = { |
| 8 | + globalStoragePath: '/tmp/vscode-tailscale', |
| 9 | +} as vscode.ExtensionContext; |
| 10 | + |
| 11 | +const configPath = path.join(extensionContext.globalStoragePath, 'config.json'); |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + if (fs.existsSync(configPath)) { |
| 15 | + fs.unlinkSync(configPath); |
| 16 | + } |
| 17 | +}); |
| 18 | + |
| 19 | +test('withContext will create directory if it does not exist', () => { |
| 20 | + fs.rmSync(extensionContext.globalStoragePath, { recursive: true, force: true }); |
| 21 | + expect(fs.existsSync(extensionContext.globalStoragePath)).toBe(false); |
| 22 | + |
| 23 | + ConfigManager.withContext(extensionContext); |
| 24 | + expect(fs.existsSync(extensionContext.globalStoragePath)).toBe(true); |
| 25 | +}); |
| 26 | + |
| 27 | +test('withContext returns an initialized ConfigManager', () => { |
| 28 | + const cm = ConfigManager.withContext(extensionContext); |
| 29 | + expect(cm.configPath).toBe(configPath); |
| 30 | +}); |
| 31 | + |
| 32 | +test('set persists config to disk', () => { |
| 33 | + const cm = new ConfigManager(configPath); |
| 34 | + const hosts = { |
| 35 | + 'host-1': { |
| 36 | + user: 'foo', |
| 37 | + rootDir: '/', |
| 38 | + }, |
| 39 | + }; |
| 40 | + |
| 41 | + cm.set('hosts', hosts); |
| 42 | + expect(cm.get('hosts')).toEqual(hosts); |
| 43 | + |
| 44 | + const f = fs.readFileSync(configPath, 'utf8'); |
| 45 | + expect(JSON.parse(f)).toEqual({ hosts }); |
| 46 | +}); |
0 commit comments