/ clis / weread / utils.test.js
utils.test.js
  1  import { describe, it, expect, vi, beforeEach } from 'vitest';
  2  import { buildWebShelfEntries, formatDate, fetchWebApi } from './utils.js';
  3  describe('formatDate', () => {
  4      it('formats a typical Unix timestamp in UTC+8', () => {
  5          // 1705276800 = 2024-01-15 00:00:00 UTC = 2024-01-15 08:00:00 Beijing
  6          expect(formatDate(1705276800)).toBe('2024-01-15');
  7      });
  8      it('handles UTC midnight edge case with UTC+8 offset', () => {
  9          // 1705190399 = 2024-01-13 23:59:59 UTC = 2024-01-14 07:59:59 Beijing
 10          expect(formatDate(1705190399)).toBe('2024-01-14');
 11      });
 12      it('returns dash for zero', () => {
 13          expect(formatDate(0)).toBe('-');
 14      });
 15      it('returns dash for negative', () => {
 16          expect(formatDate(-1)).toBe('-');
 17      });
 18      it('returns dash for NaN', () => {
 19          expect(formatDate(NaN)).toBe('-');
 20      });
 21      it('returns dash for Infinity', () => {
 22          expect(formatDate(Infinity)).toBe('-');
 23      });
 24      it('returns dash for undefined', () => {
 25          expect(formatDate(undefined)).toBe('-');
 26      });
 27      it('returns dash for null', () => {
 28          expect(formatDate(null)).toBe('-');
 29      });
 30  });
 31  describe('fetchWebApi', () => {
 32      beforeEach(() => {
 33          vi.restoreAllMocks();
 34      });
 35      it('returns parsed JSON for successful response', async () => {
 36          vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
 37              ok: true,
 38              json: () => Promise.resolve({ books: [{ title: 'Test' }] }),
 39          }));
 40          const result = await fetchWebApi('/search/global', { keyword: 'test' });
 41          expect(result).toEqual({ books: [{ title: 'Test' }] });
 42      });
 43      it('throws CliError on HTTP error', async () => {
 44          vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
 45              ok: false,
 46              status: 403,
 47              json: () => Promise.resolve({}),
 48          }));
 49          await expect(fetchWebApi('/search/global')).rejects.toThrow('HTTP 403');
 50      });
 51      it('throws PARSE_ERROR on non-JSON response', async () => {
 52          vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
 53              ok: true,
 54              json: () => Promise.reject(new SyntaxError('Unexpected token <')),
 55          }));
 56          await expect(fetchWebApi('/search/global')).rejects.toThrow('Invalid JSON');
 57      });
 58  });
 59  describe('buildWebShelfEntries', () => {
 60      it('keeps mixed shelf item reader urls aligned when shelf indexes include non-book roles', () => {
 61          const result = buildWebShelfEntries({
 62              cacheFound: true,
 63              rawBooks: [
 64                  { bookId: 'MP_WXS_1', title: '公众号文章一', author: '作者甲' },
 65                  { bookId: 'BOOK_2', title: '普通书二', author: '作者乙' },
 66                  { bookId: 'MP_WXS_3', title: '公众号文章三', author: '作者丙' },
 67              ],
 68              shelfIndexes: [
 69                  { bookId: 'MP_WXS_1', idx: 0, role: 'mp' },
 70                  { bookId: 'BOOK_2', idx: 1, role: 'book' },
 71                  { bookId: 'MP_WXS_3', idx: 2, role: 'mp' },
 72              ],
 73          }, [
 74              'https://weread.qq.com/web/reader/mp1',
 75              'https://weread.qq.com/web/reader/book2',
 76              'https://weread.qq.com/web/reader/mp3',
 77          ]);
 78          expect(result).toEqual([
 79              {
 80                  bookId: 'MP_WXS_1',
 81                  title: '公众号文章一',
 82                  author: '作者甲',
 83                  readerUrl: 'https://weread.qq.com/web/reader/mp1',
 84              },
 85              {
 86                  bookId: 'BOOK_2',
 87                  title: '普通书二',
 88                  author: '作者乙',
 89                  readerUrl: 'https://weread.qq.com/web/reader/book2',
 90              },
 91              {
 92                  bookId: 'MP_WXS_3',
 93                  title: '公众号文章三',
 94                  author: '作者丙',
 95                  readerUrl: 'https://weread.qq.com/web/reader/mp3',
 96              },
 97          ]);
 98      });
 99      it('falls back to raw cache order when shelf indexes are incomplete', () => {
100          const result = buildWebShelfEntries({
101              cacheFound: true,
102              rawBooks: [
103                  { bookId: 'BOOK_1', title: '第一本', author: '作者甲' },
104                  { bookId: 'BOOK_2', title: '第二本', author: '作者乙' },
105              ],
106              shelfIndexes: [
107                  { bookId: 'BOOK_2', idx: 0, role: 'book' },
108              ],
109          }, [
110              'https://weread.qq.com/web/reader/book1',
111              'https://weread.qq.com/web/reader/book2',
112          ]);
113          expect(result).toEqual([
114              {
115                  bookId: 'BOOK_1',
116                  title: '第一本',
117                  author: '作者甲',
118                  readerUrl: 'https://weread.qq.com/web/reader/book1',
119              },
120              {
121                  bookId: 'BOOK_2',
122                  title: '第二本',
123                  author: '作者乙',
124                  readerUrl: 'https://weread.qq.com/web/reader/book2',
125              },
126          ]);
127      });
128  });