Files
linux-odyssey/packages/game/test/exceptions.test.ts

101 lines
2.5 KiB
TypeScript

import { describe, it, expect } from 'bun:test'
import { IQuest } from '../schema'
import { GameSession } from '../Session'
import { MockFileChecker } from './mockFileChecker'
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' },
},
],
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', () => {
it('should return the stage exception', async () => {
const session = new GameSession(
{ completedEvents: [] },
quest,
new MockFileChecker()
)
expect(await session.runCommand({ command: 'ls' })).toBe('exception1')
await session.runCommand({ command: 'echo start' })
expect(await session.runCommand({ command: 'ls' })).toBe('stage2')
})
it('should return the catch all exception', async () => {
const session = new GameSession(
{ completedEvents: [] },
quest,
new MockFileChecker()
)
expect(await session.runCommand({ command: 'what is this' })).toBe(
'exception2'
)
})
it('should return the global exception', async () => {
const session = new GameSession(
{ completedEvents: [] },
quest,
new MockFileChecker()
)
expect(await session.runCommand({ command: 'rm' })).toBe('exception3')
expect(await session.runCommand({ command: 'mv' })).toBe('exception2') // catch all
session.complete('stage1')
expect(await session.runCommand({ command: 'mv' })).toBe('exception4')
})
})