|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ json: vi.fn(), empty: vi.fn() })) |
| 7 | +vi.mock('@/lib/internal/oracle-fusion/client', () => ({ |
| 8 | + requestOracleFusionJson: mocks.json, |
| 9 | + requestOracleFusionEmpty: mocks.empty, |
| 10 | +})) |
| 11 | + |
| 12 | +import { OracleFusionProviderError } from '@/lib/internal/oracle-fusion/errors' |
| 13 | +import { executeOracleFusionSalesTool } from '@/lib/internal/oracle-fusion-sales/execute-tool' |
| 14 | + |
| 15 | +const AUTH = { |
| 16 | + oauthCredential: 'credential-1', |
| 17 | + accessToken: 'test-token', |
| 18 | + instanceUrl: 'https://vision.fa.us2.oraclecloud.com', |
| 19 | +} |
| 20 | + |
| 21 | +function call(toolId: string, params: Record<string, unknown> = {}, signal?: AbortSignal) { |
| 22 | + return executeOracleFusionSalesTool({ |
| 23 | + toolId, |
| 24 | + input: { ...AUTH, ...params }, |
| 25 | + headers: new Headers(), |
| 26 | + context: { workflowId: 'workflow-1', workspaceId: 'workspace-1', userId: 'user-1' }, |
| 27 | + requestId: 'request-1', |
| 28 | + signal, |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +describe('Oracle Fusion Sales internal handler', () => { |
| 33 | + beforeEach(() => vi.clearAllMocks()) |
| 34 | + |
| 35 | + it('rejects invalid required inputs before provider requests', async () => { |
| 36 | + const response = await call('oracle_fusion_sales_create_account') |
| 37 | + expect(response.status).toBe(400) |
| 38 | + expect(await response.json()).toMatchObject({ success: false, retryable: false }) |
| 39 | + expect(mocks.json).not.toHaveBeenCalled() |
| 40 | + expect(mocks.empty).not.toHaveBeenCalled() |
| 41 | + }) |
| 42 | + |
| 43 | + it('returns documented action results without asserting business success', async () => { |
| 44 | + mocks.json.mockResolvedValue({ result: 'Failure' }) |
| 45 | + const response = await call('oracle_fusion_sales_accept_lead', { leadId: '123' }) |
| 46 | + expect(response.status).toBe(200) |
| 47 | + expect(await response.json()).toEqual({ success: true, output: { result: 'Failure' } }) |
| 48 | + }) |
| 49 | + |
| 50 | + it('uses the empty-body contract for deletes', async () => { |
| 51 | + mocks.empty.mockResolvedValue(undefined) |
| 52 | + const response = await call('oracle_fusion_sales_delete_account', { accountNumber: 'A001' }) |
| 53 | + expect(await response.json()).toEqual({ success: true, output: { deleted: true } }) |
| 54 | + expect(mocks.json).not.toHaveBeenCalled() |
| 55 | + }) |
| 56 | + |
| 57 | + it.each([401, 403, 404, 429, 503])('preserves safe provider status %s', async (status) => { |
| 58 | + mocks.json.mockRejectedValue( |
| 59 | + new OracleFusionProviderError('Oracle Fusion denied this request', status) |
| 60 | + ) |
| 61 | + const response = await call('oracle_fusion_sales_accept_lead', { leadId: '123' }) |
| 62 | + expect(response.status).toBe(status) |
| 63 | + expect(await response.json()).toEqual({ |
| 64 | + success: false, |
| 65 | + error: 'Oracle Fusion denied this request', |
| 66 | + retryable: false, |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('does not disclose unexpected provider or credential errors', async () => { |
| 71 | + mocks.json.mockRejectedValue(new Error('private-provider-canary')) |
| 72 | + const response = await call('oracle_fusion_sales_accept_lead', { leadId: '123' }) |
| 73 | + expect(response.status).toBe(500) |
| 74 | + expect(await response.text()).not.toContain('private-provider-canary') |
| 75 | + }) |
| 76 | + |
| 77 | + it('rejects malformed action output', async () => { |
| 78 | + mocks.json.mockResolvedValue({ result: { OptyId: 123 } }) |
| 79 | + const response = await call('oracle_fusion_sales_convert_lead', { leadId: '123' }) |
| 80 | + expect(response.status).toBe(502) |
| 81 | + expect(await response.json()).toMatchObject({ success: false }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('preserves cancellation before and during provider execution', async () => { |
| 85 | + const controller = new AbortController() |
| 86 | + const reason = new DOMException('Cancelled', 'AbortError') |
| 87 | + controller.abort(reason) |
| 88 | + await expect(call('oracle_fusion_sales_list_accounts', {}, controller.signal)).rejects.toBe( |
| 89 | + reason |
| 90 | + ) |
| 91 | + expect(mocks.json).not.toHaveBeenCalled() |
| 92 | + |
| 93 | + const pending = new AbortController() |
| 94 | + mocks.json.mockImplementation(async () => { |
| 95 | + pending.abort(reason) |
| 96 | + return { result: 'Successful' } |
| 97 | + }) |
| 98 | + await expect( |
| 99 | + call('oracle_fusion_sales_accept_lead', { leadId: '123' }, pending.signal) |
| 100 | + ).rejects.toBe(reason) |
| 101 | + expect(mocks.json.mock.calls[0][2]).toBe(pending.signal) |
| 102 | + }) |
| 103 | + |
| 104 | + it('does not dispatch unrelated or inherited object property names', async () => { |
| 105 | + for (const toolId of [ |
| 106 | + 'oracle_fusion_service_list_accounts', |
| 107 | + 'oracle_fusion_sales_constructor', |
| 108 | + ]) { |
| 109 | + expect((await call(toolId)).status).toBe(500) |
| 110 | + } |
| 111 | + expect(mocks.json).not.toHaveBeenCalled() |
| 112 | + }) |
| 113 | +}) |
0 commit comments