|
| 1 | +import { expect, test, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { LogLevelEnum } from '../../client-common/src/types'; |
| 4 | +import { createConsoleLogger } from '../../logger-console/src/logger'; |
| 5 | +import { algoliasearch as node_algoliasearch } from '../builds/node'; |
| 6 | +import { algoliasearch, apiClientVersion } from '../builds/worker'; |
| 7 | + |
| 8 | +test('sets the ua', () => { |
| 9 | + const client = algoliasearch('APP_ID', 'API_KEY'); |
| 10 | + expect(client.transporter.algoliaAgent).toEqual({ |
| 11 | + add: expect.any(Function), |
| 12 | + value: expect.stringContaining(`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Worker`), |
| 13 | + }); |
| 14 | +}); |
| 15 | + |
| 16 | +test('forwards node search helpers', () => { |
| 17 | + const client = algoliasearch('APP_ID', 'API_KEY'); |
| 18 | + expect(client.generateSecuredApiKey).not.toBeUndefined(); |
| 19 | + expect(client.getSecuredApiKeyRemainingValidity).not.toBeUndefined(); |
| 20 | + expect(async () => { |
| 21 | + const resp = await client.generateSecuredApiKey({ parentApiKey: 'foo', restrictions: { validUntil: 200 } }); |
| 22 | + client.getSecuredApiKeyRemainingValidity({ securedApiKey: resp }); |
| 23 | + }).not.toThrow(); |
| 24 | +}); |
| 25 | + |
| 26 | +test('web crypto implementation gives the same result as node crypto', async () => { |
| 27 | + const client = algoliasearch('APP_ID', 'API_KEY'); |
| 28 | + const nodeClient = node_algoliasearch('APP_ID', 'API_KEY'); |
| 29 | + const resp = await client.generateSecuredApiKey({ parentApiKey: 'foo-bar', restrictions: { validUntil: 200 } }); |
| 30 | + const nodeResp = await nodeClient.generateSecuredApiKey({ |
| 31 | + parentApiKey: 'foo-bar', |
| 32 | + restrictions: { validUntil: 200 }, |
| 33 | + }); |
| 34 | + |
| 35 | + expect(resp).toEqual(nodeResp); |
| 36 | +}); |
| 37 | + |
| 38 | +test('with logger', () => { |
| 39 | + vi.spyOn(console, 'debug'); |
| 40 | + vi.spyOn(console, 'info'); |
| 41 | + vi.spyOn(console, 'error'); |
| 42 | + |
| 43 | + const client = algoliasearch('APP_ID', 'API_KEY', { |
| 44 | + logger: createConsoleLogger(LogLevelEnum.Debug), |
| 45 | + }); |
| 46 | + |
| 47 | + expect(async () => { |
| 48 | + await client.setSettings({ indexName: 'foo', indexSettings: {} }); |
| 49 | + expect(console.debug).toHaveBeenCalledTimes(1); |
| 50 | + expect(console.info).toHaveBeenCalledTimes(1); |
| 51 | + expect(console.error).toHaveBeenCalledTimes(1); |
| 52 | + }).not.toThrow(); |
| 53 | +}); |
0 commit comments