Files
yuxuanhui 8206b36cd2
Deploy production / deploy (push) Successful in 53s
feat: unify backtest table controls and add refresh action
2026-09-12 13:29:00 +08:00

298 lines
11 KiB
TypeScript

import { expect, test, type Page } from "@playwright/test";
const headers = { "X-WQ-Request": "1" };
async function login(page: Page) {
await page.goto("/#backtests");
await page.getByLabel("密码", { exact: true }).fill("browser-test-password");
await page.getByRole("button", { name: "进入工作空间" }).click();
await expect(page.getByRole("button", { name: "新建回测" })).toBeVisible();
await page.request.put("/api/v1/account/credentials", {
headers,
data: { email: "test@example.com", password: "synthetic-password" },
});
await page.request.post("/api/v1/account/connect", { headers });
await expect
.poll(
async () =>
(await (await page.request.get("/api/v1/account")).json())
.connection_status,
)
.toBe("connected");
}
test("draft, immutable preview, mixed-result persistence and responsive workspace", async ({
page,
}) => {
const errors: string[] = [];
page.on("pageerror", (e) => errors.push(e.message));
await login(page);
await page.getByRole("button", { name: "新建回测" }).click();
await page.getByLabel("运行名称", { exact: true }).fill("浏览器回测验收");
await page.getByRole("button", { name: "同步合法设置", exact: true }).click();
await page.getByRole("button", { name: "选择组合", exact: true }).click();
await page.getByRole("combobox", { name: "Region", exact: true }).click();
await page.getByRole("option").filter({ hasText: /^USA$/ }).click();
await page.getByRole("combobox", { name: "Universe", exact: true }).click();
await page
.getByRole("option")
.filter({ hasText: /^TOP3000$/ })
.click();
await page.getByRole("button", { name: "应用组合", exact: true }).click();
await page.getByLabel("回测候选").fill("rank(close)\n-rank(volume)");
await page.getByRole("button", { name: "保存草稿", exact: true }).click();
await expect(page.getByText("草稿已保存", { exact: true })).toBeVisible();
await page.getByRole("button", { name: "预览回测", exact: true }).click();
await expect(
page.getByText(/浏览器回测验收 · 2 条候选 · 1 个平台批次/),
).toBeVisible();
await page.screenshot({ path: "../output/playwright/backtest-preview.png" });
await page.getByRole("button", { name: "确认启动回测", exact: true }).click();
await expect(page.getByText("2 / 2 已保存", { exact: true })).toBeVisible({
timeout: 15000,
});
await page.getByRole("button", { name: "rank(close)", exact: true }).click();
await expect(page.locator(".backtest-item-detail")).toContainText(
'"observed_at"',
);
await expect(page.locator(".backtest-item-detail")).toContainText(
'"sharpe": null',
);
for (const width of [1440, 850, 390]) {
await page.setViewportSize({ width, height: 900 });
expect(
await page.evaluate(
() => document.documentElement.scrollWidth <= innerWidth,
),
).toBe(true);
await page.screenshot({
path: `../output/playwright/backtest-results-${width}.png`,
});
}
await page.keyboard.press("Escape");
await expect(page.getByRole("button", { name: "新建回测" })).toBeVisible();
await page.reload();
await page
.getByRole("button", { name: "浏览器回测验收", exact: true })
.click();
await expect(page.getByText("2 / 2 已保存", { exact: true })).toBeVisible();
expect(errors).toEqual([]);
});
test("AI prepares one fixed preview, confirms once, and shows live run independently", async ({
page,
}) => {
await login(page);
const config = { base_url: "https://model.test/v1", model: "test-model" };
await page.request.put("/api/v1/ai/settings", {
headers,
data: { ...config, api_key: "synthetic-key" },
});
await page.request.post("/api/v1/ai/settings/test", { headers });
await page.request.put("/api/v1/ai/settings", {
headers,
data: { ...config, enabled: true },
});
await page.reload();
await page.getByRole("button", { name: "切换研究助手" }).click();
const chat = page.getByRole("complementary", { name: "AI 研究助手" });
await chat.getByRole("button", { name: "新会话", exact: true }).click();
const before = (
await (await page.request.get("/api/v1/backtests/runs")).json()
).total;
await chat.getByLabel("发送给研究助手").fill("为我准备一次回测");
await chat.getByRole("button", { name: "发送", exact: true }).click();
await expect(chat.getByRole("button", { name: "确认执行" })).toBeEnabled();
expect(
(await (await page.request.get("/api/v1/backtests/runs")).json()).total,
).toBe(before);
await chat.getByRole("button", { name: "确认执行" }).click();
await expect(chat.getByText("已保存 1/1 · 平台失败 0")).toBeVisible({
timeout: 15000,
});
await chat.getByRole("button", { name: "打开回测详情", exact: true }).click();
await expect(page.getByText("1 / 1 已保存", { exact: true })).toBeVisible();
});
test("MCP direct batch appears through its run link and source filter", async ({
page,
}) => {
await login(page);
const token = (
await (
await page.request.post("/api/v1/test/mcp-token", { headers })
).json()
).token;
const response = await page.request.post("/api/v1/mcp/", {
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json, text/event-stream",
},
data: {
jsonrpc: "2.0",
id: "browser-batch",
method: "tools/call",
params: {
name: "submit_backtests",
arguments: {
name: "MCP 浏览器留痕",
idempotency_key: "browser-mcp",
duplicate_policy: "rerun",
candidates: [
{
client_item_id: "browser-1",
expression: "rank(close) + 987",
settings: {
instrumentType: "EQUITY",
region: "USA",
universe: "TOP3000",
delay: 1,
decay: 0,
neutralization: "INDUSTRY",
truncation: 0.08,
pasteurization: "ON",
unitHandling: "VERIFY",
nanHandling: "OFF",
language: "FASTEXPR",
visualization: false,
maxTrade: "OFF",
maxPosition: "OFF",
},
},
],
source: {
reference: "browser-conversation",
hypothesis: "验证直接回测留痕",
},
},
},
},
});
expect(response.ok()).toBeTruthy();
const result = (await response.json()).result;
expect(result.isError).toBeFalsy();
const run = result.structuredContent;
await page.goto(run.web_url);
await expect(page.getByText("1 / 1 已保存", { exact: true })).toBeVisible({
timeout: 20000,
});
await expect(
page.getByText("研究来源:MCP 研究", { exact: true }),
).toBeVisible();
await expect(
page.getByText("来源引用:browser-conversation", { exact: true }),
).toBeVisible();
await page.reload();
await expect(page.getByText("1 / 1 已保存", { exact: true })).toBeVisible();
await page.keyboard.press("Escape");
await page
.getByRole("dialog")
.getByRole("heading")
.getByRole("button")
.click();
await page.getByRole("button", { name: "筛选回测研究", exact: true }).click();
await page.getByText("全部研究来源", { exact: true }).click();
await page.getByRole("option", { name: /MCP 研究/ }).click();
await page.getByRole("button", { name: "查询", exact: true }).click();
await expect(
page.getByRole("button", { name: "MCP 浏览器留痕", exact: true }),
).toBeVisible();
});
test("backtest toolbar filters, sorts, refreshes and preserves compact layout", async ({
page,
}) => {
await login(page);
let revision = 1;
const requests: URL[] = [];
await page.route("**/api/v1/backtests/runs?*", async (route) => {
const url = new URL(route.request().url());
requests.push(url);
const offset = Number(url.searchParams.get("offset") || 0);
const limit = Number(url.searchParams.get("limit") || 25);
await route.fulfill({
json: {
total: 60,
limit,
offset,
items: Array.from({ length: Math.min(limit, 60 - offset) }, (_, i) => ({
backtest_run_id: `run-${offset + i}`,
name: `工具栏验收-${revision}-${offset + i} ` + "长名称".repeat(20),
source: { kind: "manual" },
status: "running",
total: 8,
counts: { persistence: { saved: 2 }, platform: { failed: 0 } },
created_at: "2026-09-12T10:00:00Z",
})),
},
});
});
await page.reload();
const table = page.locator(".backtest-page .workspace-table");
await expect(
table.getByRole("row").filter({ hasText: "工具栏验收-1-0" }),
).toBeVisible();
revision = 2;
await page.getByRole("button", { name: "刷新回测", exact: true }).click();
await expect(
table.getByRole("row").filter({ hasText: "工具栏验收-2-0" }),
).toBeVisible();
await page.getByRole("button", { name: "筛选回测研究", exact: true }).click();
await page.getByLabel("回测名称筛选").fill("test-filter");
expect(
requests.some((url) => url.searchParams.get("q") === "test-filter"),
).toBe(false);
await page.getByRole("button", { name: "查询", exact: true }).click();
await expect
.poll(() =>
requests.some((url) => url.searchParams.get("q") === "test-filter"),
)
.toBe(true);
await page.getByRole("button", { name: "显示列设置", exact: true }).click();
await page
.locator(".catalog-column-panel .semi-checkbox")
.filter({ hasText: "失败" })
.click();
await page.keyboard.press("Escape");
await expect(
table.getByRole("columnheader", { name: "失败", exact: true }),
).toHaveCount(0);
await page
.locator(".backtest-page .semi-page-item")
.filter({ hasText: /^2$/ })
.click();
await expect
.poll(() => requests.some((url) => url.searchParams.get("offset") === "25"))
.toBe(true);
await page.getByRole("button", { name: "排序设置", exact: true }).click();
await page.getByRole("combobox", { name: "排序字段", exact: true }).click();
await page
.getByRole("option")
.filter({ hasText: /^名称$/ })
.click();
await expect
.poll(() =>
requests.some(
(url) =>
url.searchParams.get("sort") === "name" &&
url.searchParams.get("offset") === "0",
),
)
.toBe(true);
await page.keyboard.press("Escape");
for (const width of [1440, 850]) {
await page.setViewportSize({ width, height: 900 });
const row = table.locator("tbody tr.semi-table-row").first();
await expect(row).toBeVisible();
expect(await row.evaluate((el) => el.getBoundingClientRect().height)).toBe(
40,
);
const footer = await page
.locator(".backtest-page .workspace-table-footer")
.boundingBox();
expect(footer!.y + footer!.height).toBeLessThanOrEqual(900);
expect(footer!.y + footer!.height).toBeGreaterThan(860);
await page.screenshot({
path: `../output/playwright/backtest-toolbar-${width}.png`,
});
}
});