test: add vitest unit tests across packages
Cover shared zod schemas, extract traversal/refs/downloads, and tts filename/error handling. Document the test setup in the README and docs.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { getFileName } from './index.js';
|
||||
import { ItemNotFoundError, NoFileError, SteamApiError, TtsError } from './errors.js';
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('getFileName', () => {
|
||||
it('parses a quoted content-disposition filename', () => {
|
||||
expect(
|
||||
getFileName('https://example.com/save', 'attachment; filename="mod.json"'),
|
||||
).toBe('mod.json');
|
||||
});
|
||||
|
||||
it('parses an unquoted filename', () => {
|
||||
expect(
|
||||
getFileName('https://example.com/save', 'attachment; filename=mod.json'),
|
||||
).toBe('mod.json');
|
||||
});
|
||||
|
||||
it('falls back to the URL path when there is no disposition', () => {
|
||||
expect(getFileName('https://example.com/files/mod.json', null)).toBe(
|
||||
'mod.json',
|
||||
);
|
||||
});
|
||||
|
||||
it('falls back to a default when the URL has no path', () => {
|
||||
expect(getFileName('https://example.com', null)).toBe('save.json');
|
||||
});
|
||||
});
|
||||
|
||||
describe('error classes', () => {
|
||||
it('ItemNotFoundError carries a 404 status', () => {
|
||||
const err = new ItemNotFoundError('123');
|
||||
expect(err).toBeInstanceOf(TtsError);
|
||||
expect(err.status).toBe(404);
|
||||
expect(err.message).toContain('123');
|
||||
});
|
||||
|
||||
it('NoFileError carries a 404 status', () => {
|
||||
const err = new NoFileError('123');
|
||||
expect(err.status).toBe(404);
|
||||
expect(err.message).toContain('123');
|
||||
});
|
||||
|
||||
it('SteamApiError defaults to a 502 status', () => {
|
||||
const err = new SteamApiError('boom');
|
||||
expect(err.status).toBe(502);
|
||||
});
|
||||
|
||||
it('TtsError defaults to a 500 status', () => {
|
||||
const err = new TtsError('boom');
|
||||
expect(err.status).toBe(500);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user