Files
minstrel/web/src/lib/api/errors.test.ts
T
bvandeusenandClaude Opus 5.5 ad75f3ac7e
release / go (push) Successful in 1m39s
release / web (push) Successful in 1m15s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m3s
release / android (push) Successful in 4m46s
release / Build signed APK (push) Successful in 5m5s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 26s
release / Verify release artifacts (tag releases only) (push) Skipped
chore: comments and tests explain themselves, and the dev compose drops its healthcheck
Test names, assertion messages and comments no longer cite internal
task or rule numbers; each says the reason itself. The dev compose
starts Minstrel after the database without a healthcheck to gate on,
since Minstrel waits for its database at startup on its own. The
published-names test is removed; that check is a local grep before
publishing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 18:58:58 -05:00

93 lines
3.3 KiB
TypeScript

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
);
});
});