fix: test in game and file-graph

This commit is contained in:
Justin Lin committed 2025-04-26 12:52:12 +08:00
1 parent d450613816
commit f7e2b00358
8 files changed
+91 -93

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
import { describe, test, expect } from '@jest/globals'
import File from '../src/file.js'
import File from '../file.js'
describe('File', () => {
let file: File
+3 -3
View File
@@ -1,7 +1,7 @@
import { describe, test, expect } from '@jest/globals'
import FileGraph from '../src/fileGraph.js'
import FileNode from '../src/fileNode.js'
import { FileObject } from '../src/file.js'
import FileGraph from '../fileGraph.js'
import FileNode from '../fileNode.js'
import { FileObject } from '../file.js'
describe('FileGraph', () => {
let root: FileNode
+73 -75
View File
@@ -1,7 +1,7 @@
import { describe, test, expect } from '@jest/globals'
import { DuplicateItemError, ParentNotExistsError } from '../src/errors.js'
import FileNode from '../src/fileNode.js'
import { FileObject } from '../src/file.js'
import { DuplicateItemError, ParentNotExistsError } from '../errors.js'
import FileNode, { IFileNode } from '../fileNode.js'
import { FileObject } from '../file.js'
function createNodeFromFiles(files: FileObject[]) {
const node = new FileNode(files[0])
@@ -42,14 +42,13 @@ describe('FileNode', () => {
beforeEach(() => {
rootNode = new FileNode({
path: '/home/rudeus',
name: 'rudeus',
type: 'directory',
discovered: true,
})
})
test('should throw error when adding duplicate item', () => {
const child = {
const child: FileObject = {
path: '/home/rudeus/test',
type: 'directory',
discovered: true,
@@ -67,9 +66,8 @@ describe('FileNode', () => {
})
test('should throw error when parent does not exist', () => {
const child = {
const child: FileObject = {
path: '/home/rudeus/alphabet/test',
name: 'test',
type: 'directory',
discovered: true,
}
@@ -81,22 +79,19 @@ describe('FileNode', () => {
})
test('adds nested children to the tree', () => {
const files = [
const files: FileObject[] = [
{
path: '/home/rudeus/forgotten_scroll.txt',
name: 'forgotten_scroll.txt',
type: 'file',
discovered: true,
},
{
path: '/home/rudeus/test',
name: 'test',
type: 'directory',
discovered: true,
},
{
path: '/home/rudeus/test/a',
name: 'a',
type: 'file',
discovered: false,
},
@@ -111,9 +106,8 @@ describe('FileNode', () => {
})
test('adds nested children to the tree with makeParents option', () => {
const file = {
const file: FileObject = {
path: '/home/rudeus/test/a/b/c/d/e',
name: 'a',
type: 'file',
discovered: false,
}
@@ -129,34 +123,34 @@ describe('FileNode', () => {
})
test('merge-subtree', () => {
const root = {
const root: FileObject = {
path: '/',
type: 'directory',
discovered: true,
}
const files1 = [
const files1: FileObject[] = [
root,
{ path: '/a', type: 'file', discovered: true },
{ path: '/b', type: 'directory', discovered: true },
{ path: '/b/c', type: 'directory', discovered: true },
{ path: '/b/d', type: 'file', discovered: true },
{ path: '/b/c/f', type: 'file', discovered: true },
{ path: '/a', type: 'file' as const, discovered: true },
{ path: '/b', type: 'directory' as const, discovered: true },
{ path: '/b/c', type: 'directory' as const, discovered: true },
{ path: '/b/d', type: 'file' as const, discovered: true },
{ path: '/b/c/f', type: 'file' as const, discovered: true },
]
const files2 = [
{ path: '/b', type: 'directory', discovered: true },
{ path: '/b/c', type: 'directory', discovered: true },
{ path: '/b/e', type: 'file', discovered: true },
{ path: '/b/c/g', type: 'file', discovered: true },
const files2: FileObject[] = [
{ path: '/b', type: 'directory' as const, discovered: true },
{ path: '/b/c', type: 'directory' as const, discovered: true },
{ path: '/b/e', type: 'file' as const, discovered: true },
{ path: '/b/c/g', type: 'file' as const, discovered: true },
]
const expected = [
const expected: FileObject[] = [
root,
{ path: '/a', type: 'file', discovered: true },
{ path: '/b', type: 'directory', discovered: true },
{ path: '/b/c', type: 'directory', discovered: true },
{ path: '/b/e', type: 'file', discovered: true },
{ path: '/b/c/g', type: 'file', discovered: true },
{ path: '/a', type: 'file' as const, discovered: true },
{ path: '/b', type: 'directory' as const, discovered: true },
{ path: '/b/c', type: 'directory' as const, discovered: true },
{ path: '/b/e', type: 'file' as const, discovered: true },
{ path: '/b/c/g', type: 'file' as const, discovered: true },
]
mergeTest(files1, files2, expected)
@@ -166,90 +160,94 @@ describe('FileNode', () => {
// Test if the node to merge is only the middle part of the tree
// (i.e. the node to merge doesn't contains all level of children)
// The original tree should keep the children that are not in the node to merge
const root = {
const root: FileObject = {
path: '/',
type: 'directory',
discovered: true,
}
const files1 = [
const files1: FileObject[] = [
root,
{ path: '/a', type: 'file', discovered: true },
{ path: '/b', type: 'directory', discovered: true },
{ path: '/b/c', type: 'directory', discovered: true },
{ path: '/b/d', type: 'file', discovered: true },
{ path: '/b/c/f', type: 'file', discovered: true },
{ path: '/a', type: 'file' as const, discovered: true },
{ path: '/b', type: 'directory' as const, discovered: true },
{ path: '/b/c', type: 'directory' as const, discovered: true },
{ path: '/b/d', type: 'file' as const, discovered: true },
{ path: '/b/c/f', type: 'file' as const, discovered: true },
]
const files2 = [
{ path: '/b', type: 'directory', discovered: true },
{ path: '/b/c', type: 'directory', discovered: true },
{ path: '/b/e', type: 'file', discovered: true },
const files2: FileObject[] = [
{ path: '/b', type: 'directory' as const, discovered: true },
{ path: '/b/c', type: 'directory' as const, discovered: true },
{ path: '/b/e', type: 'file' as const, discovered: true },
]
const expected = [
const expected: FileObject[] = [
root,
{ path: '/a', type: 'file', discovered: true },
{ path: '/b', type: 'directory', discovered: true },
{ path: '/b/c', type: 'directory', discovered: true },
{ path: '/b/e', type: 'file', discovered: true },
{ path: '/b/c/f', type: 'file', discovered: true },
{ path: '/a', type: 'file' as const, discovered: true },
{ path: '/b', type: 'directory' as const, discovered: true },
{ path: '/b/c', type: 'directory' as const, discovered: true },
{ path: '/b/e', type: 'file' as const, discovered: true },
{ path: '/b/c/f', type: 'file' as const, discovered: true },
]
mergeTest(files1, files2, expected)
})
test('merge-hidden', () => {
const root = {
const root: FileObject = {
path: '/',
type: 'directory',
discovered: true,
}
const files1 = [
const files1: FileObject[] = [
root,
{ path: '/a', type: 'file', discovered: true },
{ path: '/.b', type: 'file', discovered: false },
{ path: '/c', type: 'directory', discovered: true },
{ path: '/c/.d', type: 'file', discovered: false },
{ path: '/a', type: 'file' as const, discovered: true },
{ path: '/.b', type: 'file' as const, discovered: false },
{ path: '/c', type: 'directory' as const, discovered: true },
{ path: '/c/.d', type: 'file' as const, discovered: false },
]
const files2 = [
{ path: '/c', type: 'directory', discovered: true },
{ path: '/c/.d', type: 'file', discovered: true },
const files2: FileObject[] = [
{ path: '/c', type: 'directory' as const, discovered: true },
{ path: '/c/.d', type: 'file' as const, discovered: true },
]
const expected = [
const expected: FileObject[] = [
root,
{ path: '/a', type: 'file', discovered: true },
{ path: '/.b', type: 'file', discovered: false },
{ path: '/c', type: 'directory', discovered: true },
{ path: '/c/.d', type: 'file', discovered: true },
{ path: '/a', type: 'file' as const, discovered: true },
{ path: '/.b', type: 'file' as const, discovered: false },
{ path: '/c', type: 'directory' as const, discovered: true },
{ path: '/c/.d', type: 'file' as const, discovered: true },
]
mergeTest(files1, files2, expected)
})
test('remove-child', () => {
const root = {
const root: FileObject = {
path: '/',
type: 'directory',
discovered: true,
}
const files = [
const files: FileObject[] = [
root,
{ path: '/a', type: 'file', discovered: true },
{ path: '/b', type: 'directory', discovered: true },
{ path: '/b/c', type: 'directory', discovered: true },
{ path: '/b/d', type: 'file', discovered: true },
{ path: '/b/c/f', type: 'file', discovered: true },
{ path: '/a', type: 'file' as const, discovered: true },
{ path: '/b', type: 'directory' as const, discovered: true },
{ path: '/b/c', type: 'directory' as const, discovered: true },
{ path: '/b/d', type: 'file' as const, discovered: true },
{ path: '/b/c/f', type: 'file' as const, discovered: true },
]
const fileToRemove = { path: '/b/c', type: 'directory', discovered: true }
const fileToRemove: FileObject = {
path: '/b/c',
type: 'directory',
discovered: true,
}
const expected = [
const expected: FileObject[] = [
root,
{ path: '/a', type: 'file', discovered: true },
{ path: '/b', type: 'directory', discovered: true },
{ path: '/b/d', type: 'file', discovered: true },
{ path: '/a', type: 'file' as const, discovered: true },
{ path: '/b', type: 'directory' as const, discovered: true },
{ path: '/b/d', type: 'file' as const, discovered: true },
]
removeChildTest(files, fileToRemove, expected)
@@ -257,7 +255,7 @@ describe('FileNode', () => {
})
test('load nested fileNode', () => {
const data = {
const data: IFileNode = {
path: '/',
type: 'directory',
discovered: true,
+3 -3
View File
@@ -1,7 +1,7 @@
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'
import { IFileExistenceChecker, IFileInput } from '../schema'
import { checkFiles } from '../condition/FileMatcher.js'
import { Condition } from '../condition/Condition.js'
export class MockFileChecker implements IFileExistenceChecker {
private files: IFileInput[] = [
+6 -6
View File
@@ -1,10 +1,10 @@
import { test, expect } from '@jest/globals'
import { Condition } from '../src/condition/Condition'
import { OutputMatcher } from '../src/condition/OutputMatcher'
import { PwdMatcher } from '../src/condition/PwdMatcher'
import { CommandMatcher } from '../src/condition/CommandMatcher'
import { ErrorMatcher } from '../src/condition/ErrorMatcher.js'
import { conditionSchema } from '../src/schema'
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 './asyncCondition.test'
test('command match', () => {
+2 -2
View File
@@ -1,6 +1,6 @@
import { expect, it, describe } from '@jest/globals'
import { IQuest } from '../src/schema'
import { GameSession } from '../src/Session'
import { IQuest } from '../schema'
import { GameSession } from '../Session'
import { MockFileChecker } from './asyncCondition.test'
const quest: IQuest = {
+2 -2
View File
@@ -1,6 +1,6 @@
import { expect, describe, it } from '@jest/globals'
import { IQuest } from '../src/schema'
import { GameSession } from '../src/Session'
import { IQuest } from '../schema'
import { GameSession } from '../Session'
import { MockFileChecker } from './asyncCondition.test'
const quest: IQuest = {
+1 -1
View File
@@ -1,5 +1,5 @@
import { test, expect } from '@jest/globals'
import { Stage } from '../src/Stage'
import { Stage } from '../Stage'
import { MockFileChecker } from './asyncCondition.test'
const checker = new MockFileChecker()