import { describe, expect, test } from 'vitest'; import { errCode, errMessage } from './errors'; import { ERROR_COPY } from './error-copy'; describe('errCode', () => { test('returns code from object', () => { expect(errCode({ code: 'foo' })).toBe('foo'); }); test("returns 'unknown' for null", () => { expect(errCode(null)).toBe('unknown'); }); test("returns 'unknown' for undefined", () => { expect(errCode(undefined)).toBe('unknown'); }); test("returns 'unknown' for non-object values", () => { expect(errCode('boom')).toBe('unknown'); expect(errCode(42)).toBe('unknown'); expect(errCode(true)).toBe('unknown'); }); test('handles missing code field', () => { expect(errCode({})).toBe('unknown'); expect(errCode({ message: 'hi' })).toBe('unknown'); }); }); describe('errMessage', () => { test('returns mapped copy when code is known', () => { const expected = ERROR_COPY.track_not_found; expect(errMessage({ code: 'track_not_found' })).toBe(expected); }); test('returns the unknown-fallback copy when code is missing', () => { const result = errMessage({ code: 'definitely_not_a_real_code_xyz' }); expect(result).toBeTruthy(); expect(typeof result).toBe('string'); expect(result.length).toBeGreaterThan(0); }); test('accepts custom fallback parameter without throwing', () => { // copyForCode itself returns a non-empty string for any input, so the // explicit fallback only matters in narrow cases. Just exercise the // signature. const result = errMessage(null, 'custom fallback'); expect(typeof result).toBe('string'); expect(result.length).toBeGreaterThan(0); }); }); describe('errMessage detail codes', () => { const detail = 'Minstrel runs as uid 1000, gid 1000 and cannot delete from /music/A (read-only file system). ' + 'The library mount must be writable by that user. Nothing was deleted.'; test('library_not_writable appends the server message to the copy', () => { expect(errMessage({ code: 'library_not_writable', message: detail })).toBe( `${ERROR_COPY.library_not_writable} ${detail}` ); }); test('file_delete_failed appends the server message to the copy', () => { const msg = 'Could not delete /music/A/01.mp3 (input/output error). Nothing was deleted.'; expect(errMessage({ code: 'file_delete_failed', message: msg })).toBe( `${ERROR_COPY.file_delete_failed} ${msg}` ); }); test('a detail code with no message shows the copy alone', () => { expect(errMessage({ code: 'library_not_writable' })).toBe(ERROR_COPY.library_not_writable); expect(errMessage({ code: 'library_not_writable', message: ' ' })).toBe( ERROR_COPY.library_not_writable ); }); test('invalid_setting appends the field and range the server names', () => { const msg = 'fingerprint setting out of range: chromaprint_length_sec must be 30-600'; expect(errMessage({ code: 'invalid_setting', message: msg })).toBe( `${ERROR_COPY.invalid_setting} ${msg}` ); }); // Server messages are usually internal detail. Appending them for every code // would leak things like driver errors into toasts; this pins the scope. test('other codes never carry the server message', () => { expect(errMessage({ code: 'track_not_found', message: 'pgx: no rows in result set' })).toBe( ERROR_COPY.track_not_found ); }); });