mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
108 lines
2.2 KiB
TypeScript
108 lines
2.2 KiB
TypeScript
import { test, expect } from '@jest/globals'
|
|
import { IFileExistenceChecker, IFileInput } from '../src/schema'
|
|
import { checkFiles } from '../src/condition/FileMatcher.js'
|
|
import { Condition } from '../src/condition/Condition.js'
|
|
|
|
export class MockFileChecker implements IFileExistenceChecker {
|
|
private files: IFileInput[] = [
|
|
{
|
|
path: '/home/user/hello.txt',
|
|
type: 'file',
|
|
},
|
|
{
|
|
path: '/home/user/world.txt',
|
|
type: 'file',
|
|
},
|
|
{
|
|
path: '/home/user/Downloads',
|
|
type: 'folder',
|
|
},
|
|
]
|
|
|
|
async exists(file: IFileInput): Promise<boolean> {
|
|
return this.files.some((f) => f.path === file.path && f.type === file.type)
|
|
}
|
|
}
|
|
|
|
test('file matcher', async () => {
|
|
const checker = new MockFileChecker()
|
|
expect(
|
|
await checkFiles(checker, [
|
|
{
|
|
path: '/home/user/hello.txt',
|
|
type: 'file',
|
|
exists: true,
|
|
},
|
|
])
|
|
).toBe(true)
|
|
expect(
|
|
await checkFiles(checker, [
|
|
{
|
|
path: '/home/user/not-exists.txt',
|
|
type: 'file',
|
|
exists: true,
|
|
},
|
|
])
|
|
).toBe(false)
|
|
expect(
|
|
await checkFiles(checker, [
|
|
{
|
|
path: '/home/user/not-exists.txt',
|
|
type: 'file',
|
|
exists: false,
|
|
},
|
|
])
|
|
).toBe(true)
|
|
expect(
|
|
await checkFiles(checker, [
|
|
{
|
|
path: '/home/user/Downloads',
|
|
type: 'folder',
|
|
exists: true,
|
|
},
|
|
])
|
|
).toBe(true)
|
|
expect(
|
|
await checkFiles(checker, [
|
|
{
|
|
path: '/home/user/hello.txt',
|
|
type: 'file',
|
|
exists: true,
|
|
},
|
|
{
|
|
path: '/home/user/Downloads',
|
|
type: 'folder',
|
|
exists: true,
|
|
},
|
|
])
|
|
).toBe(true)
|
|
expect(
|
|
await checkFiles(checker, [
|
|
{
|
|
path: '/home/user/hello.txt',
|
|
type: 'file',
|
|
exists: true,
|
|
},
|
|
{
|
|
path: '/home/user/not-exist.txt',
|
|
type: 'file',
|
|
exists: true,
|
|
},
|
|
])
|
|
).toBe(false)
|
|
})
|
|
|
|
test('condition with file matcher', async () => {
|
|
const checker = new MockFileChecker()
|
|
const condition = new Condition({
|
|
files: [
|
|
{
|
|
path: '/home/user/hello.txt',
|
|
type: 'file',
|
|
exists: true,
|
|
},
|
|
],
|
|
})
|
|
expect(await condition.check(checker)).toBe(true)
|
|
})
|