You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

68 lines
3.6 KiB

const assert = require('node:assert/strict');
const { chromium } = require('playwright');
const base = 'http://127.0.0.1:4173/pages';
(async () => {
const browser = await chromium.launch({ channel: 'msedge', headless: true });
const page = await browser.newPage({ viewport: { width: 1440, height: 900 }, locale: 'zh-CN' });
const errors = [];
page.on('pageerror', (error) => errors.push(error.message));
page.on('dialog', (dialog) => dialog.accept());
// 1. 登录并创建内容
await page.goto(`${base}/login.html`);
await page.evaluate(() => window.YouthomeLocal.reset());
await page.goto(`${base}/login.html`);
await page.locator('#panel-password input[type="tel"]').fill('13800000000');
await page.locator('#panel-password input[type="password"]').fill('qinghe123');
await page.locator('#panel-password .btn-primary').click();
await page.waitForURL('**/feed.html');
await page.evaluate(async () => {
await window.YouthomeLocal.request('/campus/moments', {
method: 'POST',
body: JSON.stringify({ content: '缓存清理回归测试动态', mediaUrls: [], tags: [], category: '校园生活', scope: 'CAMPUS' })
});
});
// 2. 设置页清理缓存:不能删掉账号和内容
await page.goto(`${base}/settings.html?section=general`);
await page.waitForTimeout(600);
const cacheRow = page.locator('.setting-item', { hasText: '清理缓存' });
const usage = (await cacheRow.locator('.setting-value').innerText()).trim();
assert.match(usage, /^[\d.]+ (B|KB|MB)$/, `清理缓存应显示真实占用,实际:${usage}`);
await cacheRow.locator('button').click();
await page.locator('.web-toast').first().waitFor();
const afterClear = await page.evaluate(() => ({
token: localStorage.getItem('youthome:web:accessToken'),
posts: JSON.parse(localStorage.getItem('youthome:web:db:v1') || '{}').moments?.filter((item) => item.content === '缓存清理回归测试动态').length || 0,
notice: Object.keys(localStorage).filter((key) => key.startsWith('youthome:web:notification-read:')).length
}));
assert.ok(afterClear.token, '清理缓存后登录状态必须保留');
assert.equal(afterClear.posts, 1, '清理缓存后已发布动态必须保留');
assert.equal(afterClear.notice, 0, '已读状态应被清理');
// 3. 清理后依然是登录态
await page.goto(`${base}/publish-post.html`);
await page.waitForTimeout(800);
assert.ok(page.url().includes('publish-post.html'), '清理缓存不应把用户踢到登录页');
assert.ok(await page.locator('.post-textarea').isVisible(), '清理缓存后仍能进入编辑器');
// 4. 退出登录:必须真的清掉登录态
await page.goto(`${base}/settings.html?section=account`);
await page.waitForTimeout(600);
await page.locator('.setting-item', { hasText: '退出登录' }).click();
await page.waitForURL('**/login.html');
const token = await page.evaluate(() => localStorage.getItem('youthome:web:accessToken') || sessionStorage.getItem('youthome:web:accessToken'));
assert.equal(token, null, '退出登录后不应残留 token');
await page.goto(`${base}/publish-post.html`);
await page.waitForURL('**/login.html?next=*', { timeout: 8000 });
const kept = await page.evaluate(() => JSON.parse(localStorage.getItem('youthome:web:db:v1') || '{}').moments?.some((item) => item.content === '缓存清理回归测试动态'));
assert.ok(kept, '退出登录后本机内容仍应保留,便于再次登录查看');
assert.deepEqual(errors, []);
await browser.close();
console.log(`session and cache behaviour passed in Edge (usage shown: ${usage})`);
})().catch((error) => { console.error(error); process.exitCode = 1; });