mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: add global exceptions
This commit is contained in:
1 parent
fb823814e0
commit
cfce5ececa
6 files changed
+71
-5
No files matched your search
@@ -25,6 +25,18 @@ export class Quest {
|
|||||||
return this.stages.filter((stage) => stage.active(completed))
|
return this.stages.filter((stage) => stage.active(completed))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getActiveExceptions(completed: string[]) {
|
||||||
|
return (
|
||||||
|
this.quest.exceptions?.filter(
|
||||||
|
(exception) =>
|
||||||
|
!completed.includes(exception.id) &&
|
||||||
|
exception.requirements.every((requirement) =>
|
||||||
|
completed.includes(requirement)
|
||||||
|
)
|
||||||
|
) ?? []
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
async findSatisfiedStage(
|
async findSatisfiedStage(
|
||||||
command: ICommand,
|
command: ICommand,
|
||||||
completed: string[]
|
completed: string[]
|
||||||
@@ -38,7 +50,13 @@ 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.match(command)) {
|
if (await condition.satisfies(command, this.checker)) {
|
||||||
|
return exception.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const exception of this.getActiveExceptions(completed)) {
|
||||||
|
const condition = new Condition(exception.condition)
|
||||||
|
if (await condition.satisfies(command, this.checker)) {
|
||||||
return exception.id
|
return exception.id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,4 +40,14 @@ export class Condition {
|
|||||||
if (this.matchers.length === 0) return true
|
if (this.matchers.length === 0) return true
|
||||||
return checkFiles(checker, this.files)
|
return checkFiles(checker, this.files)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async satisfies(
|
||||||
|
command: ICommand,
|
||||||
|
checker: IFileExistenceChecker
|
||||||
|
): Promise<boolean> {
|
||||||
|
// Error matcher is always true
|
||||||
|
if (this.matchers.length === 1 && this.files.length === 0) return false
|
||||||
|
// eslint-disable-next-line no-return-await
|
||||||
|
return this.match(command) && (await this.check(checker))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { conditionSchema } from './condition.js'
|
import { conditionSchema } from './condition.js'
|
||||||
import { responseSchema } from './response.js'
|
import { responseSchema } from './response.js'
|
||||||
|
import { requirementsSchema } from './requirements.js'
|
||||||
|
|
||||||
export const exceptionSchema = z.object({
|
export const exceptionSchema = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
@@ -17,7 +18,7 @@ export const stageExceptionSchema = exceptionSchema.extend({
|
|||||||
export type StageException = z.infer<typeof stageExceptionSchema>
|
export type StageException = z.infer<typeof stageExceptionSchema>
|
||||||
|
|
||||||
export const globalExceptionSchema = exceptionSchema.extend({
|
export const globalExceptionSchema = exceptionSchema.extend({
|
||||||
requirements: z.array(z.string()),
|
requirements: requirementsSchema.default([]),
|
||||||
})
|
})
|
||||||
|
|
||||||
export type GlobalException = z.infer<typeof globalExceptionSchema>
|
export type GlobalException = z.infer<typeof globalExceptionSchema>
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { stageSchema } from './stage.js'
|
import { stageSchema } from './stage.js'
|
||||||
import { requirementsSchema } from './requirements.js'
|
import { requirementsSchema } from './requirements.js'
|
||||||
// import { globalExceptionSchema } from './exception.js'
|
import { globalExceptionSchema } from './exception.js'
|
||||||
// import { requirementSchema } from './requirement.js'
|
|
||||||
|
|
||||||
export const questSchema = z.object({
|
export const questSchema = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
@@ -11,7 +10,7 @@ export const questSchema = z.object({
|
|||||||
instruction: z.string(),
|
instruction: z.string(),
|
||||||
requirements: requirementsSchema,
|
requirements: requirementsSchema,
|
||||||
stages: z.array(stageSchema),
|
stages: z.array(stageSchema),
|
||||||
// exceptions: z.array(globalExceptionSchema).default([]),
|
exceptions: z.array(globalExceptionSchema).optional(),
|
||||||
})
|
})
|
||||||
|
|
||||||
export type IQuest = z.infer<typeof questSchema>
|
export type IQuest = z.infer<typeof questSchema>
|
||||||
@@ -5,6 +5,7 @@ import { PwdMatcher } from '../src/condition/PwdMatcher'
|
|||||||
import { CommandMatcher } from '../src/condition/CommandMatcher'
|
import { CommandMatcher } from '../src/condition/CommandMatcher'
|
||||||
import { ErrorMatcher } from '../src/condition/ErrorMatcher.js'
|
import { ErrorMatcher } from '../src/condition/ErrorMatcher.js'
|
||||||
import { conditionSchema } from '../src/schema'
|
import { conditionSchema } from '../src/schema'
|
||||||
|
import { MockFileChecker } from './asyncCondition.test'
|
||||||
|
|
||||||
test('command match', () => {
|
test('command match', () => {
|
||||||
const matcher = new CommandMatcher('^echo start$')
|
const matcher = new CommandMatcher('^echo start$')
|
||||||
@@ -92,3 +93,10 @@ it('should parse condition', () => {
|
|||||||
}
|
}
|
||||||
expect(conditionSchema.safeParse(condition).error).toBeUndefined()
|
expect(conditionSchema.safeParse(condition).error).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should not match empty condition', async () => {
|
||||||
|
const condition = new Condition({})
|
||||||
|
expect(
|
||||||
|
await condition.satisfies({ command: 'echo start' }, new MockFileChecker())
|
||||||
|
).toBe(false)
|
||||||
|
})
|
||||||
@@ -43,6 +43,24 @@ const quest: IQuest = {
|
|||||||
response: { type: 'narrative', content: 'Narrative 2' },
|
response: { type: 'narrative', content: 'Narrative 2' },
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
exceptions: [
|
||||||
|
{
|
||||||
|
id: 'exception3',
|
||||||
|
condition: {
|
||||||
|
command: 'rm',
|
||||||
|
},
|
||||||
|
response: { type: 'narrative', content: 'Exception 3' },
|
||||||
|
requirements: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'exception4',
|
||||||
|
condition: {
|
||||||
|
command: 'mv',
|
||||||
|
},
|
||||||
|
response: { type: 'narrative', content: 'Exception 4' },
|
||||||
|
requirements: ['stage1'],
|
||||||
|
},
|
||||||
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('exceptions', () => {
|
describe('exceptions', () => {
|
||||||
@@ -57,4 +75,16 @@ describe('exceptions', () => {
|
|||||||
console.log('completed', session.completedStages)
|
console.log('completed', session.completedStages)
|
||||||
expect(await session.runCommand({ command: 'ls' })).toBe('stage2')
|
expect(await session.runCommand({ command: 'ls' })).toBe('stage2')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should return the global exception', async () => {
|
||||||
|
const session = new Session(
|
||||||
|
{ completedStages: [] },
|
||||||
|
quest,
|
||||||
|
new MockFileChecker()
|
||||||
|
)
|
||||||
|
expect(await session.runCommand({ command: 'rm' })).toBe('exception3')
|
||||||
|
expect(await session.runCommand({ command: 'mv' })).toBeNull()
|
||||||
|
session.complete('stage1')
|
||||||
|
expect(await session.runCommand({ command: 'mv' })).toBe('exception4')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
Reference in new issue
Block a user