Skip to content

Commit 0ab18ff

Browse files
authored
test: refactor tests (#22677)
* test: refactor tests * test: run *.worker.ts unit tests inside workerd
1 parent b7e706d commit 0ab18ff

51 files changed

Lines changed: 1714 additions & 1986 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ jobs:
5050
run: pnpm run vitest:coverage --reporter=github-actions
5151
env:
5252
REDIS_URL: redis://localhost:${{ job.services.redis.ports['6379'] }}/
53+
- name: Test worker runtime (workerd)
54+
run: pnpm run vitest:workerd
5355
- name: Upload coverage to Codecov
5456
if: ${{ matrix.node-version == 'lts/*' }}
5557
uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0

lib/api/namespace/all.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { Next } from 'hono';
2+
import { describe, expect, it } from 'vitest';
3+
4+
import { handler as allHandler } from '@/api/namespace/all';
5+
import { namespaces } from '@/registry';
6+
7+
const noopNext: Next = () => Promise.resolve();
8+
9+
const createCtx = (param: Record<string, string> = {}) =>
10+
({
11+
req: {
12+
valid: () => param,
13+
},
14+
json: (data: unknown) => data,
15+
}) as any;
16+
17+
describe('api/namespace/all', () => {
18+
it('returns all namespaces', async () => {
19+
const result = await allHandler(createCtx(), noopNext);
20+
expect(result).toBe(namespaces);
21+
});
22+
});
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { Next } from 'hono';
22
import { describe, expect, it } from 'vitest';
33

44
import api from '@/api';
5-
import { handler as allHandler } from '@/api/namespace/all';
65
import { handler as oneHandler } from '@/api/namespace/one';
76
import { namespaces } from '@/registry';
87

@@ -16,14 +15,9 @@ const createCtx = (param: Record<string, string> = {}) =>
1615
json: (data: unknown) => data,
1716
}) as any;
1817

19-
describe('api/namespace', () => {
18+
describe('api/namespace/one', () => {
2019
const nestedKey = Object.keys(namespaces).find((key) => key.includes('/')) as string;
2120

22-
it('returns all namespaces', async () => {
23-
const result = await allHandler(createCtx(), noopNext);
24-
expect(result).toBe(namespaces);
25-
});
26-
2721
it('returns a single namespace', async () => {
2822
const result = await oneHandler(createCtx({ namespace: 'test' }), noopNext);
2923
expect(result).toBe(namespaces.test);

lib/app.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import Parser from 'rss-parser';
12
import undici from 'undici';
23
import { describe, expect, it, vi } from 'vitest';
34

45
import app from '@/app';
6+
import { config } from '@/config';
57

68
describe('index', () => {
9+
it('exports app entrypoint', () => {
10+
expect(typeof app.request).toBe('function');
11+
});
12+
713
it('serve index', async () => {
814
const res = await app.request('/');
915
expect(res.status).toBe(200);
@@ -21,3 +27,84 @@ describe('request-rewriter', () => {
2127
expect(headers.get('user-agent')).toMatch(/Chrome/);
2228
});
2329
});
30+
31+
const parser = new Parser();
32+
33+
process.env.ALLOW_USER_SUPPLY_UNSAFE_DOMAIN = 'true';
34+
35+
const routes = {
36+
'/test/:id': '/test/1',
37+
};
38+
if (process.env.FULL_ROUTES_TEST) {
39+
const { namespaces } = await import('@/registry');
40+
for (const namespace in namespaces) {
41+
for (const route in namespaces[namespace].routes) {
42+
const requireConfig = namespaces[namespace].routes[route].features?.requireConfig;
43+
let configs;
44+
if (typeof requireConfig !== 'boolean') {
45+
configs = requireConfig
46+
?.filter((config) => !config.optional)
47+
.map((config) => config.name)
48+
.filter((name) => name !== 'ALLOW_USER_SUPPLY_UNSAFE_DOMAIN');
49+
}
50+
if (namespaces[namespace].routes[route].example && !configs?.length) {
51+
routes[`/${namespace}${route}`] = namespaces[namespace].routes[route].example;
52+
}
53+
}
54+
}
55+
}
56+
57+
async function checkRSS(response) {
58+
const checkDate = (date) => {
59+
expect(date).toEqual(expect.any(String));
60+
expect(Date.parse(date)).toEqual(expect.any(Number));
61+
expect(Date.now() - +new Date(date)).toBeGreaterThan(-1000 * 60 * 60 * 24 * 5);
62+
expect(Date.now() - +new Date(date)).toBeLessThan(1000 * 60 * 60 * 24 * 30 * 12 * 10);
63+
};
64+
65+
const parsed = await parser.parseString(await response.text());
66+
67+
expect(parsed).toEqual(expect.any(Object));
68+
expect(parsed.title).toEqual(expect.any(String));
69+
expect(parsed.title).not.toBe('RSSHub');
70+
expect(parsed.description).toEqual(expect.any(String));
71+
expect(parsed.link).toEqual(expect.any(String));
72+
expect(parsed.lastBuildDate).toEqual(expect.any(String));
73+
expect(parsed.ttl).toEqual(Math.trunc(config.cache.routeExpire / 60) + '');
74+
expect(parsed.items).toEqual(expect.any(Array));
75+
checkDate(parsed.lastBuildDate);
76+
77+
// check items
78+
const guids: Array<string | undefined> = [];
79+
for (const item of parsed.items) {
80+
expect(item).toEqual(expect.any(Object));
81+
expect(item.title).toEqual(expect.any(String));
82+
expect(item.link).toEqual(expect.any(String));
83+
expect(item.content).toEqual(expect.any(String));
84+
expect(item.guid).toEqual(expect.any(String));
85+
if (item.pubDate) {
86+
expect(item.pubDate).toEqual(expect.any(String));
87+
checkDate(item.pubDate);
88+
}
89+
90+
// guid must be unique
91+
expect(guids).not.toContain(item.guid);
92+
guids.push(item.guid);
93+
}
94+
}
95+
96+
describe('routes', () => {
97+
for (const route in routes) {
98+
it.concurrent(
99+
route,
100+
{
101+
timeout: 60000,
102+
},
103+
async () => {
104+
const response = await app.request(routes[route]);
105+
expect(response.status).toBe(200);
106+
await checkRSS(response);
107+
}
108+
);
109+
}
110+
});

lib/bilibili-video-route.test.ts

Lines changed: 0 additions & 185 deletions
This file was deleted.

lib/config.remote-error.test.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)