mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-05 08:00:20 +08:00
merge: 修复 CI E2E 失败及手机端保存栏遮挡与只读权限回归
This commit is contained in:
@@ -3,7 +3,7 @@ import { expect } from '@playwright/test';
|
||||
import type { Page, TestInfo } from '@playwright/test';
|
||||
import { test } from './support/fixtures';
|
||||
import type { Scenario } from './support/fixtures';
|
||||
import { viewports } from './support/flows';
|
||||
import { saveCorrectionToNotebook, viewports } from './support/flows';
|
||||
|
||||
async function upload(page: Page, scenario: Scenario, path = '/') {
|
||||
await scenario.control({ hold: true });
|
||||
@@ -66,7 +66,7 @@ for (const profile of viewports) {
|
||||
expect(await analysisCalls(scenario)).toBe(1);
|
||||
|
||||
await question.fill(`校对后的方程 x + 2 = 5。${scenario.marker}`);
|
||||
await page.getByRole('button', { name: /保存|Save/ }).click();
|
||||
await saveCorrectionToNotebook(page);
|
||||
await expect.poll(() => scenario.db.errorItem.count({ where: { userId: scenario.user.id } })).toBe(1);
|
||||
const task = await scenario.db.importTask.findFirstOrThrow({ where: { userId: scenario.user.id } });
|
||||
expect(task).toMatchObject({ status: 'saved', imageData: '' });
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { resolve } from 'node:path';
|
||||
import { expect, test } from './support/fixtures';
|
||||
import { batchOf, calls, expectNoHorizontalOverflow, expectNotCovered, saveShot } from './support/batch-flows';
|
||||
import { viewports } from './support/flows';
|
||||
import { saveCorrectionToNotebook, viewports } from './support/flows';
|
||||
|
||||
const PAGE_PHOTO = resolve('src/__tests__/fixtures/images/multi-question-page.jpg');
|
||||
|
||||
@@ -15,7 +15,11 @@ for (const profile of viewports) {
|
||||
await page.goto(`/notebooks/${scenario.book.id}/add`);
|
||||
await page.locator('input[type="file"]').first().setInputFiles(PAGE_PHOTO);
|
||||
await page.getByRole('button', { name: '自动分题' }).click();
|
||||
await expect(page.getByText(/AI 自动找出每道题及其位置/)).toBeVisible();
|
||||
if (profile.isMobile) {
|
||||
await expect(page.getByText(/AI 自动找出每道题及其位置/)).toBeHidden();
|
||||
} else {
|
||||
await expect(page.getByText(/AI 自动找出每道题及其位置/)).toBeVisible();
|
||||
}
|
||||
await page.getByRole('button', { name: '自动识别题目' }).click();
|
||||
await expect(page).toHaveURL(/\/import-batches\/[a-f0-9]+$/);
|
||||
await expect(page.getByTestId('draft-summary')).toHaveText(/识别出 3 题\s*·\s*已选 3 题/, { timeout: 20000 });
|
||||
@@ -81,7 +85,7 @@ test('[E2E-B04] 照片批次开启人工确认:逐题校对后回到本批,
|
||||
await expect(page).toHaveURL(/\/import-tasks\/[a-f0-9]+$/);
|
||||
const question = page.locator('textarea').first();
|
||||
await question.fill(`校对后的第 1 题 ${scenario.marker}`);
|
||||
await page.getByRole('button', { name: /保存|Save/ }).click();
|
||||
await saveCorrectionToNotebook(page);
|
||||
// 校对保存后回到本批题目
|
||||
await expect(page).toHaveURL(/\/import-batches\/[a-f0-9]+$/);
|
||||
await expect(page.getByTestId('batch-counts')).toContainText('待校对 2');
|
||||
|
||||
@@ -7,6 +7,15 @@ export const viewports = [
|
||||
{ name: '手机', viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true },
|
||||
];
|
||||
|
||||
/** 点击校对页「保存到错题本」按钮:窄屏(<1024px)优先点底部固定栏,避免与顶部栏或「保存并下一题」冲突 */
|
||||
export async function saveCorrectionToNotebook(page: Page) {
|
||||
const mobileSaveBar = page.getByTestId('correction-mobile-save-bar');
|
||||
const saveButton = (await mobileSaveBar.isVisible())
|
||||
? mobileSaveBar.getByRole('button', { name: /保存到错题本|Save to Notebook/ })
|
||||
: page.getByRole('button', { name: /保存到错题本|Save to Notebook/ });
|
||||
await saveButton.click();
|
||||
}
|
||||
|
||||
export async function importImage(page: Page, scenario: Scenario, confirm: boolean, onCorrection?: (page: Page) => Promise<void>) {
|
||||
await scenario.db.user.update({ where: { id: scenario.user.id }, data: { confirmBeforeImport: confirm } });
|
||||
await scenario.control({ hold: true });
|
||||
@@ -27,7 +36,7 @@ export async function importImage(page: Page, scenario: Scenario, confirm: boole
|
||||
await expect(question).toHaveValue(`求解方程 x + 2 = 5。${scenario.marker}`);
|
||||
await question.fill(`校对后的方程 x + 2 = 5。${scenario.marker}`);
|
||||
await expect(page.getByText('一元一次方程', { exact: true }).first()).toBeVisible();
|
||||
await page.getByRole('button', { name: /保存|Save/ }).click();
|
||||
await saveCorrectionToNotebook(page);
|
||||
} else await expect(page.getByRole('heading', { name: /校对|Review & Correct/ })).toHaveCount(0);
|
||||
await expect.poll(() => scenario.db.errorItem.count({ where: { userId: scenario.user.id } })).toBe(1);
|
||||
const item = await scenario.db.errorItem.findFirstOrThrow({ where: { userId: scenario.user.id }, include: { tags: true } });
|
||||
|
||||
81
src/__tests__/unit/markdown-field-card.test.tsx
Normal file
81
src/__tests__/unit/markdown-field-card.test.tsx
Normal file
@@ -0,0 +1,81 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* MarkdownFieldCard:只读成员隐藏编辑按钮与标题栏写操作,同时保留折叠切换
|
||||
*/
|
||||
import { act } from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import type { Root } from "react-dom/client";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { MarkdownFieldCard } from "@/components/error-detail/markdown-field-card";
|
||||
import { LanguageProvider } from "@/contexts/LanguageContext";
|
||||
|
||||
vi.mock("@/components/markdown-renderer", () => ({
|
||||
MarkdownRenderer: ({ content }: { content?: string | null }) => <div data-testid="markdown-content">{content}</div>,
|
||||
}));
|
||||
|
||||
vi.mock("@/components/markdown-editor", () => ({
|
||||
MarkdownEditor: ({ value, onChange }: { value: string; onChange: (v: string) => void }) => (
|
||||
<textarea value={value} onChange={(e) => onChange(e.target.value)} />
|
||||
),
|
||||
}));
|
||||
|
||||
let container: HTMLDivElement;
|
||||
let root: Root;
|
||||
|
||||
async function renderCard(props: { readOnly?: boolean; collapsible?: boolean; defaultCollapsed?: boolean }) {
|
||||
await act(async () => {
|
||||
root.render(
|
||||
<LanguageProvider>
|
||||
<MarkdownFieldCard
|
||||
title="参考答案"
|
||||
value="x = 3"
|
||||
onSave={vi.fn()}
|
||||
logLabel="answer"
|
||||
rows={4}
|
||||
headerActions={<button type="button">重新生成完整解答</button>}
|
||||
{...props}
|
||||
/>
|
||||
</LanguageProvider>,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
|
||||
container = document.createElement("div");
|
||||
document.body.appendChild(container);
|
||||
root = createRoot(container);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await act(async () => root.unmount());
|
||||
container.remove();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("MarkdownFieldCard 只读与折叠行为", () => {
|
||||
it("只读时隐藏 headerActions 与编辑按钮,保留折叠切换按钮", async () => {
|
||||
await renderCard({ readOnly: true, collapsible: true, defaultCollapsed: true });
|
||||
|
||||
expect(container.textContent).not.toContain("重新生成完整解答");
|
||||
expect(container.querySelector('button[title*="参考答案"]')).toBeNull();
|
||||
expect(container.querySelector('[data-testid="markdown-content"]')).toBeNull();
|
||||
|
||||
const toggle = [...container.querySelectorAll("button")].find((btn) =>
|
||||
btn.textContent?.includes("展开") || btn.textContent?.includes("Expand"));
|
||||
expect(toggle).toBeDefined();
|
||||
|
||||
await act(async () => {
|
||||
toggle?.click();
|
||||
});
|
||||
expect(container.querySelector('[data-testid="markdown-content"]')?.textContent).toBe("x = 3");
|
||||
});
|
||||
|
||||
it("可编辑时显示 headerActions 与编辑按钮", async () => {
|
||||
await renderCard({ readOnly: false, collapsible: true, defaultCollapsed: false });
|
||||
|
||||
expect(container.textContent).toContain("重新生成完整解答");
|
||||
expect(container.querySelector('button[title*="参考答案"]')).not.toBeNull();
|
||||
expect(container.querySelector('[data-testid="markdown-content"]')?.textContent).toBe("x = 3");
|
||||
});
|
||||
});
|
||||
@@ -296,8 +296,8 @@ export function CorrectionEditor({ initialData, onSave, onSaveAndNext, onCancel,
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 手机端底部固定保存栏,避免长表单滚动后还要回到顶部保存 */}
|
||||
<div data-testid="correction-mobile-save-bar" className="fixed inset-x-0 bottom-0 z-30 flex gap-2 border-t bg-background/95 p-3 pb-[calc(0.75rem+env(safe-area-inset-bottom))] backdrop-blur lg:hidden">
|
||||
{/* 手机端底部固定保存栏,避免长表单滚动后还要回到顶部保存;z-[45] 需高于全局底部导航(z-40)且低于弹窗(z-50) */}
|
||||
<div data-testid="correction-mobile-save-bar" className="fixed inset-x-0 bottom-0 z-[45] flex gap-2 border-t bg-background/95 p-3 pb-[calc(0.75rem+env(safe-area-inset-bottom))] backdrop-blur lg:hidden">
|
||||
<Button variant="outline" className="flex-1" onClick={onCancel}>
|
||||
{t.editor.cancel}
|
||||
</Button>
|
||||
|
||||
@@ -62,7 +62,7 @@ export function MarkdownFieldCard({ title, value, onSave, logLabel, rows, placeh
|
||||
<CardHeader>
|
||||
<SectionHeader
|
||||
title={<CardTitle className={titleClassName}>{title}</CardTitle>}
|
||||
actions={!field.editing && <div className="flex flex-wrap justify-end">{collapseToggle}{!readOnly && <EditButton onClick={field.start} ariaLabel={`${t.common?.edit || "Edit"} ${title}`} />}{headerActions}</div>}
|
||||
actions={!field.editing && <div className="flex flex-wrap justify-end">{collapseToggle}{!readOnly && headerActions}{!readOnly && <EditButton onClick={field.start} ariaLabel={`${t.common?.edit || "Edit"} ${title}`} />}</div>}
|
||||
/>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
|
||||
Reference in New Issue
Block a user