UNPKG

novu

Version:

Novu CLI. Run Novu Studio and sync workflows with Novu Cloud

81 lines (80 loc) 3.73 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const axios_1 = __importDefault(require("axios")); const sync_1 = require("./sync"); vitest_1.vi.mock('axios', () => { return { default: { post: vitest_1.vi.fn(), get: vitest_1.vi.fn(), }, }; }); (0, vitest_1.describe)('sync command', () => { (0, vitest_1.describe)('sync function', () => { (0, vitest_1.afterEach)(() => { vitest_1.vi.clearAllMocks(); }); (0, vitest_1.it)('happy case of execute sync functions', async () => { const bridgeUrl = 'https://bridge.novu.co'; const secretKey = 'your-api-key'; const apiUrl = 'https://api.novu.co'; const syncData = { someData: 'from sync' }; const syncRestCallSpy = vitest_1.vi.spyOn(axios_1.default, 'post'); axios_1.default.post.mockResolvedValueOnce({ data: syncData, }); const response = await (0, sync_1.sync)(bridgeUrl, secretKey, apiUrl); const expectBackendUrl = `${apiUrl}/v1/bridge/sync?source=cli`; (0, vitest_1.expect)(syncRestCallSpy).toHaveBeenCalledWith(expectBackendUrl, vitest_1.expect.objectContaining({ bridgeUrl }), vitest_1.expect.objectContaining({ headers: { Authorization: vitest_1.expect.any(String), 'Content-Type': 'application/json' } })); (0, vitest_1.expect)(response).toEqual(syncData); }); (0, vitest_1.it)('syncState - network error on sync', async () => { const bridgeUrl = 'https://bridge.novu.co'; const secretKey = 'your-api-key'; const apiUrl = 'https://api.novu.co'; axios_1.default.post.mockRejectedValueOnce(new Error('Network error')); try { await (0, sync_1.sync)(bridgeUrl, secretKey, apiUrl); } catch (error) { (0, vitest_1.expect)(error.message).toBe('Network error'); } }); (0, vitest_1.it)('syncState - unexpected error', async () => { const bridgeUrl = 'https://bridge.novu.co'; const secretKey = 'your-api-key'; const apiUrl = 'https://api.novu.co'; axios_1.default.get.mockResolvedValueOnce({ data: {} }); axios_1.default.post.mockImplementationOnce(() => { throw new Error('Unexpected error'); }); try { await (0, sync_1.sync)(bridgeUrl, secretKey, apiUrl); } catch (error) { (0, vitest_1.expect)(error.message).toBe('Unexpected error'); } }); }); (0, vitest_1.describe)('buildSignature function', () => { (0, vitest_1.it)('buildSignature - generates valid signature format', () => { const secretKey = 'your-api-key'; const signature = (0, sync_1.buildSignature)(secretKey); (0, vitest_1.expect)(signature).toMatch(/^t=\d+,v1=[0-9a-f]{64}$/); }); (0, vitest_1.it)('buildSignature - generates different signatures for different timestamps', async () => { const secretKey = 'your-api-key'; const signature1 = (0, sync_1.buildSignature)(secretKey); await new Promise((resolve) => { setTimeout(resolve, 10); }); const signature2 = (0, sync_1.buildSignature)(secretKey); (0, vitest_1.expect)(signature1).not.toEqual(signature2); }); }); });