diff --git a/eslint.config.mjs b/eslint.config.mjs index 37b28e92..21300ead 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -72,10 +72,13 @@ export default [ '@typescript-eslint/require-await': 'error', '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }], 'array-callback-return': 'off', + 'indent': ['error', 2, { 'SwitchCase': 1 }], 'new-cap': 'off', + 'no-constant-condition': 'off', 'no-return-assign': 'error', + 'no-trailing-spaces': 'error', 'object-shorthand': 'error', - 'no-constant-condition': 'off', + 'space-in-parens': 'error', 'license-header/header': [ 'error', [ diff --git a/tools/src/OpenSearchHttpClient.ts b/tools/src/OpenSearchHttpClient.ts index 08d03901..04d636f0 100644 --- a/tools/src/OpenSearchHttpClient.ts +++ b/tools/src/OpenSearchHttpClient.ts @@ -75,9 +75,9 @@ export class OpenSearchHttpClient { baseURL: opts?.url ?? DEFAULT_URL, auth: opts?.username !== undefined && opts.password !== undefined ? { - username: opts.username, - password: opts.password - } + username: opts.username, + password: opts.password + } : undefined, httpsAgent: new https.Agent({ rejectUnauthorized: !(opts?.insecure ?? DEFAULT_INSECURE) }) }) diff --git a/tools/src/tester/StoryEvaluator.ts b/tools/src/tester/StoryEvaluator.ts index 283940ce..f77f586c 100644 --- a/tools/src/tester/StoryEvaluator.ts +++ b/tools/src/tester/StoryEvaluator.ts @@ -40,7 +40,7 @@ export default class StoryEvaluator { chapters: [] } } - const variables_error = StoryEvaluator.check_story_variables(story, display_path, full_path ) + const variables_error = StoryEvaluator.check_story_variables(story, display_path, full_path) if (variables_error !== undefined) { return variables_error } @@ -96,7 +96,7 @@ export default class StoryEvaluator { return { evaluations, has_errors } } - static check_story_variables(story: Story, display_path: string, full_path: string ): StoryEvaluation | undefined { + static check_story_variables(story: Story, display_path: string, full_path: string): StoryEvaluation | undefined { const story_outputs = new StoryOutputs() const prologues = (story.prologues ?? []).map((prologue) => { return StoryEvaluator.#check_episode_variables(prologue, story_outputs) @@ -124,26 +124,26 @@ export default class StoryEvaluator { static #check_episode_variables(episode: ChapterRequest, story_outputs: StoryOutputs): ChapterEvaluation { const title = `${episode.method} ${episode.path}` - const error = StoryEvaluator.#check_used_variables(episode, story_outputs) - if (error !== undefined) { - return error - } - if (episode.id === undefined && episode.output !== undefined) { - return this.#failed_evaluation(title, 'An episode must have an id to store its output') - } - if (episode.id !== undefined && episode.output !== undefined) { - story_outputs.set_chapter_output(episode.id, new ChapterOutput(episode.output)) - } - return { title, overall: { result: Result.PASSED } } + const error = StoryEvaluator.#check_used_variables(episode, story_outputs) + if (error !== undefined) { + return error + } + if (episode.id === undefined && episode.output !== undefined) { + return this.#failed_evaluation(title, 'An episode must have an id to store its output') + } + if (episode.id !== undefined && episode.output !== undefined) { + story_outputs.set_chapter_output(episode.id, new ChapterOutput(episode.output)) + } + return { title, overall: { result: Result.PASSED } } } /** - * + * * @param chapter ChapterEvaluation { title: string overall: Evaluation - * @param story_outputs - * @returns + * @param story_outputs + * @returns */ static #check_used_variables(chapter: ChapterRequest, story_outputs: StoryOutputs): ChapterEvaluation | undefined { const variables = new Set() diff --git a/tools/src/tester/SupplementalChapterEvaluator.ts b/tools/src/tester/SupplementalChapterEvaluator.ts index 34d9972f..41961be0 100644 --- a/tools/src/tester/SupplementalChapterEvaluator.ts +++ b/tools/src/tester/SupplementalChapterEvaluator.ts @@ -15,45 +15,45 @@ import { ChapterEvaluation, Result } from "./types/eval.types"; import { SupplementalChapter } from "./types/story.types"; export default class SupplementalChapterEvaluator { - private readonly _chapter_reader: ChapterReader; + private readonly _chapter_reader: ChapterReader; - constructor(chapter_reader: ChapterReader) { - this._chapter_reader = chapter_reader; - } + constructor(chapter_reader: ChapterReader) { + this._chapter_reader = chapter_reader; + } - async evaluate(chapter: SupplementalChapter, story_outputs: StoryOutputs): Promise<{ evaluation: ChapterEvaluation, evaluation_error: boolean }> { - const title = `${chapter.method} ${chapter.path}` - const response = await this._chapter_reader.read(chapter, story_outputs) - const status = chapter.status ?? [200, 201] - const output_values = ChapterOutput.extract_output_values(response, chapter.output) - let response_evaluation: ChapterEvaluation - const passed_evaluation = { title, overall: { result: Result.PASSED } } - if (status.includes(response.status)) { - response_evaluation = passed_evaluation - } else { - response_evaluation = { title, overall: { result: Result.ERROR, message: response.message, error: response.error as Error }, output_values } - } - if (output_values) { - response_evaluation.output_values = output_values - } - const result = overall_result([response_evaluation.overall].concat(output_values ? [output_values] : [])) - if (result === Result.PASSED) { - return { evaluation: passed_evaluation, evaluation_error: false } - } else { - const message_segments = [] - if (response_evaluation.overall.result === Result.ERROR) { - message_segments.push(`${response_evaluation.overall.message}`) - } - if (output_values !== undefined && output_values.result === Result.ERROR) { - message_segments.push(`${output_values.message}`) - } - const message = message_segments.join('\n') - const evaluation = { - title, - overall: { result: Result.ERROR, message, error: response.error as Error }, - ...(output_values ? { output_values } : {}) - } - return { evaluation, evaluation_error: true } - } + async evaluate(chapter: SupplementalChapter, story_outputs: StoryOutputs): Promise<{ evaluation: ChapterEvaluation, evaluation_error: boolean }> { + const title = `${chapter.method} ${chapter.path}` + const response = await this._chapter_reader.read(chapter, story_outputs) + const status = chapter.status ?? [200, 201] + const output_values = ChapterOutput.extract_output_values(response, chapter.output) + let response_evaluation: ChapterEvaluation + const passed_evaluation = { title, overall: { result: Result.PASSED } } + if (status.includes(response.status)) { + response_evaluation = passed_evaluation + } else { + response_evaluation = { title, overall: { result: Result.ERROR, message: response.message, error: response.error as Error }, output_values } + } + if (output_values) { + response_evaluation.output_values = output_values + } + const result = overall_result([response_evaluation.overall].concat(output_values ? [output_values] : [])) + if (result === Result.PASSED) { + return { evaluation: passed_evaluation, evaluation_error: false } + } else { + const message_segments = [] + if (response_evaluation.overall.result === Result.ERROR) { + message_segments.push(`${response_evaluation.overall.message}`) + } + if (output_values !== undefined && output_values.result === Result.ERROR) { + message_segments.push(`${output_values.message}`) + } + const message = message_segments.join('\n') + const evaluation = { + title, + overall: { result: Result.ERROR, message, error: response.error as Error }, + ...(output_values ? { output_values } : {}) + } + return { evaluation, evaluation_error: true } } + } } diff --git a/tools/tests/helpers.test.ts b/tools/tests/helpers.test.ts index de8db04c..29ae225a 100644 --- a/tools/tests/helpers.test.ts +++ b/tools/tests/helpers.test.ts @@ -12,17 +12,17 @@ import { sort_array_by_keys } from '../src/helpers' describe('helpers', () => { describe('sort_array_by_keys', () => { test('sorts arrays of string', () => { - expect(sort_array_by_keys([])).toEqual([]) - expect(sort_array_by_keys(['GET', 'POST'], ['GET', 'POST'])).toEqual(['GET', 'POST']) - expect(sort_array_by_keys(['GET', 'POST'], ['POST', 'GET'])).toEqual(['POST', 'GET']) - expect(sort_array_by_keys(['GET', 'POST'], ['POST', 'GET', 'DELETE'])).toEqual(['POST', 'GET']) - expect(sort_array_by_keys(['DELETE', 'POST', 'GET'], ['POST', 'GET', 'DELETE'])).toEqual(['POST', 'GET', 'DELETE']) - }) - - test('does not modify the original object', () => { - const arr = ['GET', 'POST'] - expect(sort_array_by_keys(arr, ['POST', 'GET'])).toEqual(['POST', 'GET']) - expect(arr).toEqual(['GET', 'POST'] ) - }) + expect(sort_array_by_keys([])).toEqual([]) + expect(sort_array_by_keys(['GET', 'POST'], ['GET', 'POST'])).toEqual(['GET', 'POST']) + expect(sort_array_by_keys(['GET', 'POST'], ['POST', 'GET'])).toEqual(['POST', 'GET']) + expect(sort_array_by_keys(['GET', 'POST'], ['POST', 'GET', 'DELETE'])).toEqual(['POST', 'GET']) + expect(sort_array_by_keys(['DELETE', 'POST', 'GET'], ['POST', 'GET', 'DELETE'])).toEqual(['POST', 'GET', 'DELETE']) + }) + + test('does not modify the original object', () => { + const arr = ['GET', 'POST'] + expect(sort_array_by_keys(arr, ['POST', 'GET'])).toEqual(['POST', 'GET']) + expect(arr).toEqual(['GET', 'POST']) + }) }) }) diff --git a/tools/tests/linter/SupersededOperationsFile.test.ts b/tools/tests/linter/SupersededOperationsFile.test.ts index 696e7842..423b6852 100644 --- a/tools/tests/linter/SupersededOperationsFile.test.ts +++ b/tools/tests/linter/SupersededOperationsFile.test.ts @@ -17,24 +17,24 @@ describe('validate()', () => { file: 'superseded_operations/invalid_schema.yaml', message: "File content does not match JSON schema found in './json_schemas/_superseded_operations.schema.yaml':\n " + JSON.stringify([ - { - "instancePath": "/~1hello~1world/operations/1", - "schemaPath": "#/patternProperties/%5E~1/properties/operations/items/enum", - "keyword": "enum", - "params": { - "allowedValues": [ - "GET", - "POST", - "PUT", - "DELETE", - "HEAD", - "OPTIONS", - "PATCH" - ] - }, - "message": "must be equal to one of the allowed values" - } - ], null, 2), + { + "instancePath": "/~1hello~1world/operations/1", + "schemaPath": "#/patternProperties/%5E~1/properties/operations/items/enum", + "keyword": "enum", + "params": { + "allowedValues": [ + "GET", + "POST", + "PUT", + "DELETE", + "HEAD", + "OPTIONS", + "PATCH" + ] + }, + "message": "must be equal to one of the allowed values" + } + ], null, 2), }, ]) }) diff --git a/tools/tests/linter/factories/operation.ts b/tools/tests/linter/factories/operation.ts index 07b9dee6..b4f90363 100644 --- a/tools/tests/linter/factories/operation.ts +++ b/tools/tests/linter/factories/operation.ts @@ -27,7 +27,7 @@ interface MockedReturnedValues { } export function mocked_operation (returned_values: MockedReturnedValues): Operation { - + const op: Operation = new Operation('', '', '', {} as OperationSpec) if (returned_values.validate) { diff --git a/tools/tests/linter/lint.test.ts b/tools/tests/linter/lint.test.ts index d2205d9f..c08d7d99 100644 --- a/tools/tests/linter/lint.test.ts +++ b/tools/tests/linter/lint.test.ts @@ -18,7 +18,7 @@ const spec = (args: string[]): any => { } test('--help', () => { - const output = spec(['--help']) + const output = spec(['--help']) expect(output.stderr).toEqual('') expect(output.stdout).toContain('Usage: lint [options]') }) diff --git a/tools/tests/tester/helpers.ts b/tools/tests/tester/helpers.ts index 86312f80..f10debdc 100644 --- a/tools/tests/tester/helpers.ts +++ b/tools/tests/tester/helpers.ts @@ -67,9 +67,9 @@ export function print_yaml (obj: any): void { export function flatten_errors (evaluation: StoryEvaluation): StoryEvaluation { const flatten = (e: T): T => (e !== undefined ? { - ...e, - error: typeof e.error === 'object' ? e.error.message : e.error - } + ...e, + error: typeof e.error === 'object' ? e.error.message : e.error + } : undefined as T) const flatten_chapters = (chapters: T): T => { @@ -79,17 +79,17 @@ export function flatten_errors (evaluation: StoryEvaluation): StoryEvaluation { overall: flatten(c.overall), request: c.request !== undefined ? { - parameters: c.request.parameters !== undefined - ? Object.fromEntries(Object.entries(c.request.parameters).map(([k, v]) => [k, flatten(v)])) - : undefined, - request_body: flatten(c.request.request_body) - } + parameters: c.request.parameters !== undefined + ? Object.fromEntries(Object.entries(c.request.parameters).map(([k, v]) => [k, flatten(v)])) + : undefined, + request_body: flatten(c.request.request_body) + } : undefined, response: c.response !== undefined ? { - status: flatten(c.response.status), - payload: flatten(c.response.payload) - } + status: flatten(c.response.status), + payload: flatten(c.response.payload) + } : undefined })) as T } diff --git a/tools/tests/tester/story_outputs.test.ts b/tools/tests/tester/story_outputs.test.ts index 23acc68a..13d4a082 100644 --- a/tools/tests/tester/story_outputs.test.ts +++ b/tools/tests/tester/story_outputs.test.ts @@ -34,7 +34,7 @@ test('resolve_value', () => { }, g: 123 } - expect(story_outputs.resolve_value(value)).toEqual( + expect(story_outputs.resolve_value(value)).toEqual( { a: 1, b: [1, 2, 3], @@ -55,7 +55,7 @@ test('resolve_params', () => { c: 3, d: 'str' } - expect(story_outputs.resolve_params(parameters)).toEqual({ + expect(story_outputs.resolve_params(parameters)).toEqual({ a: 1, b: 2, c: 3, diff --git a/tools/tests/tester/test.test.ts b/tools/tests/tester/test.test.ts index 6f2c25a8..7f5fc5a0 100644 --- a/tools/tests/tester/test.test.ts +++ b/tools/tests/tester/test.test.ts @@ -115,7 +115,7 @@ function chapter(synopsis: string, request: ChapterRequest): Chapter { } } - + test('check_story_variables', () => { const check_story_variables = (s: Story): StoryEvaluation | undefined => StoryEvaluator.check_story_variables(s, 'display_path', 'full_path') const failed = (prologues: ChapterEvaluation[] = [], chapters: ChapterEvaluation[] = []): StoryEvaluation => ({ @@ -193,7 +193,7 @@ test('check_story_variables', () => { ] })).toStrictEqual(undefined) }) - + test.todo('--tab-width')