Skip to content

Commit

Permalink
Added Missing Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed Jun 26, 2024
1 parent d04268d commit 8e7618e
Show file tree
Hide file tree
Showing 32 changed files with 226 additions and 266 deletions.
2 changes: 1 addition & 1 deletion code/client/src/domain/editor/fugue/Fugue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class Fugue {
* @param operation
*/
private addNode = ({ id, value, line, styles, parent, side }: InsertOperation) => {
this.tree.addNode(id, value, parent, side, styles, line);
this.tree.addNode(id, value, parent, side, line, styles);
};

/**
Expand Down
6 changes: 3 additions & 3 deletions code/client/src/domain/editor/fugue/FugueTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export class FugueTree<T> {
* @param parent the id of the parent node
* @param side the side of the parent node where this node is located
* @param styles the styles of the node
* @param line
* @param line the line number of the node
*/
addNode(id: Id, value: T, parent: Id, side: 'L' | 'R', styles: InlineStyle[] | undefined, line: number) {
addNode(id: Id, value: T, parent: Id, side: 'L' | 'R', line: number, styles: InlineStyle[] = []) {
// create node
const node: Node<T> = treeNode(id, value, parent, side, 0, styles as InlineStyle[]);
const node: Node<T> = treeNode(id, value, parent, side, 0, styles);
if (value === '\n') {
this._root.value.splice(line, 0, node);
}
Expand Down
1 change: 1 addition & 0 deletions code/client/src/ui/components/sidebar/Sidebar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
}

ul {
padding-top: 2vh;
margin: 0;
width: 100%;
overflow-y: scroll;
Expand Down
2 changes: 0 additions & 2 deletions code/client/src/ui/pages/recent/Recent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ function Recent() {
try {
startLoading();
const documents = await http.get('/recent');
console.log(documents);
setDocuments(documents);
console.log(documents[0].updatedAt, formatTimePassed(documents[0].updatedAt));
} catch (e) {
publishError(e as Error);
} finally {
Expand Down
5 changes: 0 additions & 5 deletions code/client/src/ui/pages/workspace/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ function Workspace() {

useEffect(() => {
const docs = getDocuments(resources);
console.log(docs);
setRows(docs);
}, [resources]);

useEffect(() => {
console.log('rows', rows);
}, [rows]);

return (
<div className="workspace">
<h2>Documents in {workspace?.name}</h2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ describe('Fugue Operations', () => {
value: 'a',
parent: { sender: 'root', counter: 0 },
side: 'R',
cursor: { line: 0, column: 0 },
line: 0,
styles: [],
};

// when
Expand All @@ -48,7 +49,6 @@ describe('Fugue Operations', () => {
const deleteOperation: DeleteOperation = {
type: 'delete',
id: { sender: 'A', counter: 0 },
cursor: { line: 0, column: 0 },
};

// when
Expand Down Expand Up @@ -101,8 +101,8 @@ describe('Fugue Operations', () => {
node1.rightChildren = [node2.id];
const document: DocumentContent = {
operations: [
{ type: 'insert', ...node1, parent: root.id, styles: [], cursor: { line: 0, column: 0 } },
{ type: 'insert', ...node2, parent: node1.id, styles: [], cursor: { line: 0, column: 1 } },
{ type: 'insert', ...node1, parent: root.id, styles: [], line: 0 },
{ type: 'insert', ...node2, parent: node1.id, styles: [], line: 0 },
],
};

Expand Down
7 changes: 0 additions & 7 deletions code/client/tests/editor/domain/document/history/cut.test.ts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions code/client/tests/editor/domain/document/history/utils.ts

This file was deleted.

6 changes: 6 additions & 0 deletions code/client/tests/editor/fugue/fugue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ describe('Fugue', () => {
value: 'a',
parent: { sender: 'root', counter: 0 },
side: 'R',
line: 0,
styles: [],
};

// when
Expand Down Expand Up @@ -88,6 +90,8 @@ describe('Fugue', () => {
value: 'x',
parent: { sender: 'root', counter: 0 },
side: 'R',
line: 0,
styles: [],
};
const deleteOperation: DeleteOperation = {
type: 'delete',
Expand Down Expand Up @@ -126,6 +130,8 @@ describe('Fugue', () => {
value: 'x',
parent: { sender: 'root', counter: 0 },
side: 'R',
line: 0,
styles: [],
};
const styleOperation: InlineStyleOperation = {
type: 'inline-style',
Expand Down
30 changes: 16 additions & 14 deletions code/client/tests/editor/fugue/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ describe('FugueTree', () => {
value: 'a',
parent: { sender: 'root', counter: 0 },
side: 'L',
cursor: { line: 0, column: 0 },
line: 0,
styles: [],
};
const rootId = { sender: 'root', counter: 0 };
const { id, value, parent, side } = operation;
const { id, value, parent, side, line } = operation;

// when
tree.addNode(id, value, parent, side, []);
tree.addNode(id, value, parent, side, line);

// then
const root = tree.getById(rootId);
Expand All @@ -36,16 +37,17 @@ describe('FugueTree', () => {

test('should delete a node from the tree', () => {
// given
const { id, value, parent, side }: InsertOperation = {
const { id, value, parent, side, line }: InsertOperation = {
type: 'insert',
id: { sender: 'A', counter: 0 },
value: 'a',
parent: { sender: 'root', counter: 0 },
side: 'L',
cursor: { line: 0, column: 0 },
line: 0,
styles: [],
};
// when
tree.addNode(id, value, parent, side);
tree.addNode(id, value, parent, side, line);
tree.deleteNode(id);

// then
Expand Down Expand Up @@ -82,9 +84,9 @@ describe('FugueTree', () => {

test('should return the leftmost descendant of a node', () => {
// given
tree.addNode({ sender: 'A', counter: 0 }, 'a', { sender: 'root', counter: 0 }, 'L');
tree.addNode({ sender: 'A', counter: 1 }, 'b', { sender: 'A', counter: 0 }, 'L');
tree.addNode({ sender: 'A', counter: 2 }, 'c', { sender: 'A', counter: 1 }, 'L');
tree.addNode({ sender: 'A', counter: 0 }, 'a', { sender: 'root', counter: 0 }, 'L', 0);
tree.addNode({ sender: 'A', counter: 1 }, 'b', { sender: 'A', counter: 0 }, 'L', 0);
tree.addNode({ sender: 'A', counter: 2 }, 'c', { sender: 'A', counter: 1 }, 'L', 0);

// when
const leftmostDescendant = tree.getLeftmostDescendant({
Expand All @@ -98,10 +100,10 @@ describe('FugueTree', () => {

test('should traverse the tree in depth-first order', () => {
// given
tree.addNode({ sender: 'A', counter: 0 }, 'a', { sender: 'root', counter: 0 }, 'R');
tree.addNode({ sender: 'A', counter: 1 }, 'b', { sender: 'A', counter: 0 }, 'R');
tree.addNode({ sender: 'A', counter: 2 }, 'c', { sender: 'A', counter: 1 }, 'R');
tree.addNode({ sender: 'A', counter: 3 }, 'd', { sender: 'A', counter: 2 }, 'R');
tree.addNode({ sender: 'A', counter: 0 }, 'a', { sender: 'root', counter: 0 }, 'R', 0);
tree.addNode({ sender: 'A', counter: 1 }, 'b', { sender: 'A', counter: 0 }, 'R', 0);
tree.addNode({ sender: 'A', counter: 2 }, 'c', { sender: 'A', counter: 1 }, 'R', 0);
tree.addNode({ sender: 'A', counter: 3 }, 'd', { sender: 'A', counter: 2 }, 'R', 0);

// when
const expectedValues = ['a', 'b', 'c', 'd'];
Expand All @@ -120,7 +122,7 @@ describe('FugueTree', () => {

test('should update the inline style of a node', () => {
// given
tree.addNode({ sender: 'A', counter: 0 }, 'a', { sender: 'root', counter: 0 }, 'R');
tree.addNode({ sender: 'A', counter: 0 }, 'a', { sender: 'root', counter: 0 }, 'R', 0);

// when
tree.updateInlineStyle({ sender: 'A', counter: 0 }, 'bold', true);
Expand Down
27 changes: 0 additions & 27 deletions code/client/tests/editor/slate/utils.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions code/server/src/controllers/http/handlers/commitsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function resourcesHandlers(
name: req.user!.name,
id: req.user!.id,
};
await service.commit(id, author);
httpResponse.noContent(res).send();
const commitId = await service.commit(id, author);
httpResponse.created(res).send({ commitId });
};

const rollback = async (req: Request, res: Response) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function onOperation(service: DocumentsService) {
if (!id) throw new ForbiddenError('Not in a room');

socket.broadcast.to(id).emit('operations', operations);
await service.updateDocument(wid, id, operations);
await service.applyOperations(wid, id, operations);
socket.emit('ack');
};
}
Expand Down
6 changes: 3 additions & 3 deletions code/server/src/databases/firestore/FirestoreCommitsDB.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import db from '@src/firebaseConfig';
import { CommitsRepository } from '@databases/types';
import { Commit } from '@notespace/shared/src/document/types/commits';
import { Commit, CommitMeta } from '@notespace/shared/src/document/types/commits';
import { NotFoundError } from '@domain/errors/errors';

export class FirestoreCommitsDB implements CommitsRepository {
Expand All @@ -21,13 +21,13 @@ export class FirestoreCommitsDB implements CommitsRepository {
return { id: commitId, content, timestamp, author };
}

async getCommits(id: string): Promise<Commit[]> {
async getCommits(id: string): Promise<CommitMeta[]> {
const doc = db.collection('commits').doc(id);
const snapshot = await doc.get();
const data = snapshot.data();
if (!data) return [];
return Object.entries(data)
.map(([id, { content, timestamp, author }]) => ({ id, content, timestamp, author }))
.map(([id, { timestamp, author }]) => ({ id, timestamp, author }))
.sort((a, b) => a.timestamp - b.timestamp);
}

Expand Down
8 changes: 5 additions & 3 deletions code/server/src/databases/memory/MemoryCommitsDB.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NotFoundError } from '@domain/errors/errors';
import { CommitsRepository } from '@databases/types';
import { Commit } from '@notespace/shared/src/document/types/commits';
import { Commit, CommitMeta } from '@notespace/shared/src/document/types/commits';

export class MemoryCommitsDB implements CommitsRepository {
private readonly commits: Record<string, Record<string, Commit>> = {};
Expand All @@ -12,9 +12,11 @@ export class MemoryCommitsDB implements CommitsRepository {
return commit;
}

async getCommits(id: string): Promise<Commit[]> {
async getCommits(id: string): Promise<CommitMeta[]> {
if (!this.commits[id]) return [];
return Object.values(this.commits[id]).sort((a, b) => a.timestamp - b.timestamp);
return Object.values(this.commits[id])
.map(({ id, timestamp, author }) => ({ id, timestamp, author }))
.sort((a, b) => a.timestamp - b.timestamp);
}

async saveCommit(id: string, commit: Commit): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions code/server/src/databases/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ResourceType, Resource, DocumentResource } from '@notespace/shared/src/
import { Workspace, WorkspaceMeta } from '@notespace/shared/src/workspace/types/workspace';
import { User, UserData } from '@notespace/shared/src/users/types';
import { SearchParams } from '@src/utils/searchParams';
import { Commit } from '@notespace/shared/src/document/types/commits';
import { Commit, CommitMeta } from '@notespace/shared/src/document/types/commits';

export interface DocumentsRepository {
/**
Expand Down Expand Up @@ -154,7 +154,7 @@ export interface UsersRepository {
export interface CommitsRepository {
saveCommit: (id: string, commit: Commit) => Promise<void>;
getCommit: (id: string, commitId: string) => Promise<Commit>;
getCommits: (id: string) => Promise<Commit[]>;
getCommits: (id: string) => Promise<CommitMeta[]>;
deleteCommits: (id: string) => Promise<void>;
}

Expand Down
Loading

0 comments on commit 8e7618e

Please sign in to comment.