-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathApp.contract.test.tsx
More file actions
166 lines (136 loc) · 5.72 KB
/
App.contract.test.tsx
File metadata and controls
166 lines (136 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { describe, it, expect } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/preact';
import { http, HttpResponse } from 'msw';
import { server, buildFeedResponse } from './mocks/server';
import { App } from '../components/App';
describe('App contract', () => {
const token = 'contract-token';
const authenticate = () => {
globalThis.localStorage.setItem('html2rss_access_token', token);
};
it('shows feed result when API responds with success', async () => {
authenticate();
server.use(
http.post('/api/v1/feeds', async ({ request }) => {
const body = (await request.json()) as { url: string; strategy: string };
expect(body).toEqual({ url: 'https://example.com/articles', strategy: 'faraday' });
expect(request.headers.get('authorization')).toBe(`Bearer ${token}`);
return HttpResponse.json(
buildFeedResponse({
url: body.url,
feed_token: 'generated-token',
public_url: '/api/v1/feeds/generated-token',
json_public_url: '/api/v1/feeds/generated-token.json',
})
);
}),
http.get('/api/v1/feeds/generated-token.json', ({ request }) => {
expect(request.headers.get('accept')).toBe('application/feed+json');
return HttpResponse.json(
{
items: [
{
title: 'Contract Item',
content_text: 'Contract preview excerpt.',
url: 'https://example.com/contract-item',
date_published: '2024-01-01T00:00:00Z',
},
],
},
{
headers: { 'content-type': 'application/feed+json' },
}
);
})
);
render(<App />);
await screen.findByLabelText('Page URL');
await waitFor(() => {
expect(screen.getByRole('combobox')).toHaveValue('faraday');
});
const urlInput = screen.getByLabelText('Page URL') as HTMLInputElement;
fireEvent.input(urlInput, { target: { value: 'https://example.com/articles' } });
fireEvent.click(screen.getByRole('button', { name: 'Generate feed URL' }));
await waitFor(() => {
expect(screen.getByText('Feed ready')).toBeInTheDocument();
expect(screen.getByText('Example Feed')).toBeInTheDocument();
expect(screen.getByLabelText('Feed URL')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Copy feed URL' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Open feed' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Open JSON Feed' })).toHaveAttribute(
'href',
'http://localhost:3000/api/v1/feeds/generated-token.json'
);
expect(screen.getByRole('button', { name: 'Create another feed' })).toBeInTheDocument();
expect(screen.getByText('Preview')).toBeInTheDocument();
expect(screen.getByText('Latest items from this feed')).toBeInTheDocument();
expect(screen.getByText('Contract Item')).toBeInTheDocument();
});
});
it('loads instance metadata from /api/v1 without trailing slash', async () => {
let slashlessMetadataRequests = 0;
let trailingSlashMetadataRequests = 0;
server.use(
http.get('/api/v1', () => {
slashlessMetadataRequests += 1;
return HttpResponse.json({
success: true,
data: {
api: {
name: 'html2rss-web API',
description: 'RESTful API for converting websites to RSS feeds',
openapi_url: 'http://example.test/openapi.yaml',
},
instance: {
feed_creation: {
enabled: true,
access_token_required: true,
},
featured_feeds: [],
},
},
});
}),
http.get('/api/v1/', () => {
trailingSlashMetadataRequests += 1;
return HttpResponse.text('', { status: 404 });
})
);
render(<App />);
await screen.findByLabelText('Page URL');
expect(screen.getByRole('button', { name: 'Generate feed URL' })).toBeInTheDocument();
expect(screen.queryByText('Instance metadata unavailable')).not.toBeInTheDocument();
expect(slashlessMetadataRequests).toBeGreaterThanOrEqual(1);
expect(trailingSlashMetadataRequests).toBe(0);
});
it('shows the metadata unavailable notice when /api/v1 responds with non-JSON content', async () => {
server.use(
http.get('/api/v1', () => HttpResponse.text('not-json', { status: 502 })),
http.get('/api/v1/', () => HttpResponse.text('', { status: 404 }))
);
render(<App />);
await screen.findByText('Instance metadata unavailable');
expect(screen.getByText('Invalid response format from API metadata')).toBeInTheDocument();
});
it('reopens token recovery when a saved token is rejected by /api/v1/feeds', async () => {
authenticate();
server.use(
http.post('/api/v1/feeds', async () =>
HttpResponse.json({ success: false, error: { message: 'Unauthorized' } }, { status: 401 })
)
);
render(<App />);
await screen.findByLabelText('Page URL');
await waitFor(() => {
expect(screen.getByRole('combobox')).toHaveValue('faraday');
});
fireEvent.input(screen.getByLabelText('Page URL'), {
target: { value: 'https://example.com/articles' },
});
fireEvent.click(screen.getByRole('button', { name: 'Generate feed URL' }));
await screen.findByText('Access token was rejected. Paste a valid token to continue.');
expect(screen.getByText('Enter access token')).toBeInTheDocument();
expect(screen.queryByText('Could not create feed link')).not.toBeInTheDocument();
expect(globalThis.localStorage.getItem('html2rss_access_token')).toBeNull();
});
});