|
| 1 | +'use strict'; /*jslint node:true es9:true*/ |
| 2 | +import test from 'node:test'; |
| 3 | +import assert from 'node:assert/strict'; |
| 4 | +import {DATASET_IDS, dataset_id_schema, metadata_to_fields, FILTER_OPERATORS, |
| 5 | + filter_schema} from '../search_dataset_schema.js'; |
| 6 | + |
| 7 | +test('DATASET_IDS lists the three supported datasets', ()=>{ |
| 8 | + assert.deepEqual(DATASET_IDS, [ |
| 9 | + 'gd_l1viktl72bvl7bjuj0', |
| 10 | + 'gd_me5ppxjr2ge6icjuh0', |
| 11 | + 'gd_l1vikfnt1wgvvqz95w', |
| 12 | + ]); |
| 13 | +}); |
| 14 | + |
| 15 | +test('dataset_id_schema accepts a supported id', ()=>{ |
| 16 | + assert.equal(dataset_id_schema.parse('gd_l1viktl72bvl7bjuj0'), |
| 17 | + 'gd_l1viktl72bvl7bjuj0'); |
| 18 | +}); |
| 19 | + |
| 20 | +test('dataset_id_schema rejects an unknown id', ()=>{ |
| 21 | + assert.throws(()=>dataset_id_schema.parse('gd_not_a_real_dataset')); |
| 22 | +}); |
| 23 | + |
| 24 | +test('FILTER_OPERATORS lists the documented operators', ()=>{ |
| 25 | + assert.deepEqual(FILTER_OPERATORS, [ |
| 26 | + '=', '!=', '<', '<=', '>', '>=', |
| 27 | + 'in', 'not_in', |
| 28 | + 'includes', 'not_includes', |
| 29 | + 'array_includes', 'not_array_includes', |
| 30 | + 'is_null', 'is_not_null', |
| 31 | + ]); |
| 32 | +}); |
| 33 | + |
| 34 | +test('metadata_to_fields keeps active fields as name/type/description', ()=>{ |
| 35 | + const metadata = { |
| 36 | + id: 'gd_l1vijqt9jfj7olije', |
| 37 | + fields: { |
| 38 | + name: {type: 'text', active: true, |
| 39 | + description: 'The name of the company'}, |
| 40 | + url: {type: 'url', required: true, |
| 41 | + description: 'The company URL'}, |
| 42 | + cb_rank: {type: 'number', active: false, |
| 43 | + description: 'Crunchbase rank'}, |
| 44 | + }, |
| 45 | + }; |
| 46 | + assert.deepEqual(metadata_to_fields(metadata), [ |
| 47 | + {name: 'name', type: 'text', description: 'The name of the company'}, |
| 48 | + {name: 'url', type: 'url', description: 'The company URL'}, |
| 49 | + ]); |
| 50 | +}); |
| 51 | + |
| 52 | +test('metadata_to_fields tolerates missing fields object', ()=>{ |
| 53 | + assert.deepEqual(metadata_to_fields({id: 'x'}), []); |
| 54 | + assert.deepEqual(metadata_to_fields(null), []); |
| 55 | +}); |
| 56 | + |
| 57 | +test('metadata_to_fields skips non-object field entries', ()=>{ |
| 58 | + assert.deepEqual(metadata_to_fields({fields: {bad: null, |
| 59 | + ok: {type: 'text', description: 'fine'}}}), |
| 60 | + [{name: 'ok', type: 'text', description: 'fine'}]); |
| 61 | +}); |
| 62 | + |
| 63 | +test('filter_schema accepts the documented flat example', ()=>{ |
| 64 | + const filter = {operator: 'and', filters: [ |
| 65 | + {name: 'name', value: 'Egor', operator: 'includes'}, |
| 66 | + ]}; |
| 67 | + assert.deepEqual(filter_schema.parse(filter), filter); |
| 68 | +}); |
| 69 | + |
| 70 | +test('filter_schema accepts a single leaf node', ()=>{ |
| 71 | + const filter = {name: 'cb_rank', value: 100, operator: '<'}; |
| 72 | + assert.deepEqual(filter_schema.parse(filter), filter); |
| 73 | +}); |
| 74 | + |
| 75 | +test('filter_schema accepts array and boolean leaf values', ()=>{ |
| 76 | + const filter = {operator: 'or', filters: [ |
| 77 | + {name: 'tags', value: ['a', 'b'], operator: 'array_includes'}, |
| 78 | + {name: 'verified', value: true, operator: '='}, |
| 79 | + ]}; |
| 80 | + assert.deepEqual(filter_schema.parse(filter), filter); |
| 81 | +}); |
| 82 | + |
| 83 | +test('filter_schema accepts nesting up to depth 3', ()=>{ |
| 84 | + const filter = {operator: 'and', filters: [ |
| 85 | + {operator: 'or', filters: [ |
| 86 | + {operator: 'and', filters: [ |
| 87 | + {name: 'name', value: 'x', operator: 'includes'}, |
| 88 | + ]}, |
| 89 | + ]}, |
| 90 | + ]}; |
| 91 | + assert.deepEqual(filter_schema.parse(filter), filter); |
| 92 | +}); |
| 93 | + |
| 94 | +test('filter_schema rejects nesting deeper than 3', ()=>{ |
| 95 | + const filter = {operator: 'and', filters: [ |
| 96 | + {operator: 'or', filters: [ |
| 97 | + {operator: 'and', filters: [ |
| 98 | + {operator: 'or', filters: [ |
| 99 | + {name: 'name', value: 'x', operator: 'includes'}, |
| 100 | + ]}, |
| 101 | + ]}, |
| 102 | + ]}, |
| 103 | + ]}; |
| 104 | + assert.throws(()=>filter_schema.parse(filter)); |
| 105 | +}); |
| 106 | + |
| 107 | +test('filter_schema rejects an empty object', ()=>{ |
| 108 | + assert.throws(()=>filter_schema.parse({})); |
| 109 | +}); |
0 commit comments