diff --git a/packages/game/src/Stage.ts b/packages/game/src/Stage.ts index 7517dc9..579bb74 100644 --- a/packages/game/src/Stage.ts +++ b/packages/game/src/Stage.ts @@ -1,4 +1,4 @@ -import { IStage, ICommand } from './types' +import { IStage, ICommand, IFileExistenceChecker } from './types' import { Condition } from './condition/Condition' export class Stage { @@ -12,7 +12,10 @@ export class Stage { this.condition = new Condition(condition) } - satisfies(command: ICommand): boolean { - return this.condition.match(command) + async satisfies( + command: ICommand, + checker: IFileExistenceChecker + ): Promise { + return this.condition.match(command) && this.condition.check(checker) } } diff --git a/packages/game/test/asyncCondition.test.ts b/packages/game/test/asyncCondition.test.ts index da4399d..8ad2630 100644 --- a/packages/game/test/asyncCondition.test.ts +++ b/packages/game/test/asyncCondition.test.ts @@ -3,7 +3,7 @@ import { IFileExistenceChecker, FileType, FileInput } from '../src/types.js' import { checkFiles } from '../src/condition/FileMatcher.js' import { Condition } from '../src/condition/Condition.js' -class MockFileChecker implements IFileExistenceChecker { +export class MockFileChecker implements IFileExistenceChecker { private files: FileInput[] = [ { path: '/home/user/hello.txt', diff --git a/packages/game/test/stage.test.ts b/packages/game/test/stage.test.ts index 8fa1617..5631e21 100644 --- a/packages/game/test/stage.test.ts +++ b/packages/game/test/stage.test.ts @@ -1,7 +1,11 @@ import { test, expect } from '@jest/globals' import { Stage } from '../src/Stage' +import { FileType } from '../src/types' +import { MockFileChecker } from './asyncCondition.test' -test('check stage condition', () => { +const checker = new MockFileChecker() + +test('check stage condition', async () => { const stage = new Stage({ id: 'stage1', name: 'List the files using `ls`', @@ -11,15 +15,55 @@ test('check stage condition', () => { }, }) expect( - stage.satisfies({ - command: 'ls', - pwd: '/home/user', - }) + await stage.satisfies( + { + command: 'ls', + pwd: '/home/user', + }, + checker + ) ).toBe(true) expect( - stage.satisfies({ - command: 'ls', - pwd: '/home/user/Downloads', - }) + await stage.satisfies( + { + command: 'ls', + pwd: '/home/user/Downloads', + }, + checker + ) ).toBe(false) }) + +test('check stage with async condition truthy', async () => { + const stage = new Stage({ + id: 'stage1', + name: 'Create file', + condition: { + files: [ + { + path: '/home/user/hello.txt', + type: FileType.FILE, + exists: true, + }, + ], + }, + }) + expect(await stage.satisfies({}, checker)).toBe(true) +}) + +test('check stage with async condition falsy', async () => { + const stage = new Stage({ + id: 'stage1', + name: 'Create file', + condition: { + files: [ + { + path: '/home/user/not-exists.txt', + type: FileType.FILE, + exists: true, + }, + ], + }, + }) + expect(await stage.satisfies({}, checker)).toBe(false) +})