|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { gongConnector } from '../gong.js' |
| 3 | +import { validateConnectorManifest, type ResolvedDataSource } from '../../types.js' |
| 4 | + |
| 5 | +const ACCESS_TOKEN = 'gong_at_test' |
| 6 | +// A normally-connected Gong source carries the per-customer host the hub |
| 7 | +// persisted from the token exchange; tests that exercise real calls default |
| 8 | +// to it. The fail-loud test below passes `source({})` to drop it. |
| 9 | +const PER_CUSTOMER_BASE = 'https://company-17.api.gong.io' |
| 10 | + |
| 11 | +function source(metadata: Record<string, unknown> = { apiBaseUrlForCustomer: PER_CUSTOMER_BASE }): ResolvedDataSource { |
| 12 | + return { |
| 13 | + id: 'src_gong', |
| 14 | + projectId: 'project_1', |
| 15 | + publishedAgentId: null, |
| 16 | + kind: 'gong', |
| 17 | + label: 'Gong', |
| 18 | + consistencyModel: 'authoritative', |
| 19 | + scopes: [], |
| 20 | + metadata, |
| 21 | + credentials: { kind: 'oauth2', accessToken: ACCESS_TOKEN }, |
| 22 | + status: 'active', |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + vi.restoreAllMocks() |
| 28 | +}) |
| 29 | + |
| 30 | +function mockFetch(body: unknown, init: { status?: number; headers?: Record<string, string> } = {}) { |
| 31 | + const fetchMock = vi.fn(async (_input: URL | string, _init?: RequestInit) => new Response(JSON.stringify(body), { |
| 32 | + status: init.status ?? 200, |
| 33 | + headers: { 'content-type': 'application/json', ...init.headers }, |
| 34 | + })) |
| 35 | + vi.stubGlobal('fetch', fetchMock) |
| 36 | + return fetchMock |
| 37 | +} |
| 38 | + |
| 39 | +describe('gong adapter', () => { |
| 40 | + it('ships a valid connector manifest', () => { |
| 41 | + expect(validateConnectorManifest(gongConnector.manifest)).toEqual({ ok: true, issues: [] }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('declares authorization_code oauth2 against app.gong.io with comms classification', () => { |
| 45 | + const auth = gongConnector.manifest.auth |
| 46 | + expect(auth.kind).toBe('oauth2') |
| 47 | + if (auth.kind !== 'oauth2') throw new Error('auth narrowing failed') |
| 48 | + expect(auth.authorizationUrl).toBe('https://app.gong.io/oauth2/authorize') |
| 49 | + expect(auth.tokenUrl).toBe('https://app.gong.io/oauth2/generate-customer-token') |
| 50 | + expect(auth.clientIdEnv).toBe('GONG_OAUTH_CLIENT_ID') |
| 51 | + expect(auth.clientSecretEnv).toBe('GONG_OAUTH_CLIENT_SECRET') |
| 52 | + expect(gongConnector.manifest.category).toBe('comms') |
| 53 | + }) |
| 54 | + |
| 55 | + it('exposes the expected capability surface and read/mutation split', () => { |
| 56 | + const names = gongConnector.manifest.capabilities.map((c) => c.name).sort() |
| 57 | + expect(names).toEqual([ |
| 58 | + 'calls.create', |
| 59 | + 'calls.getExtensive', |
| 60 | + 'calls.getTranscripts', |
| 61 | + 'calls.list', |
| 62 | + 'flows.assignProspects', |
| 63 | + 'users.list', |
| 64 | + ]) |
| 65 | + const mutations = gongConnector.manifest.capabilities.filter((c) => c.class === 'mutation').map((c) => c.name).sort() |
| 66 | + expect(mutations).toEqual(['calls.create', 'flows.assignProspects']) |
| 67 | + }) |
| 68 | + |
| 69 | + it('exposes both executeRead and executeMutation handlers', () => { |
| 70 | + expect(typeof gongConnector.executeRead).toBe('function') |
| 71 | + expect(typeof gongConnector.executeMutation).toBe('function') |
| 72 | + }) |
| 73 | + |
| 74 | + it('declares a CAS strategy for every mutation', () => { |
| 75 | + for (const cap of gongConnector.manifest.capabilities) { |
| 76 | + if (cap.class !== 'mutation') continue |
| 77 | + expect(cap.cas).toBeDefined() |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + it('fails loud (no silent fallback host) when the per-customer base URL is absent', async () => { |
| 82 | + // The generic api.gong.io host is invalid for OAuth apps, so a missing |
| 83 | + // metadata.apiBaseUrlForCustomer must throw rather than route to a host |
| 84 | + // where every call would fail while the connection looks active. |
| 85 | + const fetchMock = mockFetch({ users: [] }) |
| 86 | + await expect( |
| 87 | + gongConnector.executeRead!({ source: source({}), capabilityName: 'users.list', args: {}, idempotencyKey: 'op_0' }), |
| 88 | + ).rejects.toThrow(/missing metadata\.apiBaseUrlForCustomer/) |
| 89 | + expect(fetchMock).not.toHaveBeenCalled() |
| 90 | + }) |
| 91 | + |
| 92 | + it('targets the per-customer base URL from metadata.apiBaseUrlForCustomer when present', async () => { |
| 93 | + const fetchMock = mockFetch({ calls: [] }) |
| 94 | + await gongConnector.executeRead!({ |
| 95 | + source: source({ apiBaseUrlForCustomer: 'https://company-17.api.gong.io' }), |
| 96 | + capabilityName: 'calls.list', |
| 97 | + args: { fromDateTime: '2026-01-01T00:00:00Z' }, |
| 98 | + idempotencyKey: 'op_1', |
| 99 | + }) |
| 100 | + const [url] = fetchMock.mock.calls[0] as [URL, RequestInit] |
| 101 | + expect(url.origin).toBe('https://company-17.api.gong.io') |
| 102 | + expect(url.pathname).toBe('/v2/calls') |
| 103 | + expect(url.searchParams.get('fromDateTime')).toBe('2026-01-01T00:00:00Z') |
| 104 | + }) |
| 105 | + |
| 106 | + it('retrieves transcripts via POST /v2/calls/transcript forwarding the filter object', async () => { |
| 107 | + const fetchMock = mockFetch({ callTranscripts: [] }) |
| 108 | + await gongConnector.executeRead!({ |
| 109 | + source: source(), |
| 110 | + capabilityName: 'calls.getTranscripts', |
| 111 | + args: { filter: { callIds: ['c1', 'c2'] } }, |
| 112 | + idempotencyKey: 'op_2', |
| 113 | + }) |
| 114 | + const [url, init] = fetchMock.mock.calls[0] as [URL, RequestInit] |
| 115 | + expect(url.pathname).toBe('/v2/calls/transcript') |
| 116 | + expect(init.method).toBe('POST') |
| 117 | + expect(JSON.parse(String(init.body))).toEqual({ filter: { callIds: ['c1', 'c2'] } }) |
| 118 | + }) |
| 119 | + |
| 120 | + it('assigns prospects to an Engage flow via POST /v2/flows/prospects/assign', async () => { |
| 121 | + const fetchMock = mockFetch({ ok: true }) |
| 122 | + const result = await gongConnector.executeMutation!({ |
| 123 | + source: source(), |
| 124 | + capabilityName: 'flows.assignProspects', |
| 125 | + args: { flowId: 'flow_1', prospects: [{ crmProspectId: 'p1' }] }, |
| 126 | + idempotencyKey: 'op_3', |
| 127 | + }) |
| 128 | + expect(result.status).toBe('committed') |
| 129 | + const [url, init] = fetchMock.mock.calls[0] as [URL, RequestInit] |
| 130 | + expect(url.pathname).toBe('/v2/flows/prospects/assign') |
| 131 | + expect(JSON.parse(String(init.body))).toEqual({ flowId: 'flow_1', prospects: [{ crmProspectId: 'p1' }] }) |
| 132 | + }) |
| 133 | + |
| 134 | + it('throws CredentialsExpired when Gong rejects the token', async () => { |
| 135 | + vi.stubGlobal('fetch', vi.fn(async () => new Response('unauthorized', { status: 401 }))) |
| 136 | + await expect( |
| 137 | + gongConnector.executeRead!({ source: source(), capabilityName: 'users.list', args: {}, idempotencyKey: 'unauth_1' }), |
| 138 | + ).rejects.toMatchObject({ name: 'CredentialsExpired' }) |
| 139 | + }) |
| 140 | + |
| 141 | + it('rejects unknown capabilities', async () => { |
| 142 | + await expect( |
| 143 | + gongConnector.executeRead!({ source: source(), capabilityName: 'does.not.exist', args: {}, idempotencyKey: 'unknown_1' }), |
| 144 | + ).rejects.toThrow(/unknown read capability/) |
| 145 | + }) |
| 146 | +}) |
0 commit comments