feat: catch-all exceptions

This commit is contained in:
Justin Lin committed 2025-04-21 11:45:23 +08:00
1 parent cfce5ececa
commit 3656868b66
2 files changed
+17 -3

No files matched your search

+5 -2
View File
@@ -42,6 +42,7 @@ export class Quest {
completed: string[] completed: string[]
): Promise<string | null> { ): Promise<string | null> {
const stages = this.getActiveStages(completed) const stages = this.getActiveStages(completed)
let catchAllException: string | null = null
for (const stage of stages) { for (const stage of stages) {
const satisfies = await stage.satisfies(command, this.checker) const satisfies = await stage.satisfies(command, this.checker)
if (satisfies) { if (satisfies) {
@@ -50,7 +51,9 @@ export class Quest {
} }
for (const exception of stages.flatMap((stage) => stage.exceptions)) { for (const exception of stages.flatMap((stage) => stage.exceptions)) {
const condition = new Condition(exception.condition) const condition = new Condition(exception.condition)
if (await condition.satisfies(command, this.checker)) { if (exception.catchAll && !catchAllException) {
catchAllException = exception.id
} else if (await condition.satisfies(command, this.checker)) {
return exception.id return exception.id
} }
} }
@@ -60,7 +63,7 @@ export class Quest {
return exception.id return exception.id
} }
} }
return null return catchAllException
} }
getResponses(completed: string[]): IResponse[] { getResponses(completed: string[]): IResponse[] {
+12 -1
View File
@@ -76,6 +76,17 @@ describe('exceptions', () => {
expect(await session.runCommand({ command: 'ls' })).toBe('stage2') expect(await session.runCommand({ command: 'ls' })).toBe('stage2')
}) })
it('should return the catch all exception', async () => {
const session = new Session(
{ completedStages: [] },
quest,
new MockFileChecker()
)
expect(await session.runCommand({ command: 'what is this' })).toBe(
'exception2'
)
})
it('should return the global exception', async () => { it('should return the global exception', async () => {
const session = new Session( const session = new Session(
{ completedStages: [] }, { completedStages: [] },
@@ -83,7 +94,7 @@ describe('exceptions', () => {
new MockFileChecker() new MockFileChecker()
) )
expect(await session.runCommand({ command: 'rm' })).toBe('exception3') expect(await session.runCommand({ command: 'rm' })).toBe('exception3')
expect(await session.runCommand({ command: 'mv' })).toBeNull() expect(await session.runCommand({ command: 'mv' })).toBe('exception2') // catch all
session.complete('stage1') session.complete('stage1')
expect(await session.runCommand({ command: 'mv' })).toBe('exception4') expect(await session.runCommand({ command: 'mv' })).toBe('exception4')
}) })