diff --git a/.eslintrc.js b/.eslintrc.js index b323dbd..c8110a1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -62,6 +62,8 @@ module.exports = { ], 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': 'error', + 'no-useless-constructor': 'off', + '@typescript-eslint/no-useless-constructor': 'error', }, overrides: [ { diff --git a/packages/game/src/condition.ts b/packages/game/src/condition.ts index 7a050b2..b3d65f0 100644 --- a/packages/game/src/condition.ts +++ b/packages/game/src/condition.ts @@ -3,15 +3,12 @@ import { Command } from './command' import { Matcher } from './matcher' export class PatternMatcher { - protected matcher: RegExp | null - constructor(pattern?: string, args?: string) { - this.matcher = pattern ? new RegExp(pattern, args) : null + protected matcher: RegExp + constructor(pattern: string, args?: string) { + this.matcher = new RegExp(pattern, args) } test(input: string): boolean { - if (!this.matcher) { - return true - } return this.matcher.test(input) } } @@ -23,14 +20,40 @@ export class CommandMatcher extends PatternMatcher implements Matcher { } export class ErrorMatcher extends PatternMatcher implements Matcher { + constructor(private errorPattern?: string) { + super(errorPattern || '') + } + match(command: Partial): boolean { - if (!this.matcher) { + if (!this.errorPattern) { return command.error === '' } return !!command.error && this.test(command.error) } } +export class PwdMatcher implements Matcher { + constructor(private pwd: string) {} + + match(command: Partial): boolean { + return command.pwd === this.pwd + } +} + +export interface ICondition { + command?: string + output?: string + error?: string + pwd?: string +} + export class Condition { private matchers: Matcher[] = [] + + constructor({ command, output, error, pwd }: ICondition) { + if (command) this.matchers.push(new CommandMatcher(command)) + // if (output) this.matchers.push(new OutputMatcher(output)) + if (pwd) this.matchers.push(new PwdMatcher(pwd)) + this.matchers.push(new ErrorMatcher(error)) // always check unexpected error + } } diff --git a/packages/game/test/condition.test.ts b/packages/game/test/condition.test.ts index e6a9f6c..e513de6 100644 --- a/packages/game/test/condition.test.ts +++ b/packages/game/test/condition.test.ts @@ -1,5 +1,11 @@ import { test, expect } from '@jest/globals' -import { PatternMatcher, CommandMatcher, ErrorMatcher } from '../src/condition' +import { + PatternMatcher, + CommandMatcher, + ErrorMatcher, + PwdMatcher, + Condition, +} from '../src/condition' import { Command } from '../src/command' test('pattern match', () => { @@ -22,12 +28,6 @@ test('empty pattern return true', () => { expect(matcher.test('echo start')).toBe(true) }) -test('empty error return false', () => { - const matcher = new ErrorMatcher() - expect(matcher.match({ error: '' })).toBe(true) - expect(matcher.match({ error: 'permission denied' })).toBe(false) -}) - test('command match', () => { const command: Command = { command: 'echo start', @@ -40,4 +40,45 @@ test('command match', () => { expect(matcher.match(command)).toBe(true) }) -test('condition match', () => {}) +test('error matcher', () => { + const matcher = new ErrorMatcher() + expect(matcher.match({ error: '' })).toBe(true) + expect(matcher.match({ error: 'permission denied' })).toBe(false) +}) + +test('error matcher with specific error', () => { + const matcher = new ErrorMatcher('permission denied') + expect(matcher.match({ error: '' })).toBe(false) + expect(matcher.match({ error: 'file not found' })).toBe(false) + expect(matcher.match({ error: 'cannot open files: permission denied' })).toBe( + true + ) +}) + +test('pwd matcher', () => { + const matcher = new PwdMatcher('/home/user') + expect(matcher.match({ pwd: '/home/user' })).toBe(true) + expect(matcher.match({ pwd: '/home/user1' })).toBe(false) + expect(matcher.match({ pwd: '/home/user/anotherpath' })).toBe(false) + expect(matcher.match({ pwd: '/var/home/user' })).toBe(false) +}) + +// test('condition match', () => { +// const condition = new Condition({ +// command: '^echo start$', +// pwd: '/home/user', +// }) + +// expect( +// condition.match({ +// command: 'echo start', +// pwd: '/home/user', +// }) +// ).toBe(true) +// expect( +// condition.match({ +// command: 'echo start', +// pwd: '/home/user1', +// }) +// ).toBe(false) +// })