源码索引 / CURRENT IMPLEMENTATION
site/scripts/excerpts.test.mjs
17 行 · 点击行号可定位;函数链接随声明位置更新,不手填行号。
import assert from 'node:assert/strict';
import { expandExcerpts, verifyCoverage } from './excerpts.mjs';
const sources = new Map([['x.go', {code:'package x\n\nvar a = 1\n',lang:'go',id:'x-go'}]]);
const used = new Map();
const rendered = expandExcerpts('解释\n<!-- source: x.go:1-3 -->\n逐行说明', sources, used);
assert.match(rendered, /package x/);
assert.match(rendered, /--line-start:0/);
assert.match(rendered, /\/code\/x-go\/#L1/);
assert.match(rendered, /逐行说明/);
assert.equal(verifyCoverage(sources, ['x.go'], used)[0].covered, 2);
assert.throws(()=>expandExcerpts('<!-- source: secret.env:1-2 -->',sources,new Map()),/Unlisted/);
assert.throws(()=>expandExcerpts('<!-- source: x.go:1-8 -->',sources,new Map()),/range/);
assert.throws(()=>expandExcerpts('<!-- source: x.go:L1-L2 -->',sources,new Map()),/Malformed/);
assert.throws(()=>verifyCoverage(sources,['x.go'],new Map()),/Uncovered/);
const self = new Map([['a.js',{code:'const marker = "<!-- source: not-a-file:1-2 -->";\n',lang:'js',id:'a-js'}]]);
assert.match(expandExcerpts('<!-- source: a.js:1-1 -->',self,new Map()),/not-a-file/);
console.log('Excerpt extraction, line offsets, coverage and publication boundaries passed.');