1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00
unleash.unleash/src/lib/addons/datadog.test.ts
Christopher Kolstad 6673d131fe
feat: biome lint (#4853)
This commit changes our linter/formatter to biome (https://biomejs.dev/)
Causing our prehook to run almost instantly, and our "yarn lint" task to
run in sub 100ms.

Some trade-offs:
* Biome isn't quite as well established as ESLint
* Are we ready to install a different vscode plugin (the biome plugin)
instead of the prettier plugin


The configuration set for biome also has a set of recommended rules,
this is turned on by default, in order to get to something that was
mergeable I have turned off a couple the rules we seemed to violate the
most, that we also explicitly told eslint to ignore.
2023-09-29 14:18:21 +02:00

282 lines
8.3 KiB
TypeScript

import {
FEATURE_ARCHIVED,
FEATURE_CREATED,
FEATURE_ENVIRONMENT_DISABLED,
IEvent,
} from '../types/events';
import { Logger } from '../logger';
import DatadogAddon from './datadog';
import noLogger from '../../test/fixtures/no-logger';
let fetchRetryCalls: any[] = [];
jest.mock(
'./addon',
() =>
class Addon {
logger: Logger;
constructor(definition, { getLogger }) {
this.logger = getLogger('addon/test');
fetchRetryCalls = [];
}
async fetchRetry(url, options, retries, backoff) {
fetchRetryCalls.push({
url,
options,
retries,
backoff,
});
return Promise.resolve({ status: 200 });
}
},
);
test('Should call datadog webhook', async () => {
const addon = new DatadogAddon({
getLogger: noLogger,
unleashUrl: 'http://some-url.com',
flagResolver: {
getAll: jest.fn().mockReturnValue([]),
getVariant: jest.fn(),
isEnabled: jest.fn().mockReturnValue(false),
},
});
const event: IEvent = {
id: 1,
createdAt: new Date(),
type: FEATURE_CREATED,
createdBy: 'some@user.com',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
};
const parameters = {
url: 'http://api.datadoghq.com/api/v1/events',
apiKey: 'fakeKey',
};
await addon.handleEvent(event, parameters);
expect(fetchRetryCalls.length).toBe(1);
expect(fetchRetryCalls[0].url).toBe(parameters.url);
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
});
test('Should call datadog webhook for archived toggle', async () => {
const addon = new DatadogAddon({
getLogger: noLogger,
unleashUrl: 'http://some-url.com',
flagResolver: {
getAll: jest.fn().mockReturnValue([]),
getVariant: jest.fn(),
isEnabled: jest.fn().mockReturnValue(false),
},
});
const event: IEvent = {
id: 2,
createdAt: new Date(),
type: FEATURE_ARCHIVED,
createdBy: 'some@user.com',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
},
};
const parameters = {
url: 'http://api.datadoghq.com/api/v1/events',
apiKey: 'fakeKey',
};
await addon.handleEvent(event, parameters);
expect(fetchRetryCalls.length).toBe(1);
expect(fetchRetryCalls[0].url).toBe(parameters.url);
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
});
test('Should call datadog webhook for archived toggle with project info', async () => {
const addon = new DatadogAddon({
getLogger: noLogger,
unleashUrl: 'http://some-url.com',
flagResolver: {
getAll: jest.fn().mockReturnValue([]),
getVariant: jest.fn(),
isEnabled: jest.fn().mockReturnValue(false),
},
});
const event: IEvent = {
id: 2,
createdAt: new Date(),
type: FEATURE_ARCHIVED,
createdBy: 'some@user.com',
featureName: 'some-toggle',
project: 'some-project',
data: {
name: 'some-toggle',
},
};
const parameters = {
url: 'http://api.datadoghq.com/api/v1/events',
apiKey: 'fakeKey',
};
await addon.handleEvent(event, parameters);
expect(fetchRetryCalls.length).toBe(1);
expect(fetchRetryCalls[0].url).toBe(parameters.url);
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
});
test('Should call datadog webhook for toggled environment', async () => {
const addon = new DatadogAddon({
getLogger: noLogger,
unleashUrl: 'http://some-url.com',
flagResolver: {
getAll: jest.fn().mockReturnValue([]),
getVariant: jest.fn(),
isEnabled: jest.fn().mockReturnValue(false),
},
});
const event: IEvent = {
id: 2,
createdAt: new Date(),
type: FEATURE_ENVIRONMENT_DISABLED,
createdBy: 'some@user.com',
environment: 'development',
project: 'default',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
},
};
const parameters = {
url: 'http://hooks.slack.com',
apiKey: 'fakeKey',
};
await addon.handleEvent(event, parameters);
expect(fetchRetryCalls).toHaveLength(1);
expect(fetchRetryCalls[0].url).toBe(parameters.url);
expect(fetchRetryCalls[0].options.body).toMatch(/disabled/);
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
});
test('Should include customHeaders in headers when calling service', async () => {
const addon = new DatadogAddon({
getLogger: noLogger,
unleashUrl: 'http://some-url.com',
flagResolver: {
getAll: jest.fn().mockReturnValue([]),
getVariant: jest.fn(),
isEnabled: jest.fn().mockReturnValue(false),
},
});
const event: IEvent = {
id: 2,
createdAt: new Date(),
type: FEATURE_ENVIRONMENT_DISABLED,
createdBy: 'some@user.com',
environment: 'development',
project: 'default',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
},
};
const parameters = {
url: 'http://hooks.slack.com',
apiKey: 'fakeKey',
customHeaders: `{ "MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE" }`,
};
await addon.handleEvent(event, parameters);
expect(fetchRetryCalls).toHaveLength(1);
expect(fetchRetryCalls[0].url).toBe(parameters.url);
expect(fetchRetryCalls[0].options.body).toMatch(/disabled/);
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
expect(fetchRetryCalls[0].options.headers).toMatchSnapshot();
});
test('Should not include source_type_name when included in the config', async () => {
const addon = new DatadogAddon({
getLogger: noLogger,
unleashUrl: 'http://some-url.com',
flagResolver: {
getAll: jest.fn().mockReturnValue([]),
getVariant: jest.fn(),
isEnabled: jest.fn().mockReturnValue(false),
},
});
const event: IEvent = {
id: 2,
createdAt: new Date(),
type: FEATURE_ENVIRONMENT_DISABLED,
createdBy: 'some@user.com',
environment: 'development',
project: 'default',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
},
};
const parameters = {
url: 'http://hooks.slack.com',
apiKey: 'fakeKey',
sourceTypeName: 'my-custom-source-type',
};
await addon.handleEvent(event, parameters);
expect(fetchRetryCalls).toHaveLength(1);
expect(fetchRetryCalls[0].url).toBe(parameters.url);
expect(fetchRetryCalls[0].options.body).toMatch(
/"source_type_name":"my-custom-source-type"/,
);
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
expect(fetchRetryCalls[0].options.headers).toMatchSnapshot();
});
test('Should call datadog webhook with JSON when template set', async () => {
const addon = new DatadogAddon({
getLogger: noLogger,
unleashUrl: 'http://some-url.com',
flagResolver: {
getAll: jest.fn().mockReturnValue([]),
getVariant: jest.fn(),
isEnabled: jest.fn().mockReturnValue(true),
},
});
const event: IEvent = {
id: 1,
createdAt: new Date(),
type: FEATURE_CREATED,
createdBy: 'some@user.com',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
};
const parameters = {
url: 'http://api.datadoghq.com/api/v1/events',
apiKey: 'fakeKey',
bodyTemplate:
'{\n "event": "{{event.type}}",\n "createdBy": "{{event.createdBy}}"\n}',
};
await addon.handleEvent(event, parameters);
expect(fetchRetryCalls.length).toBe(1);
expect(fetchRetryCalls[0].url).toBe(parameters.url);
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
});