/** * 收件箱渲染与已读策略的测试。 * * 每条断言都对应一次真实的错误行为(见 lib/inbox-format.js 里的注释): * 漏掉 attachment_id 模型就无从下载附件;漏掉抄送它会以为这是私信; * status=all 时标记已读会让下一轮的新邮件混在历史里认不出来。 * * node --test 'test/*.test.mjs' */ import { test } from 'node:test'; import assert from 'node:assert/strict'; import { formatSize, renderMail, renderInbox, idsToMarkRead, DEFAULT_INBOX_STATUS, DEFAULT_INBOX_LIMIT, } from '../lib/inbox-format.js'; const mail = (over = {}) => ({ mail_id: 'm-1', from_name: 'admin', subject: '缓存选型', status: 'unread', session_alias: 'brisk-harbor', body_preview: '我们需要评估一下缓存层', ...over, }); // ─── formatSize ─── test('formatSize 分档', () => { assert.equal(formatSize(512), '512 B'); assert.equal(formatSize(2048), '2.0 KB'); assert.equal(formatSize(3 * 1024 * 1024), '3.0 MB'); }); test('formatSize 容错', () => { assert.equal(formatSize(undefined), '?'); assert.equal(formatSize(NaN), '?'); assert.equal(formatSize('x'), '?'); }); // ─── renderMail ─── test('renderMail 带出 mail_id 与会话别名', () => { const got = renderMail(mail()); assert.match(got, /邮件 ID: m-1/); assert.match(got, /#brisk-harbor/); assert.match(got, /admin: 缓存选型/); }); test('无别名时显示「未命名」而不是空', () => { const got = renderMail(mail({ session_alias: '' })); assert.match(got, /#未命名/); }); test('不变量:附件必须带 attachment_id', () => { // 只说「有附件」模型就无从下载 —— download_attachment 要的正是这个 id。 const got = renderMail(mail({ attachments: [{ filename: 'report.md', size_bytes: 2048, attachment_id: 'att-9' }], })); assert.match(got, /id=att-9/, `附件行缺 id:${got}`); assert.match(got, /report\.md/); assert.match(got, /2\.0 KB/); assert.match(got, /download_attachment/, '要提示模型用哪个工具下载'); }); test('多个附件都列出来', () => { const got = renderMail(mail({ attachments: [ { filename: 'a.md', size_bytes: 10, attachment_id: 'att-1' }, { filename: 'b.md', size_bytes: 20, attachment_id: 'att-2' }, ], })); assert.match(got, /att-1/); assert.match(got, /att-2/); }); test('不变量:抄送人要显示出来', () => { // 不显示的话模型会以为这是私下发给它一个人的,回信时漏掉其他参与方。 const got = renderMail(mail({ cc_list: [{ name: 'opencode', raw: 'opencode@/home.new' }], })); assert.match(got, /抄送/); assert.match(got, /opencode@\/home\.new/, '应优先用 raw(带路径与会话段)'); }); test('无抄送时不出现抄送行', () => { assert.ok(!renderMail(mail()).includes('抄送')); assert.ok(!renderMail(mail({ cc_list: [] })).includes('抄送')); }); test('正文优先取 body_preview,缺失时退回 body', () => { assert.match(renderMail(mail({ body_preview: '预览', body: '全文' })), /内容: 预览/); assert.match(renderMail(mail({ body_preview: '', body: '全文' })), /内容: 全文/); }); test('正文按 bodyLimit 截断', () => { const got = renderMail(mail({ body_preview: 'x'.repeat(500) }), 50); const line = got.split('\n').find(l => l.startsWith('内容: ')); assert.equal(line.length, '内容: '.length + 50); }); test('renderMail 容错:字段全缺不崩', () => { const got = renderMail({}); assert.match(got, /unknown/); const got2 = renderMail(undefined); assert.equal(typeof got2, 'string'); }); test('附件字段不是数组时忽略', () => { const got = renderMail(mail({ attachments: 'oops', cc_list: 'oops' })); assert.ok(!got.includes('附件:')); assert.ok(!got.includes('抄送')); }); // ─── renderInbox ─── test('renderInbox 空收件箱给明确文案', () => { assert.equal(renderInbox([]), '收件箱为空。'); assert.equal(renderInbox(undefined), '收件箱为空。'); assert.equal(renderInbox(null), '收件箱为空。'); }); test('renderInbox 用空行分隔多封', () => { const got = renderInbox([mail({ mail_id: 'a' }), mail({ mail_id: 'b' })]); assert.match(got, /邮件 ID: a[\s\S]*\n\n[\s\S]*邮件 ID: b/); }); // ─── idsToMarkRead ─── test('不变量:只标本次列出的那些', () => { // limit 之外的还没看过,一并标掉等于让它们凭空消失。 const ids = idsToMarkRead('unread', [mail({ mail_id: 'a' }), mail({ mail_id: 'b' })]); assert.deepEqual(ids, ['a', 'b']); }); test('不变量:status=all 时不标记', () => { // 那是「回顾历史」的读法。把历史邮件标成已读会让下一轮真正的新邮件 // 混在里面认不出来。 assert.deepEqual(idsToMarkRead('all', [mail({ mail_id: 'a' })]), []); }); test('status 省略时按默认(unread)标记', () => { assert.deepEqual(idsToMarkRead(undefined, [mail({ mail_id: 'a' })]), ['a']); }); test('idsToMarkRead 过滤掉无 id 的条目', () => { const ids = idsToMarkRead('unread', [ mail({ mail_id: 'a' }), mail({ mail_id: '' }), mail({ mail_id: undefined }), { }, ]); assert.deepEqual(ids, ['a']); }); test('idsToMarkRead 容错非数组', () => { assert.deepEqual(idsToMarkRead('unread', undefined), []); assert.deepEqual(idsToMarkRead('unread', 'oops'), []); }); // ─── 默认值 ─── test('默认只看未读', () => { // 默认 all 会让模型每轮重读旧邮件,把处理过的和新来的混在一起。 assert.equal(DEFAULT_INBOX_STATUS, 'unread'); }); test('默认条数是个小数字', () => { // 收件箱一次给几十封会把上下文塞满,而模型一轮通常只处理一两封。 assert.ok(DEFAULT_INBOX_LIMIT > 0 && DEFAULT_INBOX_LIMIT <= 10); });