test: add comprehensive markdown.ts unit tests

Tests cover cardToMarkdown, cardToPlain, and cardsToMarkdown with
edge cases: empty/missing fields, anchor whitespace normalization,
null cards array, empty title fallback, plain text bullet formatting.

Change-Id: I1972705d385f0393c9fd1be7f80d6513f5328d06
This commit is contained in:
wujunchen 2026-04-27 15:28:34 +08:00
parent 1e2ccc0840
commit 0f8625874e
2 changed files with 81 additions and 1 deletions

View file

@ -10,7 +10,7 @@
"typecheck": "tsc --noEmit",
"lint": "biome check main.ts src/",
"lint:fix": "biome check --write main.ts src/",
"test": "npm run build && npm run typecheck && node tests/main.test.js && node tests/generation-job-manager.test.js && node tests/anchor.test.js && node tests/schema.test.js && node tests/cache.test.js && node tests/cards-nav.test.js && node tests/markdown.test.js && node tests/vault-batch.test.js && node tests/i18n.test.js && node tests/scroll.test.js && node tests/settings.test.js && node tests/providers.test.js && node tests/streaming.test.js && node tests/cli.test.js && node tests/direct-prompt.test.js && node tests/direct-cache.test.js && node tests/direct-providers.test.js && node tests/direct-settings.test.js && node tests/direct-i18n.test.js && node tests/direct-streaming.test.js"
"test": "npm run build && npm run typecheck && node tests/main.test.js && node tests/generation-job-manager.test.js && node tests/anchor.test.js && node tests/schema.test.js && node tests/cache.test.js && node tests/cards-nav.test.js && node tests/markdown.test.js && node tests/vault-batch.test.js && node tests/i18n.test.js && node tests/scroll.test.js && node tests/settings.test.js && node tests/providers.test.js && node tests/streaming.test.js && node tests/cli.test.js && node tests/direct-prompt.test.js && node tests/direct-markdown.test.js && node tests/direct-cache.test.js && node tests/direct-providers.test.js && node tests/direct-settings.test.js && node tests/direct-i18n.test.js && node tests/direct-streaming.test.js"
},
"devDependencies": {
"@biomejs/biome": "^2.4.13",

View file

@ -0,0 +1,80 @@
const { assert, requireBundledModule, cleanup } = require('./direct-test-setup');
(async () => {
try {
const md = await requireBundledModule('src/markdown.ts');
// ── cardToMarkdown ──
const full = md.cardToMarkdown({
title: 'Section A',
anchor: 'original quote',
gist: 'Summary line',
bullets: ['point 1', 'point 2'],
});
assert.ok(full.includes('## Section A'), 'heading');
assert.ok(full.includes('> original quote'), 'anchor blockquote');
assert.ok(full.includes('Summary line'), 'gist');
assert.ok(full.includes('- point 1'), 'bullet 1');
assert.ok(full.includes('- point 2'), 'bullet 2');
const noAnchor = md.cardToMarkdown({ title: 'B', anchor: '', gist: 'G', bullets: [] });
assert.ok(!noAnchor.includes('> '), 'no blockquote when anchor is empty');
assert.ok(noAnchor.includes('## B'), 'heading present');
assert.ok(noAnchor.includes('G'), 'gist present');
const noBullets = md.cardToMarkdown({ title: 'C', anchor: '', gist: 'G', bullets: [] });
assert.ok(!noBullets.includes('- '), 'no bullet markers for empty array');
const noGist = md.cardToMarkdown({ title: 'D', anchor: 'a', gist: '', bullets: ['b'] });
assert.ok(noGist.includes('> a'), 'anchor present');
assert.ok(noGist.includes('- b'), 'bullets present');
const anchorWhitespace = md.cardToMarkdown({ title: 'E', anchor: ' spaces \n tabs \t here ', gist: '', bullets: [] });
assert.ok(anchorWhitespace.includes('> spaces tabs here'), 'anchor whitespace normalized');
// ── cardToPlain ──
const plain = md.cardToPlain({
title: 'Section A',
anchor: 'ignored in plain',
gist: 'Summary',
bullets: ['one', 'two'],
});
assert.ok(plain.includes('Section A'), 'title in plain');
assert.ok(plain.includes('Summary'), 'gist in plain');
assert.ok(plain.includes('• one'), 'bullet with dot');
assert.ok(!plain.includes('ignored'), 'anchor not in plain text');
const plainNoGist = md.cardToPlain({ title: 'T', anchor: '', gist: '', bullets: ['b'] });
assert.ok(plainNoGist.includes('T'), 'title');
assert.ok(plainNoGist.includes('• b'), 'bullet');
assert.ok(!plainNoGist.includes('\n\n'), 'no double newline from empty gist');
const plainNoBullets = md.cardToPlain({ title: 'T', anchor: '', gist: 'G', bullets: [] });
assert.strictEqual(plainNoBullets, 'T\nG', 'just title and gist');
const plainMinimal = md.cardToPlain({ title: 'T', anchor: '', gist: '', bullets: [] });
assert.strictEqual(plainMinimal, 'T', 'title only when gist and bullets empty');
// ── cardsToMarkdown ──
const multi = md.cardsToMarkdown('My Title', [
{ title: 'A', anchor: 'q', gist: 'g', bullets: ['b'] },
{ title: 'B', anchor: '', gist: 'g2', bullets: [] },
]);
assert.ok(multi.includes('# My Title'), 'top-level heading');
assert.ok(multi.includes('## A'), 'first card');
assert.ok(multi.includes('## B'), 'second card');
const emptyTitle = md.cardsToMarkdown('', []);
assert.strictEqual(emptyTitle, '# 对照笔记', 'empty title uses default');
const nullCards = md.cardsToMarkdown('T', null);
assert.strictEqual(nullCards, '# T', 'null cards array gives title only');
const emptyCards = md.cardsToMarkdown('T', []);
assert.strictEqual(emptyCards, '# T', 'empty cards array gives title only');
console.log('direct markdown tests passed');
} finally {
cleanup();
}
})().catch((e) => { console.error(e); process.exit(1); });