import { getChinesePinyinInitials, formatChineseDateTitle, searchNotesForMention } from './searchNotesForMention'; import Note from '@joplin/lib/models/Note'; import Folder from '@joplin/lib/models/Folder'; describe('searchNotesForMention', () => { describe('getChinesePinyinInitials', () => { it('extracts correct pinyin initials from Chinese characters', () => { expect(getChinesePinyinInitials('工作周报')).toBe('gzzb'); expect(getChinesePinyinInitials('学习计划')).toBe('xxjh'); expect(getChinesePinyinInitials('会议记录')).toBe('hyjl'); expect(getChinesePinyinInitials('今天日志')).toBe('jtrz'); }); it('preserves alphanumeric characters and converts to lowercase', () => { expect(getChinesePinyinInitials('2026年OKR目标')).toBe('2026nokrmb'); expect(getChinesePinyinInitials('Joplin开发')).toBe('joplinkf'); }); }); describe('formatChineseDateTitle', () => { it('formats date with correct Chinese weekday', () => { // 2026-11-01 is a Sunday const timestamp = new Date('2026-11-01T12:00:00Z').getTime(); const title = formatChineseDateTitle(timestamp); expect(title).toMatch(/^2026-11-01 星期/); }); }); describe('searchNotesForMention query logic', () => { const originalDb = Note.db; const originalFolderAll = Folder.all; beforeEach(() => { Folder.all = jest.fn().mockResolvedValue([ { id: 'folder-1', title: '工作' }, { id: 'folder-2', title: '个人' }, ]); }); afterEach(() => { Note.db = originalDb; Folder.all = originalFolderAll; }); it('suggests create today note when no note exists for today', async () => { Note.db = jest.fn().mockReturnValue({ selectOne: jest.fn().mockResolvedValue(null), selectAll: jest.fn().mockResolvedValue([]), }) as any; const results = await searchNotesForMention('今天'); expect(results.length).toBeGreaterThanOrEqual(1); expect(results[0].id).toMatch(/^__CREATE_/); expect(results[0].parentTitle).toBe('✨ 新建今日日志'); expect(results[0].section).toBe('📅 日志推荐'); }); it('matches existing note and attaches parentTitle folder name', async () => { Note.db = jest.fn().mockReturnValue({ selectOne: jest.fn().mockResolvedValue(null), selectAll: jest.fn().mockResolvedValue([ { id: 'note-123', title: '项目进度汇报', parent_id: 'folder-1' }, ]), }) as any; const results = await searchNotesForMention('进度'); const matched = results.find(r => r.id === 'note-123'); expect(matched).toBeDefined(); expect(matched?.title).toBe('项目进度汇报'); expect(matched?.parentTitle).toBe('工作'); expect(matched?.section).toBe('📝 关联笔记'); }); it('supports direct date input like 2026-12-25 and offers to create or link', async () => { Note.db = jest.fn().mockReturnValue({ selectOne: jest.fn().mockResolvedValue(null), selectAll: jest.fn().mockResolvedValue([]), }) as any; const results = await searchNotesForMention('2026-12-25'); const dateItem = results.find(r => r.id.includes('2026-12-25')); expect(dateItem).toBeDefined(); expect(dateItem?.parentTitle).toBe('✨ 新建 2026-12-25 日志'); expect(dateItem?.title).toMatch(/^2026-12-25 星期/); }); }); });