'use strict'; // Evernote .enex: notes to cards with their to-dos as a checklist, tags as // labels and the reminder as the due date (models/lib/enexFormat.js, parsed on // the server by server/lib/enexImport.js), and a board written back as .enex. // Run: node --test tests/enexFormat.test.cjs const test = require('node:test'); const assert = require('node:assert'); const { readEnex, enmlText } = require('../server/lib/enexImport'); const ENEX = ` Shopping & errands 20261101T080000Z 20261102T080000Z homeweekly 20261120T090000Zhttps://example.com/list
Before the weekend.
Milk
Bread <whole>
]]>
iVBORw0KGgo=image/png
Done one 20261110T090000Z20261110T100000Z ok]]>
`; let enex; test.before(async () => { enex = await import('../models/lib/enexFormat.js'); }); test('notes become cards with their to-dos, tags, reminder and done state', () => { const { notes } = readEnex(ENEX); const board = enex.enexBoard(notes); assert.deepStrictEqual(board.columns.map(c => c.title), ['Notes', 'Done']); const [shop, done] = board.tasks; assert.strictEqual(shop.title, 'Shopping & errands'); assert.strictEqual(shop.description, 'Before the weekend.\n\nhttps://example.com/list'); assert.deepStrictEqual(shop.checklists, [{ title: 'To-do', items: [{ title: 'Milk', done: true }, { title: 'Bread ', done: false }] }]); assert.deepStrictEqual(shop.tags, ['home', 'weekly']); assert.strictEqual(shop.date_due, '2026-11-20T09:00:00.000Z'); assert.strictEqual(shop.date_creation, '2026-11-01T08:00:00.000Z'); assert.strictEqual(done.column_name, 'Done'); assert.strictEqual(done.due_complete, true); assert.ok(board.unsupported.some(u => /1 attached file/.test(u.reason))); }); test('negative: no script, no markup and no non-ENEX file', () => { assert.doesNotMatch(enmlText('
a
'), /alert/); const { notes } = readEnex(ENEX); assert.doesNotMatch(JSON.stringify(enex.enexBoard(notes)), / readEnex(''), /no /); }); test('a board exported as .enex reads back as the same notes', () => { const out = enex.formatEnex({ items: [ { title: 'Order ', description: 'Two & more\n]]> tricky', labels: ['shop'], listTitle: 'To Do', creator: 'anna', createdAt: '2026-11-01T08:00:00.000Z', dueAt: '2026-11-20T09:00:00.000Z', checklists: [{ title: 'Steps', items: [{ title: 'Measure', done: true }, { title: 'Fit', done: false }] }], comments: [{ author: 'matti', text: 'Ordered' }] }, ] }, { now: new Date('2026-11-16T10:00:00Z') }); assert.match(out, /'); assert.match(card.description, /^Two & more\n\]\]> tricky\nSteps\n/); assert.match(card.description, /matti: Ordered/); assert.deepStrictEqual(card.checklists[0].items, [{ title: 'Measure', done: true }, { title: 'Fit', done: false }]); assert.deepStrictEqual(card.tags, ['shop', 'To Do']); assert.strictEqual(card.date_due, '2026-11-20T09:00:00.000Z'); }); test('a large board is exported a page at a time: the pages together are the whole file', async () => { const mail = await import('../models/lib/mailFormat.js'); const items = Array.from({ length: 5 }, (_, i) => ({ cardId: `c${i}`, title: `Card ${i}`, description: `Text ${i}`, creator: 'anna', createdAt: `2026-11-0${i + 1}T10:00:00.000Z`, comments: i % 2 ? [{ text: 'ok', author: 'matti' }] : [] })); const now = new Date('2026-11-16T10:00:00Z'); const pages = [items.slice(0, 2), items.slice(2, 4), items.slice(4)]; assert.strictEqual(enex.enexHead(now) + pages.map(p => enex.enexNotes(p)).join('') + enex.ENEX_TAIL, enex.formatEnex({ items }, { now }), 'an .enex: head, the notes page by page, tail'); assert.strictEqual(pages.map(p => mail.formatMbox({ items: p })).join('\n'), mail.formatMbox({ items }), 'an .mbox: the messages page by page'); const fs = require('node:fs'); const path = require('node:path'); const read = rel => fs.readFileSync(path.join(__dirname, '..', rel), 'utf8'); const render = read('server/lib/renderExternalExport.js'); assert.match(render, /for await \(const collected of externalExportPages\(boardId, format, fields, 500, options\)\)/); assert.match(render, /if \(text && !res\.write\(text\)\) await new Promise\(resolve => res\.once\('drain', resolve\)\);/, 'it waits for the socket, so pages do not pile up in memory'); assert.match(read('models/lib/externalExporters.js'), /export async function\* externalExportPages\(boardId, format, fields, pageSize = 500, options = \{\}\)/); assert.match(read('models/export.js'), /streamExternalExport\(res, boardId, format, fields\)\) return;/); });