|
| 1 | +/** |
| 2 | + * Test file for deepSort and autoCompare functions from compare-database.js |
| 3 | + * |
| 4 | + * Usage: node .scripts/compare-database.test.js |
| 5 | + * |
| 6 | + * This file tests the deep sorting functionality that ensures consistent |
| 7 | + * ordering of arrays and objects in database comparison operations. |
| 8 | + */ |
| 9 | + |
| 10 | +import assert from 'node:assert'; |
| 11 | +import { autoCompare, deepSort } from './compare-database.js'; |
| 12 | + |
| 13 | +// Test helper function |
| 14 | +const runTest = (testName, testFn) => { |
| 15 | + try { |
| 16 | + testFn(); |
| 17 | + console.log(`✅ ${testName}`); |
| 18 | + } catch (error) { |
| 19 | + console.error(`❌ ${testName}: ${error.message}`); |
| 20 | + process.exit(1); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +// Test cases for autoCompare function |
| 25 | +runTest('autoCompare - different types', () => { |
| 26 | + assert.strictEqual(autoCompare('string', 123) > 0, true); // string > number |
| 27 | + assert.strictEqual(autoCompare(123, 'string') < 0, true); // number < string |
| 28 | +}); |
| 29 | + |
| 30 | +runTest('autoCompare - same primitive types', () => { |
| 31 | + assert.strictEqual(autoCompare('apple', 'banana') < 0, true); |
| 32 | + assert.strictEqual(autoCompare('banana', 'apple') > 0, true); |
| 33 | + assert.strictEqual(autoCompare('apple', 'apple'), 0); |
| 34 | + assert.strictEqual(autoCompare(1, 2) < 0, true); |
| 35 | + assert.strictEqual(autoCompare(2, 1) > 0, true); |
| 36 | + assert.strictEqual(autoCompare(1, 1), 0); |
| 37 | +}); |
| 38 | + |
| 39 | +runTest('autoCompare - objects', () => { |
| 40 | + const obj1 = { a: 1, b: 2 }; |
| 41 | + const obj2 = { a: 1, b: 2 }; |
| 42 | + const obj3 = { a: 1, b: 3 }; |
| 43 | + |
| 44 | + assert.strictEqual(autoCompare(obj1, obj2), 0); |
| 45 | + assert.strictEqual(autoCompare(obj1, obj3) < 0, true); |
| 46 | + assert.strictEqual(autoCompare(obj3, obj1) > 0, true); |
| 47 | +}); |
| 48 | + |
| 49 | +// Test cases for deepSort function |
| 50 | +runTest('deepSort - primitive values', () => { |
| 51 | + assert.strictEqual(deepSort('hello'), 'hello'); |
| 52 | + assert.strictEqual(deepSort(123), 123); |
| 53 | + assert.strictEqual(deepSort(null), null); |
| 54 | + assert.strictEqual(deepSort(undefined), undefined); |
| 55 | +}); |
| 56 | + |
| 57 | +runTest('deepSort - simple arrays', () => { |
| 58 | + const input = [3, 1, 2]; |
| 59 | + const expected = [1, 2, 3]; |
| 60 | + const result = deepSort(input); |
| 61 | + |
| 62 | + assert.deepStrictEqual(result, expected); |
| 63 | +}); |
| 64 | + |
| 65 | +runTest('deepSort - string arrays', () => { |
| 66 | + const input = ['banana', 'apple', 'cherry']; |
| 67 | + const expected = ['apple', 'banana', 'cherry']; |
| 68 | + const result = deepSort(input); |
| 69 | + |
| 70 | + assert.deepStrictEqual(result, expected); |
| 71 | +}); |
| 72 | + |
| 73 | +runTest('deepSort - nested arrays', () => { |
| 74 | + const input = [[3, 1], [2, 4], [1, 2]]; |
| 75 | + const expected = [[1, 2], [1, 3], [2, 4]]; |
| 76 | + const result = deepSort(input); |
| 77 | + |
| 78 | + assert.deepStrictEqual(result, expected); |
| 79 | +}); |
| 80 | + |
| 81 | +runTest('deepSort - simple objects', () => { |
| 82 | + const input = { c: 3, a: 1, b: 2 }; |
| 83 | + const expected = { a: 1, b: 2, c: 3 }; |
| 84 | + const result = deepSort(input); |
| 85 | + |
| 86 | + assert.deepStrictEqual(result, expected); |
| 87 | +}); |
| 88 | + |
| 89 | +runTest('deepSort - objects with arrays', () => { |
| 90 | + const input = { |
| 91 | + name: 'test', |
| 92 | + items: [3, 1, 2], |
| 93 | + tags: ['beta', 'alpha'] |
| 94 | + }; |
| 95 | + const expected = { |
| 96 | + items: [1, 2, 3], |
| 97 | + name: 'test', |
| 98 | + tags: ['alpha', 'beta'] |
| 99 | + }; |
| 100 | + const result = deepSort(input); |
| 101 | + |
| 102 | + assert.deepStrictEqual(result, expected); |
| 103 | +}); |
| 104 | + |
| 105 | +runTest('deepSort - arrays with objects', () => { |
| 106 | + const input = [ |
| 107 | + { name: 'Bob', age: 25 }, |
| 108 | + { name: 'Alice', age: 30 }, |
| 109 | + { name: 'Charlie', age: 20 } |
| 110 | + ]; |
| 111 | + // Objects are sorted by their keys and values in lexicographic order |
| 112 | + // First by 'age' key, then by 'name' key |
| 113 | + const expected = [ |
| 114 | + { age: 20, name: 'Charlie' }, |
| 115 | + { age: 25, name: 'Bob' }, |
| 116 | + { age: 30, name: 'Alice' } |
| 117 | + ]; |
| 118 | + const result = deepSort(input); |
| 119 | + |
| 120 | + assert.deepStrictEqual(result, expected); |
| 121 | +}); |
| 122 | + |
| 123 | +runTest('deepSort - complex nested structure', () => { |
| 124 | + const input = { |
| 125 | + users: [ |
| 126 | + { |
| 127 | + name: 'Bob', |
| 128 | + roles: ['admin', 'user'], |
| 129 | + metadata: { created: '2023-01-01', active: true } |
| 130 | + }, |
| 131 | + { |
| 132 | + name: 'Alice', |
| 133 | + roles: ['user', 'editor'], |
| 134 | + metadata: { created: '2023-01-02', active: false } |
| 135 | + } |
| 136 | + ], |
| 137 | + config: { |
| 138 | + version: '1.0', |
| 139 | + features: ['auth', 'logging', 'api'] |
| 140 | + } |
| 141 | + }; |
| 142 | + |
| 143 | + const expected = { |
| 144 | + config: { |
| 145 | + features: ['api', 'auth', 'logging'], |
| 146 | + version: '1.0' |
| 147 | + }, |
| 148 | + users: [ |
| 149 | + { |
| 150 | + metadata: { active: false, created: '2023-01-02' }, |
| 151 | + name: 'Alice', |
| 152 | + roles: ['editor', 'user'] |
| 153 | + }, |
| 154 | + { |
| 155 | + metadata: { active: true, created: '2023-01-01' }, |
| 156 | + name: 'Bob', |
| 157 | + roles: ['admin', 'user'] |
| 158 | + } |
| 159 | + ] |
| 160 | + }; |
| 161 | + |
| 162 | + const result = deepSort(input); |
| 163 | + assert.deepStrictEqual(result, expected); |
| 164 | +}); |
| 165 | + |
| 166 | +runTest('deepSort - empty structures', () => { |
| 167 | + assert.deepStrictEqual(deepSort([]), []); |
| 168 | + assert.deepStrictEqual(deepSort({}), {}); |
| 169 | + // Empty array comes before empty object in autoCompare logic |
| 170 | + assert.deepStrictEqual(deepSort([[], {}]), [[], {}]); |
| 171 | +}); |
| 172 | + |
| 173 | +runTest('deepSort - mixed types in array', () => { |
| 174 | + const input = [{ b: 2 }, 'string', 1, { a: 1 }]; |
| 175 | + // Type order in autoCompare: number < object < string |
| 176 | + // Objects are sorted by their content |
| 177 | + const expected = [1, { a: 1 }, { b: 2 }, 'string']; |
| 178 | + const result = deepSort(input); |
| 179 | + |
| 180 | + assert.deepStrictEqual(result, expected); |
| 181 | +}); |
| 182 | + |
| 183 | +console.log('\n🎉 All tests passed!'); |
0 commit comments