completer.jest.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //import { describe, it, sinon, expect } from 'test/lib/common';
  2. //import helpers from 'test/specs/helpers';
  3. import { PromCompleter } from '../completer';
  4. import { PrometheusDatasource } from '../datasource';
  5. import { BackendSrv } from 'app/core/services/backend_srv';
  6. jest.mock('../datasource');
  7. jest.mock('app/core/services/backend_srv');
  8. describe('Prometheus editor completer', function() {
  9. //beforeEach(ctx.providePhase(['templateSrv']));
  10. function getSessionStub(data) {
  11. return {
  12. getTokenAt: jest.fn(() => data.currentToken),
  13. getTokens: jest.fn(() => data.tokens),
  14. getLine: jest.fn(() => data.line),
  15. };
  16. }
  17. let editor = {};
  18. let backendSrv = <BackendSrv>{};
  19. let datasourceStub = new PrometheusDatasource({}, {}, backendSrv, {}, {});
  20. datasourceStub.performInstantQuery = jest.fn(() =>
  21. Promise.resolve({
  22. data: {
  23. data: {
  24. result: [
  25. {
  26. metric: {
  27. job: 'node',
  28. instance: 'localhost:9100',
  29. },
  30. },
  31. ],
  32. },
  33. },
  34. })
  35. );
  36. datasourceStub.performSuggestQuery = jest.fn(() => Promise.resolve(['node_cpu']));
  37. let templateSrv = {
  38. variables: [
  39. {
  40. name: 'var_name',
  41. options: [{ text: 'foo', value: 'foo', selected: false }, { text: 'bar', value: 'bar', selected: true }],
  42. },
  43. ],
  44. };
  45. let completer = new PromCompleter(datasourceStub, templateSrv);
  46. describe('When inside brackets', () => {
  47. it('Should return range vectors', () => {
  48. const session = getSessionStub({
  49. currentToken: { type: 'paren.lparen', value: '[', index: 2, start: 9 },
  50. tokens: [{ type: 'identifier', value: 'node_cpu' }, { type: 'paren.lparen', value: '[' }],
  51. line: 'node_cpu[',
  52. });
  53. return completer.getCompletions(editor, session, { row: 0, column: 10 }, '[', (s, res) => {
  54. expect(res[0].caption).toEqual('$__interval');
  55. expect(res[0].value).toEqual('[$__interval');
  56. expect(res[0].meta).toEqual('range vector');
  57. });
  58. });
  59. });
  60. describe('When inside label matcher, and located at label name', () => {
  61. it('Should return label name list', () => {
  62. const session = getSessionStub({
  63. currentToken: {
  64. type: 'entity.name.tag.label-matcher',
  65. value: 'j',
  66. index: 2,
  67. start: 9,
  68. },
  69. tokens: [
  70. { type: 'identifier', value: 'node_cpu' },
  71. { type: 'paren.lparen.label-matcher', value: '{' },
  72. {
  73. type: 'entity.name.tag.label-matcher',
  74. value: 'j',
  75. index: 2,
  76. start: 9,
  77. },
  78. { type: 'paren.rparen.label-matcher', value: '}' },
  79. ],
  80. line: 'node_cpu{j}',
  81. });
  82. return completer.getCompletions(editor, session, { row: 0, column: 10 }, 'j', (s, res) => {
  83. expect(res[0].meta).toEqual('label name');
  84. });
  85. });
  86. });
  87. describe('When inside label matcher, and located at label name with __name__ match', () => {
  88. it('Should return label name list', () => {
  89. const session = getSessionStub({
  90. currentToken: {
  91. type: 'entity.name.tag.label-matcher',
  92. value: 'j',
  93. index: 5,
  94. start: 22,
  95. },
  96. tokens: [
  97. { type: 'paren.lparen.label-matcher', value: '{' },
  98. { type: 'entity.name.tag.label-matcher', value: '__name__' },
  99. { type: 'keyword.operator.label-matcher', value: '=~' },
  100. { type: 'string.quoted.label-matcher', value: '"node_cpu"' },
  101. { type: 'punctuation.operator.label-matcher', value: ',' },
  102. {
  103. type: 'entity.name.tag.label-matcher',
  104. value: 'j',
  105. index: 5,
  106. start: 22,
  107. },
  108. { type: 'paren.rparen.label-matcher', value: '}' },
  109. ],
  110. line: '{__name__=~"node_cpu",j}',
  111. });
  112. return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'j', (s, res) => {
  113. expect(res[0].meta).toEqual('label name');
  114. });
  115. });
  116. });
  117. describe('When inside label matcher, and located at label value', () => {
  118. it('Should return label value list', () => {
  119. const session = getSessionStub({
  120. currentToken: {
  121. type: 'string.quoted.label-matcher',
  122. value: '"n"',
  123. index: 4,
  124. start: 13,
  125. },
  126. tokens: [
  127. { type: 'identifier', value: 'node_cpu' },
  128. { type: 'paren.lparen.label-matcher', value: '{' },
  129. { type: 'entity.name.tag.label-matcher', value: 'job' },
  130. { type: 'keyword.operator.label-matcher', value: '=' },
  131. {
  132. type: 'string.quoted.label-matcher',
  133. value: '"n"',
  134. index: 4,
  135. start: 13,
  136. },
  137. { type: 'paren.rparen.label-matcher', value: '}' },
  138. ],
  139. line: 'node_cpu{job="n"}',
  140. });
  141. return completer.getCompletions(editor, session, { row: 0, column: 15 }, 'n', (s, res) => {
  142. expect(res[0].meta).toEqual('label value');
  143. });
  144. });
  145. });
  146. describe('When inside by', () => {
  147. it('Should return label name list', () => {
  148. const session = getSessionStub({
  149. currentToken: {
  150. type: 'entity.name.tag.label-list-matcher',
  151. value: 'm',
  152. index: 9,
  153. start: 22,
  154. },
  155. tokens: [
  156. { type: 'paren.lparen', value: '(' },
  157. { type: 'keyword', value: 'count' },
  158. { type: 'paren.lparen', value: '(' },
  159. { type: 'identifier', value: 'node_cpu' },
  160. { type: 'paren.rparen', value: '))' },
  161. { type: 'text', value: ' ' },
  162. { type: 'keyword.control', value: 'by' },
  163. { type: 'text', value: ' ' },
  164. { type: 'paren.lparen.label-list-matcher', value: '(' },
  165. {
  166. type: 'entity.name.tag.label-list-matcher',
  167. value: 'm',
  168. index: 9,
  169. start: 22,
  170. },
  171. { type: 'paren.rparen.label-list-matcher', value: ')' },
  172. ],
  173. line: '(count(node_cpu)) by (m)',
  174. });
  175. return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'm', (s, res) => {
  176. expect(res[0].meta).toEqual('label name');
  177. });
  178. });
  179. });
  180. });