|
| 1 | +import ERROR_MESSAGES from 'src/constants/error-messages'; |
| 2 | +import { isClusterCursorValid, parseClusterCursor } from './clusterCursor'; |
| 3 | + |
| 4 | +const isClusterCursorValidTests = [ |
| 5 | + { input: '172.17.0.1:7001@22||172.17.0.1:7002@33', expected: true }, |
| 6 | + { input: '172.17.0.1:7001@-1||172.17.0.1:7002@-1', expected: true }, |
| 7 | + { |
| 8 | + input: '172.17.0.1:7001@10' |
| 9 | + + '||172.17.0.1:7002@10' |
| 10 | + + '||172.17.0.1:7003@10' |
| 11 | + + '||172.17.0.1:7004@10' |
| 12 | + + '||172.17.0.1:7005@10' |
| 13 | + + '||172.17.0.1:7006@10', |
| 14 | + expected: true, |
| 15 | + }, |
| 16 | + { input: '172.17.0.1:7001@-1', expected: true }, |
| 17 | + { input: 'domain.com:7001@-1', expected: true }, |
| 18 | + { input: '172.17.0.1:7001@1228822', expected: true }, |
| 19 | + { input: '172.17.0.1:7001@', expected: false }, |
| 20 | + { input: '172.17.0.1:7001@text', expected: false }, |
| 21 | + { input: '172,17,0,1:7001@-1', expected: false }, |
| 22 | + { input: 'plain text', expected: false }, |
| 23 | + { input: 'text@text||text@text', expected: false }, |
| 24 | + { input: 'text@text', expected: false }, |
| 25 | + { input: '', expected: false }, |
| 26 | +]; |
| 27 | + |
| 28 | +describe('isClusterCursorValid', () => { |
| 29 | + it.each(isClusterCursorValidTests)('%j', ({ input, expected }) => { |
| 30 | + expect(isClusterCursorValid(input)).toBe(expected); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +const defaultNodeScanResult = { |
| 35 | + total: 0, scanned: 0, host: '172.17.0.1', port: 0, cursor: 0, keys: [], |
| 36 | +}; |
| 37 | +const parsingError = new Error(ERROR_MESSAGES.INCORRECT_CLUSTER_CURSOR_FORMAT); |
| 38 | +const parseClusterCursorTests = [ |
| 39 | + { |
| 40 | + input: '172.17.0.1:7001@22||172.17.0.1:7002@33', |
| 41 | + expected: [ |
| 42 | + { ...defaultNodeScanResult, port: 7001, cursor: 22 }, |
| 43 | + { ...defaultNodeScanResult, port: 7002, cursor: 33 }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + { |
| 47 | + input: '172.17.0.1:7001@-1' |
| 48 | + + '||172.17.0.1:7002@10' |
| 49 | + + '||172.17.0.1:7003@-1' |
| 50 | + + '||172.17.0.1:7004@10' |
| 51 | + + '||172.17.0.1:7005@-1' |
| 52 | + + '||172.17.0.1:7006@10', |
| 53 | + expected: [ |
| 54 | + { ...defaultNodeScanResult, port: 7002, cursor: 10 }, |
| 55 | + { ...defaultNodeScanResult, port: 7004, cursor: 10 }, |
| 56 | + { ...defaultNodeScanResult, port: 7006, cursor: 10 }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + { |
| 60 | + input: '172.17.0.1:7001@-1||172.17.0.1:7002@-1', |
| 61 | + expected: [], |
| 62 | + }, |
| 63 | + { input: '172.17.0.1:7001@', expected: parsingError }, |
| 64 | + { input: '172.17.0.1:7001@text', expected: parsingError }, |
| 65 | + { input: '172,17,0,1:7001@-1', expected: parsingError }, |
| 66 | + { input: 'plain text', expected: parsingError }, |
| 67 | + { input: 'text@text||text@text', expected: parsingError }, |
| 68 | + { input: 'text@text', expected: parsingError }, |
| 69 | + { input: '', expected: parsingError }, |
| 70 | + { input: '', expected: parsingError }, |
| 71 | +]; |
| 72 | + |
| 73 | +describe('parseClusterCursor', () => { |
| 74 | + it.each(parseClusterCursorTests)('%j', ({ input, expected }) => { |
| 75 | + if (expected instanceof Error) { |
| 76 | + try { |
| 77 | + parseClusterCursor(input); |
| 78 | + } catch (e) { |
| 79 | + expect(e.message).toEqual(expected.message); |
| 80 | + } |
| 81 | + } else { |
| 82 | + expect(parseClusterCursor(input)).toEqual(expected); |
| 83 | + } |
| 84 | + }); |
| 85 | +}); |
0 commit comments