|
|
|
|
@ -43,8 +43,9 @@ class Node {
|
|
|
|
|
const flush = async () => { for (let turn = 0; turn < 16; turn += 1) await Promise.resolve(); };
|
|
|
|
|
|
|
|
|
|
function fixture() {
|
|
|
|
|
const names = ['drawer', 'state', 'refresh', 'more', 'count', 'list', 'scroll', 'close'];
|
|
|
|
|
const names = ['drawer', 'state', 'read-time', 'refresh', 'more', 'count', 'list', 'scroll', 'close'];
|
|
|
|
|
const nodes = Object.fromEntries(names.map(name => [name, new Node(name === 'drawer' ? 'dialog' : 'div')]));
|
|
|
|
|
nodes['read-time'].hidden = true;
|
|
|
|
|
const document = new Node(); document.visibilityState = 'visible'; document.hidden = false;
|
|
|
|
|
document.documentElement = new Node(); document.body = new Node();
|
|
|
|
|
const trigger = new Node('button'); trigger.isTrigger = true;
|
|
|
|
|
@ -55,6 +56,15 @@ function fixture() {
|
|
|
|
|
const window = new Node(); window.scrollX = 0; window.scrollY = 125; window.scrollTo = () => {};
|
|
|
|
|
const history = { state: null, replaceState(value) { this.state = value; } };
|
|
|
|
|
let now = 0; let timerId = 0;
|
|
|
|
|
const epoch = Date.UTC(2026, 8, 30, 6, 27, 20);
|
|
|
|
|
class FakeDate extends Date {
|
|
|
|
|
constructor(...values) { super(...(values.length ? values : [epoch + now])); }
|
|
|
|
|
static now() { return epoch + now; }
|
|
|
|
|
toLocaleTimeString(locale, options) {
|
|
|
|
|
assert.equal(locale, 'zh-CN'); assert.equal(options.hour12, false);
|
|
|
|
|
return this.toISOString().slice(11, 19);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const timers = new Map(); const requests = [];
|
|
|
|
|
const setTimeout = (callback, delay = 0) => {
|
|
|
|
|
const id = ++timerId; timers.set(id, { callback, due: now + delay }); return id;
|
|
|
|
|
@ -64,7 +74,7 @@ function fixture() {
|
|
|
|
|
});
|
|
|
|
|
vm.runInNewContext(source, {
|
|
|
|
|
document, window, history, location: { href: 'https://example.test/user/cloud' },
|
|
|
|
|
URL, AbortController, fetch, setTimeout, clearTimeout: id => timers.delete(id),
|
|
|
|
|
URL, Date: FakeDate, AbortController, fetch, setTimeout, clearTimeout: id => timers.delete(id),
|
|
|
|
|
requestAnimationFrame: callback => setTimeout(callback, 0), console,
|
|
|
|
|
}, { filename: 'announcements.js' });
|
|
|
|
|
return {
|
|
|
|
|
@ -149,6 +159,44 @@ function assertQuiet(test) {
|
|
|
|
|
assert.equal(articles(test).length, 0); assert.match(test.nodes.list.textContent, /暂无已发布/); assertQuiet(test);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only a complete successful read updates the independent timestamp, never status text.
|
|
|
|
|
{
|
|
|
|
|
const test = fixture(); const readTime = test.nodes['read-time'];
|
|
|
|
|
const first = Array.from({ length: 10 }, (_, index) => announcement(11 - index));
|
|
|
|
|
assert.equal(readTime.hidden, true); assert.equal(readTime.textContent, '');
|
|
|
|
|
await test.click('open'); await test.advance(2000);
|
|
|
|
|
assert.equal(readTime.hidden, true); assert.equal(readTime.textContent, '');
|
|
|
|
|
await test.respond(page(first, 1, 11));
|
|
|
|
|
assert.equal(readTime.hidden, false); assert.equal(readTime.textContent, '读取于 06:27:22'); assertQuiet(test);
|
|
|
|
|
await test.advance(1000); await test.click('more'); await test.advance(2000);
|
|
|
|
|
assert.equal(readTime.textContent, '读取于 06:27:22');
|
|
|
|
|
await test.respond(page([announcement(1)], 2, 11));
|
|
|
|
|
assert.equal(readTime.textContent, '读取于 06:27:25');
|
|
|
|
|
const renders = test.nodes.list.replacements;
|
|
|
|
|
await test.advance(30000); await test.advance(1000);
|
|
|
|
|
await test.respond(page(first, 1, 11));
|
|
|
|
|
assert.equal(readTime.textContent, '读取于 06:27:25', 'A successful first page is not a completed multi-page refresh.');
|
|
|
|
|
await test.advance(1000); await test.respond(page([announcement(1)], 2, 11));
|
|
|
|
|
assert.equal(readTime.textContent, '读取于 06:27:57');
|
|
|
|
|
assert.equal(test.nodes.list.replacements, renders, 'Updating read time must not redraw unchanged announcements.');
|
|
|
|
|
assertQuiet(test);
|
|
|
|
|
await test.advance(30000); await test.respond(page(first, 1, 11));
|
|
|
|
|
await test.advance(1000); await test.respond({ detail: '暂时不可用' }, 503);
|
|
|
|
|
assert.equal(readTime.textContent, '读取于 06:27:57', 'Failed automatic refreshes retain the last successful time.');
|
|
|
|
|
assertQuiet(test);
|
|
|
|
|
await test.click('refresh'); await test.respond(page(first, 1, 11));
|
|
|
|
|
await test.advance(1000); await test.respond({ detail: '暂时不可用' }, 503);
|
|
|
|
|
assert.equal(readTime.textContent, '读取于 06:27:57', 'Manual errors cannot be labelled as successful reads.');
|
|
|
|
|
assert.match(test.nodes.state.textContent, /暂时不可用/);
|
|
|
|
|
await test.click('refresh'); await test.respond(page(first, 1, 11));
|
|
|
|
|
await test.advance(1000); await test.respond(page([announcement(1)], 2, 11));
|
|
|
|
|
assert.equal(readTime.textContent, '读取于 06:28:30'); assertQuiet(test);
|
|
|
|
|
await test.advance(30000); const cancelled = test.requests.at(-1);
|
|
|
|
|
await test.click('close'); await test.advance(1000);
|
|
|
|
|
await test.respond(page(first, 1, 11), 200, cancelled);
|
|
|
|
|
assert.equal(readTime.textContent, '读取于 06:28:30', 'Cancelled reads never advance the displayed timestamp.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Refresh the entire loaded window atomically, including edits/withdrawals on later pages.
|
|
|
|
|
{
|
|
|
|
|
const test = fixture(); const first = Array.from({ length: 10 }, (_, index) => announcement(12 - index));
|
|
|
|
|
@ -198,6 +246,7 @@ function assertQuiet(test) {
|
|
|
|
|
const test = fixture(); await test.click('open');
|
|
|
|
|
await test.respond({ detail: '公告服务暂不可用' }, 503);
|
|
|
|
|
assert.match(test.nodes.state.textContent, /暂不可用/);
|
|
|
|
|
assert.equal(test.nodes['read-time'].hidden, true); assert.equal(test.nodes['read-time'].textContent, '');
|
|
|
|
|
assert.doesNotMatch(test.nodes.list.textContent, /暂无已发布/);
|
|
|
|
|
await test.click('refresh'); await test.respond(page([announcement(1)]));
|
|
|
|
|
assert.equal(articles(test).length, 1); assertQuiet(test);
|
|
|
|
|
@ -264,4 +313,4 @@ function assertQuiet(test) {
|
|
|
|
|
await test.click('refresh'); await test.respond(page([announcement(1)])); assertQuiet(test);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('Announcement drawer lazy loading, silent polling, atomic pagination, cancellation and plain-text behavior passed.');
|
|
|
|
|
console.log('Announcement drawer lazy loading, silent polling, successful read time, atomic pagination, cancellation and plain-text behavior passed.');
|
|
|
|
|
|