Files
readest/apps/readest-app/src/__tests__/components/BookCover.test.tsx
T
Huang Xin ae42dcb53a fix(txt): parse author from txt filename and use edited metadata on fallback cover (#4095) (#4102)
When importing a `.txt` file the author field stayed empty unless the
text content itself contained an `作者:…` header, even when the filename
already encoded it. Common Chinese naming patterns like `《书名》作者:张三.txt`,
`《书名》[张三].txt`, or `《书名》张三.txt` now contribute the author when
the file body doesn't.

- Added `extractTxtFilenameMetadata` in `utils/txt.ts` and replaced the
  ad-hoc `extractBookTitle` regex used by both convertSmallFile and
  convertLargeFile. Content-extracted author still wins; the filename
  author is the next fallback before the caller-provided one.
- `BookCover` now reads `book.author || book.metadata?.author` so the
  author typed into the metadata edit dialog shows on auto-generated
  fallback covers when the original `book.author` was empty.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 05:34:56 +02:00

53 lines
1.7 KiB
TypeScript

import { describe, it, expect, vi, afterEach } from 'vitest';
import { render, cleanup } from '@testing-library/react';
import BookCover from '@/components/BookCover';
import { Book } from '@/types/book';
vi.mock('next/image', () => ({
__esModule: true,
default: (props: Record<string, unknown>) => {
// biome-ignore lint/a11y/useAltText: test mock; alt comes from spread props
return <img {...props} />;
},
}));
afterEach(cleanup);
const makeBook = (overrides?: Partial<Book>): Book =>
({
hash: 'abc123',
title: 'Test Book',
author: 'Test Author',
format: 'epub',
coverImageUrl: 'https://example.com/cover.jpg',
...overrides,
}) as Book;
describe('BookCover', () => {
it('passes loading="lazy" to crop-mode Image', () => {
const { container } = render(<BookCover book={makeBook()} coverFit='crop' />);
const img = container.querySelector('img.cover-image');
expect(img).toBeTruthy();
expect(img?.getAttribute('loading')).toBe('lazy');
});
it('passes loading="lazy" to fit-mode Image', () => {
const { container } = render(<BookCover book={makeBook()} coverFit='fit' />);
const img = container.querySelector('img.cover-image');
expect(img).toBeTruthy();
expect(img?.getAttribute('loading')).toBe('lazy');
});
it('falls back to metadata.author on the fallback cover when book.author is empty', () => {
const book = makeBook({
author: '',
coverImageUrl: undefined,
metadata: { author: 'Edited Author' } as Book['metadata'],
});
const { container } = render(<BookCover book={book} coverFit='crop' />);
const fallback = container.querySelector('.fallback-cover');
expect(fallback?.textContent).toContain('Edited Author');
});
});