|
| 1 | +import type {FileSystem} from '@/application/fs/fileSystem'; |
| 2 | +import type {Validator, ValidationResult} from '@/application/validation'; |
| 3 | +import {VirtualizedWorkingDirectory} from '@/application/fs/workingDirectory/virtualizedWorkingDirectory'; |
| 4 | +import type { |
| 5 | + JsonProjectConfiguration, |
| 6 | + JsonPartialProjectConfiguration, |
| 7 | +} from '@/application/project/configuration/manager/jsonConfigurationFileManager'; |
| 8 | +import {JsonConfigurationFileManager} from '@/application/project/configuration/manager/jsonConfigurationFileManager'; |
| 9 | +import type {ProjectConfiguration} from '@/application/project/configuration/projectConfiguration'; |
| 10 | + |
| 11 | +describe('JsonConfigurationFileManager', () => { |
| 12 | + const configuration: ProjectConfiguration = { |
| 13 | + organization: 'org', |
| 14 | + workspace: 'workspace', |
| 15 | + applications: { |
| 16 | + development: 'dev', |
| 17 | + }, |
| 18 | + defaultLocale: 'en', |
| 19 | + locales: ['en'], |
| 20 | + slots: {}, |
| 21 | + components: {}, |
| 22 | + }; |
| 23 | + |
| 24 | + function createFileSystem(files: Map<string, string>): FileSystem { |
| 25 | + return { |
| 26 | + exists: (path: string): Promise<boolean> => Promise.resolve(files.has(path)), |
| 27 | + readTextFile: (path: string): Promise<string> => { |
| 28 | + const content = files.get(path); |
| 29 | + |
| 30 | + if (content === undefined) { |
| 31 | + return Promise.reject(new Error(`File not found: ${path}`)); |
| 32 | + } |
| 33 | + |
| 34 | + return Promise.resolve(content); |
| 35 | + }, |
| 36 | + writeTextFile: (path: string, content: string): Promise<void> => { |
| 37 | + files.set(path, content); |
| 38 | + |
| 39 | + return Promise.resolve(); |
| 40 | + }, |
| 41 | + isAbsolutePath: (path: string): boolean => path.startsWith('/'), |
| 42 | + joinPaths: (...paths: string[]): string => paths.join('/'), |
| 43 | + } as Partial<FileSystem> as FileSystem; |
| 44 | + } |
| 45 | + |
| 46 | + function createValidator<T>(): Validator<T> { |
| 47 | + return { |
| 48 | + validate: (data: unknown): ValidationResult<T> => ({ |
| 49 | + valid: true, |
| 50 | + data: data as T, |
| 51 | + }), |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + it('should resolve the configuration file against the current project directory', async () => { |
| 56 | + const files = new Map<string, string>(); |
| 57 | + const projectDirectory = new VirtualizedWorkingDirectory('/initial'); |
| 58 | + |
| 59 | + const manager = new JsonConfigurationFileManager({ |
| 60 | + fileSystem: createFileSystem(files), |
| 61 | + projectDirectory: projectDirectory, |
| 62 | + fullValidator: createValidator<JsonProjectConfiguration>(), |
| 63 | + partialValidator: createValidator<JsonPartialProjectConfiguration>(), |
| 64 | + configurationFile: 'croct.json', |
| 65 | + }); |
| 66 | + |
| 67 | + projectDirectory.setCurrentDirectory('/initial/project'); |
| 68 | + |
| 69 | + await manager.update(configuration); |
| 70 | + |
| 71 | + expect(files.has('/initial/croct.json')).toBe(false); |
| 72 | + expect(files.has('/initial/project/croct.json')).toBe(true); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should load the configuration from the current project directory', async () => { |
| 76 | + const files = new Map<string, string>(); |
| 77 | + const projectDirectory = new VirtualizedWorkingDirectory('/initial'); |
| 78 | + |
| 79 | + const manager = new JsonConfigurationFileManager({ |
| 80 | + fileSystem: createFileSystem(files), |
| 81 | + projectDirectory: projectDirectory, |
| 82 | + fullValidator: createValidator<JsonProjectConfiguration>(), |
| 83 | + partialValidator: createValidator<JsonPartialProjectConfiguration>(), |
| 84 | + configurationFile: 'croct.json', |
| 85 | + }); |
| 86 | + |
| 87 | + files.set('/initial/project/croct.json', JSON.stringify(configuration)); |
| 88 | + |
| 89 | + await expect(manager.isInitialized()).resolves.toBe(false); |
| 90 | + |
| 91 | + projectDirectory.setCurrentDirectory('/initial/project'); |
| 92 | + |
| 93 | + await expect(manager.isInitialized()).resolves.toBe(true); |
| 94 | + await expect(manager.load()).resolves.toEqual(configuration); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should use an absolute configuration file path as given', async () => { |
| 98 | + const files = new Map<string, string>(); |
| 99 | + const projectDirectory = new VirtualizedWorkingDirectory('/initial'); |
| 100 | + |
| 101 | + const manager = new JsonConfigurationFileManager({ |
| 102 | + fileSystem: createFileSystem(files), |
| 103 | + projectDirectory: projectDirectory, |
| 104 | + fullValidator: createValidator<JsonProjectConfiguration>(), |
| 105 | + partialValidator: createValidator<JsonPartialProjectConfiguration>(), |
| 106 | + configurationFile: '/custom/croct.json', |
| 107 | + }); |
| 108 | + |
| 109 | + projectDirectory.setCurrentDirectory('/initial/project'); |
| 110 | + |
| 111 | + await manager.update(configuration); |
| 112 | + |
| 113 | + expect(files.has('/custom/croct.json')).toBe(true); |
| 114 | + }); |
| 115 | +}); |
0 commit comments