-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: configuration test additions (#588)
- Loading branch information
1 parent
95b14c1
commit dcd6ac1
Showing
29 changed files
with
448 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
export { default as RuntimeConfigurationInvalid } from './errors/RuntimeConfigurationInvalid'; | ||
export { default as RuntimeConfiguration } from './definitions/RuntimeConfiguration'; | ||
export { default as RuntimeConfigurationBuilder } from './ConfigurationBuilder'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/configuration/src/utils/errors/InvalidConfigurationFile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
export default class InvalidConfigurationFile extends Error | ||
{ | ||
constructor(filename: string) | ||
{ | ||
super(`${filename} is not a valid configuration file.`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
|
||
export { default as InvalidConfigurationFile } from './errors/InvalidConfigurationFile'; | ||
export { default as ConfigurationReader } from './ConfigurationReader'; |
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
packages/configuration/test/environment/Configurator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
import { describe, expect, it } from 'vitest'; | ||
|
||
describe('Configurator', () => | ||
{ | ||
it('should not test standard library functions', async () => | ||
{ | ||
expect(true).toBeTruthy(); | ||
}); | ||
}); |
86 changes: 86 additions & 0 deletions
86
packages/configuration/test/fixtures/fileManager.fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
import { File, FileManager } from '@jitar/sourcing'; | ||
|
||
export default class TestFileManager implements FileManager | ||
{ | ||
readonly #files: Record<string, File>; | ||
|
||
constructor(files: Record<string, File>) | ||
{ | ||
this.#files = files; | ||
} | ||
|
||
getRootLocation(): string | ||
{ | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
getAbsoluteLocation(filename: string): string | ||
{ | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
getRelativeLocation(filename: string): string | ||
{ | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
getType(filename: string): Promise<string> | ||
{ | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
getContent(filename: string): Promise<Buffer | string> | ||
{ | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
exists(filename: string): Promise<boolean> | ||
{ | ||
for (const key in this.#files) | ||
{ | ||
const value = this.#files[key]; | ||
|
||
if (value.location === filename) | ||
{ | ||
return Promise.resolve(true); | ||
} | ||
} | ||
|
||
return Promise.resolve(false); | ||
} | ||
|
||
read(filename: string): Promise<File> | ||
{ | ||
let file: File | undefined; | ||
|
||
for (const key in this.#files) | ||
{ | ||
const value = this.#files[key]; | ||
|
||
if (value.location === filename) | ||
{ | ||
file = value; | ||
|
||
break; | ||
} | ||
} | ||
|
||
return Promise.resolve(file as File); | ||
} | ||
|
||
write(filename: string, content: string): Promise<void> | ||
{ | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
delete(filename: string): Promise<void> | ||
{ | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
filter(pattern: string): Promise<string[]> | ||
{ | ||
throw new Error('Method not implemented.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
export { default as FileManager } from './fileManager.fixture'; |
35 changes: 35 additions & 0 deletions
35
packages/configuration/test/runtime/ConfigurationBuilder.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { configurationBuilder, FILENAMES, CONFIGURATIONS, RuntimeConfigurationInvalid, VALIDATION_RESULT } from './fixtures'; | ||
|
||
describe('runtime/ConfigurationBuilder', () => | ||
{ | ||
it('should build a default runtime configuration without configuration file', async () => | ||
{ | ||
const promise = configurationBuilder.build(); | ||
|
||
await expect(promise).resolves.toEqual(CONFIGURATIONS.DEFAULT); | ||
}); | ||
|
||
it('should build a valid runtime configuration from a valid file', async () => | ||
{ | ||
const promise = configurationBuilder.build(FILENAMES.VALID); | ||
|
||
await expect(promise).resolves.toEqual(CONFIGURATIONS.RUNTIME); | ||
}); | ||
|
||
it('should build a default runtime when the configuration file does not exist', async () => | ||
{ | ||
const promise = configurationBuilder.build(FILENAMES.MISSING); | ||
|
||
await expect(promise).resolves.toEqual(CONFIGURATIONS.DEFAULT); | ||
}); | ||
|
||
it('should reject an invalid runtime configuration', async () => | ||
{ | ||
const promise = configurationBuilder.build(FILENAMES.INVALID); | ||
|
||
await expect(promise).rejects.toEqual(new RuntimeConfigurationInvalid(VALIDATION_RESULT)); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
packages/configuration/test/runtime/fixtures/configuration.fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
import { RuntimeConfiguration } from '../../../src/runtime'; | ||
|
||
const defaultConfiguration: RuntimeConfiguration = | ||
{ | ||
source: './src', | ||
target: './dist', | ||
} as const; | ||
|
||
const runtimeConfiguration: RuntimeConfiguration = | ||
{ | ||
source: './source', | ||
target: './target', | ||
} as const; | ||
|
||
const invalidConfiguration: any = | ||
{ | ||
invalid: true | ||
} as const; | ||
|
||
export const CONFIGURATIONS: Record<string, RuntimeConfiguration> = | ||
{ | ||
DEFAULT: defaultConfiguration, | ||
RUNTIME: runtimeConfiguration, | ||
INVALID: invalidConfiguration, | ||
} as const; |
7 changes: 7 additions & 0 deletions
7
packages/configuration/test/runtime/fixtures/filenames.fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
export const FILENAMES = { | ||
DEFAULT: './jitar.json', | ||
VALID: 'valid-runtime-configuration.json', | ||
INVALID: 'invalid-runtime-configuration.json', | ||
MISSING: 'missing-runtime-configuration.json', | ||
} as const; |
12 changes: 12 additions & 0 deletions
12
packages/configuration/test/runtime/fixtures/files.fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
import { File } from '@jitar/sourcing'; | ||
|
||
import { CONFIGURATIONS } from './configuration.fixture'; | ||
import { FILENAMES } from './filenames.fixture'; | ||
|
||
export const FILES: Record<string, File> = | ||
{ | ||
DEFAULT: new File(FILENAMES.DEFAULT, 'text/json', JSON.stringify(CONFIGURATIONS.DEFAULT)), | ||
VALID: new File(FILENAMES.VALID, 'text/json', JSON.stringify(CONFIGURATIONS.RUNTIME)), | ||
INVALID: new File(FILENAMES.INVALID, 'text/json', JSON.stringify(CONFIGURATIONS.INVALID)) | ||
} as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
import { Validator } from '@jitar/validation'; | ||
|
||
import { RuntimeConfigurationBuilder } from '../../../src/runtime'; | ||
import RuntimeConfigurationInvalid from '../../../src/runtime/errors/RuntimeConfigurationInvalid'; | ||
import { ConfigurationReader } from '../../../src/utils'; | ||
|
||
import { FileManager } from '../../fixtures'; | ||
|
||
import { FILES } from './files.fixture'; | ||
import { FILENAMES } from './filenames.fixture'; | ||
import { CONFIGURATIONS } from './configuration.fixture'; | ||
import { VALIDATION_RESULT } from './validation.fixture'; | ||
|
||
const fileManager = new FileManager(FILES); | ||
const configurationReader = new ConfigurationReader(fileManager); | ||
const validator = new Validator(); | ||
|
||
const configurationBuilder = new RuntimeConfigurationBuilder(configurationReader, validator); | ||
|
||
export { configurationBuilder, FILENAMES, CONFIGURATIONS, RuntimeConfigurationInvalid, VALIDATION_RESULT }; |
12 changes: 12 additions & 0 deletions
12
packages/configuration/test/runtime/fixtures/validation.fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
import { ValidationResult } from "@jitar/validation"; | ||
|
||
const VALIDATION_RESULT: ValidationResult = | ||
{ | ||
valid: false, | ||
errors: [ | ||
"Unknown field 'invalid'", | ||
] | ||
} as const; | ||
|
||
export { VALIDATION_RESULT }; |
21 changes: 21 additions & 0 deletions
21
packages/configuration/test/server/ConfigurationBuilder.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { configurationBuilder, FILENAMES, SERVER_CONFIGURATION, ServerConfigurationInvalid, VALIDATION_RESULT } from './fixtures'; | ||
|
||
describe('server/ConfigurationBuilder', () => | ||
{ | ||
it('should build a valid server configuration', async () => | ||
{ | ||
const promise = configurationBuilder.build(FILENAMES.VALID_CONFIGURATION); | ||
|
||
await expect(promise).resolves.toEqual(SERVER_CONFIGURATION); | ||
}); | ||
|
||
it('should reject an invalid server configuration', async () => | ||
{ | ||
const promise = configurationBuilder.build(FILENAMES.INVALID_CONFIGURATION); | ||
|
||
await expect(promise).rejects.toEqual(new ServerConfigurationInvalid(VALIDATION_RESULT)); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
packages/configuration/test/server/fixtures/configuration.fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
import type { GatewayConfiguration, ProxyConfiguration, RepositoryConfiguration, ServerConfiguration, StandaloneConfiguration, WorkerConfiguration } from '../../../src/server'; | ||
|
||
const serveIndexOnNotFound = true; | ||
const assets = ['index.html', 'favicon.ico']; | ||
const segments = ['segment']; | ||
const indexFilename = 'index.html'; | ||
const trustKey = 'trust-key'; | ||
const gateway = 'https://gateway'; | ||
const repository = 'https://repository'; | ||
|
||
const gatewayConfiguration: GatewayConfiguration = { monitor: 5000, trustKey } as const; | ||
const proxyConfiguration: ProxyConfiguration = { gateway, repository } as const; | ||
const repositoryConfiguration: RepositoryConfiguration = { indexFilename, serveIndexOnNotFound, assets } as const; | ||
const standaloneConfiguration: StandaloneConfiguration = { segments, indexFilename, serveIndexOnNotFound, assets } as const; | ||
const workerConfiguration: WorkerConfiguration = { gateway, segments, trustKey } as const; | ||
|
||
export const SERVER_CONFIGURATION: ServerConfiguration = | ||
{ | ||
url: 'https://server', | ||
setUp: ['setup'], | ||
tearDown: ['tearDown'], | ||
middleware: ['middleware'], | ||
healthChecks: ['healthChecks'], | ||
|
||
gateway: gatewayConfiguration, | ||
proxy: proxyConfiguration, | ||
repository: repositoryConfiguration, | ||
standalone: standaloneConfiguration, | ||
worker: workerConfiguration | ||
} as const; |
Oops, something went wrong.