import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import type { Ticket } from '@wa-ticketing/shared';
import { getWhatsAppCapabilities } from './whatsappCapabilities.js';

// db.ts throws without DATABASE_URL at import time; the postgres client connects lazily, so a dummy is harmless.
process.env.DATABASE_URL ??= 'postgres://test:test@127.0.0.1:1/test';
const { mapTicketRow } = await import('./ticketService.js');

const ticketWithChannel = (channel: Record<string, unknown> | null) =>
  ({ id: 't1', created_at: '2026-01-01T00:00:00Z', intake: null, channel }) as unknown as Ticket;

describe('mapTicketRow embedded channel', () => {
  it('enriches capabilities and strips the inbound token', () => {
    const out = mapTicketRow(
      ticketWithChannel({ id: 'c1', kind: 'whatsapp', provider: 'africas_talking', inbound_token: 'secret' }),
    );
    assert.deepEqual(out.channel?.capabilities, getWhatsAppCapabilities('africas_talking'));
    assert.equal(JSON.stringify(out).includes('secret'), false);
  });

  it('strips email channel tokens and tolerates a missing channel', () => {
    const email = mapTicketRow(ticketWithChannel({ id: 'c2', kind: 'email', provider: null, inbound_token: 'secret' }));
    assert.equal(JSON.stringify(email).includes('secret'), false);
    assert.equal(mapTicketRow(ticketWithChannel(null)).channel, null);
  });
});
