mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: add Session and stage active check
This commit is contained in:
1 parent
5bea2ea78c
commit
b449ea455a
4 files changed
+62
-5
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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[]
|
||||
}
|
||||
@@ -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)
|
||||
})
|
||||
Reference in new issue
Block a user