feat: add stage exceptions

This commit is contained in:
Justin Lin committed 2025-04-21 11:19:59 +08:00
1 parent f7780c3d98
commit fb823814e0
11 files changed
+123 -43

No files matched your search

+15 -15
View File
@@ -1,25 +1,25 @@
import { test, expect } from '@jest/globals'
import { IFileExistenceChecker, FileType, FileInput } from '../src/schema'
import { IFileExistenceChecker, IFileInput } from '../src/schema'
import { checkFiles } from '../src/condition/FileMatcher.js'
import { Condition } from '../src/condition/Condition.js'
export class MockFileChecker implements IFileExistenceChecker {
private files: FileInput[] = [
private files: IFileInput[] = [
{
path: '/home/user/hello.txt',
type: FileType.FILE,
type: 'file',
},
{
path: '/home/user/world.txt',
type: FileType.FILE,
type: 'file',
},
{
path: '/home/user/Downloads',
type: FileType.FOLDER,
type: 'folder',
},
]
async exists(file: FileInput): Promise<boolean> {
async exists(file: IFileInput): Promise<boolean> {
return this.files.some((f) => f.path === file.path && f.type === file.type)
}
}
@@ -30,7 +30,7 @@ test('file matcher', async () => {
await checkFiles(checker, [
{
path: '/home/user/hello.txt',
type: FileType.FILE,
type: 'file',
exists: true,
},
])
@@ -39,7 +39,7 @@ test('file matcher', async () => {
await checkFiles(checker, [
{
path: '/home/user/not-exists.txt',
type: FileType.FILE,
type: 'file',
exists: true,
},
])
@@ -48,7 +48,7 @@ test('file matcher', async () => {
await checkFiles(checker, [
{
path: '/home/user/not-exists.txt',
type: FileType.FILE,
type: 'file',
exists: false,
},
])
@@ -57,7 +57,7 @@ test('file matcher', async () => {
await checkFiles(checker, [
{
path: '/home/user/Downloads',
type: FileType.FOLDER,
type: 'folder',
exists: true,
},
])
@@ -66,12 +66,12 @@ test('file matcher', async () => {
await checkFiles(checker, [
{
path: '/home/user/hello.txt',
type: FileType.FILE,
type: 'file',
exists: true,
},
{
path: '/home/user/Downloads',
type: FileType.FOLDER,
type: 'folder',
exists: true,
},
])
@@ -80,12 +80,12 @@ test('file matcher', async () => {
await checkFiles(checker, [
{
path: '/home/user/hello.txt',
type: FileType.FILE,
type: 'file',
exists: true,
},
{
path: '/home/user/not-exist.txt',
type: FileType.FILE,
type: 'file',
exists: true,
},
])
@@ -98,7 +98,7 @@ test('condition with file matcher', async () => {
files: [
{
path: '/home/user/hello.txt',
type: FileType.FILE,
type: 'file',
exists: true,
},
],
+60
View File
@@ -0,0 +1,60 @@
import { expect, it, describe } from '@jest/globals'
import { IQuest } from '../src/schema'
import { Session } from '../src/Session'
import { MockFileChecker } from './asyncCondition.test'
const quest: IQuest = {
id: 'quest1',
title: 'Quest 1',
image: 'image1',
instruction: 'Instruction 1',
requirements: [],
stages: [
{
id: 'stage1',
requirements: [],
condition: {
command: 'echo start',
},
exceptions: [
{
id: 'exception1',
condition: {
command: 'ls',
},
response: { type: 'narrative', content: 'Exception 1' },
catchAll: false,
},
{
id: 'exception2',
condition: {},
catchAll: true,
response: { type: 'narrative', content: 'Exception 2' },
},
],
response: { type: 'narrative', content: 'Narrative 1' },
},
{
id: 'stage2',
requirements: ['stage1'],
condition: {
command: 'ls',
},
response: { type: 'narrative', content: 'Narrative 2' },
},
],
}
describe('exceptions', () => {
it('should return the stage exception', async () => {
const session = new Session(
{ completedStages: [] },
quest,
new MockFileChecker()
)
expect(await session.runCommand({ command: 'ls' })).toBe('exception1')
await session.runCommand({ command: 'echo start' })
console.log('completed', session.completedStages)
expect(await session.runCommand({ command: 'ls' })).toBe('stage2')
})
})
+1 -1
View File
@@ -93,7 +93,7 @@ describe('Session', () => {
)
})
it('should return responses', () => {
it('should return all responses', () => {
const session = new Session(
{
completedStages: ['stage1'],
+2 -3
View File
@@ -1,6 +1,5 @@
import { test, expect } from '@jest/globals'
import { Stage } from '../src/Stage'
import { FileType } from '../src/schema'
import { MockFileChecker } from './asyncCondition.test'
const checker = new MockFileChecker()
@@ -47,7 +46,7 @@ test('check stage with async condition truthy', async () => {
files: [
{
path: '/home/user/hello.txt',
type: FileType.FILE,
type: 'file',
exists: true,
},
],
@@ -69,7 +68,7 @@ test('check stage with async condition falsy', async () => {
files: [
{
path: '/home/user/not-exists.txt',
type: FileType.FILE,
type: 'file',
exists: true,
},
],