mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
103 lines
3.1 KiB
TypeScript
103 lines
3.1 KiB
TypeScript
import { it, test, expect } from 'bun:test'
|
|
import { Condition } from '../condition/Condition'
|
|
import { OutputMatcher } from '../condition/OutputMatcher'
|
|
import { PwdMatcher } from '../condition/PwdMatcher'
|
|
import { CommandMatcher } from '../condition/CommandMatcher'
|
|
import { ErrorMatcher } from '../condition/ErrorMatcher'
|
|
import { conditionSchema } from '../schema'
|
|
import { MockFileChecker } from './mockFileChecker'
|
|
|
|
test('command match', () => {
|
|
const matcher = new CommandMatcher('^echo start$')
|
|
expect(matcher.match({ command: 'echo start' })).toBe(true)
|
|
expect(matcher.match({ command: 'echo start1' })).toBe(false)
|
|
// Currently not supported
|
|
expect(matcher.match({ command: 'echo "start"' })).toBe(false)
|
|
})
|
|
|
|
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('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 command', () => {
|
|
const condition = new Condition({
|
|
command: '^echo start$',
|
|
})
|
|
|
|
expect(
|
|
condition.match({
|
|
command: 'echo start',
|
|
})
|
|
).toBe(true)
|
|
expect(
|
|
condition.match({
|
|
command: 'echo hello',
|
|
})
|
|
).toBe(false)
|
|
expect(
|
|
condition.match({ command: 'echo start', error: 'permission denied' })
|
|
).toBe(false)
|
|
})
|
|
|
|
test('condition with OR', () => {
|
|
const condition = new Condition({
|
|
or: [{ command: '^echo start$' }, { command: '^echo hello$' }],
|
|
})
|
|
expect(condition.match({ command: 'echo start' })).toBe(true)
|
|
expect(condition.match({ command: 'echo hello' })).toBe(true)
|
|
expect(condition.match({ command: 'echo start1' })).toBe(false)
|
|
})
|
|
|
|
test('condition with NOT', () => {
|
|
const condition = new Condition({
|
|
not: { command: '^echo start$' },
|
|
})
|
|
|
|
expect(condition.match({ command: 'echo start' })).toBe(false)
|
|
expect(condition.match({ command: 'echo hello' })).toBe(true)
|
|
})
|
|
|
|
it('should parse condition', () => {
|
|
const condition = {
|
|
command: 'echo start',
|
|
output: 'start',
|
|
not: {
|
|
pwd: '/home/user',
|
|
},
|
|
}
|
|
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)
|
|
})
|