Clarify receive requests and add retry countdowns and device unblock controls
This commit is contained in:
205
tests/e2e/receive-controls.mjs
Normal file
205
tests/e2e/receive-controls.mjs
Normal file
@@ -0,0 +1,205 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { chromium, expect } from '@playwright/test';
|
||||
import { mkdtemp, rm } from 'node:fs/promises';
|
||||
import { resolve } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createApp } from '../../apps/server/src/app.ts';
|
||||
import { loadConfig } from '../../apps/server/src/config.ts';
|
||||
|
||||
const folder = await mkdtemp(resolve(tmpdir(), 'hop-receive-controls-'));
|
||||
const runtime = await createApp({
|
||||
...loadConfig({
|
||||
APP_URL: 'http://127.0.0.1',
|
||||
DATABASE_PATH: resolve(folder, 'db.sqlite'),
|
||||
UPLOAD_DIR: resolve(folder, 'uploads'),
|
||||
MIN_FREE_DISK_MB: '0',
|
||||
SESSION_SECRET: 's'.repeat(40),
|
||||
DISCOVERY_SECRET: 'd'.repeat(40),
|
||||
PAIR_REQUESTS_PER_MINUTE: '100',
|
||||
GUEST_REQUESTS_PER_MINUTE: '100',
|
||||
IP_REQUESTS_PER_MINUTE: '200',
|
||||
}),
|
||||
webRoot: resolve('apps/web/dist'),
|
||||
});
|
||||
let browser;
|
||||
try {
|
||||
await runtime.app.listen({ host: '127.0.0.1', port: 0 });
|
||||
const url = `http://127.0.0.1:${runtime.app.server.address().port}`;
|
||||
runtime.setDevOrigin(url);
|
||||
browser = await chromium.launch({
|
||||
headless: true,
|
||||
...(process.env.HOP_BROWSER_EXECUTABLE
|
||||
? { executablePath: process.env.HOP_BROWSER_EXECUTABLE }
|
||||
: {}),
|
||||
});
|
||||
const a = await browser.newContext({ viewport: { width: 1280, height: 900 } }),
|
||||
b = await browser.newContext({ viewport: { width: 390, height: 844 } });
|
||||
const sender = await a.newPage(),
|
||||
receiver = await b.newPage(),
|
||||
errors = [];
|
||||
sender.setDefaultTimeout(10000);
|
||||
receiver.setDefaultTimeout(10000);
|
||||
for (const page of [sender, receiver]) page.on('pageerror', (err) => errors.push(err.message));
|
||||
await Promise.all([sender.goto(url), receiver.goto(url)]);
|
||||
await sender.locator('.peer-card').first().waitFor();
|
||||
await receiver.locator('.peer-card').first().waitFor();
|
||||
async function api(page, path, method = 'GET', body) {
|
||||
return page.evaluate(
|
||||
async ({ path, method, body }) => {
|
||||
if (!window.__testCsrf) {
|
||||
const s = await fetch('/api/guest/session', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: '{}',
|
||||
});
|
||||
window.__testCsrf = (await s.json()).csrfToken;
|
||||
}
|
||||
const response = await fetch(`/api${path}`, {
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-Token': window.__testCsrf,
|
||||
'Idempotency-Key': crypto.randomUUID(),
|
||||
},
|
||||
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
||||
});
|
||||
if (!response.ok) throw Error(`${response.status}: ${await response.text()}`);
|
||||
return response.json();
|
||||
},
|
||||
{ path, method, body },
|
||||
);
|
||||
}
|
||||
const recipientId = (await api(receiver, '/device/me')).device.id;
|
||||
async function request(kind, size = 10) {
|
||||
return (
|
||||
await api(sender, '/transfer-requests', 'POST', {
|
||||
targetDeviceId: recipientId,
|
||||
kind,
|
||||
byteSize: size,
|
||||
count: 1,
|
||||
...(kind === 'file'
|
||||
? {
|
||||
files: [
|
||||
{
|
||||
name: 'Screenshot_20261012_152625_Samsung Browser.jpg',
|
||||
size,
|
||||
type: 'image/jpeg',
|
||||
},
|
||||
],
|
||||
}
|
||||
: {}),
|
||||
})
|
||||
).requestId;
|
||||
}
|
||||
for (const [kind, size, label] of [
|
||||
['text', 10, '텍스트'],
|
||||
['url', 20, '링크'],
|
||||
['file', 1333196, '파일'],
|
||||
['file', 100000, '파일'],
|
||||
['file', 1700000000, '파일'],
|
||||
]) {
|
||||
const id = await request(kind, size);
|
||||
const card = receiver.getByRole('dialog', { name: '전송 수락 요청' });
|
||||
await expect(card.locator('.consent-kind')).toHaveText(`${label} 전송 요청`);
|
||||
assert.equal(await card.locator('.request-note').count(), 0);
|
||||
assert.equal(await card.locator('.incoming-icon svg').count(), 1);
|
||||
const ratio = await card.evaluate((el) => {
|
||||
const a = el.querySelector('.incoming-actions .primary').getBoundingClientRect(),
|
||||
b = el.querySelector('.incoming-actions .button:not(.primary)').getBoundingClientRect();
|
||||
return a.width / b.width;
|
||||
});
|
||||
assert(Math.abs(ratio - 2) < 0.02);
|
||||
assert.equal(await card.getByText('기기 이름은 신원이 인증된 표시가 아닙니다.').count(), 0);
|
||||
if (kind === 'file') {
|
||||
await expect(card.locator('.request-file-size')).toHaveText(
|
||||
size === 1700000000 ? '1.7 GB' : size === 100000 ? '0.1 MB' : '1.3 MB',
|
||||
);
|
||||
const spacing = await card.locator('.request-files').evaluate((el) => ({
|
||||
padding: getComputedStyle(el).paddingLeft,
|
||||
margin: getComputedStyle(el).marginLeft,
|
||||
overflow: el.scrollWidth > el.clientWidth,
|
||||
}));
|
||||
assert.deepEqual(spacing, { padding: '0px', margin: '0px', overflow: false });
|
||||
assert(
|
||||
!(await card
|
||||
.locator('.incoming-info p')
|
||||
.textContent()
|
||||
.then((v) => /바이트|MB|GB/.test(v))),
|
||||
'single file size is shown once',
|
||||
);
|
||||
if (size === 1333196)
|
||||
await receiver.screenshot({ path: 'tests/e2e/receive-file-mobile.png' });
|
||||
}
|
||||
if (kind === 'text') {
|
||||
await receiver.setViewportSize({ width: 1280, height: 900 });
|
||||
const desktop = await card.evaluate((el) => {
|
||||
const a = el.querySelector('.incoming-actions .primary').getBoundingClientRect(),
|
||||
b = el.querySelector('.incoming-actions .button:not(.primary)').getBoundingClientRect();
|
||||
return a.width / b.width;
|
||||
});
|
||||
assert(Math.abs(desktop - 2) < 0.02);
|
||||
await receiver.screenshot({ path: 'tests/e2e/receive-text-desktop.png' });
|
||||
await receiver.setViewportSize({ width: 390, height: 844 });
|
||||
}
|
||||
await api(sender, `/transfer-requests/${id}/cancel`, 'POST', {});
|
||||
await card.waitFor({ state: 'hidden' });
|
||||
}
|
||||
await sender.locator('.peer-card').first().click();
|
||||
await sender
|
||||
.getByLabel('보낼 텍스트', { exact: true })
|
||||
.fill('제한이 걸려도 이 초안은 유지됩니다.');
|
||||
await sender.route('**/api/transfer-requests', (route) =>
|
||||
route.fulfill({
|
||||
status: 429,
|
||||
contentType: 'application/json',
|
||||
headers: { 'Retry-After': '3' },
|
||||
body: JSON.stringify({
|
||||
code: 'RATE_LIMIT',
|
||||
message: '전송 요청이 많아요.',
|
||||
retryAfterSeconds: 3,
|
||||
limitScope: 'pair',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
await sender.getByRole('button', { name: '전송하기', exact: true }).click();
|
||||
const submit = sender.locator('.content-submit');
|
||||
await expect(submit).toBeDisabled();
|
||||
await expect(submit).toHaveText(/\d+초 후 전송 가능/);
|
||||
await expect(sender.getByLabel('보낼 텍스트', { exact: true })).toBeEnabled();
|
||||
await expect(sender.getByLabel('보낼 텍스트', { exact: true })).toHaveValue(
|
||||
'제한이 걸려도 이 초안은 유지됩니다.',
|
||||
);
|
||||
await expect(submit).toHaveText('전송하기', { timeout: 5500 });
|
||||
await expect(submit).toBeEnabled();
|
||||
await sender.unroute('**/api/transfer-requests');
|
||||
await sender.getByRole('button', { name: '전송 화면 닫기' }).click();
|
||||
await request('text');
|
||||
const card = receiver.getByRole('dialog', { name: '전송 수락 요청' });
|
||||
await card.getByRole('button', { name: '이 기기 차단', exact: true }).click();
|
||||
assert.equal(
|
||||
(await api(receiver, '/device/blocks')).blocks.length,
|
||||
0,
|
||||
'first click does not block',
|
||||
);
|
||||
await card.locator('.consent-footer').getByRole('button', { name: '취소', exact: true }).click();
|
||||
await card.getByRole('button', { name: '이 기기 차단', exact: true }).click();
|
||||
await card.getByRole('button', { name: '차단하기', exact: true }).click();
|
||||
await card.waitFor({ state: 'hidden' });
|
||||
await expect(receiver.locator('.peer-card')).toHaveCount(0);
|
||||
await receiver.getByRole('button', { name: '내 기기 설정', exact: true }).click();
|
||||
await receiver.getByRole('button', { name: '관리', exact: true }).click();
|
||||
const blocked = receiver.getByRole('dialog', { name: '차단한 기기', exact: true });
|
||||
await expect(blocked.locator('.blocked-device-list li')).toHaveCount(1);
|
||||
await blocked.getByRole('button', { name: '차단 해제', exact: true }).click();
|
||||
await expect(blocked.getByText('차단한 기기가 없어요.')).toBeVisible();
|
||||
await expect(receiver.locator('.peer-card')).toHaveCount(1);
|
||||
assert.equal((await api(receiver, '/device/blocks')).blocks.length, 0);
|
||||
assert.deepEqual(errors, []);
|
||||
console.log(
|
||||
'PASS: content icons/titles, desktop/mobile 2:1 buttons, single file sizes 0.1/1.3MB and 1.7GB, no list indent or overflow, retry countdown and draft preservation, block confirmation and private unblock UI.',
|
||||
);
|
||||
} finally {
|
||||
await browser?.close();
|
||||
await runtime.app.close();
|
||||
await rm(folder, { recursive: true, force: true });
|
||||
}
|
||||
Reference in New Issue
Block a user