-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock-test.js
More file actions
147 lines (129 loc) · 5.38 KB
/
mock-test.js
File metadata and controls
147 lines (129 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Mock test for auto-generation logic (without database)
// Demonstrates the algorithm with sample data
function testAutoGenerationLogic() {
console.log('Testing Auto-Generation Logic (Mock Test)\n');
// Sample data: 5 series books + 3 non-series
const books = [
{ id: 1, title: 'Harry Potter 1', series_id: 1, series_name: 'Harry Potter' },
{ id: 2, title: 'Harry Potter 2', series_id: 1, series_name: 'Harry Potter' },
{ id: 3, title: 'Harry Potter 3', series_id: 1, series_name: 'Harry Potter' },
{ id: 4, title: 'LOTR 1', series_id: 2, series_name: 'Lord of the Rings' },
{ id: 5, title: 'LOTR 2', series_id: 2, series_name: 'Lord of the Rings' },
{ id: 6, title: 'Standalone Book 1', series_id: null },
{ id: 7, title: 'Standalone Book 2', series_id: null },
{ id: 8, title: 'Standalone Book 3', series_id: null }
];
// Group books by series
const seriesBooks = {};
const nonSeriesBooks = [];
books.forEach(book => {
if (book.series_id) {
if (!seriesBooks[book.series_id]) {
seriesBooks[book.series_id] = {
seriesName: book.series_name,
books: []
};
}
seriesBooks[book.series_id].books.push(book);
} else {
nonSeriesBooks.push(book);
}
});
console.log('Input Books:');
console.log('- Series Books:', Object.values(seriesBooks).flatMap(s => s.books.map(b => b.title)));
console.log('- Non-Series Books:', nonSeriesBooks.map(b => b.title));
console.log();
// Test different rules
console.log('=== Testing Auto-Generation Rules ===\n');
// Rule 1: Simple concatenation (no interspersing)
console.log('1. Simple concatenation (no interspersing):');
const queue1 = generateQueue(Object.values(seriesBooks), nonSeriesBooks, {
intersperseSeries: false,
seriesInterval: 2,
alternateGenres: false,
prioritizeSeries: true,
maxBooks: 50
});
console.log('Queue:', queue1.map(id => books.find(b => b.id === id).title));
console.log();
// Rule 2: Series interspersing every 2 books
console.log('2. Series interspersing (every 2 series books):');
const queue2 = generateQueue(Object.values(seriesBooks), nonSeriesBooks, {
intersperseSeries: true,
seriesInterval: 2,
alternateGenres: false,
prioritizeSeries: true,
maxBooks: 50
});
console.log('Queue:', queue2.map(id => books.find(b => b.id === id).title));
console.log();
// Rule 3: Series interspersing every 1 book (alternate)
console.log('3. Series interspersing (every 1 series book):');
const queue3 = generateQueue(Object.values(seriesBooks), nonSeriesBooks, {
intersperseSeries: true,
seriesInterval: 1,
alternateGenres: false,
prioritizeSeries: true,
maxBooks: 50
});
console.log('Queue:', queue3.map(id => books.find(b => b.id === id).title));
console.log();
// Rule 4: Limited to 6 books
console.log('4. Limited to 6 books with interspersing:');
const queue4 = generateQueue(Object.values(seriesBooks), nonSeriesBooks, {
intersperseSeries: true,
seriesInterval: 2,
alternateGenres: false,
prioritizeSeries: true,
maxBooks: 6
});
console.log('Queue:', queue4.map(id => books.find(b => b.id === id).title));
console.log();
// Calculate estimates for queue3
const selectedBooks = queue3.map(id => books.find(b => b.id === id));
const totalPages = selectedBooks.reduce((sum, book) => sum + (book.page_count || 300), 0); // Mock page counts
const readingSpeed = 50; // pages per hour
const estimatedHours = Math.ceil(totalPages / readingSpeed);
console.log(`Estimates for Queue 3:`);
console.log(`- Total books: ${selectedBooks.length}`);
console.log(`- Total pages: ${totalPages}`);
console.log(`- Estimated hours (${readingSpeed} pages/hour): ${estimatedHours}`);
console.log(`- Estimated days (8h/day): ${Math.ceil(estimatedHours / 8)}`);
console.log();
console.log('=== Mock Test completed successfully! ===');
}
function generateQueue(seriesList, nonSeriesBooks, rules) {
const queue = [];
if (!rules.intersperseSeries) {
// Simple concatenation: all series books first, then non-series
seriesList.forEach(series => {
series.books.forEach(book => queue.push(book.id));
});
nonSeriesBooks.forEach(book => queue.push(book.id));
} else {
// Advanced interspersing logic
const seriesQueues = seriesList.map(series => [...series.books]);
let seriesIndex = 0;
let nonSeriesIndex = 0;
let seriesBookCount = 0;
while (queue.length < rules.maxBooks && (seriesQueues.some(q => q.length > 0) || nonSeriesIndex < nonSeriesBooks.length)) {
// Add series books according to interval
if (seriesBookCount < rules.seriesInterval && seriesQueues[seriesIndex % seriesQueues.length].length > 0) {
const book = seriesQueues[seriesIndex % seriesQueues.length].shift();
queue.push(book.id);
seriesBookCount++;
seriesIndex++;
} else {
// Add non-series book(s) to break up series
const booksToAdd = rules.alternateGenres ? 1 : Math.min(2, nonSeriesBooks.length - nonSeriesIndex);
for (let i = 0; i < booksToAdd && nonSeriesIndex < nonSeriesBooks.length; i++) {
queue.push(nonSeriesBooks[nonSeriesIndex++].id);
}
seriesBookCount = 0; // Reset counter after inserting non-series books
}
}
}
return queue.slice(0, rules.maxBooks); // Ensure we don't exceed max books
}
// Run the mock test
testAutoGenerationLogic();