Add single task debug mode

This commit is contained in:
2026-07-07 10:22:16 +08:00
parent 3f0ee98a54
commit 4f184c9e86
5 changed files with 518 additions and 75 deletions

View File

@@ -18,6 +18,7 @@ const {
waitForElements,
getPageInfo,
} = require(path.join(__dirname, '..', 'utils', 'page'));
const { runDebugSteps } = require(path.join(__dirname, '..', 'utils', 'debugSteps'));
const PROJECT_CARD_SELECTORS = [
'.hot-selection .recommend-card',
@@ -32,44 +33,91 @@ const PROJECT_CARD_SELECTORS = [
* 执行查看热门任务
* @param {import('playwright').Page} page - 主页面
*/
async function dailyTrending(page) {
async function dailyTrending(page, options = {}) {
logger.info('👉 执行查看热门');
// 从主页面找到"查看热门"任务卡片
const tasks = await page.locator('.novice-task-item-box').all();
const trendingTask = await findTaskByTitle(tasks, '查看热门');
const ctx = {
page,
trendingTask: null,
trendingPage: null,
cards: [],
detailPage: null,
};
if (!trendingTask) {
throw new Error('未找到"查看热门"任务卡片');
}
// ── 第一步:打开热门页面 ──
const trendingPage = await openNewPage(
page.context(),
() => trendingTask.locator('.toComplete').click()
);
const steps = [
{
name: '查找查看热门任务卡片',
page: (state) => state.page,
run: async (state) => {
const tasks = await state.page.locator('.novice-task-item-box').all();
state.trendingTask = await findTaskByTitle(tasks, '查看热门');
if (!state.trendingTask) {
throw new Error('未找到"查看热门"任务卡片');
}
},
inspect: async (state) => ({
taskCardCount: await state.page.locator('.novice-task-item-box').count(),
titleLabelCount: await state.page.locator('.task-card-title__label-text').count(),
toCompleteCount: await state.page.locator('.toComplete').count(),
}),
},
{
name: '点击去完成并打开热门页',
page: (state) => state.page,
run: async (state) => {
state.trendingPage = await openNewPage(
state.page.context(),
() => state.trendingTask.locator('.toComplete').click()
);
state.currentPage = state.trendingPage;
},
inspect: async (state) => ({
taskToCompleteVisible: await state.trendingTask.locator('.toComplete').isVisible().catch(() => false),
}),
},
{
name: '查找热门项目卡片',
page: (state) => state.trendingPage,
run: async (state) => {
state.cards = await findProjectCards(state.trendingPage);
},
inspect: async (state) => ({
hotSelectionCount: await state.trendingPage.locator('.hot-selection').count(),
recommendCardCount: await state.trendingPage.locator('a.recommend-card').count(),
sourceModuleLinkCount: await state.trendingPage.locator('a[href*="source_module"]').count(),
allLinkCount: await state.trendingPage.locator('a[href]').count(),
}),
},
{
name: '打开第一个热门项目详情页',
page: (state) => state.trendingPage,
run: async (state) => {
state.detailPage = await openLinkPage(
state.trendingPage.context(),
state.cards[0],
{ label: '热门项目详情页' }
);
state.currentPage = state.detailPage;
},
inspect: async (state) => ({
cardCount: state.cards.length,
}),
},
{
name: '等待热门项目详情页加载',
page: (state) => state.detailPage,
run: async (state) => {
await waitPageReady(state.detailPage);
logger.info('热门项目详情页已打开');
},
},
];
try {
const cards = await findProjectCards(trendingPage);
// ── 第二步:点击第一个热门项目,打开详情页 ──
const firstCard = cards[0];
const detailPage = await openLinkPage(
trendingPage.context(),
firstCard,
{ label: '热门项目详情页' }
);
try {
// 等待详情页加载
await waitPageReady(detailPage);
logger.info('热门项目详情页已打开');
} finally {
await safeClose(detailPage);
}
await runDebugSteps('查看热门', steps, ctx, options);
} finally {
await safeClose(trendingPage);
await safeClose(ctx.detailPage);
await safeClose(ctx.trendingPage);
}
}

View File

@@ -11,59 +11,151 @@ const path = require('path');
const config = require(path.join(__dirname, '..', 'config'));
const logger = require(path.join(__dirname, '..', 'utils', 'logger'));
const { safeClose, safeGoto } = require(path.join(__dirname, '..', 'utils', 'page'));
const { runDebugSteps } = require(path.join(__dirname, '..', 'utils', 'debugSteps'));
/**
* 执行更新项目任务
* @param {import('playwright').Page} page - 主页面
*/
async function updateProject(page) {
async function updateProject(page, options = {}) {
logger.info('👉 执行项目更新');
if (!config.urls.testRepo) {
throw new Error('未配置 GITCODE_TEST_REPO_URL无法执行更新项目任务');
}
const ctx = {
page,
newPage: null,
frame: null,
content: '',
};
const context = page.context();
const newPage = await context.newPage();
const steps = [
{
name: '检查 GITCODE_TEST_REPO_URL 配置',
page: (state) => state.page,
run: async () => {
if (!config.urls.testRepo) {
throw new Error('未配置 GITCODE_TEST_REPO_URL无法执行更新项目任务');
}
},
inspect: async () => ({
testRepoConfigured: Boolean(config.urls.testRepo),
}),
},
{
name: '打开仓库编辑页',
page: (state) => state.newPage || state.page,
run: async (state) => {
state.newPage = await state.page.context().newPage();
state.currentPage = state.newPage;
await safeGoto(state.newPage, config.urls.testRepo, {
label: '仓库编辑页',
timeout: config.navigationTimeout,
waitFor: ['#codeartside'],
});
logger.info('已进入仓库编辑页面');
},
inspect: async (state) => ({
codeartsideCount: state.newPage ? await state.newPage.locator('#codeartside').count() : 0,
}),
},
{
name: '等待 #codeartside iframe',
page: (state) => state.newPage,
run: async (state) => {
await state.newPage.locator('#codeartside').waitFor({
state: 'attached',
timeout: 15000,
});
state.frame = state.newPage.frameLocator('#codeartside');
},
inspect: async (state) => ({
codeartsideCount: await state.newPage.locator('#codeartside').count(),
}),
},
{
name: '定位 Monaco 编辑器',
page: (state) => state.newPage,
run: async (state) => {
await state.frame.locator('.monaco-editor').click({
position: { x: 200, y: 100 },
});
},
inspect: async (state) => ({
iframeCount: await state.newPage.locator('#codeartside').count(),
monacoEditorCount: await state.frame.locator('.monaco-editor').count(),
textareaCount: await state.frame.locator('textarea').count(),
}),
},
{
name: '移动光标到文件末尾',
page: (state) => state.newPage,
run: async (state) => {
await state.newPage.keyboard.press('Control+End');
await state.newPage.waitForTimeout(300);
},
},
{
name: '输入自动更新内容',
page: (state) => state.newPage,
run: async (state) => {
state.content = `\n# 自动更新 - ${new Date().toISOString()}`;
await state.newPage.keyboard.type(state.content, { delay: 50 });
logger.info('已写入内容');
},
},
{
name: '点击第一个提交修改按钮',
page: (state) => state.newPage,
run: async (state) => {
if (state.debug.dryRun) {
logger.warn('DEBUG_DRY_RUN=true跳过点击提交修改按钮');
return;
}
await state.newPage.locator('button:has-text("提交修改")').first().click();
await state.newPage.waitForTimeout(1000);
},
inspect: async (state) => ({
submitButtonCount: await state.newPage.locator('button:has-text("提交修改")').count(),
}),
},
{
name: '等待确认弹窗提交按钮',
page: (state) => state.newPage,
run: async (state) => {
if (state.debug.dryRun) {
logger.warn('DEBUG_DRY_RUN=true跳过等待确认弹窗');
return;
}
const confirmBtn = state.newPage.locator('button:has-text("提交修改")').nth(1);
await confirmBtn.waitFor({ state: 'visible', timeout: 10000 });
},
inspect: async (state) => ({
submitButtonCount: await state.newPage.locator('button:has-text("提交修改")').count(),
dialogCount: await state.newPage.locator('[role="dialog"], .modal, .el-dialog').count(),
}),
},
{
name: '点击弹窗里的提交修改',
page: (state) => state.newPage,
run: async (state) => {
if (state.debug.dryRun) {
logger.warn('DEBUG_DRY_RUN=true跳过最终提交');
return;
}
const confirmBtn = state.newPage.locator('button:has-text("提交修改")').nth(1);
await confirmBtn.click();
await state.newPage.waitForTimeout(1000);
logger.info('项目更新已提交');
},
inspect: async (state) => ({
submitButtonCount: await state.newPage.locator('button:has-text("提交修改")').count(),
}),
},
];
try {
// 打开编辑页面
await safeGoto(newPage, config.urls.testRepo, {
label: '仓库编辑页',
timeout: config.navigationTimeout,
waitFor: ['#codeartside'],
});
logger.info('已进入仓库编辑页面');
// 获取编辑器 iframe
const frame = newPage.frameLocator('#codeartside');
// 点击编辑器区域
await frame.locator('.monaco-editor').click({
position: { x: 200, y: 100 },
});
// 光标移动到末尾
await newPage.keyboard.press('Control+End');
await newPage.waitForTimeout(300); // 极短等待确保光标到位(键盘事件无法 await
// 追加内容
const content = `\n# 自动更新 - ${new Date().toISOString()}`;
await newPage.keyboard.type(content, { delay: 50 });
logger.info('已写入内容');
// 点击"提交修改"按钮(页面上的第一个)
await newPage.locator('button:has-text("提交修改")').first().click();
await page.waitForTimeout(1000);
// 等待弹框中的"提交修改"按钮出现并点击
const confirmBtn = newPage.locator('button:has-text("提交修改")').nth(1);
await confirmBtn.waitFor({ state: 'visible', timeout: 10000 });
await confirmBtn.click();
await page.waitForTimeout(1000);
logger.info('项目更新已提交');
await runDebugSteps('更新项目', steps, ctx, options);
} finally {
await safeClose(newPage);
await safeClose(ctx.newPage);
}
}