forked from opensearch-project/opensearch-api-specification
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Theo Truong <[email protected]>
- Loading branch information
Showing
6 changed files
with
66 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,67 @@ | ||
import { type Chapter } from './types/story.types' | ||
import { type ChapterEvaluation, Result } from './types/eval.types' | ||
import { type Chapter, type FullResponse } from './types/story.types' | ||
import { type ChapterEvaluation, type Evaluation, Result } from './types/eval.types' | ||
import { type ParsedOperation } from './types/spec.types' | ||
|
||
export default class ChapterEvaluator { | ||
chapter: Chapter | ||
result: Result = Result.PASSED | ||
|
||
constructor (chapter: Chapter) { | ||
this.chapter = chapter | ||
} | ||
|
||
evaluate (skipped: boolean): ChapterEvaluation { | ||
const synopsis = this.chapter.synopsis | ||
if (skipped) return { result: Result.SKIPPED, synopsis } | ||
async evaluate (skipped: boolean): Promise<ChapterEvaluation> { | ||
try { | ||
if (skipped) return { result: Result.SKIPPED, synopsis: this.chapter.synopsis } | ||
const operation = globalThis.spec_parser.locate_operation(this.chapter) | ||
const response = await globalThis.chapter_reader.read(this.chapter, true) | ||
return { | ||
synopsis: this.chapter.synopsis, | ||
request: { | ||
parameters: this.#evaluate_parameters(operation), | ||
requestBody: this.#evaluate_request_body(operation) | ||
}, | ||
response: { | ||
status: this.#evaluate_status(response), | ||
payload: this.#evaluate_payload(operation, response) | ||
}, | ||
result: this.result | ||
} | ||
} catch (error) { | ||
return { result: Result.ERROR, synopsis: this.chapter.synopsis, message: (error as Error).message } | ||
} | ||
} | ||
|
||
#evaluate_parameters (operation: ParsedOperation): Record<string, Evaluation> { | ||
return Object.fromEntries(Object.entries(this.chapter.parameters ?? {}).map(([name, parameter]) => { | ||
const schema = operation.parameters[name]?.schema | ||
const evaluation = globalThis.schema_validator.validate(schema, parameter) | ||
if (evaluation.result === Result.FAILED) this.result = Result.FAILED | ||
return [name, evaluation] | ||
})) | ||
} | ||
|
||
#evaluate_request_body (operation: ParsedOperation): Evaluation | undefined { | ||
if (!this.chapter.requestBody) return undefined | ||
const schema = operation.requestBody?.content[this.chapter.requestBody?.content_type ?? '']?.schema | ||
if (schema == null) return { result: Result.FAILED, message: `Schema for "${this.chapter.requestBody.content_type}" request body not found.` } | ||
const evaluation = globalThis.schema_validator.validate(schema, this.chapter.requestBody?.payload ?? {}) | ||
if (evaluation.result === Result.FAILED) this.result = Result.FAILED | ||
return evaluation | ||
} | ||
|
||
#evaluate_status (response: FullResponse): Evaluation { | ||
const expected_status = this.chapter.response?.status ?? 200 | ||
return response.status === expected_status | ||
? { result: Result.PASSED } | ||
: { result: Result.FAILED, message: `Expected status ${expected_status}, but received ${response.status}` } | ||
} | ||
|
||
#evaluate_payload (operation: ParsedOperation, response: FullResponse): Evaluation { | ||
const schema = operation.responses[response.status]?.content[response.content_type]?.schema | ||
if (schema == null) return { result: Result.FAILED, message: `Schema for "${response.status}: ${response.content_type}" response not found.` } | ||
const evaluation = globalThis.schema_validator.validate(schema, response.payload) | ||
if (evaluation.result === Result.FAILED) this.result = Result.FAILED | ||
return evaluation | ||
} | ||
} |
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
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