-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathreceipt.e2e.ts
More file actions
220 lines (174 loc) · 8.06 KB
/
receipt.e2e.ts
File metadata and controls
220 lines (174 loc) · 8.06 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { expect, Page, test } from '@playwright/test';
test.describe.configure({ retries: 2 });
/** Delay between tests to avoid RPC rate limiting (429) */
const RPC_COOLDOWN_MS = 500;
const NAV_TIMEOUT = 60000;
const CONTENT_TIMEOUT = 15000;
const VALID_TX = '5VERv8NMvzbJMEkV8xnrLkEaWRtSz9CosKDYjCJjBRnbJLgp8uirBgmQpjKhoR4tjF3ZpRzrFmBV6UjKdiSZkQUW';
const INVALID_TX = '34qZEWsTW85uZSd4N53DptMaQ5HTx2cczgwWatW6CPhLbaetEX67jELmrCyz2M2ydkQin3NR9sMbgVxeqDwT8Sqp';
const FEATURE_ENABLED = process.env.NEXT_PUBLIC_RECEIPT_ENABLED === 'true';
test.describe('receipt feature validation', () => {
test.afterEach(async () => {
await delay(RPC_COOLDOWN_MS);
});
test('respects NEXT_PUBLIC_RECEIPT_ENABLED flag', async ({ page }) => {
await waitForPage(page, VALID_TX, 'receipt');
await page
.locator('h3:has-text("Solana Receipt")')
.or(page.locator('h2:has-text("Transaction")'))
.or(page.locator('text=Receipts can only be generated'))
.first()
.waitFor({ state: 'visible', timeout: CONTENT_TIMEOUT });
const hasReceipt = await hasElement(page, 'h3:has-text("Solana Receipt")');
const hasNoReceipt = await hasElement(page, 'text=Receipts can only be generated');
const hasTransactionPage = await hasElement(page, 'h2:has-text("Transaction")');
if (FEATURE_ENABLED) {
expect(hasReceipt || hasNoReceipt).toBe(true);
} else {
expect(hasReceipt).toBe(false);
expect(hasTransactionPage).toBe(true);
}
});
});
test.describe('when feature enabled', () => {
test.describe.configure({ mode: 'serial' });
test.skip(!FEATURE_ENABLED);
test.afterEach(async () => {
await delay(RPC_COOLDOWN_MS);
});
test('renders receipt for valid transaction', async ({ page }) => {
const hasReceipt = await navigateToReceipt(page);
const hasError = await hasElement(page, 'text=Not Found');
const hasNoReceipt = await hasElement(page, 'text=Receipts can only be generated');
expect(hasReceipt || hasError || hasNoReceipt).toBe(true);
if (hasReceipt) {
const bodyText = await page.textContent('body');
// eslint-disable-next-line no-restricted-syntax -- Verify the receipt contains at least one of the expected fields
expect(bodyText).toMatch(/Sender|Receiver|Status|Network/i);
}
});
test('handles invalid transaction', async ({ page }) => {
await waitForPage(page, INVALID_TX, 'receipt');
await page.waitForFunction(
() => {
const text = document.body?.innerText || '';
return (
text.includes('Receipts can only be generated') ||
text.includes('Fetch Failed') ||
text.includes('Error')
);
},
{ timeout: CONTENT_TIMEOUT },
);
const text = await page.textContent('body');
const showsError =
text?.includes('Receipts can only be generated') ||
text?.includes('Fetch Failed') ||
text?.includes('Error');
expect(showsError).toBe(true);
});
test('shows CSV and PDF options in download menu', async ({ page }) => {
const hasReceipt = await navigateToReceipt(page);
if (!hasReceipt) return;
await page.locator('button:has-text("Download")').click();
await expect(page.locator('button:has-text("CSV")')).toBeVisible({ timeout: CONTENT_TIMEOUT });
await expect(page.locator('button:has-text("PDF")')).toBeVisible({ timeout: CONTENT_TIMEOUT });
});
test('triggers CSV download with correct filename', async ({ page }) => {
const hasReceipt = await navigateToReceipt(page);
if (!hasReceipt) return;
await page.locator('button:has-text("Download")').click();
const [download] = await Promise.all([
page.waitForEvent('download'),
page.locator('button:has-text("CSV")').click(),
]);
// eslint-disable-next-line no-restricted-syntax -- regex needed to validate filename pattern: solana-receipt-<signature>.csv
expect(download.suggestedFilename()).toMatch(/^solana-receipt-.+\.csv$/);
});
test('shows Downloaded! state after CSV download completes', async ({ page }) => {
const hasReceipt = await navigateToReceipt(page);
if (!hasReceipt) return;
await page.locator('button:has-text("Download")').click();
await page.locator('button:has-text("CSV")').click();
await expect(page.locator('button:has-text("Downloaded!")')).toBeVisible({ timeout: CONTENT_TIMEOUT });
});
test('shows View Receipt button', async ({ page }) => {
await waitForPage(page, VALID_TX);
await page
.locator('h3:has-text("Overview")')
.or(page.locator('text=Fetch Failed'))
.or(page.locator('text=Not Found'))
.first()
.waitFor({ state: 'visible', timeout: CONTENT_TIMEOUT });
const hasOverview = await hasElement(page, 'h3:has-text("Overview")');
if (hasOverview) {
await expect(page.locator('a:has-text("View Receipt")')).toBeVisible({ timeout: CONTENT_TIMEOUT });
} else {
// just verify no error page (Overview didn't load - page may have errored)
const bodyText = await page.textContent('body');
expect(bodyText?.includes('Fetch Failed') || bodyText?.includes('Not Found')).toBe(true);
}
});
});
test.describe('when feature disabled', () => {
test.describe.configure({ mode: 'serial' });
test.skip(FEATURE_ENABLED);
test.afterEach(async () => {
await delay(RPC_COOLDOWN_MS);
});
test('ignores ?view=receipt parameter', async ({ page }) => {
await waitForPage(page, VALID_TX, 'receipt');
await expect(page.locator('h2:has-text("Transaction")')).toBeVisible({ timeout: CONTENT_TIMEOUT });
expect(await hasElement(page, 'h3:has-text("Solana Receipt")')).toBe(false);
expect(await hasElement(page, 'h2:has-text("Transaction")')).toBe(true);
});
test('hides View Receipt button', async ({ page }) => {
await waitForPage(page, VALID_TX);
await page
.locator('h3:has-text("Overview")')
.or(page.locator('h2:has-text("Transaction")'))
.first()
.waitFor({ state: 'visible', timeout: CONTENT_TIMEOUT });
const hasOverview = await hasElement(page, 'h3:has-text("Overview")');
if (hasOverview) {
await expect(page.locator('a:has-text("View Receipt")')).toBeHidden();
} else {
expect(await hasElement(page, 'h3:has-text("Solana Receipt")')).toBe(false);
}
});
});
async function hasElement(page: Page, selector: string, timeout = 10000): Promise<boolean> {
try {
await page.locator(selector).waitFor({ state: 'visible', timeout });
return true;
} catch {
return false;
}
}
async function navigateToReceipt(page: Page): Promise<boolean> {
await waitForPage(page, VALID_TX, 'receipt');
await page
.locator('h3:has-text("Solana Receipt")')
.or(page.locator('text=Not Found'))
.or(page.locator('text=Receipts can only be generated'))
.first()
.waitFor({ state: 'visible', timeout: CONTENT_TIMEOUT });
return hasElement(page, 'h3:has-text("Solana Receipt")');
}
async function waitForPage(page: Page, tx: string, view?: 'receipt') {
const url = view ? `/tx/${tx}?view=${view}` : `/tx/${tx}`;
const responsePromise = page
.waitForResponse(response => response.url().includes('api.') || response.url().includes('rpc'), {
timeout: NAV_TIMEOUT,
})
.catch(() => null);
await page.goto(url, { timeout: NAV_TIMEOUT, waitUntil: 'domcontentloaded' });
await responsePromise;
await page
.locator('text=Loading')
.waitFor({ state: 'hidden', timeout: 30000 })
.catch(() => {});
}
function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}