Files
alibaba/tests/payload.test.js
T

98 lines
3.0 KiB
JavaScript
Raw Normal View History

2026-08-27 16:12:37 +08:00
import { describe, it, expect } from 'vitest';
import { buildIngestPayload } from '../push/payload.js';
describe('buildIngestPayload', () => {
it('maps record + messages to ingest schema', () => {
const record = {
inquiryId: '13776617920',
manager: 'cara Hu',
createTimeFromApi: '2026-03-13T10:11:50Z',
updateTimeFromApi: '2026-07-13T10:11:50Z',
status: '洽谈中',
msgPreview: 'hi',
isFlagged: false,
customerId: 'cid-1',
detailBuyerName: 'Carlos',
detailCountry: 'PE',
company: 'World Shop',
registerTime: '2025-06-23T00:00:00Z',
behavior: { productViews: '0', validRFQ: '39' },
};
const messages = [
{
messageId: 'm1',
sendTime: '2026-07-13T02:11:50Z',
sender: 'cara Hu',
original: 'hello',
direction: 'outgoing',
},
];
const out = buildIngestPayload(record, messages);
expect(out.inquiry.inquiryId).toBe('13776617920');
expect(out.inquiry.createTime).toBe('2026-03-13T10:11:50Z');
expect(out.customer.customerId).toBe('cid-1');
expect(out.customer.country).toBe('PE');
expect(out.customer.behavior.productViews).toBe(0); // numeric string -> number
expect(out.customer.behavior.validRfq).toBe(39);
expect(out.messages[0].messageId).toBe('m1');
expect(out.messages[0].content).toBe('hello'); // original -> content
});
it('null customer when no customerId', () => {
const out = buildIngestPayload(
{
inquiryId: 'X',
createTimeFromApi: '2026-01-01T00:00:00Z',
updateTimeFromApi: '2026-01-01T00:00:00Z',
},
[
{
messageId: 'm',
sendTime: '2026-01-01T00:00:00Z',
direction: 'outgoing',
content: 'c',
},
],
);
expect(out.customer).toBeNull();
});
it('allows empty messages (chat deferred to later phase)', () => {
const out = buildIngestPayload(
{ inquiryId: 'X', createTimeFromApi: '2026-01-01T00:00:00Z', updateTimeFromApi: '2026-01-01T00:00:00Z' },
[],
);
expect(out.messages).toEqual([]);
expect(out.inquiry.inquiryId).toBe('X');
});
it('non-ISO createTime (list page "11:58") falls back to current ISO', () => {
const out = buildIngestPayload({ inquiryId: 'X', createTime: '11:58', updateTime: '13:39' }, []);
expect(out.inquiry.createTime).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/);
expect(out.inquiry.updateTime).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/);
});
it('empty string -> null for optional fields', () => {
const out = buildIngestPayload(
{
inquiryId: 'X',
createTimeFromApi: '2026-01-01T00:00:00Z',
updateTimeFromApi: '2026-01-01T00:00:00Z',
customerId: 'c',
email: '',
company: '',
},
[
{
messageId: 'm',
sendTime: '2026-01-01T00:00:00Z',
direction: 'outgoing',
content: 'c',
},
],
);
expect(out.customer.email).toBeNull();
expect(out.customer.company).toBeNull();
});
});