diff --git a/.eslintrc.js b/.eslintrc.js index d177747..b323dbd 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -60,6 +60,8 @@ module.exports = { tsx: 'never', }, ], + 'no-unused-vars': 'off', + '@typescript-eslint/no-unused-vars': 'error', }, overrides: [ { diff --git a/packages/game/src/command.ts b/packages/game/src/command.ts new file mode 100644 index 0000000..6faf783 --- /dev/null +++ b/packages/game/src/command.ts @@ -0,0 +1,6 @@ +export interface Command { + command: string + output: string + error: string + pwd: string +} diff --git a/packages/game/src/condition.ts b/packages/game/src/condition.ts new file mode 100644 index 0000000..7a050b2 --- /dev/null +++ b/packages/game/src/condition.ts @@ -0,0 +1,36 @@ +/* eslint-disable max-classes-per-file */ +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 + } + + test(input: string): boolean { + if (!this.matcher) { + return true + } + return this.matcher.test(input) + } +} + +export class CommandMatcher extends PatternMatcher implements Matcher { + match(command: Partial): boolean { + return !!command.command && this.test(command.command) + } +} + +export class ErrorMatcher extends PatternMatcher implements Matcher { + match(command: Partial): boolean { + if (!this.matcher) { + return command.error === '' + } + return !!command.error && this.test(command.error) + } +} + +export class Condition { + private matchers: Matcher[] = [] +} diff --git a/packages/game/src/matcher.ts b/packages/game/src/matcher.ts new file mode 100644 index 0000000..b1de51d --- /dev/null +++ b/packages/game/src/matcher.ts @@ -0,0 +1,5 @@ +import { Command } from './command' + +export interface Matcher { + match(command: Partial): boolean +} diff --git a/packages/game/test/condition.test.ts b/packages/game/test/condition.test.ts new file mode 100644 index 0000000..e6a9f6c --- /dev/null +++ b/packages/game/test/condition.test.ts @@ -0,0 +1,43 @@ +import { test, expect } from '@jest/globals' +import { PatternMatcher, CommandMatcher, ErrorMatcher } from '../src/condition' +import { Command } from '../src/command' + +test('pattern match', () => { + const matcher = new PatternMatcher('^echo start$') + + expect(matcher.test('echo start')).toBe(true) + expect(matcher.test('echo start ')).toBe(false) +}) + +test('pattern match with flag', () => { + const matcher = new PatternMatcher('^echo start$', 'i') + + expect(matcher.test('echo start')).toBe(true) + expect(matcher.test('echo Start')).toBe(true) + expect(matcher.test('echo Start ')).toBe(false) +}) + +test('empty pattern return true', () => { + const matcher = new PatternMatcher('') + 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', + output: 'start', + error: '', + pwd: '/home/user', + } + + const matcher = new CommandMatcher('^echo start$', 'i') + expect(matcher.match(command)).toBe(true) +}) + +test('condition match', () => {})