Files
worldquant-alpha-system/frontend/tests/backtests.spec.ts
T

106 lines
4.5 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("textbox", { name: "Region", exact: true }).fill("USA");
await page.getByRole("textbox", { name: "Universe", exact: true }).fill("TOP3000");
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();
});