-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathApp.contract.test.tsx
More file actions
151 lines (132 loc) · 5.45 KB
/
App.contract.test.tsx
File metadata and controls
151 lines (132 loc) · 5.45 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
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/preact';
import { http, HttpResponse } from 'msw';
import { server, buildFeedResponse, buildStructuredErrorResponse } from './mocks/server';
import { App } from '../components/App';
describe('App contract', () => {
const token = 'contract-token';
beforeEach(() => {
globalThis.history.replaceState({}, '', 'http://localhost:3000/#/create');
globalThis.localStorage.clear();
globalThis.sessionStorage.clear();
globalThis.sessionStorage.setItem('html2rss_access_token', token);
});
it('shows feed result when the API returns structured create payload and preview feed', async () => {
const nativeFetch = globalThis.fetch;
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation((input, init) => {
if (String(input).endsWith('/api/v1/feeds/generated-token.json')) {
expect((init?.headers as Record<string, string> | undefined)?.Accept).toBe('application/feed+json');
return Promise.resolve(
new Response(
JSON.stringify({
items: [
{
title: 'Contract Item',
content_text: 'Contract preview excerpt.',
url: 'https://example.com/contract-item',
date_published: '2024-01-01T00:00:00Z',
},
],
}),
{ status: 200, headers: { 'Content-Type': 'application/feed+json' } }
)
);
}
return nativeFetch(input, init);
});
server.use(
http.post('/api/v1/feeds', async ({ request }) => {
const body = (await request.json()) as { url: string };
expect(body).toEqual({ url: 'https://example.com/articles' });
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',
}),
{ status: 201 }
);
}),
http.get('http://localhost:3000/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' },
}
);
}),
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',
},
],
});
})
);
render(<App />);
await waitFor(() => {
expect(screen.getByLabelText('Page URL')).toBeInTheDocument();
});
expect(screen.queryByRole('combobox')).not.toBeInTheDocument();
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(document.querySelector('.result-shell')).toHaveAttribute('data-state', 'result');
expect(screen.getByLabelText('Feed URL')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Copy feed URL' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Create another feed' })).toBeInTheDocument();
expect(screen.getByText('Latest items from this feed')).toBeInTheDocument();
});
fetchSpy.mockRestore();
});
it('reopens token recovery when a saved token is rejected by structured auth metadata', async () => {
server.use(
http.post('/api/v1/feeds', async () =>
HttpResponse.json(
buildStructuredErrorResponse({
code: 'UNAUTHORIZED',
message: 'Authentication required',
kind: 'auth',
retryable: false,
next_action: 'enter_token',
retry_action: 'none',
}),
{ status: 401 }
)
)
);
render(<App />);
await waitFor(() => {
expect(screen.getByLabelText('Page URL')).toBeInTheDocument();
});
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("Couldn't create feed yet")).not.toBeInTheDocument();
expect(globalThis.sessionStorage.getItem('html2rss_access_token')).toBeNull();
});
});