Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BookServiceManagerFactoryImpl from "./bookService";
import BookServiceManager from "./bookService";
import { performance } from "perf_hooks";

class EnterpriseBookManagementSystem {
Expand All @@ -8,22 +8,22 @@ class EnterpriseBookManagementSystem {

public static async executeBookManagementWorkflow(): Promise<void> {
console.log("Initializing Enterprise Book Management System...");
const bookService = BookServiceManagerFactoryImpl;
const bookService = BookServiceManager;

// Create some initial books
for (let i = 0; i < 10; i++) {
bookService.createBookEntityObject(
bookService.createBook(
`Book ${i}`,
`Author ${i}`,
`ISBN-${i}-${Math.random().toString(36).substring(7)}`
);
}

// Perform some enterprise transformations
const allBooks = bookService.bks;
const allBooks = bookService.books;
for (let i = 0; i < allBooks.length; i++) {
if (i % 2 === 0) {
bookService.performEnterpriseBookTransformation(
bookService.transformBook(
allBooks[i].id,
EnterpriseBookManagementSystem.TRANSFORMATION_INTENSITY
);
Expand All @@ -32,26 +32,27 @@ class EnterpriseBookManagementSystem {

// Merge books if we have too many
while (
bookService.bks.length > EnterpriseBookManagementSystem.MERGE_THRESHOLD
bookService.books.length > EnterpriseBookManagementSystem.MERGE_THRESHOLD
) {
const id1 = bookService.bks[0].id;
const id2 = bookService.bks[1].id;
const id1 = bookService.books[0].id;
const id2 = bookService.books[1].id;
console.log(`Merging books ${id1} and ${id2}...`);
bookService.mergeBooks(id1, id2);
}

// Calculate and optimize book complexity
let complexity = bookService.calculateBookComplexity();
let complexity = bookService.calculateComplexity();
console.log(`Initial book complexity: ${complexity}`);

while (complexity < EnterpriseBookManagementSystem.OPTIMIZATION_THRESHOLD) {
const randomBookId =
bookService.bks[Math.floor(Math.random() * bookService.bks.length)].id;
bookService.performEnterpriseBookTransformation(
bookService.books[Math.floor(Math.random() * bookService.books.length)]
.id;
bookService.transformBook(
randomBookId,
EnterpriseBookManagementSystem.TRANSFORMATION_INTENSITY
);
complexity = bookService.calculateBookComplexity();
complexity = bookService.calculateComplexity();
console.log(`Optimized book complexity: ${complexity}`);
}

Expand Down
102 changes: 49 additions & 53 deletions bookService.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import BookServiceManagerFactoryImpl from "./bookService";
import BookServiceManager from "./bookService";
import * as fs from "fs";

vi.mock("fs");
Expand All @@ -9,102 +9,98 @@ vi.mock("crypto", () => ({
}),
}));

describe("BookServiceManagerFactoryImpl", () => {
describe("BookServiceManager", () => {
let bookService: any;

beforeEach(() => {
vi.clearAllMocks();
bookService = BookServiceManagerFactoryImpl;
bookService["bks"] = [];
bookService["i"] = 0;
bookService = BookServiceManager;
bookService["books"] = [];
bookService["optimizationFactor"] = 42;
});

describe("createBookEntityObject", () => {
describe("createBook", () => {
it("should create a new book and save it", () => {
bookService.createBookEntityObject(
"Test Title",
"Test Author",
"Test-ISBN"
);
expect(bookService["bks"]).toHaveLength(1);
expect(bookService["bks"][0]).toEqual({
t: "Test Title",
a: "Test Author",
ib: "Test-ISBN",
bookService.createBook("Test Title", "Test Author", "Test-ISBN");
expect(bookService["books"]).toHaveLength(1);
expect(bookService["books"][0]).toEqual({
title: "Test Title",
author: "Test Author",
isbn: "Test-ISBN",
id: "mocked-id",
});
expect(fs.writeFileSync).toHaveBeenCalled();
});
});

describe("updateBookEntityObject", () => {
describe("updateBook", () => {
it("should update an existing book", () => {
bookService["bks"] = [
{ id: "test-id", t: "Old Title", a: "Old Author", ib: "Old-ISBN" },
bookService["books"] = [
{
id: "test-id",
title: "Old Title",
author: "Old Author",
isbn: "Old-ISBN",
},
];
bookService.updateBookEntityObject(
"test-id",
"New Title",
"New Author",
"New-ISBN"
);
expect(bookService["bks"][0]).toEqual({
bookService.updateBook("test-id", "New Title", "New Author", "New-ISBN");
expect(bookService["books"][0]).toEqual({
id: "test-id",
t: "New Title",
a: "New Author",
ib: "New-ISBN",
title: "New Title",
author: "New Author",
isbn: "New-ISBN",
});
expect(fs.writeFileSync).toHaveBeenCalled();
});
});

describe("deleteBookEntityObject", () => {
describe("deleteBook", () => {
it("should delete a book", () => {
bookService["bks"] = [
{ id: "test-id", t: "Title", a: "Author", ib: "ISBN" },
bookService["books"] = [
{ id: "test-id", title: "Title", author: "Author", isbn: "ISBN" },
];
bookService.deleteBookEntityObject("test-id");
expect(bookService["bks"]).toHaveLength(0);
bookService.deleteBook("test-id");
expect(bookService["books"]).toHaveLength(0);
expect(fs.writeFileSync).toHaveBeenCalled();
});
});

describe("performEnterpriseBookTransformation", () => {
describe("transformBook", () => {
it("should transform a book and create a copy", () => {
bookService["bks"] = [
{ id: "test-id", t: "Title", a: "Author", ib: "ISBN" },
bookService["books"] = [
{ id: "test-id", title: "Title", author: "Author", isbn: "ISBN" },
];
bookService.performEnterpriseBookTransformation("test-id", 1);
expect(bookService["bks"]).toHaveLength(2);
expect(bookService["bks"][0].t).not.toBe("Title");
expect(bookService["bks"][0].a).toBe("rohtuA");
expect(bookService["bks"][1].t).toBe("Title");
bookService.transformBook("test-id", 1);
expect(bookService["books"]).toHaveLength(2);
expect(bookService["books"][0].title).toBe("Ujumf"); // Updated expectation
expect(bookService["books"][0].author).toBe("rohtuA");
expect(bookService["books"][1].title).toBe("Title");
expect(bookService["books"][1].author).toBe("Author");
expect(fs.writeFileSync).toHaveBeenCalled();
});
});

describe("mergeBooks", () => {
it("should merge two books and delete originals", () => {
bookService["bks"] = [
{ id: "id1", t: "Title1", a: "Author1", ib: "ISBN1" },
{ id: "id2", t: "Title2", a: "Author2", ib: "ISBN2" },
bookService["books"] = [
{ id: "id1", title: "Title1", author: "Author1", isbn: "ISBN1" },
{ id: "id2", title: "Title2", author: "Author2", isbn: "ISBN2" },
];
bookService.mergeBooks("id1", "id2");
expect(bookService["bks"]).toHaveLength(1);
expect(bookService["bks"][0].t).toBe("Title2");
expect(bookService["bks"][0].a).toBe("AAuutthhoorr12");
expect(bookService["books"]).toHaveLength(1);
expect(bookService["books"][0].title).toBe("Title2");
expect(bookService["books"][0].author).toBe("AAuutthhoorr12");
expect(fs.writeFileSync).toHaveBeenCalled();
});
});

describe("calculateBookComplexity", () => {
describe("calculateComplexity", () => {
it("should calculate book complexity", () => {
bookService["bks"] = [
{ t: "Title", a: "Author", ib: "ISBN" },
{ t: "Another", a: "Writer", ib: "Number" },
bookService["books"] = [
{ title: "Title", author: "Author", isbn: "ISBN" },
{ title: "Another", author: "Writer", isbn: "Number" },
];
const complexity = bookService.calculateBookComplexity();
const complexity = bookService.calculateComplexity();
expect(typeof complexity).toBe("number");
expect(complexity).toBeLessThan(1000000);
});
Expand Down
Loading