From 569016077efe03aaf4759896c807302a99663780 Mon Sep 17 00:00:00 2001 From: wancat Date: Wed, 16 Aug 2023 17:24:40 +0800 Subject: [PATCH] feat: add error handling for duplicate or not exists --- packages/file-graph/errors.js | 14 ++++ packages/file-graph/fileNode.js | 15 ++-- packages/file-graph/fileNode.test.js | 113 +++++++++++++++------------ 3 files changed, 86 insertions(+), 56 deletions(-) create mode 100644 packages/file-graph/errors.js diff --git a/packages/file-graph/errors.js b/packages/file-graph/errors.js new file mode 100644 index 0000000..16539c1 --- /dev/null +++ b/packages/file-graph/errors.js @@ -0,0 +1,14 @@ +/* eslint-disable max-classes-per-file */ +export class DuplicateItemError extends Error { + constructor(message) { + super(message) + this.name = 'DuplicateItemError' + } +} + +export class ParentNotExistsError extends Error { + constructor(message) { + super(message) + this.name = 'ParentNotExistsError' + } +} diff --git a/packages/file-graph/fileNode.js b/packages/file-graph/fileNode.js index 8b4ec8d..a224cd4 100644 --- a/packages/file-graph/fileNode.js +++ b/packages/file-graph/fileNode.js @@ -1,5 +1,6 @@ import path from 'path' import File from './file.js' +import { DuplicateItemError, ParentNotExistsError } from './errors.js' export default class FileNode extends File { constructor(file) { @@ -10,7 +11,11 @@ export default class FileNode extends File { addChild(fileInput) { const file = new File(fileInput) if (file.path === this.path) { - throw new Error('File already exists in the tree') + throw new DuplicateItemError('File already exists in the tree') + } + + if (this.children.some((child) => child.path === file.path)) { + throw new DuplicateItemError('File already exists in the tree') } const parentPath = path.dirname(file.path) @@ -23,19 +28,17 @@ export default class FileNode extends File { if (childNode) { childNode.addChild(file) } else { - console.log(fileInput, this) - throw new Error('Parent path not found in the tree') + throw new ParentNotExistsError('Parent path not found in the tree') } } } toString() { const indent = ' '.repeat(this.path.split('/').length - 1) + const suffix = this.isDirectory() ? '/' : '' const childrenString = this.children .map((child) => child.toString()) .join('') - return `${indent}${this.name}${ - this.isDirectory() ? '/' : '' - }\n${childrenString}` + return `${indent}${this.name}${suffix}\n${childrenString}` } } diff --git a/packages/file-graph/fileNode.test.js b/packages/file-graph/fileNode.test.js index fdb9ec0..b1584c7 100644 --- a/packages/file-graph/fileNode.test.js +++ b/packages/file-graph/fileNode.test.js @@ -1,64 +1,77 @@ +import { DuplicateItemError, ParentNotExistsError } from './errors.js' import FileNode from './fileNode.js' -test('addChild method adds a child node to the tree', () => { - const files = [ - { +describe('FileNode', () => { + let rootNode + + beforeEach(() => { + rootNode = new FileNode({ path: '/home/rudeus', name: 'rudeus', type: 'folder', - discovered: 'test', - }, - { - path: '/home/rudeus/.bash_history', - name: '.bash_history', - type: 'file', - discovered: 'test', - }, - { - path: '/home/rudeus/.viminfo', - name: '.viminfo', - type: 'file', - discovered: 'test', - }, - { - path: '/home/rudeus/.zshrc', - name: '.zshrc', - type: 'file', - discovered: 'test', - }, - { - path: '/home/rudeus/forgotten_scroll.txt', - name: 'forgotten_scroll.txt', - type: 'file', - discovered: 'test', - }, - { + discovered: true, + }) + }) + + test('should throw error when adding duplicate item', () => { + const child = { path: '/home/rudeus/test', name: 'test', type: 'folder', - discovered: 'test', - }, - { - path: '/home/rudeus/test/.b', - name: '.b', - type: 'file', - discovered: false, - }, - { - path: '/home/rudeus/test/a', - name: 'a', - type: 'file', - discovered: false, - }, - ] + } - const root = new FileNode(files[0]) + // Adding the child for the first time + rootNode.addChild(child) - files.slice(1).forEach((file) => { - root.addChild(file) + expect(rootNode.children.length).toBe(1) + + // Trying to add the same child again should throw an error + expect(() => { + rootNode.addChild(child) + }).toThrow(DuplicateItemError) }) - console.log(root.toString()) + test('should throw error when parent does not exist', () => { + const child = { + path: '/home/rudeus/alphabet/test', + name: 'test', + type: 'folder', + } - expect(root.children.length).toBe(5) + // Trying to add to a nonexistent parent should throw an error + expect(() => { + rootNode.addChild(child) + }).toThrow(ParentNotExistsError) + }) + + test('adds nested children to the tree', () => { + const files = [ + { + path: '/home/rudeus/forgotten_scroll.txt', + name: 'forgotten_scroll.txt', + type: 'file', + discovered: 'test', + }, + { + path: '/home/rudeus/test', + name: 'test', + type: 'folder', + discovered: 'test', + }, + { + path: '/home/rudeus/test/a', + name: 'a', + type: 'file', + discovered: false, + }, + ] + + files.forEach((file) => { + rootNode.addChild(file) + }) + + expect(rootNode.children.length).toBe(2) + expect(rootNode.children[1].children.length).toBe(1) + console.log(rootNode.toString()) + }) })