|
| 1 | +import { describe, it, expect } from 'bun:test'; |
| 2 | +import { |
| 3 | + applyAccountFilters, |
| 4 | + applyGenericFilters, |
| 5 | + applyCodeActionFilters |
| 6 | +} from '../../src/api/routes/v2-history/get_actions/functions.js'; |
| 7 | + |
| 8 | +// get_actions builds its query with these clauses in *filter* context (not must/should), because |
| 9 | +// results are always sorted by global_sequence and never by _score — so scoring is wasted work. |
| 10 | +// These tests pin that placement so a regression back to scoring context is caught. |
| 11 | + |
| 12 | +const newQueryStruct = () => ({ bool: { must: [] as any[], must_not: [] as any[], boost: 1.0 } }); |
| 13 | + |
| 14 | +describe('applyAccountFilters — filter context', () => { |
| 15 | + it('puts the account should-array in bool.filter with minimum_should_match, not bool.must', () => { |
| 16 | + const qs = newQueryStruct(); |
| 17 | + applyAccountFilters({ account: 'eosio.token' }, qs); |
| 18 | + |
| 19 | + expect(qs.bool.must).toHaveLength(0); |
| 20 | + expect(qs.bool.filter).toHaveLength(1); |
| 21 | + const clause = qs.bool.filter[0]; |
| 22 | + expect(clause.bool.minimum_should_match).toBe(1); |
| 23 | + // notified, receipts.receiver, act.authorization.actor — all bound to the account |
| 24 | + expect(clause.bool.should).toEqual([ |
| 25 | + { term: { notified: 'eosio.token' } }, |
| 26 | + { term: { 'receipts.receiver': 'eosio.token' } }, |
| 27 | + { term: { 'act.authorization.actor': 'eosio.token' } } |
| 28 | + ]); |
| 29 | + }); |
| 30 | + |
| 31 | + it('is a no-op when no account is given', () => { |
| 32 | + const qs = newQueryStruct(); |
| 33 | + applyAccountFilters({}, qs); |
| 34 | + expect(qs.bool.filter).toBeUndefined(); |
| 35 | + expect(qs.bool.must).toHaveLength(0); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe('applyGenericFilters — filter context', () => { |
| 40 | + it('puts a single primary-term match in bool.filter', () => { |
| 41 | + const qs = newQueryStruct(); |
| 42 | + applyGenericFilters({ producer: 'eosio' }, qs, new Set()); |
| 43 | + expect(qs.bool.must).toHaveLength(0); |
| 44 | + expect(qs.bool.filter).toEqual([{ term: { producer: 'eosio' } }]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('puts a comma multi-value clause in bool.filter as a should with minimum_should_match', () => { |
| 48 | + const qs = newQueryStruct(); |
| 49 | + applyGenericFilters({ producer: 'a,b' }, qs, new Set()); |
| 50 | + expect(qs.bool.must).toHaveLength(0); |
| 51 | + expect(qs.bool.filter).toHaveLength(1); |
| 52 | + expect(qs.bool.filter[0].bool.minimum_should_match).toBe(1); |
| 53 | + expect(qs.bool.filter[0].bool.should).toEqual([ |
| 54 | + { term: { producer: 'a' } }, |
| 55 | + { term: { producer: 'b' } } |
| 56 | + ]); |
| 57 | + }); |
| 58 | + |
| 59 | + it('puts a range clause in bool.filter', () => { |
| 60 | + const qs = newQueryStruct(); |
| 61 | + applyGenericFilters({ block_num: '100-200' }, qs, new Set()); |
| 62 | + expect(qs.bool.must).toHaveLength(0); |
| 63 | + expect(qs.bool.filter).toEqual([{ range: { block_num: { gte: '100', lte: '200' } } }]); |
| 64 | + }); |
| 65 | + |
| 66 | + it('keeps negation in must_not (unchanged)', () => { |
| 67 | + const qs = newQueryStruct(); |
| 68 | + applyGenericFilters({ producer: '!eosio' }, qs, new Set()); |
| 69 | + expect(qs.bool.must_not).toEqual([{ term: { producer: 'eosio' } }]); |
| 70 | + expect(qs.bool.filter).toBeUndefined(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('keeps the @transfer.memo full-text match in must so sortedBy=_score still ranks by relevance', () => { |
| 74 | + const qs = newQueryStruct(); |
| 75 | + applyGenericFilters({ 'transfer.memo': 'hello' }, qs, new Set(['transfer'])); |
| 76 | + expect(qs.bool.must).toEqual([{ match: { '@transfer.memo': { query: 'hello' } } }]); |
| 77 | + expect(qs.bool.filter).toBeUndefined(); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe('applyCodeActionFilters — filter context', () => { |
| 82 | + it('puts the code:name filter in bool.filter (not root-level should)', () => { |
| 83 | + const qs = newQueryStruct(); |
| 84 | + applyCodeActionFilters({ filter: 'eosio.token:transfer' }, qs); |
| 85 | + expect((qs.bool as any).should).toBeUndefined(); |
| 86 | + expect(qs.bool.filter).toHaveLength(1); |
| 87 | + expect(qs.bool.filter[0].bool.minimum_should_match).toBe(1); |
| 88 | + expect(qs.bool.filter[0].bool.should).toEqual([ |
| 89 | + { bool: { must: [{ term: { 'act.account': 'eosio.token' } }, { term: { 'act.name': 'transfer' } }] } } |
| 90 | + ]); |
| 91 | + }); |
| 92 | +}); |
0 commit comments