From cb26e1dcb0cc250e165faefa2461ab77489009fa Mon Sep 17 00:00:00 2001 From: wancat Date: Wed, 6 Mar 2024 23:32:23 +0800 Subject: [PATCH] feat: add output matcher --- packages/game/src/condition.ts | 6 ++++++ packages/game/test/condition.test.ts | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/packages/game/src/condition.ts b/packages/game/src/condition.ts index b3d65f0..c5febc9 100644 --- a/packages/game/src/condition.ts +++ b/packages/game/src/condition.ts @@ -40,6 +40,12 @@ export class PwdMatcher implements Matcher { } } +export class OutputMatcher extends PatternMatcher implements Matcher { + match(command: Partial): boolean { + return command.output ? this.test(command.output.trim()) : false + } +} + export interface ICondition { command?: string output?: string diff --git a/packages/game/test/condition.test.ts b/packages/game/test/condition.test.ts index e513de6..358f388 100644 --- a/packages/game/test/condition.test.ts +++ b/packages/game/test/condition.test.ts @@ -4,6 +4,7 @@ import { CommandMatcher, ErrorMatcher, PwdMatcher, + OutputMatcher, Condition, } from '../src/condition' import { Command } from '../src/command' @@ -63,6 +64,13 @@ test('pwd matcher', () => { expect(matcher.match({ pwd: '/var/home/user' })).toBe(false) }) +test('output matcher', () => { + const matcher = new OutputMatcher('^start$') + expect(matcher.match({ output: 'start' })).toBe(true) + expect(matcher.match({ output: 'start1' })).toBe(false) + expect(matcher.match({ output: 'start\n' })).toBe(true) +}) + // test('condition match', () => { // const condition = new Condition({ // command: '^echo start$',