misc.test.ts 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { sortAlerts } from 'app/features/alerting/unified/utils/misc';
  2. import { SortOrder } from 'app/plugins/panel/alertlist/types';
  3. import { Alert } from 'app/types/unified-alerting';
  4. import { GrafanaAlertState } from 'app/types/unified-alerting-dto';
  5. function withState(state: GrafanaAlertState, labels?: {}): Alert {
  6. return { activeAt: '', annotations: {}, labels: labels || {}, state: state, value: '' };
  7. }
  8. function withDate(activeAt?: string, labels?: {}): Alert {
  9. return {
  10. activeAt: activeAt || '',
  11. annotations: {},
  12. labels: labels || {},
  13. state: GrafanaAlertState.Alerting,
  14. value: '',
  15. };
  16. }
  17. function permute(inputArray: any[]): any[] {
  18. return inputArray.reduce(function permute(res, item, key, arr) {
  19. return res.concat(
  20. (arr.length > 1 &&
  21. arr
  22. .slice(0, key)
  23. .concat(arr.slice(key + 1))
  24. .reduce(permute, [])
  25. .map(function (perm: any) {
  26. return [item].concat(perm);
  27. })) ||
  28. item
  29. );
  30. }, []);
  31. }
  32. describe('Unified Altering misc', () => {
  33. describe('sortAlerts', () => {
  34. describe('when using any sortOrder with a list of alert instances', () => {
  35. it.each`
  36. alerts | sortOrder | expected
  37. ${[withState(GrafanaAlertState.Pending), withState(GrafanaAlertState.Alerting), withState(GrafanaAlertState.Normal)]} | ${SortOrder.Importance} | ${[withState(GrafanaAlertState.Alerting), withState(GrafanaAlertState.Pending), withState(GrafanaAlertState.Normal)]}
  38. ${[withState(GrafanaAlertState.Pending), withState(GrafanaAlertState.Alerting), withState(GrafanaAlertState.NoData)]} | ${SortOrder.Importance} | ${[withState(GrafanaAlertState.Alerting), withState(GrafanaAlertState.Pending), withState(GrafanaAlertState.NoData)]}
  39. ${[withState(GrafanaAlertState.Pending), withState(GrafanaAlertState.Error), withState(GrafanaAlertState.Normal)]} | ${SortOrder.Importance} | ${[withState(GrafanaAlertState.Error), withState(GrafanaAlertState.Pending), withState(GrafanaAlertState.Normal)]}
  40. ${[withDate('2021-11-29T14:10:07-05:00'), withDate('2021-11-29T15:10:07-05:00'), withDate('2021-11-29T13:10:07-05:00')]} | ${SortOrder.TimeAsc} | ${[withDate('2021-11-29T13:10:07-05:00'), withDate('2021-11-29T14:10:07-05:00'), withDate('2021-11-29T15:10:07-05:00')]}
  41. ${[withDate('2021-11-29T14:10:07-05:00'), withDate('2021-11-29T15:10:07-05:00'), withDate('2021-11-29T13:10:07-05:00')]} | ${SortOrder.TimeDesc} | ${[withDate('2021-11-29T15:10:07-05:00'), withDate('2021-11-29T14:10:07-05:00'), withDate('2021-11-29T13:10:07-05:00')]}
  42. ${[withDate('', { mno: 'pqr' }), withDate('', { abc: 'def' }), withDate('', { ghi: 'jkl' })]} | ${SortOrder.AlphaAsc} | ${[withDate('', { abc: 'def' }), withDate('', { ghi: 'jkl' }), withDate('', { mno: 'pqr' })]}
  43. ${[withDate('', { mno: 'pqr' }), withDate('', { abc: 'def' }), withDate('', { ghi: 'jkl' })]} | ${SortOrder.AlphaDesc} | ${[withDate('', { mno: 'pqr' }), withDate('', { ghi: 'jkl' }), withDate('', { abc: 'def' })]}
  44. `('then it should sort the alerts correctly', ({ alerts, sortOrder, expected }) => {
  45. const result = sortAlerts(sortOrder, alerts);
  46. expect(result).toEqual(expected);
  47. });
  48. });
  49. describe('when sorting ties', () => {
  50. it.each`
  51. alerts | sortOrder
  52. ${[withState(GrafanaAlertState.Alerting, { ghi: 'jkl' }), withState(GrafanaAlertState.Alerting, { abc: 'def' }), withState(GrafanaAlertState.Alerting)]} | ${SortOrder.Importance}
  53. ${[withDate('2021-11-29T13:10:07-05:00', { ghi: 'jkl' }), withDate('2021-11-29T13:10:07-05:00'), withDate('2021-11-29T13:10:07-05:00', { abc: 'def' })]} | ${SortOrder.TimeAsc}
  54. ${[withDate('2021-11-29T13:10:07-05:00', { ghi: 'jkl' }), withDate('2021-11-29T13:10:07-05:00'), withDate('2021-11-29T13:10:07-05:00', { abc: 'def' })]} | ${SortOrder.TimeDesc}
  55. `('then tie order should be deterministic', ({ alerts, sortOrder }) => {
  56. // All input permutations should result in the same sorted order
  57. const sortedPermutations = permute(alerts).map((a) => sortAlerts(sortOrder, a));
  58. sortedPermutations.forEach((p) => {
  59. expect(p).toEqual(sortedPermutations[0]);
  60. });
  61. });
  62. });
  63. });
  64. });