feat: add Session and stage active check

This commit is contained in:
wancat committed 2024-03-08 01:05:26 +08:00
1 parent 5bea2ea78c
commit b449ea455a
4 files changed
+62 -5

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
import { ISession } from './types'
export class Session implements ISession {
private completed: string[] = []
constructor({ completedStages }: ISession) {
this.completed = completedStages
}
get completedStages() {
return this.completed
}
complete(stageId: string) {
this.completed.push(stageId)
}
}
+20 -4
View File
@@ -1,15 +1,17 @@
import { IStage, ICommand, IFileExistenceChecker } from './types'
import { IStage, ICommand, IFileExistenceChecker, ISession } from './types'
import { Condition } from './condition/Condition'
export class Stage {
id: string
name: string
condition: Condition
private condition: Condition
private requirements: string[]
constructor({ id, name, condition }: IStage) {
constructor({ id, name, condition, requirements }: IStage) {
this.id = id
this.name = name
this.name = name || ''
this.condition = new Condition(condition)
this.requirements = requirements || []
}
async satisfies(
@@ -18,4 +20,18 @@ export class Stage {
): Promise<boolean> {
return this.condition.match(command) && this.condition.check(checker)
}
private checkRequirements(session: ISession): boolean {
return this.requirements.every((req) =>
session.completedStages.includes(req)
)
}
private finished(session: ISession): boolean {
return session.completedStages.includes(this.id)
}
active(session: ISession): boolean {
return !this.finished(session) && this.checkRequirements(session)
}
}
+6 -1
View File
@@ -35,6 +35,11 @@ export interface IFileExistenceChecker {
export interface IStage {
id: string
name: string
name?: string
requirements?: string[]
condition: ICondition
}
export interface ISession {
completedStages: string[]
}
+19
View File
@@ -2,6 +2,7 @@ import { test, expect } from '@jest/globals'
import { Stage } from '../src/Stage'
import { FileType } from '../src/types'
import { MockFileChecker } from './asyncCondition.test'
import { Session } from '../src/Session'
const checker = new MockFileChecker()
@@ -67,3 +68,21 @@ test('check stage with async condition falsy', async () => {
})
expect(await stage.satisfies({}, checker)).toBe(false)
})
test('stage active checks', () => {
const session = new Session({
completedStages: ['stage1'],
})
const stage3 = new Stage({
id: 'stage3',
requirements: ['stage1', 'stage2'],
condition: {},
})
expect(stage3.active(session)).toBe(false)
session.complete('stage2')
expect(stage3.active(session)).toBe(true)
session.complete('stage3')
expect(stage3.active(session)).toBe(false)
})