|
| 1 | +import { vi, afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { v4 as uuidv4 } from 'uuid'; |
| 3 | +import exerciseDb from '../models/exercise.js'; |
| 4 | +import { getSystemClient } from '../db/poolManager.js'; |
| 5 | +import { resolveExerciseIdToUuid } from '../utils/uuidUtils.js'; |
| 6 | +import exerciseRepository from '../models/exerciseRepository.js'; |
| 7 | + |
| 8 | +vi.mock('../db/poolManager', () => ({ |
| 9 | + getClient: vi.fn(), |
| 10 | + getSystemClient: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock('../config/logging', () => ({ |
| 14 | + log: vi.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('../models/exerciseRepository', () => ({ |
| 18 | + default: { |
| 19 | + getExerciseBySourceAndSourceId: vi.fn(), |
| 20 | + }, |
| 21 | +})); |
| 22 | + |
| 23 | +describe('exercise source/source_id scoping', () => { |
| 24 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 25 | + let mockClient: any; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + mockClient = { query: vi.fn(), release: vi.fn() }; |
| 29 | + // @ts-expect-error mock typing |
| 30 | + getSystemClient.mockResolvedValue(mockClient); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + vi.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('exerciseDb.getExerciseBySourceAndSourceId', () => { |
| 38 | + it('filters on user_id when userId is provided', async () => { |
| 39 | + const userId = uuidv4(); |
| 40 | + const rowId = uuidv4(); |
| 41 | + mockClient.query.mockResolvedValueOnce({ |
| 42 | + rows: [ |
| 43 | + { |
| 44 | + id: rowId, |
| 45 | + source: 'free-exercise-db', |
| 46 | + source_id: 'Barbell_Bench_Press', |
| 47 | + user_id: userId, |
| 48 | + images: null, |
| 49 | + }, |
| 50 | + ], |
| 51 | + }); |
| 52 | + |
| 53 | + const result = await exerciseDb.getExerciseBySourceAndSourceId( |
| 54 | + 'free-exercise-db', |
| 55 | + 'Barbell_Bench_Press', |
| 56 | + userId |
| 57 | + ); |
| 58 | + |
| 59 | + expect(result.id).toBe(rowId); |
| 60 | + expect(mockClient.query).toHaveBeenCalledTimes(1); |
| 61 | + const [sql, params] = mockClient.query.mock.calls[0]; |
| 62 | + expect(sql).toContain('AND user_id = $3'); |
| 63 | + expect(params).toEqual([ |
| 64 | + 'free-exercise-db', |
| 65 | + 'Barbell_Bench_Press', |
| 66 | + userId, |
| 67 | + ]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns undefined when the requested user has no copy even if another user does', async () => { |
| 71 | + const userId = uuidv4(); |
| 72 | + mockClient.query.mockResolvedValueOnce({ rows: [] }); |
| 73 | + |
| 74 | + const result = await exerciseDb.getExerciseBySourceAndSourceId( |
| 75 | + 'free-exercise-db', |
| 76 | + 'Barbell_Bench_Press', |
| 77 | + userId |
| 78 | + ); |
| 79 | + |
| 80 | + expect(result).toBeUndefined(); |
| 81 | + const [sql, params] = mockClient.query.mock.calls[0]; |
| 82 | + expect(sql).toContain('AND user_id = $3'); |
| 83 | + expect(params[2]).toBe(userId); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('resolveExerciseIdToUuid', () => { |
| 88 | + it('returns the input unchanged when it is already a UUID', async () => { |
| 89 | + const existingUuid = uuidv4(); |
| 90 | + |
| 91 | + const result = await resolveExerciseIdToUuid(existingUuid, uuidv4()); |
| 92 | + |
| 93 | + expect(result).toBe(existingUuid); |
| 94 | + expect( |
| 95 | + exerciseRepository.getExerciseBySourceAndSourceId |
| 96 | + ).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('forwards the userId to getExerciseBySourceAndSourceId so each user resolves to their own copy', async () => { |
| 100 | + const userId = uuidv4(); |
| 101 | + const resolved = uuidv4(); |
| 102 | + exerciseRepository.getExerciseBySourceAndSourceId.mockResolvedValueOnce({ |
| 103 | + id: resolved, |
| 104 | + }); |
| 105 | + |
| 106 | + const result = await resolveExerciseIdToUuid( |
| 107 | + 'Barbell_Bench_Press', |
| 108 | + userId |
| 109 | + ); |
| 110 | + |
| 111 | + expect(result).toBe(resolved); |
| 112 | + expect( |
| 113 | + exerciseRepository.getExerciseBySourceAndSourceId |
| 114 | + ).toHaveBeenCalledWith('free-exercise-db', 'Barbell_Bench_Press', userId); |
| 115 | + }); |
| 116 | + |
| 117 | + it('throws when no exercise is found for the caller', async () => { |
| 118 | + exerciseRepository.getExerciseBySourceAndSourceId.mockResolvedValueOnce( |
| 119 | + undefined |
| 120 | + ); |
| 121 | + |
| 122 | + await expect( |
| 123 | + resolveExerciseIdToUuid('Barbell_Bench_Press', uuidv4()) |
| 124 | + ).rejects.toThrow(/not found or is not a valid UUID/); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments