Files
Thesisly/tests/session/papers.spec.mjs
T

63 lines
3.8 KiB
JavaScript

import { test, expect } from "@playwright/test";
const core = "http://localhost:17324";
const errors = new WeakMap();
async function control(request, data) { expect((await request.post(core + "/__test/control", { data })).ok()).toBe(true); }
test.beforeEach(async ({ page, request }) => {
expect((await request.post(core + "/__test/reset")).ok()).toBe(true);
errors.set(page, []);
page.on("pageerror", error => errors.get(page).push(error.message));
});
test.afterEach(async ({ page }) => {
expect(errors.get(page)).toEqual([]);
expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)).toBe(true);
});
test("search failures retain controls and recover after a revised query", async ({ page, request }) => {
for (const [status, heading] of [[504, "这次检索耗时较长"], [429, "论文索引正在处理较多请求"], [503, "论文暂时无法读取"], [400, "请调整搜索条件"]]) {
await control(request, { paperStatus: status });
await page.goto("/?q=original&topic=cs.LG&searchIn=author");
await expect(page.getByRole("heading", { name: heading, exact: true })).toBeVisible();
await expect(page.getByRole("textbox", { name: "搜索论文", exact: true })).toHaveValue("original");
await expect(page.getByLabel("搜索范围")).toHaveValue("author");
await expect(page.locator('.search-form input[name="topic"]')).toHaveValue("cs.LG");
await expect(page.locator(".index-heading h2 span, .pagination, .sample-note")).toHaveCount(0);
await expect(page.getByRole("heading", { name: "没有找到符合条件的论文" })).toHaveCount(0);
await expect(page.getByRole("link", { name: "重新尝试", exact: true })).toHaveAttribute("href", /q=original/);
}
await control(request, { paperStatus: 0 });
await page.setViewportSize({ width: 390, height: 844 });
await page.getByRole("textbox", { name: "搜索论文", exact: true }).fill("revised query");
await page.getByRole("button", { name: "搜索论文", exact: true }).click();
await expect(page).toHaveURL(/q=revised\+query/);
await expect(page.locator(".paper-row")).toHaveCount(8);
await expect(page.locator(".paper-index [role=alert]")).toHaveCount(0);
});
test("malformed research responses do not become empty results or sample mode", async ({ page, request }) => {
await control(request, { paperMalformed: true });
await page.goto("/?q=keep-this-query");
await expect(page.getByRole("heading", { name: "论文暂时无法读取", exact: true })).toBeVisible();
await expect(page.getByRole("textbox", { name: "搜索论文", exact: true })).toHaveValue("keep-this-query");
await expect(page.locator(".paper-row, .sample-note, .index-heading h2 span, .pagination")).toHaveCount(0);
});
test("slow results stream after usable search controls and searches retain the document", async ({ page, request }) => {
await control(request, { paperDelay: 3000 });
await page.goto("/", { waitUntil: "commit" });
await expect(page.getByRole("textbox", {name:"搜索论文",exact:true})).toBeVisible({timeout:2000});
await expect(page.getByRole("status")).toContainText("正在读取论文索引");
await expect(page.locator(".paper-row")).toHaveCount(8);
await page.evaluate(() => { window.__readerDocument = "preserved"; });
let documents = 0, sessions = 0;
page.on("request", req => { if(req.isNavigationRequest() && req.frame() === page.mainFrame()) documents++; if(new URL(req.url()).pathname.endsWith("/auth/me")) sessions++; });
await page.getByRole("textbox", {name:"搜索论文",exact:true}).fill("graph");
await page.getByRole("button", {name:"搜索论文",exact:true}).click();
await expect(page).toHaveURL(/q=graph/);
await expect(page.locator(".paper-row")).toHaveCount(8);
expect(await page.evaluate(() => window.__readerDocument)).toBe("preserved");
expect(documents).toBe(0); expect(sessions).toBe(0);
});