javdict 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,55 +1,55 @@
1
- import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
- import { display } from '../lib/display.js';
3
-
4
- const MOCK_INFO = {
5
- id: 'SSIS-001',
6
- title: '测试标题',
7
- actresses: ['天使もえ'],
8
- actors: ['男优A'],
9
- releaseDate: '2021-01-01',
10
- duration: '120分钟',
11
- studio: 'SOD Create',
12
- label: 'SOD',
13
- director: '导演A',
14
- series: '系列A',
15
- tags: ['独占', '美少女'],
16
- coverUrl: 'https://example.com/cover.jpg',
17
- score: '4.5分',
18
- };
19
-
20
- describe('display.js', () => {
21
- beforeEach(() => {
22
- vi.spyOn(console, 'log').mockImplementation(() => {});
23
- });
24
-
25
- afterEach(() => {
26
- vi.restoreAllMocks();
27
- });
28
-
29
- it('display:正常调用不抛出异常', () => {
30
- expect(() => display(MOCK_INFO)).not.toThrow();
31
- });
32
-
33
- it('display:输出内容包含番号', () => {
34
- display(MOCK_INFO);
35
- const allOutput = console.log.mock.calls.flat().join(' ');
36
- expect(allOutput).toContain('SSIS-001');
37
- });
38
-
39
- it('display:输出内容包含女优名', () => {
40
- display(MOCK_INFO);
41
- const allOutput = console.log.mock.calls.flat().join(' ');
42
- expect(allOutput).toContain('天使もえ');
43
- });
44
-
45
- it('display:raw 模式输出 JSON 字符串', () => {
46
- display(MOCK_INFO, true);
47
- const allOutput = console.log.mock.calls.flat().join(' ');
48
- expect(() => JSON.parse(allOutput)).not.toThrow();
49
- });
50
-
51
- it('display:空字段不影响正常输出', () => {
52
- const sparseInfo = { ...MOCK_INFO, actors: [], tags: [], coverUrl: '' };
53
- expect(() => display(sparseInfo)).not.toThrow();
54
- });
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import { display } from '../lib/display.js';
3
+
4
+ const MOCK_INFO = {
5
+ id: 'SSIS-001',
6
+ title: '测试标题',
7
+ actresses: ['天使もえ'],
8
+ actors: ['男优A'],
9
+ releaseDate: '2021-01-01',
10
+ duration: '120分钟',
11
+ studio: 'SOD Create',
12
+ label: 'SOD',
13
+ director: '导演A',
14
+ series: '系列A',
15
+ tags: ['独占', '美少女'],
16
+ coverUrl: 'https://example.com/cover.jpg',
17
+ score: '4.5分',
18
+ };
19
+
20
+ describe('display.js', () => {
21
+ beforeEach(() => {
22
+ vi.spyOn(console, 'log').mockImplementation(() => {});
23
+ });
24
+
25
+ afterEach(() => {
26
+ vi.restoreAllMocks();
27
+ });
28
+
29
+ it('display:正常调用不抛出异常', () => {
30
+ expect(() => display(MOCK_INFO)).not.toThrow();
31
+ });
32
+
33
+ it('display:输出内容包含番号', () => {
34
+ display(MOCK_INFO);
35
+ const allOutput = console.log.mock.calls.flat().join(' ');
36
+ expect(allOutput).toContain('SSIS-001');
37
+ });
38
+
39
+ it('display:输出内容包含女优名', () => {
40
+ display(MOCK_INFO);
41
+ const allOutput = console.log.mock.calls.flat().join(' ');
42
+ expect(allOutput).toContain('天使もえ');
43
+ });
44
+
45
+ it('display:raw 模式输出 JSON 字符串', () => {
46
+ display(MOCK_INFO, true);
47
+ const allOutput = console.log.mock.calls.flat().join(' ');
48
+ expect(() => JSON.parse(allOutput)).not.toThrow();
49
+ });
50
+
51
+ it('display:空字段不影响正常输出', () => {
52
+ const sparseInfo = { ...MOCK_INFO, actors: [], tags: [], coverUrl: '' };
53
+ expect(() => display(sparseInfo)).not.toThrow();
54
+ });
55
55
  });
@@ -1,103 +1,131 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
-
3
- // mock execSync,避免真实的 curl 网络请求
4
- vi.mock('child_process', () => ({
5
- execSync: vi.fn(),
6
- spawnSync: vi.fn(),
7
- }));
8
-
9
- // mock cache,避免缓存干扰测试结果
10
- vi.mock('../lib/cache.js', () => ({
11
- getCache: vi.fn().mockReturnValue(null),
12
- setCache: vi.fn(),
13
- getConfig: vi.fn().mockReturnValue({ session: 'mock_session' }),
14
- }));
15
-
16
- import { execSync } from 'child_process';
17
- import { search } from '../lib/fetcher.js';
18
-
19
- // 模拟 JAVBUS 详情页 HTML
20
- const MOCK_JAVBUS_HTML = `
21
- <html>
22
- <head><title>SSIS-001</title></head>
23
- <body>
24
- <div class="container">
25
- <div class="row">
26
- <h3>SSIS-001 测试标题</h3>
27
- <div class="screencap">
28
- <img src="https://example.com/cover.jpg" />
29
- </div>
30
- <div class="info">
31
- <p><span class="header">發行日期:</span> 2021-01-01</p>
32
- <p><span class="header">長度:</span> 150分鐘</p>
33
- <p><span class="header">導演:</span> <a>导演A</a></p>
34
- <p><span class="header">製作商:</span> <a>SOD Create</a></p>
35
- <p><span class="header">發行商:</span> <a>SOD</a></p>
36
- <p><span class="header">系列:</span> <a>系列A</a></p>
37
- </div>
38
- <span class="genre"><a>独占</a></span>
39
- <span class="genre"><a>美少女</a></span>
40
- <div class="star-name"><a>天使もえ</a></div>
41
- </div>
42
- </div>
43
- </body>
44
- </html>
45
- `;
46
-
47
- // 模拟 404 页面
48
- const MOCK_404_HTML = `
49
- <html>
50
- <head><title>404</title></head>
51
- <body></body>
52
- </html>
53
- `;
54
-
55
- describe('fetcher.js', () => {
56
- beforeEach(() => {
57
- vi.clearAllMocks();
58
- });
59
-
60
- it('search:正常番号能返回结构完整的对象', async () => {
61
- execSync.mockReturnValue(Buffer.from(MOCK_JAVBUS_HTML));
62
-
63
- const result = await search('SSIS-001');
64
-
65
- expect(result).not.toBeNull();
66
- expect(result.id).toBe('SSIS-001');
67
- expect(result.source).toBe('JAVBUS');
68
- expect(result.actresses).toContain('天使もえ');
69
- expect(result.releaseDate).toBe('2021-01-01');
70
- expect(result.studio).toBe('SOD Create');
71
- });
72
-
73
- it('search:搜索结果为空时返回 null', async () => {
74
- execSync.mockReturnValue(Buffer.from(MOCK_404_HTML));
75
-
76
- const result = await search('INVALID-999');
77
- expect(result).toBeNull();
78
- });
79
-
80
- it('search:网络请求失败时返回 null', async () => {
81
- execSync.mockImplementation(() => {
82
- throw new Error('Network Error');
83
- });
84
-
85
- const result = await search('SSIS-001');
86
- expect(result).toBeNull();
87
- });
88
-
89
- it('search:返回对象包含所有预期字段', async () => {
90
- execSync.mockReturnValue(Buffer.from(MOCK_JAVBUS_HTML));
91
-
92
- const result = await search('SSIS-001');
93
- const expectedFields = [
94
- 'id', 'title', 'actresses', 'actors',
95
- 'releaseDate', 'duration', 'studio', 'label',
96
- 'director', 'series', 'tags', 'coverUrl', 'score',
97
- ];
98
-
99
- expectedFields.forEach(field => {
100
- expect(result).toHaveProperty(field);
101
- });
102
- });
1
+ // Node 18 提供 Web API polyfill
2
+ import { beforeAll } from 'vitest';
3
+
4
+ beforeAll(() => {
5
+ if (typeof global.File === 'undefined') {
6
+ global.File = class File {
7
+ constructor(bits, name, options = {}) {
8
+ this.bits = bits;
9
+ this.name = name;
10
+ this.type = options.type || '';
11
+ this.size = bits?.length || 0;
12
+ }
13
+ };
14
+ }
15
+
16
+ if (typeof global.FormData === 'undefined') {
17
+ global.FormData = class FormData {
18
+ constructor() {
19
+ this.data = new Map();
20
+ }
21
+ append(key, value) {
22
+ this.data.set(key, value);
23
+ }
24
+ };
25
+ }
26
+ });
27
+
28
+ // =====================================================
29
+
30
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
31
+
32
+ // Mock child_process
33
+ vi.mock('child_process', () => ({
34
+ execSync: vi.fn(),
35
+ spawnSync: vi.fn(),
36
+ }));
37
+
38
+ // Mock cache
39
+ vi.mock('../lib/cache.js', () => ({
40
+ getCache: vi.fn().mockReturnValue(null),
41
+ setCache: vi.fn(),
42
+ getConfig: vi.fn().mockReturnValue({ session: 'mock_session' }),
43
+ }));
44
+
45
+ import { execSync } from 'child_process';
46
+ import { search } from '../lib/fetcher.js';
47
+
48
+ // 模拟 HTML 数据
49
+ const MOCK_JAVBUS_HTML = `
50
+ <html>
51
+ <head><title>SSIS-001</title></head>
52
+ <body>
53
+ <div class="container">
54
+ <div class="row">
55
+ <h3>SSIS-001 测试标题</h3>
56
+ <div class="screencap">
57
+ <img src="https://example.com/cover.jpg" />
58
+ </div>
59
+ <div class="info">
60
+ <p><span class="header">發行日期:</span> 2021-01-01</p>
61
+ <p><span class="header">長度:</span> 150分鐘</p>
62
+ <p><span class="header">導演:</span> <a>导演A</a></p>
63
+ <p><span class="header">製作商:</span> <a>SOD Create</a></p>
64
+ <p><span class="header">發行商:</span> <a>SOD</a></p>
65
+ <p><span class="header">系列:</span> <a>系列A</a></p>
66
+ </div>
67
+ <span class="genre"><a>独占</a></span>
68
+ <span class="genre"><a>美少女</a></span>
69
+ <div class="star-name"><a>天使もえ</a></div>
70
+ </div>
71
+ </div>
72
+ </body>
73
+ </html>
74
+ `;
75
+
76
+ const MOCK_404_HTML = `
77
+ <html>
78
+ <head><title>404</title></head>
79
+ <body></body>
80
+ </html>
81
+ `;
82
+
83
+ describe('fetcher.js', () => {
84
+ beforeEach(() => {
85
+ vi.clearAllMocks();
86
+ });
87
+
88
+ it('search:正常番号能返回结构完整的对象', async () => {
89
+ execSync.mockReturnValue(Buffer.from(MOCK_JAVBUS_HTML));
90
+
91
+ const result = await search('SSIS-001');
92
+
93
+ expect(result).not.toBeNull();
94
+ expect(result.id).toBe('SSIS-001');
95
+ expect(result.source).toBe('JAVBUS');
96
+ expect(result.actresses).toContain('天使もえ');
97
+ expect(result.releaseDate).toBe('2021-01-01');
98
+ expect(result.studio).toBe('SOD Create');
99
+ });
100
+
101
+ it('search:搜索结果为空时返回 null', async () => {
102
+ execSync.mockReturnValue(Buffer.from(MOCK_404_HTML));
103
+
104
+ const result = await search('INVALID-999');
105
+ expect(result).toBeNull();
106
+ });
107
+
108
+ it('search:网络请求失败时返回 null', async () => {
109
+ execSync.mockImplementation(() => {
110
+ throw new Error('Network Error');
111
+ });
112
+
113
+ const result = await search('SSIS-001');
114
+ expect(result).toBeNull();
115
+ });
116
+
117
+ it('search:返回对象包含所有预期字段', async () => {
118
+ execSync.mockReturnValue(Buffer.from(MOCK_JAVBUS_HTML));
119
+
120
+ const result = await search('SSIS-001');
121
+ const expectedFields = [
122
+ 'id', 'title', 'actresses', 'actors',
123
+ 'releaseDate', 'duration', 'studio', 'label',
124
+ 'director', 'series', 'tags', 'coverUrl', 'score',
125
+ ];
126
+
127
+ expectedFields.forEach(field => {
128
+ expect(result).toHaveProperty(field);
129
+ });
130
+ });
103
131
  });
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: 'node',
6
+ include: ['test/**/*.test.js'],
7
+ pool: 'forks',
8
+ },
9
+ });