|
| 1 | +let selectCallCount = 0; |
| 2 | +const selectSpy = jest.fn(); |
| 3 | +const queryResults: unknown[][] = []; |
| 4 | + |
| 5 | +function createChain(resolveIndex: number) { |
| 6 | + const resolver = () => queryResults[resolveIndex] ?? []; |
| 7 | + const chain: Record<string, unknown> = {}; |
| 8 | + for (const m of ['select', 'from', 'where', 'innerJoin', 'leftJoin', 'limit', 'orderBy']) { |
| 9 | + chain[m] = jest.fn(() => chain); |
| 10 | + } |
| 11 | + chain.then = (onFulfill: (v: unknown) => unknown, onReject?: (e: unknown) => unknown) => |
| 12 | + Promise.resolve(resolver()).then(onFulfill, onReject); |
| 13 | + return chain; |
| 14 | +} |
| 15 | + |
| 16 | +jest.mock('@wxyc/database', () => ({ |
| 17 | + db: { |
| 18 | + select: (...args: unknown[]) => { |
| 19 | + selectSpy(...args); |
| 20 | + return createChain(selectCallCount++); |
| 21 | + }, |
| 22 | + }, |
| 23 | + show_djs: { dj_id: 'dj_id', show_id: 'show_id' }, |
| 24 | + shows: { id: 'id', specialty_id: 'specialty_id' }, |
| 25 | + specialty_shows: { id: 'id', specialty_name: 'specialty_name' }, |
| 26 | + flowsheet: { show_id: 'show_id', message: 'message' }, |
| 27 | + user: { id: 'id', djName: 'djName' }, |
| 28 | + bins: {}, |
| 29 | + library: {}, |
| 30 | + artists: {}, |
| 31 | + format: {}, |
| 32 | + genres: {}, |
| 33 | +})); |
| 34 | + |
| 35 | +jest.mock('drizzle-orm', () => ({ |
| 36 | + eq: jest.fn((a, b) => ({ eq: [a, b] })), |
| 37 | + and: jest.fn((...args: unknown[]) => ({ and: args })), |
| 38 | + isNull: jest.fn((col) => ({ isNull: col })), |
| 39 | + inArray: jest.fn((col, vals) => ({ inArray: [col, vals] })), |
| 40 | +})); |
| 41 | + |
| 42 | +import { getPlaylistsForDJ } from '../../../apps/backend/services/djs.service'; |
| 43 | + |
| 44 | +const DJ_ID = 'dj-1'; |
| 45 | + |
| 46 | +function makeShowDjRows(count: number) { |
| 47 | + return Array.from({ length: count }, (_, i) => ({ |
| 48 | + show_id: i + 1, |
| 49 | + dj_id: DJ_ID, |
| 50 | + active: true, |
| 51 | + })); |
| 52 | +} |
| 53 | + |
| 54 | +function makeShowRows(count: number) { |
| 55 | + return Array.from({ length: count }, (_, i) => ({ |
| 56 | + id: i + 1, |
| 57 | + primary_dj_id: DJ_ID, |
| 58 | + specialty_id: i === 0 ? 10 : null, |
| 59 | + show_name: `Show ${i + 1}`, |
| 60 | + start_time: new Date(`2024-01-${String(i + 1).padStart(2, '0')}T20:00:00Z`), |
| 61 | + end_time: null, |
| 62 | + })); |
| 63 | +} |
| 64 | + |
| 65 | +function makeDjsForShows(count: number) { |
| 66 | + return Array.from({ length: count }, (_, i) => ({ |
| 67 | + dj_id: DJ_ID, |
| 68 | + dj_name: 'DJ One', |
| 69 | + show_id: i + 1, |
| 70 | + })); |
| 71 | +} |
| 72 | + |
| 73 | +function makeFlowsheetRows(showCount: number) { |
| 74 | + const entries = []; |
| 75 | + for (let s = 0; s < showCount; s++) { |
| 76 | + for (let e = 0; e < 4; e++) { |
| 77 | + entries.push({ |
| 78 | + id: s * 4 + e + 1, |
| 79 | + show_id: s + 1, |
| 80 | + album_id: null, |
| 81 | + rotation_id: null, |
| 82 | + entry_type: 'track', |
| 83 | + track_title: `Track ${e + 1}`, |
| 84 | + album_title: `Album ${e + 1}`, |
| 85 | + artist_name: `Artist ${e + 1}`, |
| 86 | + record_label: null, |
| 87 | + play_order: e + 1, |
| 88 | + request_flag: false, |
| 89 | + message: null, |
| 90 | + add_time: new Date(), |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + return entries; |
| 95 | +} |
| 96 | + |
| 97 | +describe('djs.service - getPlaylistsForDJ', () => { |
| 98 | + beforeEach(() => { |
| 99 | + selectCallCount = 0; |
| 100 | + selectSpy.mockClear(); |
| 101 | + queryResults.length = 0; |
| 102 | + }); |
| 103 | + |
| 104 | + const NUM_SHOWS = 5; |
| 105 | + const MAX_ALLOWED_SELECTS = 6; |
| 106 | + |
| 107 | + it(`should make at most ${MAX_ALLOWED_SELECTS} select() calls, not O(N) for ${NUM_SHOWS} shows`, async () => { |
| 108 | + const showDjRows = makeShowDjRows(NUM_SHOWS); |
| 109 | + const showRows = makeShowRows(NUM_SHOWS); |
| 110 | + const djRows = makeDjsForShows(NUM_SHOWS); |
| 111 | + const specialtyRows = [{ id: 10, specialty_name: 'Jazz After Hours' }]; |
| 112 | + const flowsheetRows = makeFlowsheetRows(NUM_SHOWS); |
| 113 | + |
| 114 | + // Provide enough results for both the batched (5 queries) and N+1 (17+ queries) paths. |
| 115 | + // Batched order: [showDjs, shows, allDjs, specialties, flowsheet] |
| 116 | + // N+1 order: [showDjs, show1, djs1, specialty1, fs1, show2, djs2, fs2, ...] |
| 117 | + // We fill enough slots so either code path can run without crashing. |
| 118 | + queryResults.push(showDjRows); // 0: show_djs for DJ |
| 119 | + queryResults.push(showRows); // 1: batched shows / N+1 show[0] |
| 120 | + queryResults.push(djRows); // 2: batched djs / N+1 djs[0] |
| 121 | + queryResults.push(specialtyRows); // 3: batched specialty / N+1 specialty[0] |
| 122 | + queryResults.push(flowsheetRows); // 4: batched flowsheet / N+1 flowsheet[0] |
| 123 | + // Extra slots for N+1 loop iterations (shows 2-5) |
| 124 | + for (let i = 1; i < NUM_SHOWS; i++) { |
| 125 | + queryResults.push([showRows[i]]); // show |
| 126 | + queryResults.push([{ dj_id: DJ_ID, dj_name: 'DJ One' }]); // djs |
| 127 | + if (showRows[i].specialty_id != null) { |
| 128 | + queryResults.push(specialtyRows); |
| 129 | + } |
| 130 | + queryResults.push(flowsheetRows.filter((e) => e.show_id === i + 1)); // flowsheet |
| 131 | + } |
| 132 | + |
| 133 | + await getPlaylistsForDJ(DJ_ID); |
| 134 | + |
| 135 | + const totalSelectCalls = selectSpy.mock.calls.length; |
| 136 | + expect(totalSelectCalls).toBeLessThanOrEqual(MAX_ALLOWED_SELECTS); |
| 137 | + }); |
| 138 | + |
| 139 | + it('returns correct ShowPeek structures', async () => { |
| 140 | + const showDjRows = makeShowDjRows(2); |
| 141 | + const showRows = makeShowRows(2); |
| 142 | + const djRows = makeDjsForShows(2); |
| 143 | + const specialtyRows = [{ id: 10, specialty_name: 'Jazz After Hours' }]; |
| 144 | + const flowsheetRows = makeFlowsheetRows(2); |
| 145 | + |
| 146 | + queryResults.push(showDjRows); |
| 147 | + queryResults.push(showRows); |
| 148 | + queryResults.push(djRows); |
| 149 | + queryResults.push(specialtyRows); |
| 150 | + queryResults.push(flowsheetRows); |
| 151 | + // N+1 fallback slots |
| 152 | + queryResults.push([showRows[1]]); |
| 153 | + queryResults.push([{ dj_id: DJ_ID, dj_name: 'DJ One' }]); |
| 154 | + queryResults.push(flowsheetRows.filter((e) => e.show_id === 2)); |
| 155 | + |
| 156 | + const result = await getPlaylistsForDJ(DJ_ID); |
| 157 | + |
| 158 | + expect(result).toHaveLength(2); |
| 159 | + expect(result[0]).toHaveProperty('show'); |
| 160 | + expect(result[0]).toHaveProperty('show_name'); |
| 161 | + expect(result[0]).toHaveProperty('date'); |
| 162 | + expect(result[0]).toHaveProperty('djs'); |
| 163 | + expect(result[0]).toHaveProperty('specialty_show'); |
| 164 | + expect(result[0]).toHaveProperty('preview'); |
| 165 | + }); |
| 166 | +}); |
0 commit comments