/ clis / web / read.test.js
read.test.js
 1  import { beforeEach, describe, expect, it, vi } from 'vitest';
 2  
 3  const { mockDownloadArticle } = vi.hoisted(() => ({
 4      mockDownloadArticle: vi.fn(),
 5  }));
 6  
 7  vi.mock('@jackwener/opencli/download/article-download', () => ({
 8      downloadArticle: mockDownloadArticle,
 9  }));
10  
11  const { __test__ } = await import('./read.js');
12  
13  describe('web/read stdout behavior', () => {
14      const read = __test__.command;
15      const page = {
16          goto: vi.fn().mockResolvedValue(undefined),
17          wait: vi.fn().mockResolvedValue(undefined),
18          evaluate: vi.fn().mockResolvedValue({
19              title: 'Example Article',
20              author: 'Author',
21              publishTime: '2026-04-22',
22              contentHtml: '<p>hello</p>',
23              imageUrls: ['https://example.com/a.jpg'],
24          }),
25      };
26  
27      beforeEach(() => {
28          mockDownloadArticle.mockReset();
29          mockDownloadArticle.mockResolvedValue([{
30              title: 'Example Article',
31              author: 'Author',
32              publish_time: '2026-04-22',
33              status: 'success',
34              size: '1 KB',
35              saved: '-',
36          }]);
37          page.goto.mockClear();
38          page.wait.mockClear();
39          page.evaluate.mockClear();
40      });
41  
42      it('returns null in --stdout mode so the CLI does not append result rows to stdout', async () => {
43          const result = await read.func(page, {
44              url: 'https://example.com/article',
45              output: '/tmp/out',
46              'download-images': false,
47              stdout: true,
48          });
49  
50          expect(result).toBeNull();
51          expect(mockDownloadArticle).toHaveBeenCalledWith(
52              expect.objectContaining({
53                  title: 'Example Article',
54                  sourceUrl: 'https://example.com/article',
55              }),
56              expect.objectContaining({
57                  output: '/tmp/out',
58                  stdout: true,
59              }),
60          );
61      });
62  
63      it('still returns the saved-row payload when writing to disk', async () => {
64          const rows = [{ title: 'Example Article', saved: '/tmp/out/Example Article/example.md' }];
65          mockDownloadArticle.mockResolvedValue(rows);
66  
67          const result = await read.func(page, {
68              url: 'https://example.com/article',
69              output: '/tmp/out',
70              'download-images': false,
71              stdout: false,
72          });
73  
74          expect(result).toBe(rows);
75      });
76  });