Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes. Co-authored-by: Cursor <cursoragent@cursor.com>
49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
/**
|
||
* 章节跳转 / 回钉 自测(不依赖微信运行时)
|
||
* Run: node miniapp/scripts/test-scroll-jump.mjs
|
||
*/
|
||
import assert from 'node:assert/strict'
|
||
|
||
function chapterAnchorId(idx) {
|
||
return `chap${idx}`
|
||
}
|
||
|
||
function computePageScrollTop({ currentScrollTop, targetTop, topbarOffset = 72 }) {
|
||
return Math.max(0, currentScrollTop + targetTop - topbarOffset)
|
||
}
|
||
|
||
function shouldAcceptJumpMeasurement(targetRect) {
|
||
if (!targetRect) return false
|
||
if (typeof targetRect.top !== 'number') return false
|
||
if (typeof targetRect.height === 'number' && targetRect.height <= 0) return false
|
||
return true
|
||
}
|
||
|
||
function needsRepin(targetTop, topbarOffset = 72, slop = 8) {
|
||
return Math.abs(targetTop - topbarOffset) > slop
|
||
}
|
||
|
||
assert.equal(chapterAnchorId(5), 'chap5')
|
||
|
||
// 跳到第6章后,上方 CDN 图加载把锚点顶到 220(偏离顶栏72)→ 必须回钉
|
||
{
|
||
const driftedTop = 220
|
||
assert.equal(needsRepin(driftedTop, 72), true)
|
||
const next = computePageScrollTop({
|
||
currentScrollTop: 4728,
|
||
targetTop: driftedTop,
|
||
topbarOffset: 72,
|
||
})
|
||
// 回钉后锚点应回到顶栏下
|
||
assert.equal(next, 4728 + 220 - 72)
|
||
}
|
||
|
||
assert.equal(needsRepin(72, 72), false)
|
||
assert.equal(needsRepin(80, 72), false)
|
||
assert.equal(needsRepin(90, 72), true)
|
||
|
||
assert.equal(shouldAcceptJumpMeasurement({ top: 10, height: 100 }), true)
|
||
assert.equal(shouldAcceptJumpMeasurement({ top: 10, height: 0 }), false)
|
||
|
||
console.log('scroll-jump self-test: OK')
|