muse-agent-example/web/tests/旅程/备份恢复.spec.ts
zizi ae02fd1732 前端:工作台页面、事件标签与旅程用例
- 上下文清单、任务列表与详情、恢复控制、运行观察、创作对话与消息流、正式变更决策面板、正文写作与候选审阅、资料研究等页面同步本轮后端能力。
- 事件标签表补齐后端事件类型;查询键与生成客户端随 HTTP 契约重新生成。
- 依赖调整后 lockfile 同步;交互用例与浏览器旅程 spec 一并更新。
- 证据:make 前端检查(lint + typecheck + Pi 工具桥 tsc)exit 0;make 前端测试 37 文件 / 120 用例通过。
2026-09-18 01:15:07 +08:00

86 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { readFileSync, statSync } from "node:fs";
import { expect, test } from "@playwright/test";
// @muse-case {"case_id":"NC-browser-backup-roundtrip","environment":"浏览器","given":"显式隔离配置、合成数据与已知发布构建","when":"经真实维护、业务或浏览器入口执行并读取持久结果","then":["NC-browser-backup-roundtrip:真实备份核对与下载"],"contract":"docs/系统架构/新版设计/迁移与验收.md#备份与恢复入口"}
test("NC-browser-backup-roundtrip:真实备份核对与下载", async ({
page,
}, info) => {
const passwordFile = process.env.MUSE_AUTHOR_PASSWORD_FILE;
const backupId = process.env.MUSE_BACKUP_ID;
const downloadPath = process.env.MUSE_BACKUP_DOWNLOAD_PATH;
if (!passwordFile || !backupId || !downloadPath)
throw new Error(
"需要 MUSE_AUTHOR_PASSWORD_FILE、MUSE_BACKUP_ID 和 MUSE_BACKUP_DOWNLOAD_PATH",
);
const passwordMode = statSync(passwordFile).mode & 0o777;
if (passwordMode !== 0o600)
throw new Error("MUSE_AUTHOR_PASSWORD_FILE 权限必须为 600");
const errors: string[] = [];
page.on("pageerror", (error) => errors.push(error.message));
await page.goto("/system/backups");
await page
.getByLabel("访问口令")
.fill(readFileSync(passwordFile, "utf8").trim());
await page.getByRole("button", { name: "进入工作台" }).click();
await page.goto("/system/backups");
const panel = page.getByRole("region", { name: "备份核验状态" });
await expect(
page.getByRole("heading", { name: "备份与恢复", exact: true }),
).toBeVisible();
await expect(panel.getByRole("listitem")).toHaveCount(2);
const valid = panel.getByRole("listitem").filter({ hasText: backupId });
const unreadable = panel
.getByRole("listitem")
.filter({ hasText: "清单无法读取" });
await expect(valid).toHaveCount(1);
await expect(valid).toContainText("数据库归档:");
await expect(valid).toContainText("原文/草稿文件:");
await expect(unreadable).toHaveCount(1);
await expect(unreadable.getByRole("button")).toHaveCount(0);
const verifiedResponse = page.waitForResponse(
(response) =>
response.url().includes("/verify?") &&
response
.url()
.includes(`expected_backup_id=${encodeURIComponent(backupId)}`),
);
await valid.getByRole("button", { name: "核对字节" }).click();
const verified = await verifiedResponse;
expect(verified.status()).toBe(200);
expect(await verified.json()).toMatchObject({
backup_id: backupId,
archive_verified: true,
restore_verified: false,
});
await expect(
page.getByText(
`备份 ${backupId} 的归档字节已核对;尚未完成恢复核验。`,
{ exact: true },
),
).toBeVisible();
const downloadEvent = page.waitForEvent("download");
await valid.getByRole("button", { name: "核对并下载" }).click();
const download = await downloadEvent;
expect(download.suggestedFilename()).toBe(`${backupId}.tar`);
await download.saveAs(downloadPath);
expect(statSync(downloadPath).size).toBeGreaterThan(0);
await expect(
page.getByText(/完整备份已交给浏览器下载;下载不代表已完成恢复/),
).toBeVisible();
await page.reload();
await expect(panel.getByRole("listitem")).toHaveCount(2);
await expect(page.getByText("恢复成功", { exact: true })).toHaveCount(0);
await expect(page.getByText("已恢复", { exact: true })).toHaveCount(0);
await expect(valid).toContainText("归档字节尚待核对");
await page.screenshot({
path: info.outputPath("备份核对与下载.png"),
fullPage: true,
});
expect(errors).toEqual([]);
});