import { runtime, getTodayDatePrefix, getTodayNoteTitle, normalizeDateWeekdayToChinese } from './openTodayNote'; import { setupDatabaseAndSynchronizer, switchClient } from '@joplin/lib/testing/test-utils'; import Note from '@joplin/lib/models/Note'; import Folder from '@joplin/lib/models/Folder'; import Setting from '@joplin/lib/models/Setting'; describe('openTodayNote', () => { beforeEach(async () => { await setupDatabaseAndSynchronizer(1); await switchClient(1); }); it('formats date prefix and note title correctly in Chinese weekday', () => { const fixedDate = new Date(2026, 10, 1, 12, 0, 0); // 2026-11-01 (星期日) const prefix = getTodayDatePrefix(fixedDate.getTime()); expect(prefix).toBe('2026-11-01'); const titleSunday = getTodayNoteTitle(fixedDate.getTime()); expect(titleSunday).toBe('2026-11-01 星期日'); const mondayDate = new Date(2026, 10, 2, 12, 0, 0); // 2026-11-02 (星期一) const titleMonday = getTodayNoteTitle(mondayDate.getTime()); expect(titleMonday).toBe('2026-11-02 星期一'); }); it('normalizes English weekdays to Chinese weekdays', () => { expect(normalizeDateWeekdayToChinese('2026-11-02 Monday')).toBe('2026-11-02 星期一'); expect(normalizeDateWeekdayToChinese('# 2026-11-02 Monday\n\n• [ ]')).toBe('# 2026-11-02 星期一\n\n• [ ]'); expect(normalizeDateWeekdayToChinese('2026-11-03 Tuesday')).toBe('2026-11-03 星期二'); }); it('creates a new today note if none exists', async () => { const activeFolder = await Folder.save({ title: 'Work' }); Setting.setValue('activeFolderId', activeFolder.id); await runtime().execute(null); const prefix = getTodayDatePrefix(); const notes = await Note.search({ query: prefix }); expect(notes.length).toBe(1); expect(notes[0].title.startsWith(prefix)).toBe(true); expect(notes[0].title).toMatch(/星期[一二三四五六日]/); expect(notes[0].body).toContain(`- [ ] `); }); it('reuses existing today note and migrates English weekday to Chinese weekday', async () => { const activeFolder = await Folder.save({ title: 'Work' }); Setting.setValue('activeFolderId', activeFolder.id); const todayPrefix = getTodayDatePrefix(); const existing = await Note.save({ title: `${todayPrefix} Monday`, body: `# ${todayPrefix} Monday\n\n• [ ] 备忘录`, parent_id: activeFolder.id, }); await runtime().execute(null); const loaded = await Note.load(existing.id); expect(loaded.id).toBe(existing.id); expect(loaded.title).toMatch(/星期[一二三四五六日]/); expect(loaded.title).not.toContain('Monday'); expect(loaded.body).toMatch(/星期[一二三四五六日]/); expect(loaded.body).not.toContain('Monday'); }); });