From 4d4d42aec07275b68e78e3bf4fe69e9fdacbb074 Mon Sep 17 00:00:00 2001 From: Opey Adeyemi <60050089+Opeyem1a@users.noreply.github.com> Date: Sun, 13 Oct 2024 22:04:15 -0600 Subject: [PATCH] add tests for tree service (#33) --- src/services/store.ts | 4 +- src/services/tree.test.ts | 164 ++++++++++++++++++++++++++++++++++++++ src/services/tree.ts | 1 + 3 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/services/tree.test.ts diff --git a/src/services/store.ts b/src/services/store.ts index 5c35464..bb20501 100644 --- a/src/services/store.ts +++ b/src/services/store.ts @@ -4,7 +4,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'; type JSONPrimitive = string | number | boolean | null | undefined; -type JSONValue = +export type JSONValue = | JSONPrimitive | JSONValue[] | { @@ -42,9 +42,11 @@ export interface StoreService { write: (data: JSONValue) => void; read: () => JSONValue; } + export interface ServiceConfig { filename: string; } + export const createStoreService = (config: ServiceConfig): StoreService => { return { write: (data: JSONValue) => { diff --git a/src/services/tree.test.ts b/src/services/tree.test.ts new file mode 100644 index 0000000..e22b06e --- /dev/null +++ b/src/services/tree.test.ts @@ -0,0 +1,164 @@ +import { JSONValue } from './store.js'; +import { createTreeService } from './tree.js'; +import { describe, expect, it, vi } from 'vitest'; + +vi.mock('./store.js', () => { + let fileData = ''; + return { + createStoreService: vi.fn(({}) => { + return { + read: (): JSONValue => { + return JSON.parse(fileData) as JSONValue; + }, + write: (data: JSONValue) => { + fileData = JSON.stringify(data); + }, + }; + }), + }; +}); + +describe('tree service is working', () => { + it('registers root branch', () => { + const { registerRoot, get } = createTreeService(); + registerRoot('root'); + expect(get()).to.deep.equal([{ key: 'root', parent: null }]); + }); + + it('overwrites past roots with the new root branch', () => { + const { registerRoot, attachTo, get } = createTreeService(); + registerRoot('root'); + registerRoot('root_2'); + registerRoot('root_3'); + expect(get()).to.deep.equal([{ key: 'root_3', parent: null }]); + + registerRoot('root'); + attachTo({ newBranch: 'some-new-branch', parent: 'root' }); + attachTo({ newBranch: 'some-new-branch-2', parent: 'some-new-branch' }); + registerRoot('root_new'); + expect(get()).to.deep.equal([{ key: 'root_new', parent: null }]); + }); + + it('can attach a branch to a parent', () => { + const { registerRoot, attachTo, get } = createTreeService(); + registerRoot('root'); + attachTo({ newBranch: 'some-new-branch', parent: 'root' }); + expect(get()).to.deep.equal([ + { key: 'root', parent: null }, + { key: 'some-new-branch', parent: 'root' }, + ]); + }); + + it('can move a branch to a parent', () => { + const { registerRoot, attachTo, moveOnto, get } = createTreeService(); + registerRoot('root'); + attachTo({ newBranch: 'branch_a', parent: 'root' }); + attachTo({ newBranch: 'branch_b', parent: 'root' }); + attachTo({ newBranch: 'branch_a_a', parent: 'branch_a' }); + + expect(get()).to.deep.equal([ + { key: 'root', parent: null }, + { key: 'branch_a', parent: 'root' }, + { key: 'branch_b', parent: 'root' }, + { key: 'branch_a_a', parent: 'branch_a' }, + ]); + + moveOnto({ branch: 'branch_a_a', parent: 'root' }); + + expect(get()).to.deep.equal([ + { key: 'root', parent: null }, + { key: 'branch_a', parent: 'root' }, + { key: 'branch_b', parent: 'root' }, + { key: 'branch_a_a', parent: 'root' }, + ]); + + moveOnto({ branch: 'branch_b', parent: 'branch_a' }); + + expect(get()).to.deep.equal([ + { key: 'root', parent: null }, + { key: 'branch_a', parent: 'root' }, + { key: 'branch_b', parent: 'branch_a' }, + { key: 'branch_a_a', parent: 'root' }, + ]); + }); + + it('can remove branches', () => { + const { registerRoot, attachTo, removeBranch, get } = + createTreeService(); + + registerRoot('root'); + attachTo({ newBranch: 'branch_a', parent: 'root' }); + attachTo({ newBranch: 'branch_b', parent: 'root' }); + attachTo({ newBranch: 'branch_a_a', parent: 'branch_a' }); + attachTo({ newBranch: 'branch_b_a', parent: 'branch_b' }); + attachTo({ newBranch: 'branch_b_b', parent: 'branch_b' }); + + expect(get()).to.deep.equal([ + { key: 'root', parent: null }, + { key: 'branch_a', parent: 'root' }, + { key: 'branch_b', parent: 'root' }, + { key: 'branch_a_a', parent: 'branch_a' }, + { key: 'branch_b_a', parent: 'branch_b' }, + { key: 'branch_b_b', parent: 'branch_b' }, + ]); + + removeBranch('branch_a_a'); + + expect(get()).to.deep.equal([ + { key: 'root', parent: null }, + { key: 'branch_a', parent: 'root' }, + { key: 'branch_b', parent: 'root' }, + { key: 'branch_b_a', parent: 'branch_b' }, + { key: 'branch_b_b', parent: 'branch_b' }, + ]); + + removeBranch('branch_b'); + + expect(get()).to.deep.equal([ + { key: 'root', parent: null }, + { key: 'branch_a', parent: 'root' }, + { key: 'branch_b_a', parent: 'branch_b' }, + { key: 'branch_b_b', parent: 'branch_b' }, + ]); + }); + + it('can remove parent branches without removing the child branches', () => { + const { registerRoot, attachTo, removeBranch, get } = + createTreeService(); + + registerRoot('root'); + attachTo({ newBranch: 'branch_a', parent: 'root' }); + attachTo({ newBranch: 'branch_b', parent: 'root' }); + attachTo({ newBranch: 'branch_a_a', parent: 'branch_a' }); + attachTo({ newBranch: 'branch_b_a', parent: 'branch_b' }); + attachTo({ newBranch: 'branch_b_b', parent: 'branch_b' }); + + expect(get()).to.deep.equal([ + { key: 'root', parent: null }, + { key: 'branch_a', parent: 'root' }, + { key: 'branch_b', parent: 'root' }, + { key: 'branch_a_a', parent: 'branch_a' }, + { key: 'branch_b_a', parent: 'branch_b' }, + { key: 'branch_b_b', parent: 'branch_b' }, + ]); + + removeBranch('branch_a'); + + expect(get()).to.deep.equal([ + { key: 'root', parent: null }, + { key: 'branch_b', parent: 'root' }, + { key: 'branch_a_a', parent: 'branch_a' }, + { key: 'branch_b_a', parent: 'branch_b' }, + { key: 'branch_b_b', parent: 'branch_b' }, + ]); + + removeBranch('branch_b'); + + expect(get()).to.deep.equal([ + { key: 'root', parent: null }, + { key: 'branch_a_a', parent: 'branch_a' }, + { key: 'branch_b_a', parent: 'branch_b' }, + { key: 'branch_b_b', parent: 'branch_b' }, + ]); + }); +}); diff --git a/src/services/tree.ts b/src/services/tree.ts index 947d185..ab259f7 100644 --- a/src/services/tree.ts +++ b/src/services/tree.ts @@ -45,6 +45,7 @@ const moveOnto = ( }, deps: { storeService: StoreService } ) => { + // todo: validate they cant be the same branch const tree = _readTree(deps); const parentBranch = _findParent({ parent, tree });