PSA/shared/workflow/actions/__tests__/emailWorkflowActions.responseSource.test.ts
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

209 lines
6.2 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
const updateTicketStateMock = vi.fn();
const trxMock = vi.fn((table: string) => {
if (table !== 'tickets') {
throw new Error(`Unexpected table access in test: ${table}`);
}
return {
where: vi.fn().mockReturnValue({
update: updateTicketStateMock,
}),
};
});
const withAdminTransactionMock = vi.fn(async (callback: (trx: any) => Promise<any>) =>
callback(trxMock)
);
const createCommentMock = vi.fn();
const publishWorkflowEventMock = vi.fn();
vi.mock('@alga-psa/db', () => ({
withAdminTransaction: (callback: (trx: any) => Promise<any>) =>
withAdminTransactionMock(callback),
}));
vi.mock('@alga-psa/shared/models/ticketModel', () => ({
TicketModel: {
createComment: (...args: any[]) => createCommentMock(...args),
},
}));
vi.mock('../../adapters/workflowEventPublisher', () => ({
WorkflowEventPublisher: class WorkflowEventPublisher {},
}));
vi.mock('../../adapters/workflowAnalyticsTracker', () => ({
WorkflowAnalyticsTracker: class WorkflowAnalyticsTracker {},
}));
vi.mock('@alga-psa/event-bus/publishers', () => ({
publishWorkflowEvent: (...args: any[]) => publishWorkflowEventMock(...args),
}));
describe('createCommentFromEmail response source metadata', () => {
beforeEach(() => {
vi.clearAllMocks();
createCommentMock.mockResolvedValue({
comment_id: 'comment-1',
});
updateTicketStateMock.mockResolvedValue(1);
});
it('T002: persists metadata.responseSource=inbound_email', async () => {
const { createCommentFromEmail } = await import('../emailWorkflowActions');
await createCommentFromEmail(
{
ticket_id: 'ticket-1',
content: 'hello',
author_type: 'contact',
metadata: {
email: {
messageId: 'message-1',
},
},
},
'tenant-1'
);
const createCommentInput = createCommentMock.mock.calls[0][0];
expect(createCommentInput.metadata.responseSource).toBe('inbound_email');
});
it('T003: includes normalized provider type when available', async () => {
const { createCommentFromEmail } = await import('../emailWorkflowActions');
await createCommentFromEmail(
{
ticket_id: 'ticket-1',
content: 'hello',
author_type: 'contact',
metadata: {
email: {
messageId: 'message-2',
},
},
inboundReplyEvent: {
messageId: 'message-2',
from: 'client@example.com',
to: ['support@example.com'],
provider: 'microsoft',
matchedBy: 'thread_headers',
},
},
'tenant-1'
);
const createCommentInput = createCommentMock.mock.calls[0][0];
expect(createCommentInput.metadata.email.provider).toBe('microsoft');
expect(createCommentInput.metadata.email.providerType).toBe('microsoft');
});
it.each([
['google', 'T017'],
['microsoft', 'T018'],
['imap', 'T019'],
])(
'%s inbound flow resolves to inbound_email metadata source (%s)',
async (provider) => {
const { createCommentFromEmail } = await import('../emailWorkflowActions');
await createCommentFromEmail(
{
ticket_id: `ticket-${provider}`,
content: `content-${provider}`,
author_type: 'contact',
metadata: {
email: {
messageId: `message-${provider}`,
},
},
inboundReplyEvent: {
messageId: `message-${provider}`,
from: 'client@example.com',
to: ['support@example.com'],
provider,
matchedBy: 'thread_headers',
},
},
'tenant-1'
);
const createCommentInput = createCommentMock.mock.calls[0][0];
expect(createCommentInput.metadata.responseSource).toBe('inbound_email');
expect(createCommentInput.metadata.email.provider).toBe(provider);
}
);
it('T011: forwards contact_id to TicketModel.createComment when provided', async () => {
const { createCommentFromEmail } = await import('../emailWorkflowActions');
await createCommentFromEmail(
{
ticket_id: 'ticket-1',
content: 'hello',
author_type: 'contact',
contact_id: '00000000-0000-0000-0000-000000000456',
},
'tenant-1'
);
const createCommentInput = createCommentMock.mock.calls[0][0];
expect(createCommentInput.contact_id).toBe('00000000-0000-0000-0000-000000000456');
});
it('T012: forwards both author_id and contact_id when both are present', async () => {
const { createCommentFromEmail } = await import('../emailWorkflowActions');
await createCommentFromEmail(
{
ticket_id: 'ticket-1',
content: 'hello',
author_type: 'contact',
author_id: '00000000-0000-0000-0000-000000000123',
contact_id: '00000000-0000-0000-0000-000000000456',
},
'tenant-1'
);
const createCommentInput = createCommentMock.mock.calls[0][0];
expect(createCommentInput.author_id).toBe('00000000-0000-0000-0000-000000000123');
expect(createCommentInput.contact_id).toBe('00000000-0000-0000-0000-000000000456');
});
it('T020: keeps comment response-state semantics unchanged (still non-internal)', async () => {
const { createCommentFromEmail } = await import('../emailWorkflowActions');
await createCommentFromEmail(
{
ticket_id: 'ticket-1',
content: 'reply',
},
'tenant-1'
);
const createCommentInput = createCommentMock.mock.calls[0][0];
expect(createCommentInput.is_internal).toBe(false);
expect(createCommentInput.is_resolution).toBe(false);
});
it('T021: forwards author_id when provided for matched client user association', async () => {
const { createCommentFromEmail } = await import('../emailWorkflowActions');
await createCommentFromEmail(
{
ticket_id: 'ticket-1',
content: 'hello',
author_type: 'contact',
author_id: '00000000-0000-0000-0000-000000000123',
},
'tenant-1'
);
const createCommentInput = createCommentMock.mock.calls[0][0];
expect(createCommentInput.author_id).toBe('00000000-0000-0000-0000-000000000123');
});
});