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