-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconcile-events.test.js
More file actions
346 lines (297 loc) Β· 9.18 KB
/
Copy pathreconcile-events.test.js
File metadata and controls
346 lines (297 loc) Β· 9.18 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* Tests for library:reconcile event handling
*
* Verifies that the frontend correctly applies backend reconciliation
* events instead of computing local state.
*/
import { beforeEach, describe, expect, it, vi } from "vitest";
// Minimal library store stub for testing reconcile behavior
function createTestLibraryStore(initialTracks = []) {
const _sectionTracks = [...initialTracks];
const _trackPages = {};
let _dataVersion = 0;
let _cacheCleared = false;
return {
totalTracks: initialTracks.length,
totalDuration: initialTracks.reduce((sum, t) => sum + (t.duration || 0), 0),
currentSection: "all",
_lastRevision: 0,
_sectionTracks,
_trackPages,
_dataVersion,
get filteredTracks() {
return this._sectionTracks;
},
_setSectionTracks(tracks) {
this._sectionTracks.length = 0;
this._sectionTracks.push(...tracks);
},
_clearCache() {
_cacheCleared = true;
},
get cacheCleared() {
return _cacheCleared;
},
_removeFromView(idSet) {
if (this._sectionTracks.length > 0) {
const newTracks = this._sectionTracks.filter((t) => !idSet.has(t.id));
this._setSectionTracks(newTracks);
this._dataVersion++;
} else {
for (const [pageIdx, page] of Object.entries(this._trackPages)) {
this._trackPages[pageIdx] = page.filter((t) => !idSet.has(t.id));
}
this._dataVersion++;
}
this._clearCache();
},
fetchTracks: vi.fn(),
};
}
function createTestQueueStore(initialItems = []) {
return {
items: [...initialItems],
_originalOrder: [...initialItems],
_initializing: false,
_updating: false,
currentIndex: 0,
load: vi.fn(),
};
}
// Simulate the reconcile handler from events.js
function handleLibraryReconcile(library, queue, payload) {
const {
mutation,
affected_sections,
removed_ids,
total_tracks,
total_duration,
revision,
} = payload;
library.totalTracks = total_tracks;
library.totalDuration = total_duration;
library._lastRevision = revision;
if (
(mutation === "delete" || mutation === "dedup") &&
removed_ids.length > 0
) {
const idSet = new Set(removed_ids);
library._removeFromView(idSet);
// Simplified queue cleanup for testing
queue.items = queue.items.filter((t) => !idSet.has(t.id));
} else if (
mutation === "scan_complete" ||
mutation === "delete" ||
mutation === "dedup"
) {
library.fetchTracks();
}
if (
affected_sections.includes("liked") &&
library.currentSection === "liked"
) {
library.fetchTracks();
}
}
describe("Library Reconcile Event Handler", () => {
let library;
let queue;
const sampleTracks = [
{ id: 1, title: "Track 1", duration: 180000 },
{ id: 2, title: "Track 2", duration: 240000 },
{ id: 3, title: "Track 3", duration: 200000 },
{ id: 4, title: "Track 4", duration: 300000 },
];
beforeEach(() => {
library = createTestLibraryStore(sampleTracks);
queue = createTestQueueStore(sampleTracks);
});
describe("delete mutation with specific IDs", () => {
it("applies authoritative totals from backend", () => {
handleLibraryReconcile(library, queue, {
mutation: "delete",
affected_sections: ["all"],
removed_ids: [2, 3],
added_ids: [],
total_tracks: 2,
total_duration: 480000,
revision: 5,
});
expect(library.totalTracks).toBe(2);
expect(library.totalDuration).toBe(480000);
expect(library._lastRevision).toBe(5);
});
it("removes specified tracks from view", () => {
handleLibraryReconcile(library, queue, {
mutation: "delete",
affected_sections: ["all"],
removed_ids: [2, 3],
added_ids: [],
total_tracks: 2,
total_duration: 480000,
revision: 5,
});
const remainingIds = library.filteredTracks.map((t) => t.id);
expect(remainingIds).toEqual([1, 4]);
expect(remainingIds).not.toContain(2);
expect(remainingIds).not.toContain(3);
});
it("removes deleted tracks from queue", () => {
handleLibraryReconcile(library, queue, {
mutation: "delete",
affected_sections: ["all"],
removed_ids: [1, 3],
added_ids: [],
total_tracks: 2,
total_duration: 540000,
revision: 6,
});
const queueIds = queue.items.map((t) => t.id);
expect(queueIds).toEqual([2, 4]);
});
it("does not call fetchTracks for targeted deletes", () => {
handleLibraryReconcile(library, queue, {
mutation: "delete",
affected_sections: ["all"],
removed_ids: [2],
added_ids: [],
total_tracks: 3,
total_duration: 680000,
revision: 7,
});
expect(library.fetchTracks).not.toHaveBeenCalled();
});
});
describe("delete mutation without IDs (bulk)", () => {
it("triggers full refetch", () => {
handleLibraryReconcile(library, queue, {
mutation: "delete",
affected_sections: ["all"],
removed_ids: [],
added_ids: [],
total_tracks: 0,
total_duration: 0,
revision: 8,
});
expect(library.fetchTracks).toHaveBeenCalled();
expect(library.totalTracks).toBe(0);
});
});
describe("scan_complete mutation", () => {
it("triggers full refetch with authoritative totals", () => {
handleLibraryReconcile(library, queue, {
mutation: "scan_complete",
affected_sections: ["all", "added"],
removed_ids: [],
added_ids: [],
total_tracks: 150,
total_duration: 45000000,
revision: 10,
});
expect(library.fetchTracks).toHaveBeenCalled();
expect(library.totalTracks).toBe(150);
expect(library.totalDuration).toBe(45000000);
expect(library._lastRevision).toBe(10);
});
});
describe("dedup mutation", () => {
it("removes deduplicated tracks and updates totals", () => {
handleLibraryReconcile(library, queue, {
mutation: "dedup",
affected_sections: ["all"],
removed_ids: [3, 4],
added_ids: [],
total_tracks: 2,
total_duration: 420000,
revision: 12,
});
const remainingIds = library.filteredTracks.map((t) => t.id);
expect(remainingIds).toEqual([1, 2]);
expect(library.totalTracks).toBe(2);
expect(library.totalDuration).toBe(420000);
});
});
describe("favorite mutation", () => {
it("refreshes liked section when viewing it", () => {
library.currentSection = "liked";
handleLibraryReconcile(library, queue, {
mutation: "favorite_add",
affected_sections: ["liked"],
removed_ids: [],
added_ids: [],
total_tracks: 4,
total_duration: 920000,
revision: 15,
});
expect(library.fetchTracks).toHaveBeenCalled();
});
it("does not refetch when not viewing liked section", () => {
library.currentSection = "all";
handleLibraryReconcile(library, queue, {
mutation: "favorite_add",
affected_sections: ["liked"],
removed_ids: [],
added_ids: [],
total_tracks: 4,
total_duration: 920000,
revision: 15,
});
expect(library.fetchTracks).not.toHaveBeenCalled();
});
});
describe("authoritative totals override local state", () => {
it("overrides even when local and backend disagree", () => {
library.totalTracks = 999;
library.totalDuration = 9999999;
handleLibraryReconcile(library, queue, {
mutation: "delete",
affected_sections: ["all"],
removed_ids: [1],
added_ids: [],
total_tracks: 3,
total_duration: 740000,
revision: 20,
});
expect(library.totalTracks).toBe(3);
expect(library.totalDuration).toBe(740000);
});
});
});
describe("_removeFromView", () => {
it("filters section tracks without touching totals", () => {
const store = createTestLibraryStore([
{ id: 1, title: "A", duration: 100 },
{ id: 2, title: "B", duration: 200 },
{ id: 3, title: "C", duration: 300 },
]);
const originalTotal = store.totalTracks;
const originalDuration = store.totalDuration;
store._removeFromView(new Set([2]));
expect(store.filteredTracks.map((t) => t.id)).toEqual([1, 3]);
// Totals are NOT recomputed by _removeFromView
expect(store.totalTracks).toBe(originalTotal);
expect(store.totalDuration).toBe(originalDuration);
});
it("filters paginated tracks", () => {
const store = createTestLibraryStore([]);
store._sectionTracks.length = 0; // Clear section tracks
store._trackPages[0] = [
{ id: 1, title: "A" },
{ id: 2, title: "B" },
];
store._trackPages[1] = [
{ id: 3, title: "C" },
{ id: 4, title: "D" },
];
store._removeFromView(new Set([2, 3]));
expect(store._trackPages[0].map((t) => t.id)).toEqual([1]);
expect(store._trackPages[1].map((t) => t.id)).toEqual([4]);
});
it("clears cache after removal", () => {
const store = createTestLibraryStore([
{ id: 1, title: "A", duration: 100 },
]);
store._removeFromView(new Set([1]));
expect(store.cacheCleared).toBe(true);
});
});