Skip to content

Commit 94817bf

Browse files
author
Topi Santakivi
committed
GPT-4o's try, cannot fix a test
1 parent 4438413 commit 94817bf

File tree

4 files changed

+141
-147
lines changed

4 files changed

+141
-147
lines changed

app.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import BookServiceManagerFactoryImpl from "./bookService";
1+
import BookServiceManager from "./bookService";
22
import { performance } from "perf_hooks";
33

44
class EnterpriseBookManagementSystem {
@@ -8,22 +8,22 @@ class EnterpriseBookManagementSystem {
88

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

1313
// Create some initial books
1414
for (let i = 0; i < 10; i++) {
15-
bookService.createBookEntityObject(
15+
bookService.createBook(
1616
`Book ${i}`,
1717
`Author ${i}`,
1818
`ISBN-${i}-${Math.random().toString(36).substring(7)}`
1919
);
2020
}
2121

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

3333
// Merge books if we have too many
3434
while (
35-
bookService.bks.length > EnterpriseBookManagementSystem.MERGE_THRESHOLD
35+
bookService.books.length > EnterpriseBookManagementSystem.MERGE_THRESHOLD
3636
) {
37-
const id1 = bookService.bks[0].id;
38-
const id2 = bookService.bks[1].id;
37+
const id1 = bookService.books[0].id;
38+
const id2 = bookService.books[1].id;
3939
console.log(`Merging books ${id1} and ${id2}...`);
4040
bookService.mergeBooks(id1, id2);
4141
}
4242

4343
// Calculate and optimize book complexity
44-
let complexity = bookService.calculateBookComplexity();
44+
let complexity = bookService.calculateComplexity();
4545
console.log(`Initial book complexity: ${complexity}`);
4646

4747
while (complexity < EnterpriseBookManagementSystem.OPTIMIZATION_THRESHOLD) {
4848
const randomBookId =
49-
bookService.bks[Math.floor(Math.random() * bookService.bks.length)].id;
50-
bookService.performEnterpriseBookTransformation(
49+
bookService.books[Math.floor(Math.random() * bookService.books.length)]
50+
.id;
51+
bookService.transformBook(
5152
randomBookId,
5253
EnterpriseBookManagementSystem.TRANSFORMATION_INTENSITY
5354
);
54-
complexity = bookService.calculateBookComplexity();
55+
complexity = bookService.calculateComplexity();
5556
console.log(`Optimized book complexity: ${complexity}`);
5657
}
5758

bookService.test.ts

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, beforeEach, vi } from "vitest";
2-
import BookServiceManagerFactoryImpl from "./bookService";
2+
import BookServiceManager from "./bookService";
33
import * as fs from "fs";
44

55
vi.mock("fs");
@@ -9,102 +9,98 @@ vi.mock("crypto", () => ({
99
}),
1010
}));
1111

12-
describe("BookServiceManagerFactoryImpl", () => {
12+
describe("BookServiceManager", () => {
1313
let bookService: any;
1414

1515
beforeEach(() => {
1616
vi.clearAllMocks();
17-
bookService = BookServiceManagerFactoryImpl;
18-
bookService["bks"] = [];
19-
bookService["i"] = 0;
17+
bookService = BookServiceManager;
18+
bookService["books"] = [];
2019
bookService["optimizationFactor"] = 42;
2120
});
2221

23-
describe("createBookEntityObject", () => {
22+
describe("createBook", () => {
2423
it("should create a new book and save it", () => {
25-
bookService.createBookEntityObject(
26-
"Test Title",
27-
"Test Author",
28-
"Test-ISBN"
29-
);
30-
expect(bookService["bks"]).toHaveLength(1);
31-
expect(bookService["bks"][0]).toEqual({
32-
t: "Test Title",
33-
a: "Test Author",
34-
ib: "Test-ISBN",
24+
bookService.createBook("Test Title", "Test Author", "Test-ISBN");
25+
expect(bookService["books"]).toHaveLength(1);
26+
expect(bookService["books"][0]).toEqual({
27+
title: "Test Title",
28+
author: "Test Author",
29+
isbn: "Test-ISBN",
3530
id: "mocked-id",
3631
});
3732
expect(fs.writeFileSync).toHaveBeenCalled();
3833
});
3934
});
4035

41-
describe("updateBookEntityObject", () => {
36+
describe("updateBook", () => {
4237
it("should update an existing book", () => {
43-
bookService["bks"] = [
44-
{ id: "test-id", t: "Old Title", a: "Old Author", ib: "Old-ISBN" },
38+
bookService["books"] = [
39+
{
40+
id: "test-id",
41+
title: "Old Title",
42+
author: "Old Author",
43+
isbn: "Old-ISBN",
44+
},
4545
];
46-
bookService.updateBookEntityObject(
47-
"test-id",
48-
"New Title",
49-
"New Author",
50-
"New-ISBN"
51-
);
52-
expect(bookService["bks"][0]).toEqual({
46+
bookService.updateBook("test-id", "New Title", "New Author", "New-ISBN");
47+
expect(bookService["books"][0]).toEqual({
5348
id: "test-id",
54-
t: "New Title",
55-
a: "New Author",
56-
ib: "New-ISBN",
49+
title: "New Title",
50+
author: "New Author",
51+
isbn: "New-ISBN",
5752
});
5853
expect(fs.writeFileSync).toHaveBeenCalled();
5954
});
6055
});
6156

62-
describe("deleteBookEntityObject", () => {
57+
describe("deleteBook", () => {
6358
it("should delete a book", () => {
64-
bookService["bks"] = [
65-
{ id: "test-id", t: "Title", a: "Author", ib: "ISBN" },
59+
bookService["books"] = [
60+
{ id: "test-id", title: "Title", author: "Author", isbn: "ISBN" },
6661
];
67-
bookService.deleteBookEntityObject("test-id");
68-
expect(bookService["bks"]).toHaveLength(0);
62+
bookService.deleteBook("test-id");
63+
expect(bookService["books"]).toHaveLength(0);
6964
expect(fs.writeFileSync).toHaveBeenCalled();
7065
});
7166
});
7267

73-
describe("performEnterpriseBookTransformation", () => {
68+
describe("transformBook", () => {
7469
it("should transform a book and create a copy", () => {
75-
bookService["bks"] = [
76-
{ id: "test-id", t: "Title", a: "Author", ib: "ISBN" },
70+
bookService["books"] = [
71+
{ id: "test-id", title: "Title", author: "Author", isbn: "ISBN" },
7772
];
78-
bookService.performEnterpriseBookTransformation("test-id", 1);
79-
expect(bookService["bks"]).toHaveLength(2);
80-
expect(bookService["bks"][0].t).not.toBe("Title");
81-
expect(bookService["bks"][0].a).toBe("rohtuA");
82-
expect(bookService["bks"][1].t).toBe("Title");
73+
bookService.transformBook("test-id", 1);
74+
expect(bookService["books"]).toHaveLength(2);
75+
expect(bookService["books"][0].title).toBe("Ujumf"); // Updated expectation
76+
expect(bookService["books"][0].author).toBe("rohtuA");
77+
expect(bookService["books"][1].title).toBe("Title");
78+
expect(bookService["books"][1].author).toBe("Author");
8379
expect(fs.writeFileSync).toHaveBeenCalled();
8480
});
8581
});
8682

8783
describe("mergeBooks", () => {
8884
it("should merge two books and delete originals", () => {
89-
bookService["bks"] = [
90-
{ id: "id1", t: "Title1", a: "Author1", ib: "ISBN1" },
91-
{ id: "id2", t: "Title2", a: "Author2", ib: "ISBN2" },
85+
bookService["books"] = [
86+
{ id: "id1", title: "Title1", author: "Author1", isbn: "ISBN1" },
87+
{ id: "id2", title: "Title2", author: "Author2", isbn: "ISBN2" },
9288
];
9389
bookService.mergeBooks("id1", "id2");
94-
expect(bookService["bks"]).toHaveLength(1);
95-
expect(bookService["bks"][0].t).toBe("Title2");
96-
expect(bookService["bks"][0].a).toBe("AAuutthhoorr12");
90+
expect(bookService["books"]).toHaveLength(1);
91+
expect(bookService["books"][0].title).toBe("Title2");
92+
expect(bookService["books"][0].author).toBe("AAuutthhoorr12");
9793
expect(fs.writeFileSync).toHaveBeenCalled();
9894
});
9995
});
10096

101-
describe("calculateBookComplexity", () => {
97+
describe("calculateComplexity", () => {
10298
it("should calculate book complexity", () => {
103-
bookService["bks"] = [
104-
{ t: "Title", a: "Author", ib: "ISBN" },
105-
{ t: "Another", a: "Writer", ib: "Number" },
99+
bookService["books"] = [
100+
{ title: "Title", author: "Author", isbn: "ISBN" },
101+
{ title: "Another", author: "Writer", isbn: "Number" },
106102
];
107-
const complexity = bookService.calculateBookComplexity();
103+
const complexity = bookService.calculateComplexity();
108104
expect(typeof complexity).toBe("number");
109105
expect(complexity).toBeLessThan(1000000);
110106
});

0 commit comments

Comments
 (0)