| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import assert from 'node:assert/strict';
- import { test } from 'node:test';
- import {
- buildApiUsageExamples,
- buildInboundMessageApiUsageExamples,
- buildMailboxApiUsageExamples,
- canCopyFullApiToken,
- formatApiTokenPrefix,
- getCopyableApiToken,
- getCreatedApiTokenSecret
- } from '../src/frontend/api-token-model.js';
- test('only exposes full API token when the create response includes the secret', () => {
- const created = { tokenPrefix: 'mh_123456789', token: 'mh_123456789.full-secret' };
- const explicitlyUnrecoverable = { ...created, tokenRecoverable: false };
- const listed = { tokenPrefix: 'mh_987654321' };
- // Keep compatibility with older create-response fixtures that predate tokenRecoverable.
- assert.equal(canCopyFullApiToken(created), true);
- assert.equal(getCreatedApiTokenSecret(created), 'mh_123456789.full-secret');
- assert.equal(getCopyableApiToken(created), 'mh_123456789.full-secret');
- assert.equal(canCopyFullApiToken(explicitlyUnrecoverable), false);
- assert.equal(canCopyFullApiToken(listed), false);
- assert.equal(getCreatedApiTokenSecret(listed), '');
- assert.equal(getCopyableApiToken(listed), '');
- });
- test('formats token prefixes without pretending the full secret is available', () => {
- assert.equal(formatApiTokenPrefix({ tokenPrefix: 'mh_abcdef1234' }), 'mh_abcdef1234...');
- assert.equal(formatApiTokenPrefix({}), '-');
- });
- test('builds API usage examples with endpoint, bearer token, and message body', () => {
- const examples = buildApiUsageExamples({
- endpoint: 'https://mailhub.example.com/api/send',
- token: 'mh_example_token',
- from: 'noreply@example.com',
- to: 'user@example.com'
- });
- assert.match(examples.curl, /Authorization: Bearer mh_example_token/);
- assert.match(examples.curl, /https:\/\/mailhub\.example\.com\/api\/send/);
- assert.match(examples.nodeFetch, /fetch\('https:\/\/mailhub\.example\.com\/api\/send'/);
- assert.match(examples.nodeFetch, /from: 'noreply@example.com'/);
- assert.match(examples.requestBody, /"to": "user@example.com"/);
- assert.match(examples.successResponse, /"queued": true/);
- });
- test('builds persistent and temporary mailbox API examples', () => {
- const examples = buildMailboxApiUsageExamples({
- endpoint: 'https://mailhub.example.com/api/mailboxes',
- token: 'mh_mailbox_token',
- domain: 'example.com'
- });
- assert.match(examples.permanentCurl, /"mode": "permanent"/);
- assert.match(examples.temporaryCurl, /Authorization: Bearer mh_mailbox_token/);
- assert.match(examples.temporary, /"expiresInMinutes": 60/);
- assert.match(examples.successResponse, /"temporary": true/);
- });
- test('builds inbound message list detail and folder API examples', () => {
- const examples = buildInboundMessageApiUsageExamples({
- endpoint: 'https://mailhub.example.com/api/inbound-messages',
- mailboxEndpoint: 'https://mailhub.example.com/api/inbound-mailboxes',
- token: 'mh_messages_token',
- mailboxId: 42,
- messageId: 108
- });
- assert.match(examples.listCurl, /page=1&pageSize=20&mailboxId=42&folder=INBOX&read=false&q=invoice/);
- assert.match(examples.detailCurl, /api\/inbound-messages\/108/);
- assert.match(examples.foldersCurl, /api\/inbound-mailboxes\/42\/folders/);
- assert.match(examples.listCurl, /Authorization: Bearer mh_messages_token/);
- assert.match(examples.listResponse, /"total": 1/);
- assert.match(examples.detailResponse, /"textBody": "Invoice details"/);
- assert.equal(JSON.parse(examples.foldersResponse).folders[1].specialUse, '\\Sent');
- });
|