Skip to content

Commit

Permalink
#
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Truong <[email protected]>
  • Loading branch information
nhtruong committed May 19, 2024
1 parent 4140397 commit f695bca
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 15 deletions.
4 changes: 4 additions & 0 deletions json_schemas/story.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ type: object
properties:
$schema:
type: string
skipped:
type: boolean
description: If true, the story will be skipped.
default: false
description:
type: string
prologues:
Expand Down
15 changes: 15 additions & 0 deletions tools/src/tester/ChapterEvaluator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type Chapter } from './types/story.types'
import { type ChapterEvaluation, Result } from './types/eval.types'

export default class ChapterEvaluator {
chapter: Chapter

constructor (chapter: Chapter) {
this.chapter = chapter
}

evaluate (skipped: boolean): ChapterEvaluation {
const synopsis = this.chapter.synopsis
if (skipped) return { result: Result.SKIPPED, synopsis }
}
}
2 changes: 1 addition & 1 deletion tools/src/tester/SchemaValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class SchemaValidator {
const validate = this.ajv.compile(schema)
const valid = validate(data)
return {
result: valid ? 'PASS' : 'FAIL',
result: valid ? 'PASSED' : 'FAILED',
message: valid ? undefined : this.ajv.errorsText(validate.errors)
}
}
Expand Down
36 changes: 34 additions & 2 deletions tools/src/tester/StoryEvaluator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { type Chapter, type Story, type SupplementalChapter } from './types/story.types'
import { type ChapterEvaluation, type Evaluation, type StoryEvaluation } from './types/eval.types'
import { type ChapterEvaluation, type Evaluation, Result, type StoryEvaluation } from './types/eval.types'
import ChapterEvaluator from './ChapterEvaluator'
import SupplementalChapterEvaluator from './SupplementalChapterEvaluator'

export interface StoryFile {
display_path: string
Expand All @@ -11,6 +13,8 @@ export default class StoryEvaluator {
story: Story
display_path: string
full_path: string
skipped: boolean = false
result: Result = Result.PASSED

constructor (story_file: StoryFile) {
this.story = story_file.story
Expand All @@ -19,21 +23,49 @@ export default class StoryEvaluator {
}

evaluate (): StoryEvaluation {
if (this.story.skipped) {
return {
result: Result.SKIPPED,
display_path: this.display_path,
full_path: this.full_path,
description: this.story.description
}
}
return {
display_path: this.display_path,
full_path: this.full_path,
description: this.story.description,
prologues: this.#evaluate_supplemental_chapters(this.story.prologues),
chapters: this.#evaluate_chapters(this.story.chapters),
epilogues: this.#evaluate_supplemental_chapters(this.story.epilogues)
epilogues: this.#evaluate_supplemental_chapters(this.story.epilogues),
result: this.result
}
}

#evaluate_chapters (chapters: Chapter[]): ChapterEvaluation[] {
let skipped: boolean = this.skipped
if (skipped) return []

return chapters.map(chapter => {
const evaluator = new ChapterEvaluator(chapter)
const evaluation = evaluator.evaluate(skipped)
skipped = skipped || evaluation.result === Result.FAILED
if (evaluation.result === Result.FAILED) this.result = Result.FAILED
return evaluation
})
}

#evaluate_supplemental_chapters (chapters: SupplementalChapter[]): Evaluation {
chapters.forEach(chapter => {
const evaluator = new SupplementalChapterEvaluator(chapter)
const evaluation = evaluator.evaluate()
if (evaluation.result === Result.ERROR) {
this.result = Result.ERROR
this.skipped = true
return evaluation
}
})

return { result: Result.OK }
}
}
13 changes: 13 additions & 0 deletions tools/src/tester/SupplementalChapterEvaluator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type SupplementalChapter } from './types/story.types'
import { type Evaluation, Result } from './types/eval.types'

Check failure on line 2 in tools/src/tester/SupplementalChapterEvaluator.ts

View workflow job for this annotation

GitHub Actions / test

'Result' is defined but never used

export default class SupplementalChapterEvaluator {
chapter: SupplementalChapter

constructor (chapter: SupplementalChapter) {
this.chapter = chapter
}

evaluate (): Evaluation {
}
}
18 changes: 12 additions & 6 deletions tools/src/tester/TestsRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ import fs from 'fs'
import { type Story } from './types/story.types'
import { read_yaml } from '../../helpers'

declare global {
// eslint-disable-next-line no-var
var chapter_reader: ChapterReader
// eslint-disable-next-line no-var
var schema_validator: SchemaValidator
// eslint-disable-next-line no-var
var spec_parser: SpecParser
}

export default class TestsRunner {
schema_validator: SchemaValidator
spec_parser: SpecParser
chapter_reader: ChapterReader
path: string // Path to a story file or a directory containing story files

constructor (spec: OpenAPIV3.Document, path: string) {
// TODO: Grab server URL from environment variable and add authentication.
this.chapter_reader = new ChapterReader('http://localhost:9200')
this.spec_parser = new SpecParser(spec)
this.schema_validator = new SchemaValidator()
globalThis.chapter_reader = new ChapterReader('http://localhost:9200')
globalThis.spec_parser = new SpecParser(spec)
globalThis.schema_validator = new SchemaValidator()
this.path = path
}

Expand Down
22 changes: 16 additions & 6 deletions tools/src/tester/types/eval.types.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
export type LibraryEvaluation = StoryEvaluation[]

export interface StoryEvaluation {
result: Result
display_path: string
full_path: string
description: string
chapters: ChapterEvaluation[]
epilogues: Evaluation
prologues: Evaluation
chapters?: ChapterEvaluation[]
epilogues?: Evaluation
prologues?: Evaluation
}

export interface ChapterEvaluation {
result: Result
synopsis: string
request: {
request?: {
operation: Evaluation
parameters?: Record<string, Evaluation>
requestBody?: Evaluation
}
response: {
response?: {
status: Evaluation
payload: Evaluation
}
}

export interface Evaluation {
result: 'PASS' | 'FAIL' | 'SKIPPED'
result: Result
message?: string
}

export enum Result {
PASSED = 'PASSED',
FAILED = 'FAILED',
SKIPPED = 'SKIPPED',
ERROR = 'ERROR',
OK = 'OK'
}
4 changes: 4 additions & 0 deletions tools/src/tester/types/story.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export type ReadChapter = Chapter & {

export interface Story {
$schema?: string;
/**
* If true, the story will be skipped.
*/
skipped?: boolean;
description: string;
prologues: SupplementalChapter[];
epilogues: SupplementalChapter[];
Expand Down

0 comments on commit f695bca

Please sign in to comment.