import assert from 'node:assert/strict';
import { test } from 'node:test';

import { decodeModifiedUtf7, encodeModifiedUtf7 } from '../src/imap-utf7.js';

test('Modified UTF-7 keeps printable ASCII and escapes ampersands', () => {
  assert.equal(encodeModifiedUtf7('INBOX/Projects 2026'), 'INBOX/Projects 2026');
  assert.equal(encodeModifiedUtf7('R&D'), 'R&-D');
  assert.equal(decodeModifiedUtf7('R&-D'), 'R&D');
});

test('Modified UTF-7 encodes Unicode mailbox names as UTF-16BE', () => {
  assert.equal(encodeModifiedUtf7('中文'), '&Ti1lhw-');
  assert.equal(decodeModifiedUtf7('&Ti1lhw-'), '中文');
  assert.equal(decodeModifiedUtf7('~peter/mail/&U,BTFw-/&ZeVnLIqe-'), '~peter/mail/台北/日本語');

  const mailbox = '归档/客户 📧 & 2026';
  assert.equal(decodeModifiedUtf7(encodeModifiedUtf7(mailbox)), mailbox);
});

test('Modified UTF-7 preserves malformed encoded sections', () => {
  assert.equal(decodeModifiedUtf7('Broken &Ti1lhw'), 'Broken &Ti1lhw');
  assert.equal(decodeModifiedUtf7('Broken &!!!!- name'), 'Broken &!!!!- name');
  assert.equal(decodeModifiedUtf7('Broken &2AA- name'), 'Broken &2AA- name');
});
